Skip to content

Commit

Permalink
Fixes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
bmitch committed Dec 4, 2016
1 parent ce5a5fd commit 3593f33
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 60 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Codor\Tests;

use Codor\Tests\CodeSnifferRunner;
use PHPUnit\Framework\TestCase;

class BaseTestCase extends TestCase
{
/**
* The CodeSnifferRunner;
* @var CodeSnifferRunner
*/
protected $runner;

/**
* Sets up the test class.
* @return void
*/
public function setup()
{
parent::setup();

$this->runner = new CodeSnifferRunner();
}
}
67 changes: 59 additions & 8 deletions tests/CodeSnifferRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
24 changes: 13 additions & 11 deletions tests/Sniffs/ControlStructures/NoElseSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
26 changes: 16 additions & 10 deletions tests/Sniffs/Files/FunctionLengthSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
48 changes: 17 additions & 31 deletions tests/Sniffs/Files/FunctionParameterSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 3593f33

Please sign in to comment.