Skip to content

Commit

Permalink
Implement use of the new AbstractMethodUnitTest class
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed Aug 27, 2019
1 parent a9d2351 commit 511a538
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 448 deletions.
108 changes: 32 additions & 76 deletions tests/Core/File/FindEndOfStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,11 @@

namespace PHP_CodeSniffer\Tests\Core\File;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Files\DummyFile;
use PHPUnit\Framework\TestCase;
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;

class FindEndOfStatementTest extends TestCase
class FindEndOfStatementTest extends AbstractMethodUnitTest
{

/**
* The PHP_CodeSniffer_File object containing parsed contents of the test case file.
*
* @var \PHP_CodeSniffer\Files\File
*/
private $phpcsFile;


/**
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
*
* Methods used for these tests can be found in a test case file in the same
* directory and with the same name, using the .inc extension.
*
* @return void
*/
public function setUp()
{
$config = new Config();
$config->standards = ['Generic'];

$ruleset = new Ruleset($config);

$pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc';
$this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config);
$this->phpcsFile->process();

}//end setUp()


/**
* Clean up after finished test.
*
* @return void
*/
public function tearDown()
{
unset($this->phpcsFile);

}//end tearDown()


/**
* Test a simple assignment.
Expand All @@ -66,10 +22,10 @@ public function tearDown()
*/
public function testSimpleAssignment()
{
$start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSimpleAssignment */') + 2);
$found = $this->phpcsFile->findEndOfStatement($start);
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSimpleAssignment */') + 2);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = $this->phpcsFile->getTokens();
$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 5)], $tokens[$found]);

}//end testSimpleAssignment()
Expand All @@ -82,10 +38,10 @@ public function testSimpleAssignment()
*/
public function testControlStructure()
{
$start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testControlStructure */') + 2);
$found = $this->phpcsFile->findEndOfStatement($start);
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testControlStructure */') + 2);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = $this->phpcsFile->getTokens();
$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 6)], $tokens[$found]);

}//end testControlStructure()
Expand All @@ -98,10 +54,10 @@ public function testControlStructure()
*/
public function testClosureAssignment()
{
$start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testClosureAssignment */') + 2);
$found = $this->phpcsFile->findEndOfStatement($start);
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testClosureAssignment */') + 2);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = $this->phpcsFile->getTokens();
$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 13)], $tokens[$found]);

}//end testClosureAssignment()
Expand All @@ -115,24 +71,24 @@ public function testClosureAssignment()
public function testHeredocFunctionArg()
{
// Find the end of the function.
$start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testHeredocFunctionArg */') + 2);
$found = $this->phpcsFile->findEndOfStatement($start);
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testHeredocFunctionArg */') + 2);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = $this->phpcsFile->getTokens();
$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 10)], $tokens[$found]);

// Find the end of the heredoc.
$start += 2;
$found = $this->phpcsFile->findEndOfStatement($start);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = $this->phpcsFile->getTokens();
$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 4)], $tokens[$found]);

// Find the end of the last arg.
$start = ($found + 2);
$found = $this->phpcsFile->findEndOfStatement($start);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = $this->phpcsFile->getTokens();
$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[$start], $tokens[$found]);

}//end testHeredocFunctionArg()
Expand All @@ -146,24 +102,24 @@ public function testHeredocFunctionArg()
public function testSwitch()
{
// Find the end of the switch.
$start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSwitch */') + 2);
$found = $this->phpcsFile->findEndOfStatement($start);
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSwitch */') + 2);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = $this->phpcsFile->getTokens();
$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 28)], $tokens[$found]);

// Find the end of the case.
$start += 9;
$found = $this->phpcsFile->findEndOfStatement($start);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = $this->phpcsFile->getTokens();
$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 8)], $tokens[$found]);

// Find the end of default case.
$start += 11;
$found = $this->phpcsFile->findEndOfStatement($start);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = $this->phpcsFile->getTokens();
$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 6)], $tokens[$found]);

}//end testSwitch()
Expand All @@ -177,24 +133,24 @@ public function testSwitch()
public function testStatementAsArrayValue()
{
// Test short array syntax.
$start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testStatementAsArrayValue */') + 7);
$found = $this->phpcsFile->findEndOfStatement($start);
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testStatementAsArrayValue */') + 7);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = $this->phpcsFile->getTokens();
$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 2)], $tokens[$found]);

// Test long array syntax.
$start += 12;
$found = $this->phpcsFile->findEndOfStatement($start);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = $this->phpcsFile->getTokens();
$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 2)], $tokens[$found]);

// Test same statement outside of array.
$start += 10;
$found = $this->phpcsFile->findEndOfStatement($start);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = $this->phpcsFile->getTokens();
$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 3)], $tokens[$found]);

}//end testStatementAsArrayValue()
Expand Down
56 changes: 6 additions & 50 deletions tests/Core/File/FindExtendedClassNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,11 @@

namespace PHP_CodeSniffer\Tests\Core\File;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Files\DummyFile;
use PHPUnit\Framework\TestCase;
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;

class FindExtendedClassNameTest extends TestCase
class FindExtendedClassNameTest extends AbstractMethodUnitTest
{

/**
* The PHP_CodeSniffer_File object containing parsed contents of the test case file.
*
* @var \PHP_CodeSniffer\Files\File
*/
private $phpcsFile;


/**
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
*
* Methods used for these tests can be found in a test case file in the same
* directory and with the same name, using the .inc extension.
*
* @return void
*/
public function setUp()
{
$config = new Config();
$config->standards = ['Generic'];

$ruleset = new Ruleset($config);

$pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc';
$this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config);
$this->phpcsFile->process();

}//end setUp()


/**
* Clean up after finished test.
*
* @return void
*/
public function tearDown()
{
unset($this->phpcsFile);

}//end tearDown()


/**
* Test retrieving the name of the class being extended by another class
Expand All @@ -72,17 +28,17 @@ public function tearDown()
*/
public function testFindExtendedClassName($identifier, $expected)
{
$start = ($this->phpcsFile->numTokens - 1);
$delim = $this->phpcsFile->findPrevious(
$start = (self::$phpcsFile->numTokens - 1);
$delim = self::$phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
$identifier
);
$OOToken = $this->phpcsFile->findNext([T_CLASS, T_ANON_CLASS, T_INTERFACE], ($delim + 1));
$OOToken = self::$phpcsFile->findNext([T_CLASS, T_ANON_CLASS, T_INTERFACE], ($delim + 1));

$result = $this->phpcsFile->findExtendedClassName($OOToken);
$result = self::$phpcsFile->findExtendedClassName($OOToken);
$this->assertSame($expected, $result);

}//end testFindExtendedClassName()
Expand Down
56 changes: 6 additions & 50 deletions tests/Core/File/FindImplementedInterfaceNamesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,11 @@

namespace PHP_CodeSniffer\Tests\Core\File;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Files\DummyFile;
use PHPUnit\Framework\TestCase;
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;

class FindImplementedInterfaceNamesTest extends TestCase
class FindImplementedInterfaceNamesTest extends AbstractMethodUnitTest
{

/**
* The \PHP_CodeSniffer\Files\File object containing parsed contents of the test case file.
*
* @var \PHP_CodeSniffer\Files\File
*/
private $phpcsFile;


/**
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
*
* Methods used for these tests can be found in a test case file in the same
* directory and with the same name, using the .inc extension.
*
* @return void
*/
public function setUp()
{
$config = new Config();
$config->standards = ['Generic'];

$ruleset = new Ruleset($config);

$pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc';
$this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config);
$this->phpcsFile->process();

}//end setUp()


/**
* Clean up after finished test.
*
* @return void
*/
public function tearDown()
{
unset($this->phpcsFile);

}//end tearDown()


/**
* Test retrieving the name(s) of the interfaces being implemented by a class.
Expand All @@ -71,17 +27,17 @@ public function tearDown()
*/
public function testFindImplementedInterfaceNames($identifier, $expected)
{
$start = ($this->phpcsFile->numTokens - 1);
$delim = $this->phpcsFile->findPrevious(
$start = (self::$phpcsFile->numTokens - 1);
$delim = self::$phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
$identifier
);
$OOToken = $this->phpcsFile->findNext([T_CLASS, T_ANON_CLASS, T_INTERFACE], ($delim + 1));
$OOToken = self::$phpcsFile->findNext([T_CLASS, T_ANON_CLASS, T_INTERFACE], ($delim + 1));

$result = $this->phpcsFile->findImplementedInterfaceNames($OOToken);
$result = self::$phpcsFile->findImplementedInterfaceNames($OOToken);
$this->assertSame($expected, $result);

}//end testFindImplementedInterfaceNames()
Expand Down
Loading

0 comments on commit 511a538

Please sign in to comment.