From d4a4a3cff68283428471d5e5e0eeb968d2862d76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Mon, 16 Mar 2020 14:12:54 +0100 Subject: [PATCH] Add tests (#14) * test cli * increase coverage --- .gitignore | 1 + composer.json | 4 ++- src/Options.php | 2 +- tests/AppTest.php | 11 --------- tests/CliTest.php | 48 ++++++++++++++++++++++++++++++++++++ tests/Helpers/TestServer.php | 27 ++++++++++++++++++++ tests/OptionsTest.php | 41 ++++++++++++++++++++++++++++++ tests/ScannerTest.php | 19 ++++++++++++++ 8 files changed, 140 insertions(+), 13 deletions(-) delete mode 100755 tests/AppTest.php create mode 100644 tests/CliTest.php create mode 100644 tests/Helpers/TestServer.php create mode 100644 tests/OptionsTest.php create mode 100644 tests/ScannerTest.php diff --git a/.gitignore b/.gitignore index ce4ef82..1e624b7 100755 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ clover.xml /tmp sonar-project.properties .scannerwork +.phpunit.result.cache diff --git a/composer.json b/composer.json index a8b177b..5783cc3 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,9 @@ "tivie/php-os-detector": "^1.1" }, "require-dev": { - "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0" + "creativestyle/app-http-server-mock": "^1.0", + "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0", + "symfony/process": "^4.1" }, "autoload": { "psr-4": { diff --git a/src/Options.php b/src/Options.php index 4133c36..1c11c03 100755 --- a/src/Options.php +++ b/src/Options.php @@ -34,7 +34,7 @@ class Options /** * @var array */ - private $arguments; + private $arguments = []; /** * @var array diff --git a/tests/AppTest.php b/tests/AppTest.php deleted file mode 100755 index b813571..0000000 --- a/tests/AppTest.php +++ /dev/null @@ -1,11 +0,0 @@ -assertTrue(true); - } -} diff --git a/tests/CliTest.php b/tests/CliTest.php new file mode 100644 index 0000000..6cd4abb --- /dev/null +++ b/tests/CliTest.php @@ -0,0 +1,48 @@ +start(); + + // Entrypoint must exist + $this->assertTrue(file_exists($entrypoint)); + + $params = ' -X -Dsonar.verbose=true -Dsonar.host.url=' . $server->getBaseUrl(); + $command = 'php ' . $entrypoint . $params; + + exec($command, $output); + + // SonarQube recieves expected parameters + $this->assertStringContainsString($params, $output[2]); + } + + public function test_expected_scanner_versions() + { + $expectedScannerVersions = 3; + + $zipfiles = Dir::scan( + __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'scanners', + [ + 'type' => 'file', + 'skipDots' => true, + 'leavesOnly' => true, + 'followSymlinks' => true, + 'recursive' => true, + ] + ); + + $this->assertCount($expectedScannerVersions, $zipfiles); + } +} diff --git a/tests/Helpers/TestServer.php b/tests/Helpers/TestServer.php new file mode 100644 index 0000000..4cd849a --- /dev/null +++ b/tests/Helpers/TestServer.php @@ -0,0 +1,27 @@ +registerRequestHandler(['GET','PUT', 'POST'], '/', function (Request $request) { + return new Response('TODO'); + }); + + $this->registerRequestHandler(['GET','PUT', 'POST'], '/batch/index', function (Request $request) { + $xml = ' '; + $xml .= 'TODO'; + + $response = new Response($xml); + $response->headers->set('Content-Type', 'xml'); + + return $response; + }); + } +} diff --git a/tests/OptionsTest.php b/tests/OptionsTest.php new file mode 100644 index 0000000..4aed3c0 --- /dev/null +++ b/tests/OptionsTest.php @@ -0,0 +1,41 @@ +parse(['this one will be deleted', '-Dsonar.prop=something']); + + // make sure other methods can be called + $options->setSourceManagerBranch('foo'); + $options->setEdition(123); + + $this->assertStringContainsString( + $options->cli(), + '-Dsonar.prop=something -Dsonar.sources=' . __DIR__ . ' -Dsonar.exclusions="vendor/**, node_modules/**, .scannerwork/**"' + ); + } + + public function test_arguments_come_from_composer_json() + { + $content = [ + 'name' => 'john/doe', + 'description' => 'test' + ]; + + $options = new \Sonar\Options(__DIR__); + $options->setComposerConfiguration($content); + $options->parse([]); + + $this->assertStringContainsString( + $options->cli(), + '-Dsonar.projectKey=john_doe -Dsonar.projectName=doe -Dsonar.projectDescription="test" -Dsonar.sources=' . __DIR__ . ' -Dsonar.exclusions="vendor/**, node_modules/**, .scannerwork/**"' + ); + } +} diff --git a/tests/ScannerTest.php b/tests/ScannerTest.php new file mode 100644 index 0000000..2b263d5 --- /dev/null +++ b/tests/ScannerTest.php @@ -0,0 +1,19 @@ +run($options); + + // If we arrive here, no exception has been thrown + $this->assertTrue(true); + } +}