Skip to content

Commit

Permalink
Add tests (#14)
Browse files Browse the repository at this point in the history
* test cli

* increase coverage
  • Loading branch information
rogervila authored Mar 16, 2020
1 parent 608ddcd commit d4a4a3c
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ clover.xml
/tmp
sonar-project.properties
.scannerwork
.phpunit.result.cache
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Options
/**
* @var array
*/
private $arguments;
private $arguments = [];

/**
* @var array
Expand Down
11 changes: 0 additions & 11 deletions tests/AppTest.php

This file was deleted.

48 changes: 48 additions & 0 deletions tests/CliTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Tests\Sonar;

use PHPUnit\Framework\TestCase;
use Tests\Sonar\Helpers\TestServer;
use Lead\Dir\Dir;

final class CliTest extends TestCase
{
public function test_sonar_scanner_command_runs_properly()
{
$entrypoint = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'sonar-scanner';
$server = new TestServer();

// Start the mockup server
$server->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);
}
}
27 changes: 27 additions & 0 deletions tests/Helpers/TestServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Tests\Sonar\Helpers;

use Creativestyle\AppHttpServerMock\Server;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class TestServer extends Server
{
protected function registerRequestHandlers()
{
$this->registerRequestHandler(['GET','PUT', 'POST'], '/', function (Request $request) {
return new Response('TODO');
});

$this->registerRequestHandler(['GET','PUT', 'POST'], '/batch/index', function (Request $request) {
$xml = '<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
$xml .= '<mynode><content>TODO</content></mynode>';

$response = new Response($xml);
$response->headers->set('Content-Type', 'xml');

return $response;
});
}
}
41 changes: 41 additions & 0 deletions tests/OptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Tests\Sonar;

use PHPUnit\Framework\TestCase;

final class OptionsTest extends TestCase
{
public function test_arguments_are_parsed()
{
$options = new \Sonar\Options(__DIR__);

$options->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/**"'
);
}
}
19 changes: 19 additions & 0 deletions tests/ScannerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Sonar;

use PHPUnit\Framework\TestCase;

final class ScannerTest extends TestCase
{
public function test_sonar_scanner_app_runs_properly()
{
$app = new \Sonar\Scanner();
$options = new \Sonar\Options(getcwd());

$app->run($options);

// If we arrive here, no exception has been thrown
$this->assertTrue(true);
}
}

0 comments on commit d4a4a3c

Please sign in to comment.