Skip to content

Commit

Permalink
Added basic tests for a couple of PHP_CodeSniffer_File methods (ref s…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Aug 3, 2016
1 parent 301d520 commit 90a2bfc
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 0 deletions.
6 changes: 6 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ http://pear.php.net/dtd/package-2.0.xsd">
<dir name="tests">
<dir name="Core">
<dir name="File">
<file baseinstalldir="" name="FindExtendedClassNameTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
<file baseinstalldir="" name="FindImplementedInterfaceNamesTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
<file baseinstalldir="" name="GetMethodParametersTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
Expand Down
4 changes: 4 additions & 0 deletions tests/Core/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
require_once 'IsCamelCapsTest.php';
require_once 'ErrorSuppressionTest.php';
require_once 'File/GetMethodParametersTest.php';
require_once 'File/FindExtendedClassNameTest.php';
require_once 'File/FindImplementedInterfaceNamesTest.php';

if (is_file(dirname(__FILE__).'/../../CodeSniffer.php') === true) {
// We are not installed.
Expand Down Expand Up @@ -70,6 +72,8 @@ public static function suite()
$suite->addTestSuite('Core_IsCamelCapsTest');
$suite->addTestSuite('Core_ErrorSuppressionTest');
$suite->addTestSuite('Core_File_GetMethodParametersTest');
$suite->addTestSuite('Core_File_FindExtendedClassNameTest');
$suite->addTestSuite('Core_File_FindImplementedInterfaceNamesTest');
return $suite;

}//end suite()
Expand Down
169 changes: 169 additions & 0 deletions tests/Core/File/FindExtendedClassNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php
/**
* Tests for the PHP_CodeSniffer_File:findExtendedClassName method.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <[email protected]>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

/**
* Tests for the PHP_CodeSniffer_File:findExtendedClassName method.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <[email protected]>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @version Release: @package_version@
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Core_File_FindExtendedClassNameTest extends PHPUnit_Framework_TestCase
{

/**
* The PHP_CodeSniffer_File object containing parsed contents of this file.
*
* @var PHP_CodeSniffer_File
*/
private $_phpcsFile;


/**
* Initialize & tokenize PHP_CodeSniffer_File with code from this file.
*
* Methods used for these tests can be found at the bottom of
* this file.
*
* @return void
*/
public function setUp()
{
$phpcs = new PHP_CodeSniffer();
$this->_phpcsFile = new PHP_CodeSniffer_File(
__FILE__,
array(),
array(),
$phpcs
);

$contents = file_get_contents(__FILE__);
$this->_phpcsFile->start($contents);

}//end setUp()


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

}//end tearDown()


/**
* Test a class that extends another.
*
* @return void
*/
public function testExtendedClass()
{
$start = ($this->_phpcsFile->numTokens - 1);
$class = $this->_phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
'/* testExtendedClass */'
);

$found = $this->_phpcsFile->findExtendedClassName(($class + 2));
$this->assertSame('testFECNClass', $found);

}//end testExtendedClass()


/**
* Test a class that extends another, using namespaces.
*
* @return void
*/
public function testNamespacedClass()
{
$start = ($this->_phpcsFile->numTokens - 1);
$class = $this->_phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
'/* testNamespacedClass */'
);

$found = $this->_phpcsFile->findExtendedClassName(($class + 2));
$this->assertSame('\testFECNClass', $found);

}//end testNamespacedClass()


/**
* Test a class that doesn't extend another.
*
* @return void
*/
public function testNonExtendedClass()
{
$start = ($this->_phpcsFile->numTokens - 1);
$class = $this->_phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
'/* testNonExtendedClass */'
);

$found = $this->_phpcsFile->findExtendedClassName(($class + 2));
$this->assertFalse($found);

}//end testNonExtendedClass()


/**
* Test an interface.
*
* @return void
*/
public function testInterface()
{
$start = ($this->_phpcsFile->numTokens - 1);
$class = $this->_phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
'/* testInterface */'
);

$found = $this->_phpcsFile->findExtendedClassName(($class + 2));
$this->assertFalse($found);

}//end testInterface()


}//end class

// @codingStandardsIgnoreStart
class testFECNClass {}
/* testExtendedClass */ class testFECNExtendedClass extends testFECNClass {}
/* testNamespacedClass */ class testFECNNamespacedClass extends \testFECNClass {}
/* testNonExtendedClass */ class testFECNNonExtendedClass {}
/* testInterface */ interface testFECNInterface {}
// @codingStandardsIgnoreEnd
192 changes: 192 additions & 0 deletions tests/Core/File/FindImplementedInterfaceNamesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php
/**
* Tests for the PHP_CodeSniffer_File:findImplementedInterfaceNames method.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <[email protected]>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

/**
* Tests for the PHP_CodeSniffer_File:findImplementedInterfaceNames method.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <[email protected]>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @version Release: @package_version@
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Core_File_FindImplementedInterfaceNamesTest extends PHPUnit_Framework_TestCase
{

/**
* The PHP_CodeSniffer_File object containing parsed contents of this file.
*
* @var PHP_CodeSniffer_File
*/
private $_phpcsFile;


/**
* Initialize & tokenize PHP_CodeSniffer_File with code from this file.
*
* Methods used for these tests can be found at the bottom of
* this file.
*
* @return void
*/
public function setUp()
{
$phpcs = new PHP_CodeSniffer();
$this->_phpcsFile = new PHP_CodeSniffer_File(
__FILE__,
array(),
array(),
$phpcs
);

$contents = file_get_contents(__FILE__);
$this->_phpcsFile->start($contents);

}//end setUp()


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

}//end tearDown()


/**
* Test a class that implements a single interface.
*
* @return void
*/
public function testImplementedClass()
{
$start = ($this->_phpcsFile->numTokens - 1);
$class = $this->_phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
'/* testImplementedClass */'
);

$found = $this->_phpcsFile->findImplementedInterfaceNames(($class + 2));
$this->assertSame(array('testFIINInterface'), $found);

}//end testImplementedClass()


/**
* Test a class that implements multiple interfaces.
*
* @return void
*/
public function testMultiImplementedClass()
{
$start = ($this->_phpcsFile->numTokens - 1);
$class = $this->_phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
'/* testMultiImplementedClass */'
);

$found = $this->_phpcsFile->findImplementedInterfaceNames(($class + 2));
$this->assertSame(array('testFIINInterface', 'testFIINInterface2'), $found);

}//end testMultiImplementedClass()


/**
* Test a class that implements an interface, using namespaces.
*
* @return void
*/
public function testNamespacedClass()
{
$start = ($this->_phpcsFile->numTokens - 1);
$class = $this->_phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
'/* testNamespacedClass */'
);

$found = $this->_phpcsFile->findImplementedInterfaceNames(($class + 2));
$this->assertSame(array('\testFIINInterface'), $found);

}//end testNamespacedClass()


/**
* Test a class that doesn't implement an interface.
*
* @return void
*/
public function testNonImplementedClass()
{
$start = ($this->_phpcsFile->numTokens - 1);
$class = $this->_phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
'/* testNonImplementedClass */'
);

$found = $this->_phpcsFile->findImplementedInterfaceNames(($class + 2));
$this->assertFalse($found);

}//end testNonImplementedClass()


/**
* Test an interface.
*
* @return void
*/
public function testInterface()
{
$start = ($this->_phpcsFile->numTokens - 1);
$class = $this->_phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
'/* testInterface */'
);

$found = $this->_phpcsFile->findImplementedInterfaceNames(($class + 2));
$this->assertFalse($found);

}//end testInterface()


}//end class

// @codingStandardsIgnoreStart
interface testFIINInterface2 {}
/* testInterface */ interface testFIINInterface {}
/* testImplementedClass */ class testFIINImplementedClass implements testFIINInterface {}
/* testMultiImplementedClass */ class testFIINMultiImplementedClass implements testFIINInterface, testFIINInterface2 {}
/* testNamespacedClass */ class testFIINNamespacedClass implements \testFIINInterface {}
/* testNonImplementedClass */ class testFIINNonImplementedClass {}
// @codingStandardsIgnoreEnd

0 comments on commit 90a2bfc

Please sign in to comment.