first commit

This commit is contained in:
root
2022-03-06 11:49:27 +00:00
commit 1984e55837
1387 changed files with 121949 additions and 0 deletions

View File

@ -0,0 +1,169 @@
<?php
/*
* This file is part of php-token-stream.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;
class PHP_Token_ClassTest extends TestCase
{
/**
* @var PHP_Token_CLASS
*/
private $class;
/**
* @var PHP_Token_FUNCTION
*/
private $function;
protected function setUp()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source2.php');
foreach ($ts as $token) {
if ($token instanceof PHP_Token_CLASS) {
$this->class = $token;
}
if ($token instanceof PHP_Token_FUNCTION) {
$this->function = $token;
break;
}
}
}
/**
* @covers PHP_Token_CLASS::getKeywords
*/
public function testGetClassKeywords()
{
$this->assertEquals('abstract', $this->class->getKeywords());
}
/**
* @covers PHP_Token_FUNCTION::getKeywords
*/
public function testGetFunctionKeywords()
{
$this->assertEquals('abstract,static', $this->function->getKeywords());
}
/**
* @covers PHP_Token_FUNCTION::getVisibility
*/
public function testGetFunctionVisibility()
{
$this->assertEquals('public', $this->function->getVisibility());
}
public function testIssue19()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'issue19.php');
foreach ($ts as $token) {
if ($token instanceof PHP_Token_CLASS) {
$this->assertFalse($token->hasInterfaces());
}
}
}
public function testIssue30()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'issue30.php');
$this->assertCount(1, $ts->getClasses());
}
public function testAnonymousClassesAreHandledCorrectly()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_that_declares_anonymous_class.php');
$classes = $ts->getClasses();
$this->assertEquals(
[
'class_with_method_that_declares_anonymous_class',
'AnonymousClass:9#31',
'AnonymousClass:10#55',
'AnonymousClass:11#75',
'AnonymousClass:12#91',
'AnonymousClass:13#107'
],
array_keys($classes)
);
}
/**
* @ticket https://github.com/sebastianbergmann/php-token-stream/issues/52
*/
public function testAnonymousClassesAreHandledCorrectly2()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_that_declares_anonymous_class2.php');
$classes = $ts->getClasses();
$this->assertEquals(['Test', 'AnonymousClass:4#23'], array_keys($classes));
$this->assertEquals(['methodOne', 'methodTwo'], array_keys($classes['Test']['methods']));
$this->assertEmpty($ts->getFunctions());
}
public function testImportedFunctionsAreHandledCorrectly()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'classUsesNamespacedFunction.php');
$this->assertEmpty($ts->getFunctions());
$this->assertCount(1, $ts->getClasses());
}
/**
* @ticket https://github.com/sebastianbergmann/php-code-coverage/issues/543
*/
public function testClassWithMultipleAnonymousClassesAndFunctionsIsHandledCorrectly()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_multiple_anonymous_classes_and_functions.php');
$classes = $ts->getClasses();
$this->assertArrayHasKey('class_with_multiple_anonymous_classes_and_functions', $classes);
$this->assertArrayHasKey('AnonymousClass:6#23', $classes);
$this->assertArrayHasKey('AnonymousClass:12#53', $classes);
$this->assertArrayHasKey('m', $classes['class_with_multiple_anonymous_classes_and_functions']['methods']);
$this->assertArrayHasKey('anonymousFunction:18#81', $classes['class_with_multiple_anonymous_classes_and_functions']['methods']);
$this->assertArrayHasKey('anonymousFunction:22#108', $classes['class_with_multiple_anonymous_classes_and_functions']['methods']);
}
/**
* @ticket https://github.com/sebastianbergmann/php-token-stream/issues/68
*/
public function testClassWithMethodNamedEmptyIsHandledCorrectly()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_named_empty.php');
$classes = $ts->getClasses();
$this->assertArrayHasKey('class_with_method_named_empty', $classes);
$this->assertArrayHasKey('empty', $classes['class_with_method_named_empty']['methods']);
}
/**
* @ticket https://github.com/sebastianbergmann/php-code-coverage/issues/424
*/
public function testSomething()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'php-code-coverage-issue-424.php');
$classes = $ts->getClasses();
$this->assertSame(5, $classes['Example']['methods']['even']['startLine']);
$this->assertSame(12, $classes['Example']['methods']['even']['endLine']);
$this->assertSame(7, $classes['Example']['methods']['anonymousFunction:7#28']['startLine']);
$this->assertSame(9, $classes['Example']['methods']['anonymousFunction:7#28']['endLine']);
}
}

View File

@ -0,0 +1,78 @@
<?php
/*
* This file is part of php-token-stream.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;
class PHP_Token_ClosureTest extends TestCase
{
/**
* @var PHP_Token_FUNCTION[]
*/
private $functions;
protected function setUp()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'closure.php');
foreach ($ts as $token) {
if ($token instanceof PHP_Token_FUNCTION) {
$this->functions[] = $token;
}
}
}
/**
* @covers PHP_Token_FUNCTION::getArguments
*/
public function testGetArguments()
{
$this->assertEquals(['$foo' => null, '$bar' => null], $this->functions[0]->getArguments());
$this->assertEquals(['$foo' => 'Foo', '$bar' => null], $this->functions[1]->getArguments());
$this->assertEquals(['$foo' => null, '$bar' => null, '$baz' => null], $this->functions[2]->getArguments());
$this->assertEquals(['$foo' => 'Foo', '$bar' => null, '$baz' => null], $this->functions[3]->getArguments());
$this->assertEquals([], $this->functions[4]->getArguments());
$this->assertEquals([], $this->functions[5]->getArguments());
}
/**
* @covers PHP_Token_FUNCTION::getName
*/
public function testGetName()
{
$this->assertEquals('anonymousFunction:2#5', $this->functions[0]->getName());
$this->assertEquals('anonymousFunction:3#27', $this->functions[1]->getName());
$this->assertEquals('anonymousFunction:4#51', $this->functions[2]->getName());
$this->assertEquals('anonymousFunction:5#71', $this->functions[3]->getName());
$this->assertEquals('anonymousFunction:6#93', $this->functions[4]->getName());
$this->assertEquals('anonymousFunction:7#106', $this->functions[5]->getName());
}
/**
* @covers PHP_Token::getLine
*/
public function testGetLine()
{
$this->assertEquals(2, $this->functions[0]->getLine());
$this->assertEquals(3, $this->functions[1]->getLine());
$this->assertEquals(4, $this->functions[2]->getLine());
$this->assertEquals(5, $this->functions[3]->getLine());
}
/**
* @covers PHP_TokenWithScope::getEndLine
*/
public function testGetEndLine()
{
$this->assertEquals(2, $this->functions[0]->getLine());
$this->assertEquals(3, $this->functions[1]->getLine());
$this->assertEquals(4, $this->functions[2]->getLine());
$this->assertEquals(5, $this->functions[3]->getLine());
}
}

View File

@ -0,0 +1,139 @@
<?php
/*
* This file is part of php-token-stream.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;
class PHP_Token_FunctionTest extends TestCase
{
/**
* @var PHP_Token_FUNCTION[]
*/
private $functions;
protected function setUp()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source.php');
foreach ($ts as $token) {
if ($token instanceof PHP_Token_FUNCTION) {
$this->functions[] = $token;
}
}
}
/**
* @covers PHP_Token_FUNCTION::getArguments
*/
public function testGetArguments()
{
$this->assertEquals([], $this->functions[0]->getArguments());
$this->assertEquals(
['$baz' => 'Baz'], $this->functions[1]->getArguments()
);
$this->assertEquals(
['$foobar' => 'Foobar'], $this->functions[2]->getArguments()
);
$this->assertEquals(
['$barfoo' => 'Barfoo'], $this->functions[3]->getArguments()
);
$this->assertEquals([], $this->functions[4]->getArguments());
$this->assertEquals(['$x' => null, '$y' => null], $this->functions[5]->getArguments());
}
/**
* @covers PHP_Token_FUNCTION::getName
*/
public function testGetName()
{
$this->assertEquals('foo', $this->functions[0]->getName());
$this->assertEquals('bar', $this->functions[1]->getName());
$this->assertEquals('foobar', $this->functions[2]->getName());
$this->assertEquals('barfoo', $this->functions[3]->getName());
$this->assertEquals('baz', $this->functions[4]->getName());
}
/**
* @covers PHP_Token::getLine
*/
public function testGetLine()
{
$this->assertEquals(5, $this->functions[0]->getLine());
$this->assertEquals(10, $this->functions[1]->getLine());
$this->assertEquals(17, $this->functions[2]->getLine());
$this->assertEquals(21, $this->functions[3]->getLine());
$this->assertEquals(29, $this->functions[4]->getLine());
}
/**
* @covers PHP_TokenWithScope::getEndLine
*/
public function testGetEndLine()
{
$this->assertEquals(5, $this->functions[0]->getEndLine());
$this->assertEquals(12, $this->functions[1]->getEndLine());
$this->assertEquals(19, $this->functions[2]->getEndLine());
$this->assertEquals(23, $this->functions[3]->getEndLine());
$this->assertEquals(31, $this->functions[4]->getEndLine());
}
/**
* @covers PHP_Token_FUNCTION::getDocblock
*/
public function testGetDocblock()
{
$this->assertNull($this->functions[0]->getDocblock());
$this->assertEquals(
"/**\n * @param Baz \$baz\n */",
$this->functions[1]->getDocblock()
);
$this->assertEquals(
"/**\n * @param Foobar \$foobar\n */",
$this->functions[2]->getDocblock()
);
$this->assertNull($this->functions[3]->getDocblock());
$this->assertNull($this->functions[4]->getDocblock());
}
public function testSignature()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source5.php');
$f = $ts->getFunctions();
$c = $ts->getClasses();
$i = $ts->getInterfaces();
$this->assertEquals(
'foo($a, array $b, array $c = array())',
$f['foo']['signature']
);
$this->assertEquals(
'm($a, array $b, array $c = array())',
$c['c']['methods']['m']['signature']
);
$this->assertEquals(
'm($a, array $b, array $c = array())',
$c['a']['methods']['m']['signature']
);
$this->assertEquals(
'm($a, array $b, array $c = array())',
$i['i']['methods']['m']['signature']
);
}
}

View File

@ -0,0 +1,65 @@
<?php
/*
* This file is part of php-token-stream.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;
class PHP_Token_IncludeTest extends TestCase
{
/**
* @var PHP_Token_Stream
*/
private $ts;
protected function setUp()
{
$this->ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source3.php');
}
/**
* @covers PHP_Token_Includes::getName
* @covers PHP_Token_Includes::getType
*/
public function testGetIncludes()
{
$this->assertSame(
['test4.php', 'test3.php', 'test2.php', 'test1.php'],
$this->ts->getIncludes()
);
}
/**
* @covers PHP_Token_Includes::getName
* @covers PHP_Token_Includes::getType
*/
public function testGetIncludesCategorized()
{
$this->assertSame(
[
'require_once' => ['test4.php'],
'require' => ['test3.php'],
'include_once' => ['test2.php'],
'include' => ['test1.php']
],
$this->ts->getIncludes(true)
);
}
/**
* @covers PHP_Token_Includes::getName
* @covers PHP_Token_Includes::getType
*/
public function testGetIncludesCategory()
{
$this->assertSame(
['test4.php'],
$this->ts->getIncludes(true, 'require_once')
);
}
}

View File

@ -0,0 +1,195 @@
<?php
/*
* This file is part of php-token-stream.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;
class PHP_Token_InterfaceTest extends TestCase
{
/**
* @var PHP_Token_CLASS
*/
private $class;
/**
* @var PHP_Token_INTERFACE[]
*/
private $interfaces;
protected function setUp()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source4.php');
$i = 0;
foreach ($ts as $token) {
if ($token instanceof PHP_Token_CLASS) {
$this->class = $token;
} elseif ($token instanceof PHP_Token_INTERFACE) {
$this->interfaces[$i] = $token;
$i++;
}
}
}
/**
* @covers PHP_Token_INTERFACE::getName
*/
public function testGetName()
{
$this->assertEquals(
'iTemplate', $this->interfaces[0]->getName()
);
}
/**
* @covers PHP_Token_INTERFACE::getParent
*/
public function testGetParentNotExists()
{
$this->assertFalse(
$this->interfaces[0]->getParent()
);
}
/**
* @covers PHP_Token_INTERFACE::hasParent
*/
public function testHasParentNotExists()
{
$this->assertFalse(
$this->interfaces[0]->hasParent()
);
}
/**
* @covers PHP_Token_INTERFACE::getParent
*/
public function testGetParentExists()
{
$this->assertEquals(
'a', $this->interfaces[2]->getParent()
);
}
/**
* @covers PHP_Token_INTERFACE::hasParent
*/
public function testHasParentExists()
{
$this->assertTrue(
$this->interfaces[2]->hasParent()
);
}
/**
* @covers PHP_Token_INTERFACE::getInterfaces
*/
public function testGetInterfacesExists()
{
$this->assertEquals(
['b'],
$this->class->getInterfaces()
);
}
/**
* @covers PHP_Token_INTERFACE::hasInterfaces
*/
public function testHasInterfacesExists()
{
$this->assertTrue(
$this->class->hasInterfaces()
);
}
/**
* @covers PHP_Token_INTERFACE::getPackage
*/
public function testGetPackageNamespace()
{
$tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php');
foreach ($tokenStream as $token) {
if ($token instanceof PHP_Token_INTERFACE) {
$package = $token->getPackage();
$this->assertSame('Foo\\Bar', $package['namespace']);
}
}
}
public function provideFilesWithClassesWithinMultipleNamespaces()
{
return [
[TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingBraces.php'],
[TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingNonBraceSyntax.php'],
];
}
/**
* @dataProvider provideFilesWithClassesWithinMultipleNamespaces
* @covers PHP_Token_INTERFACE::getPackage
*/
public function testGetPackageNamespaceForFileWithMultipleNamespaces($filepath)
{
$tokenStream = new PHP_Token_Stream($filepath);
$firstClassFound = false;
foreach ($tokenStream as $token) {
if ($firstClassFound === false && $token instanceof PHP_Token_INTERFACE) {
$package = $token->getPackage();
$this->assertSame('TestClassInBar', $token->getName());
$this->assertSame('Foo\\Bar', $package['namespace']);
$firstClassFound = true;
continue;
}
// Secound class
if ($token instanceof PHP_Token_INTERFACE) {
$package = $token->getPackage();
$this->assertSame('TestClassInBaz', $token->getName());
$this->assertSame('Foo\\Baz', $package['namespace']);
return;
}
}
$this->fail('Seachring for 2 classes failed');
}
public function testGetPackageNamespaceIsEmptyForInterfacesThatAreNotWithinNamespaces()
{
foreach ($this->interfaces as $token) {
$package = $token->getPackage();
$this->assertSame('', $package['namespace']);
}
}
/**
* @covers PHP_Token_INTERFACE::getPackage
*/
public function testGetPackageNamespaceWhenExtentingFromNamespaceClass()
{
$tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classExtendsNamespacedClass.php');
$firstClassFound = false;
foreach ($tokenStream as $token) {
if ($firstClassFound === false && $token instanceof PHP_Token_INTERFACE) {
$package = $token->getPackage();
$this->assertSame('Baz', $token->getName());
$this->assertSame('Foo\\Bar', $package['namespace']);
$firstClassFound = true;
continue;
}
if ($token instanceof PHP_Token_INTERFACE) {
$package = $token->getPackage();
$this->assertSame('Extender', $token->getName());
$this->assertSame('Other\\Space', $package['namespace']);
return;
}
}
$this->fail('Searching for 2 classes failed');
}
}

View File

@ -0,0 +1,69 @@
<?php
/*
* This file is part of php-token-stream.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;
class PHP_Token_NamespaceTest extends TestCase
{
/**
* @covers PHP_Token_NAMESPACE::getName
*/
public function testGetName()
{
$tokenStream = new PHP_Token_Stream(
TEST_FILES_PATH . 'classInNamespace.php'
);
foreach ($tokenStream as $token) {
if ($token instanceof PHP_Token_NAMESPACE) {
$this->assertSame('Foo\\Bar', $token->getName());
}
}
}
public function testGetStartLineWithUnscopedNamespace()
{
$tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php');
foreach ($tokenStream as $token) {
if ($token instanceof PHP_Token_NAMESPACE) {
$this->assertSame(2, $token->getLine());
}
}
}
public function testGetEndLineWithUnscopedNamespace()
{
$tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php');
foreach ($tokenStream as $token) {
if ($token instanceof PHP_Token_NAMESPACE) {
$this->assertSame(2, $token->getEndLine());
}
}
}
public function testGetStartLineWithScopedNamespace()
{
$tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInScopedNamespace.php');
foreach ($tokenStream as $token) {
if ($token instanceof PHP_Token_NAMESPACE) {
$this->assertSame(2, $token->getLine());
}
}
}
public function testGetEndLineWithScopedNamespace()
{
$tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInScopedNamespace.php');
foreach ($tokenStream as $token) {
if ($token instanceof PHP_Token_NAMESPACE) {
$this->assertSame(8, $token->getEndLine());
}
}
}
}