Skip to content

Commit

Permalink
Merge pull request #369 from jrfnl/feature/test-consistency-and-speed
Browse files Browse the repository at this point in the history
Test suite consistency & restrict tests to run only against their own sniff
  • Loading branch information
wimg committed Apr 2, 2017
2 parents e572fe5 + e35b1a6 commit 043b66b
Show file tree
Hide file tree
Showing 59 changed files with 846 additions and 426 deletions.
2 changes: 1 addition & 1 deletion Sniffs/PHP/DefaultTimezoneRequiredSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @author Wim Godden <wim.godden@cu.be>
* @copyright 2012 Cu.be Solutions bvba
*/
class PHPCompatibility_Sniffs_PHP_DefaultTimeZoneRequiredSniff extends PHPCompatibility_Sniff
class PHPCompatibility_Sniffs_PHP_DefaultTimezoneRequiredSniff extends PHPCompatibility_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
Expand Down
29 changes: 27 additions & 2 deletions Tests/BaseSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
class BaseSniffTest extends PHPCompatibility_Testcase_Wrapper
{
const STANDARD_NAME = 'PHPCompatibility';

/**
* The PHP_CodeSniffer object used for testing.
*
Expand Down Expand Up @@ -58,7 +60,14 @@ protected function setUp()
self::$phpcs->cli->setCommandLineValues(array('-pq', '--colors'));
}

self::$phpcs->process(array(), dirname( __FILE__ ) . '/../');
// Restrict the sniffing of the test case files to the particular sniff being tested.
if (method_exists('PHP_CodeSniffer', 'initStandard')) {
self::$phpcs->initStandard(self::STANDARD_NAME, array($this->getSniffCode()));
} else {
// PHPCS 1.x
self::$phpcs->process(array(), dirname( __FILE__ ) . '/../', array($this->getSniffCode()));
}

self::$phpcs->setIgnorePatterns(array());

parent::setUp();
Expand All @@ -85,6 +94,16 @@ public static function tearDownAfterClass()
self::$sniffFiles = array();
}

/**
* Get the sniff code for the current sniff being tested.
*
* @return string
*/
protected function getSniffCode()
{
return self::STANDARD_NAME . '.PHP.' . str_replace('SniffTest', '', get_class($this));
}

/**
* Sniff a file and return resulting file object
*
Expand All @@ -104,7 +123,13 @@ public function sniffFile($filename, $targetPhpVersion = 'none')

$pathToFile = realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . $filename;
try {
self::$sniffFiles[$filename][$targetPhpVersion] = self::$phpcs->processFile($pathToFile);
if (method_exists('PHP_CodeSniffer', 'initStandard')) {
self::$sniffFiles[$filename][$targetPhpVersion] = self::$phpcs->processFile($pathToFile);
} else {
// PHPCS 1.x - Sniff code restrictions have to be passed via the function call.
self::$sniffFiles[$filename][$targetPhpVersion] = self::$phpcs->processFile($pathToFile, null, array($this->getSniffCode()));
}

} catch (Exception $e) {
$this->fail('An unexpected exception has been caught: ' . $e->getMessage());
return false;
Expand Down
17 changes: 13 additions & 4 deletions Tests/Sniffs/PHP/ConstantArraysUsingDefineSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ConstantArraysUsingDefineSniffTest extends BaseSniffTest
const TEST_FILE = 'sniff-examples/constant_arrays_using_define.php';

/**
* Verify that checking for a specific version works
* testConstantArraysUsingDefine
*
* @dataProvider dataConstantArraysUsingDefine
*
Expand All @@ -33,9 +33,6 @@ class ConstantArraysUsingDefineSniffTest extends BaseSniffTest
*/
public function testConstantArraysUsingDefine($line)
{
$file = $this->sniffFile(self::TEST_FILE, '7.0');
$this->assertNoViolation($file, $line);

$file = $this->sniffFile(self::TEST_FILE, '5.6');
$this->assertError($file, $line, 'Constant arrays using define are not allowed in PHP 5.6 or earlier');
}
Expand Down Expand Up @@ -93,4 +90,16 @@ public function dataNoFalsePositives()
);
}


/**
* Verify no notices are thrown at all.
*
* @return void
*/
public function testNoViolationsInFileOnValidVersion()
{
$file = $this->sniffFile(self::TEST_FILE, '7.0');
$this->assertNoViolation($file);
}

}
15 changes: 9 additions & 6 deletions Tests/Sniffs/PHP/DefaultTimezoneRequiredSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
class DefaultTimezoneRequiredSniffTest extends BaseSniffTest
{
const TEST_FILE = 'sniff-examples/timezone.php';

/**
* Test ini timezone setting
*
Expand All @@ -31,22 +33,23 @@ public function testIniTimezoneIsSet()
// We'll supress this so PHPunit wont catch the warning
@ini_set('date.timezone', false);

$file = $this->sniffFile('sniff-examples/timezone.php');
$file = $this->sniffFile(self::TEST_FILE, '5.4');

$this->assertError($file, 1, 'Default timezone is required since PHP 5.4');

ini_set('date.timezone', $timezone);
}


/**
* Test setting the testVersion in the PHPCS object
* Verify no notices are thrown at all.
*
* @return void
*/
public function testSettingTestVersion()
public function testNoViolationsInFileOnValidVersion()
{
$file = $this->sniffFile('sniff-examples/timezone.php', '5.3');

$this->assertNoViolation($file, 1);
$file = $this->sniffFile(self::TEST_FILE, '5.3');
$this->assertNoViolation($file);
}

}
14 changes: 13 additions & 1 deletion Tests/Sniffs/PHP/DeprecatedFunctionsSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ public function dataDeprecatedRemovedFunctionWithAlternative()
*/
public function testNoFalsePositives($line)
{
$file = $this->sniffFile(self::TEST_FILE, '99.0'); // High version beyond latest deprecated function version.
$file = $this->sniffFile(self::TEST_FILE, '99.0'); // High version beyond latest deprecation.
$this->assertNoViolation($file, $line);
}

Expand Down Expand Up @@ -465,4 +465,16 @@ public function dataNoFalsePositives()
return $testCases;
}


/**
* Verify no notices are thrown at all.
*
* @return void
*/
public function testNoViolationsInFileOnValidVersion()
{
$file = $this->sniffFile(self::TEST_FILE, '5.0'); // Low version below the first deprecation.
$this->assertNoViolation($file);
}

}
36 changes: 24 additions & 12 deletions Tests/Sniffs/PHP/DeprecatedIniDirectivesSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,17 @@ public function testRemovedDirectives($iniName, $removedIn, $lines, $okVersion,
public function dataRemovedDirectives()
{
return array(
array('ifx.allow_persistent', '5.2.1', array(92, 93), '5.1', '5.3'),
array('ifx.blobinfile', '5.2.1', array(95, 96), '5.1', '5.3'),
array('ifx.byteasvarchar', '5.2.1', array(98, 99), '5.1', '5.3'),
array('ifx.charasvarchar', '5.2.1', array(101, 102), '5.1', '5.3'),
array('ifx.default_host', '5.2.1', array(104, 105), '5.1', '5.3'),
array('ifx.default_password', '5.2.1', array(107, 108), '5.1', '5.3'),
array('ifx.default_user', '5.2.1', array(110, 111), '5.1', '5.3'),
array('ifx.max_links', '5.2.1', array(113, 114), '5.1', '5.3'),
array('ifx.max_persistent', '5.2.1', array(116, 117), '5.1', '5.3'),
array('ifx.nullformat', '5.2.1', array(119, 120), '5.1', '5.3'),
array('ifx.textasvarchar', '5.2.1', array(122, 123), '5.1', '5.3'),
array('ifx.allow_persistent', '5.2.1', array(92, 93), '5.2', '5.3'),
array('ifx.blobinfile', '5.2.1', array(95, 96), '5.2', '5.3'),
array('ifx.byteasvarchar', '5.2.1', array(98, 99), '5.2', '5.3'),
array('ifx.charasvarchar', '5.2.1', array(101, 102), '5.2', '5.3'),
array('ifx.default_host', '5.2.1', array(104, 105), '5.2', '5.3'),
array('ifx.default_password', '5.2.1', array(107, 108), '5.2', '5.3'),
array('ifx.default_user', '5.2.1', array(110, 111), '5.2', '5.3'),
array('ifx.max_links', '5.2.1', array(113, 114), '5.2', '5.3'),
array('ifx.max_persistent', '5.2.1', array(116, 117), '5.2', '5.3'),
array('ifx.nullformat', '5.2.1', array(119, 120), '5.2', '5.3'),
array('ifx.textasvarchar', '5.2.1', array(122, 123), '5.2', '5.3'),

array('zend.ze1_compatibility_mode', '5.3', array(36, 37), '5.2'),

Expand All @@ -288,7 +288,7 @@ public function dataRemovedDirectives()
*/
public function testNoFalsePositives($line)
{
$file = $this->sniffFile(self::TEST_FILE);
$file = $this->sniffFile(self::TEST_FILE, '99.0'); // High version beyond latest deprecation.
$this->assertNoViolation($file, $line);
}

Expand All @@ -314,4 +314,16 @@ public function dataNoFalsePositives()
);
}


/**
* Verify no notices are thrown at all.
*
* @return void
*/
public function testNoViolationsInFileOnValidVersion()
{
$file = $this->sniffFile(self::TEST_FILE, '5.0'); // Low version below the first deprecation.
$this->assertNoViolation($file);
}

}
15 changes: 12 additions & 3 deletions Tests/Sniffs/PHP/DeprecatedNewReferenceSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class DeprecatedNewReferenceSniffTest extends BaseSniffTest
*/
public function testDeprecatedNewReference($line)
{
$file = $this->sniffFile(self::TEST_FILE, '5.2');
$this->assertNoViolation($file, $line);

$file = $this->sniffFile(self::TEST_FILE, '5.3');
$this->assertWarning($file, $line, 'Assigning the return value of new by reference is deprecated in PHP 5.3');

Expand Down Expand Up @@ -71,4 +68,16 @@ public function testNoFalsePositives()
$this->assertNoViolation($file, 8);
}


/**
* Verify no notices are thrown at all.
*
* @return void
*/
public function testNoViolationsInFileOnValidVersion()
{
$file = $this->sniffFile(self::TEST_FILE, '5.2');
$this->assertNoViolation($file);
}

}
20 changes: 15 additions & 5 deletions Tests/Sniffs/PHP/DeprecatedPHP4StyleConstructorsSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ class DeprecatedPHP4StyleConstructorsSniffTest extends BaseSniffTest
*/
public function testIsDeprecated($line)
{
$file = $this->sniffFile(self::TEST_FILE, '5.6');
$this->assertNoViolation($file, $line);

$file = $this->sniffFile(self::TEST_FILE, '7.0');
$this->assertWarning($file, $line, 'Use of deprecated PHP4 style class constructor is not supported since PHP 7');
}
Expand All @@ -46,7 +43,7 @@ public function testIsDeprecated($line)
* @return array
*/
public function dataIsDeprecated()
{
{
return array(
array(3),
array(18),
Expand Down Expand Up @@ -77,7 +74,7 @@ public function testNoFalsePositives($line)
* @return array
*/
public function dataNoFalsePositives()
{
{
$testCases = array(
array(9),
);
Expand All @@ -89,4 +86,17 @@ public function dataNoFalsePositives()

return $testCases;
}


/**
* Verify no notices are thrown at all.
*
* @return void
*/
public function testNoViolationsInFileOnValidVersion()
{
$file = $this->sniffFile(self::TEST_FILE, '5.6');
$this->assertNoViolation($file);
}

}
18 changes: 14 additions & 4 deletions Tests/Sniffs/PHP/EmptyNonVariableSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public function testEmptyNonVariable($line)
{
$file = $this->sniffFile(self::TEST_FILE, '5.4');
$this->assertError($file, $line, 'Only variables can be passed to empty() prior to PHP 5.5.');

$file = $this->sniffFile(self::TEST_FILE, '5.5');
$this->assertNoViolation($file, $line);
}

/**
Expand Down Expand Up @@ -90,7 +87,7 @@ public function dataEmptyNonVariable()
*/
public function testNoFalsePositives($line)
{
$file = $this->sniffFile(self::TEST_FILE, '5.3');
$file = $this->sniffFile(self::TEST_FILE, '5.4');
$this->assertNoViolation($file, $line);
}

Expand Down Expand Up @@ -137,4 +134,17 @@ public function dataNoFalsePositives()
array(65),
);
}


/**
* Verify no notices are thrown at all.
*
* @return void
*/
public function testNoViolationsInFileOnValidVersion()
{
$file = $this->sniffFile(self::TEST_FILE, '5.5');
$this->assertNoViolation($file);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,11 @@ public function dataNoFalsePositives()
array(106),
);
}


/*
* `testNoViolationsInFileOnValidVersion` test omitted as this sniff will throw a warning
* on invalid use of the construct in pre-PHP 7 versions.
*/

}
Loading

0 comments on commit 043b66b

Please sign in to comment.