diff --git a/.travis.yml b/.travis.yml index 53517fa..a4fdf9f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,7 @@ script: - php vendor/sensiolabs/security-checker/security-checker security:check composer.lock - vendor/bin/phpcs --standard=psr2 src - vendor/bin/phpcs --standard=phpcs.xml src + - vendor/bin/phpcs --standard=phpcs.xml tests --ignore=tests/Sniffs - vendor/bin/phpunit --debug --coverage-clover=coverage.xml - vendor/bin/phpmd src text codesize,unusedcode,naming - vendor/bin/phploc src --progress diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php new file mode 100644 index 0000000..8940b8c --- /dev/null +++ b/tests/BaseTestCase.php @@ -0,0 +1,26 @@ +runner = new CodeSnifferRunner(); + } +} \ No newline at end of file diff --git a/tests/CodeSnifferRunner.php b/tests/CodeSnifferRunner.php index c3639df..8f986c8 100644 --- a/tests/CodeSnifferRunner.php +++ b/tests/CodeSnifferRunner.php @@ -9,27 +9,78 @@ class CodeSnifferRunner /** * @var PHP_CodeSniffer */ - private $codeSniffer; + protected $codeSniffer; + /** + * The current sniff rule being tested. + * @var string + */ + protected $sniff; + + /** + * Path of the folder that will contain the file + * to test against. + * @var string + */ + protected $path; + + /** + * The file path to the file to test against. + * @var string + */ + protected $filePath; + + /** + * Class Constructor. + */ public function __construct() { $this->codeSniffer = new PHP_CodeSniffer(); } - public function detectErrorCountInFileForSniff($testedFile, $sniffName) + /** + * Sets the sniff we will test. + * @param string $sniff The sniff to test. + * @return this + */ + public function setSniff($sniff) + { + $this->sniff = $sniff; + + return $this; + } + + /** + * Sets the folder the sample files live in. + * @param string $path Path to sample files. + * @return void + */ + public function setFolder($path) { - return $this->processCodeSniffer($testedFile, $sniffName)->getErrorCount(); + $this->path = $path; } - public function detectWarningCountInFileForSniff($testedFile, $sniffName) + /** + * Sets the file to run the sniffer on then + * calls the run method to run the sniffer. + * @param string $file Filename. + * @return PHP_CodeSniffer_File Sniffer Results. + */ + public function sniff($file) { - return $this->processCodeSniffer($testedFile, $sniffName)->getWarningCount(); + $this->filePath = $this->path . $file; + + return $this->run(); } - private function processCodeSniffer($testedFile, $sniffName) + /** + * Runs the actual sniffer on the file. + * @return PHP_CodeSniffer_File Sniffer Results. + */ + protected function run() { - $this->codeSniffer->initStandard(__DIR__.'/../src/Codor', [$sniffName]); + $this->codeSniffer->initStandard(__DIR__.'/../src/Codor', [$this->sniff]); - return $this->codeSniffer->processFile($testedFile); + return $this->codeSniffer->processFile($this->filePath); } } \ No newline at end of file diff --git a/tests/Sniffs/ControlStructures/NoElseSniffTest.php b/tests/Sniffs/ControlStructures/NoElseSniffTest.php index 66abe9e..3077646 100644 --- a/tests/Sniffs/ControlStructures/NoElseSniffTest.php +++ b/tests/Sniffs/ControlStructures/NoElseSniffTest.php @@ -2,20 +2,22 @@ namespace Codor\Tests\Sniffs\ControlStructures; -use Codor\Tests\CodeSnifferRunner; -use PHPUnit\Framework\TestCase; +use Codor\Tests\BaseTestCase; - -class NoElseSniffTest extends TestCase +/** @group ControlStructures */ +class NoElseSniffTest extends BaseTestCase { + + public function setup() + { + parent::setup(); + + $this->runner->setSniff('Codor.ControlStructures.NoElse')->setFolder(__DIR__.'/'); + } + public function testSniff() { - $codeSnifferRunner = new CodeSnifferRunner(); - $errorCount = $codeSnifferRunner->detectErrorCountInFileForSniff( - __DIR__.'/NoElseSniffTest.inc', - 'Codor.ControlStructures.NoElse' - ); - - $this->assertSame(5, $errorCount); + $results = $this->runner->sniff('NoElseSniffTest.inc'); + $this->assertSame(5, $results->getErrorCount()); } } \ No newline at end of file diff --git a/tests/Sniffs/Files/FunctionLengthSniffTest.php b/tests/Sniffs/Files/FunctionLengthSniffTest.php index c304439..be4fe12 100644 --- a/tests/Sniffs/Files/FunctionLengthSniffTest.php +++ b/tests/Sniffs/Files/FunctionLengthSniffTest.php @@ -2,21 +2,27 @@ namespace Codor\Tests\Sniffs\ControlStructures; -use Codor\Tests\CodeSnifferRunner; -use PHPUnit\Framework\TestCase; +use Codor\Tests\BaseTestCase; /** @group Files */ -class FunctionLengthSniffTest extends TestCase +class FunctionLengthSniffTest extends BaseTestCase { + + /** + * Sets up the test class. + * @return void + */ + public function setup() + { + parent::setup(); + + $this->runner->setSniff('Codor.Files.FunctionLength')->setFolder(__DIR__.'/Assets/FunctionLengthSniff/'); + } + /** @test */ public function it_detects_functions_that_are_over_the_max_allowed() { - $codeSnifferRunner = new CodeSnifferRunner(); - $errorCount = $codeSnifferRunner->detectErrorCountInFileForSniff( - __DIR__.'/Assets/FunctionLengthSniff/FunctionLengthSniff.inc', - 'Codor.Files.FunctionLength' - ); - - $this->assertSame(2, $errorCount); + $results = $this->runner->sniff('FunctionLengthSniff.inc'); + $this->assertSame(2, $results->getErrorCount()); } } \ No newline at end of file diff --git a/tests/Sniffs/Files/FunctionParameterSniffTest.php b/tests/Sniffs/Files/FunctionParameterSniffTest.php index 6b72110..647ce97 100644 --- a/tests/Sniffs/Files/FunctionParameterSniffTest.php +++ b/tests/Sniffs/Files/FunctionParameterSniffTest.php @@ -2,52 +2,38 @@ namespace Codor\Tests\Sniffs\ControlStructures; -use Codor\Tests\CodeSnifferRunner; -use PHPUnit\Framework\TestCase; +use Codor\Tests\BaseTestCase; /** @group Files */ -class FunctionParameterSniffTest extends TestCase +class FunctionParameterSniffTest extends BaseTestCase { + + public function setup() + { + parent::setup(); + + $this->runner->setSniff('Codor.Files.FunctionParameter')->setFolder(__DIR__.'/Assets/FunctionParameterSniff/'); + } + /** @test */ public function it_produces_an_error_when_a_function_has_4_or_more_parameters() { - $codeSnifferRunner = new CodeSnifferRunner(); - $errorCount = $codeSnifferRunner->detectErrorCountInFileForSniff( - __DIR__.'/Assets/FunctionParameterSniff/FunctionsWithFourOrMoreParameters.inc', - 'Codor.Files.FunctionParameter' - ); - - $this->assertSame(4, $errorCount); + $results = $this->runner->sniff('FunctionsWithFourOrMoreParameters.inc'); + $this->assertEquals(4, $results->getErrorCount()); } /** @test */ public function it_produces_a_warning_when_a_function_has_3_parameters() { - $codeSnifferRunner = new CodeSnifferRunner(); - $warningCount = $codeSnifferRunner->detectWarningCountInFileForSniff( - __DIR__.'/Assets/FunctionParameterSniff/FunctionWithThreeParameters.inc', - 'Codor.Files.FunctionParameter' - ); - - $this->assertSame(2, $warningCount); + $results = $this->runner->sniff('FunctionWithThreeParameters.inc'); + $this->assertEquals(2, $results->getWarningCount()); } /** @test */ public function it_produces_no_warnings_or_errors_with_functions_with_2_or_fewer_parameters() { - $codeSnifferRunner = new CodeSnifferRunner(); - - $errorCount = $codeSnifferRunner->detectErrorCountInFileForSniff( - __DIR__.'/Assets/FunctionParameterSniff/FunctionWithTwoOrFewerParameters.inc', - 'Codor.Files.FunctionParameter' - ); - - $warningCount = $codeSnifferRunner->detectWarningCountInFileForSniff( - __DIR__.'/Assets/FunctionParameterSniff/FunctionWithTwoOrFewerParameters.inc', - 'Codor.Files.FunctionParameter' - ); - - $this->assertSame(0, $errorCount); - $this->assertSame(0, $warningCount); + $results = $this->runner->sniff('FunctionWithTwoOrFewerParameters.inc'); + $this->assertEquals(0, $results->getWarningCount()); + $this->assertEquals(0, $results->getErrorCount()); } } \ No newline at end of file