From ec35dcee7b8c0a23f9aa681fb76e906128e41b16 Mon Sep 17 00:00:00 2001 From: davidribeiro Date: Wed, 2 Oct 2019 10:29:33 +0100 Subject: [PATCH] Added tests to improve code coverage to 100% --- .gitignore | 3 +- tests/PHPJasperTest.php | 65 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 3ee0a42..567c8b5 100755 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /vendor tests/*.jasper +tests/fixture tests/codeCoverage # IDE @@ -8,4 +9,4 @@ tests/codeCoverage .project .settings ## PHPStorm -.idea/ \ No newline at end of file +.idea/ diff --git a/tests/PHPJasperTest.php b/tests/PHPJasperTest.php index b328699..1f915b1 100644 --- a/tests/PHPJasperTest.php +++ b/tests/PHPJasperTest.php @@ -45,7 +45,7 @@ public function testCompile() $expected = '.*jasperstarter compile ".*hello_world.jrxml" -o "{output_file}"'; - $this->assertRegExp('/'.$expected.'/', $result->output()); + $this->expectOutputRegex('/'.$expected.'/', $result->output()); } public function testProcess() @@ -54,15 +54,42 @@ public function testProcess() $expected = '.*jasperstarter process ".*hello_world.jrxml" -o "{output_file}"'; - $this->assertRegExp('/'.$expected.'/', $result->output()); + $this->expectOutputRegex('/'.$expected.'/', $result->output()); + } + public function testProcessWithOptions() + { + $options = [ + 'locale' => 'en_US', + 'params' => [ + 'param_1' => 'value_1', + 'param_2' => 'value_2', + ], + 'db_connection' => [ + 'driver' => 'driver', + 'username' => 'user', + 'password' => '12345678', + 'database' => 'db' + ], + 'resources' => 'foo', + ]; + + $result = $this->instance->process('examples/hello_world.jrxml', '{output_file}', $options); + + $expected = '.*jasperstarter --locale en_US process ".*hello_world.jrxml" -o "{output_file}" '; + $expected .= '-f pdf -P param_1="value_1" param_2="value_2" -t driver -u user -p 12345678 -n db -r foo'; + + $this->expectOutputRegex( + '/'.$expected.'/', + $result->output() + ); } public function testListParameters() { $result = $this->instance->listParameters('examples/hello_world.jrxml'); - $this->assertRegExp( + $this->expectOutputRegex( '/.*jasperstarter list_parameters ".*hello_world.jrxml"/', $result->output() ); @@ -79,7 +106,18 @@ public function testCompileHelloWorld() { $result = $this->instance->compile('examples/hello_world.jrxml'); - $this->assertRegExp('/.*jasperstarter compile ".*hello_world.jrxml"/', $result->output()); + $this->expectOutputRegex('/.*jasperstarter compile ".*hello_world.jrxml"/', $result->output()); + } + + public function testOutputWithUserOnExecute() + { + $this->expectException(Exception\ErrorCommandExecutable::class); + + $this->instance->compile(__DIR__ . '/test.jrxml', __DIR__ . '/test')->execute('phpjasper'); + + $expected = 'su -u 1000 -c "./jasperstarter compile "/var/www/app/tests/test.jrxml" -o "/var/www/app/tests/test""'; + + $this->expectOutputRegex('/' . $expected . '/', $this->instance->output()); } public function testExecuteWithoutCompile() @@ -103,6 +141,25 @@ public function testExecute() $this->assertInternalType('array', $actual); } + public function testExecuteWithOutput() + { + $actual = $this->instance->compile(__DIR__ . '/test.jrxml', __DIR__ . '/test')->execute(); + + $this->assertInternalType('array', $actual); + } + + public function testExecuteThrowsInvalidResourceDirectory() + { + $reflectionObject = new \ReflectionObject($this->instance); + $reflectionProperty = $reflectionObject->getProperty('pathExecutable'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($this->instance, ''); + + $this->expectException(Exception\InvalidResourceDirectory::class); + + $this->instance->compile(__DIR__ . '/test.jrxml', __DIR__ . '/test')->execute(); + } + public function testListParametersWithWrongInput() { $this->expectException(Exception\InvalidInputFile::class);