Skip to content

Commit ec802a9

Browse files
committed
Refacotr in JasperPHP::compile()
1 parent d56749c commit ec802a9

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

src/Exception/InvalidInputFile.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace JasperPHP\Exception;
3+
/**
4+
* Class InvalidInputFile
5+
* @package JasperPHP\Exception
6+
*/
7+
class InvalidInputFile extends \Exception
8+
{
9+
10+
/**
11+
* InvalidInputFile constructor.
12+
* @param string $message
13+
* @param int $code
14+
* @param Exception|null $previous
15+
*/
16+
public function __construct($message = "", $code = 0, Exception $previous = null)
17+
{
18+
$message = 'No input file';
19+
parent::__construct($message, $code, $previous);
20+
}
21+
}

src/JasperPHP.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,26 @@ public static function __callStatic($method, $parameters)
4747
return call_user_func_array(array(new $model, $method), $parameters);
4848
}
4949

50+
/**
51+
* @param $input_file
52+
* @param bool $output_file
53+
* @return $this
54+
* @throws Exception\InvalidInputFile
55+
*/
5056
public function compile($input_file, $output_file = false)
5157
{
52-
if (is_null($input_file) || empty($input_file)) {
53-
throw new \Exception('No input file', 1);
58+
if (!$input_file) {
59+
throw new \JasperPHP\Exception\InvalidInputFile();
5460
}
5561

56-
$command = ($this->windows) ? $this->executable : './' . $this->executable;
57-
58-
$command .= ' compile ';
59-
60-
$command .= "\"$input_file\"";
62+
$this->the_command = $this->windows ? $this->executable : './' . $this->executable;
63+
$this->the_command .= ' compile ';
64+
$this->the_command .= "\"$input_file\"";
6165

6266
if ($output_file !== false) {
63-
$command .= ' -o ' . "\"$output_file\"";
67+
$this->the_command .= ' -o ' . "\"$output_file\"";
6468
}
6569

66-
$this->the_command = $command;
67-
6870
return $this;
6971
}
7072

tests/JasperPHP/JasperPHPTest.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,35 @@ class JasperPHPTest extends \PHPUnit_Framework_TestCase
1010
{
1111

1212
/**
13-
* @return JasperPHP
13+
*
1414
*/
1515
public function testConstructor()
1616
{
1717
$this->assertInstanceOf(JasperPHP::class, new JasperPHP());
1818
}
1919

20+
/**
21+
*
22+
*/
23+
public function testCompileWithWrongInput()
24+
{
25+
$this->setExpectedException(\JasperPHP\Exception\InvalidInputFile::class);
26+
27+
$jasper = new JasperPHP();
28+
$jasper->compile(null);
29+
}
30+
31+
/**
32+
*
33+
*/
34+
public function testCompile()
35+
{
36+
$jasper = new JasperPHP();
37+
$result = $jasper->compile('hello_world.jrxml');
38+
39+
$this->assertInstanceOf(JasperPHP::class, $result);
40+
}
41+
2042
/**
2143
*
2244
*/
@@ -31,7 +53,7 @@ public function testExecuteWithoutCompile()
3153
/**
3254
*
3355
*/
34-
public function testExecuteWithCompileAndWrongInput()
56+
public function testExecuteWithCompile()
3557
{
3658
$this->setExpectedException(\JasperPHP\Exception\ErrorCommandExecutable::class);
3759

0 commit comments

Comments
 (0)