Add Composer autoloading functions and PHPStan for testing

This commit is contained in:
Alex Cabal 2019-02-26 13:03:45 -06:00
parent e198c4db65
commit f5d7d4e02a
1518 changed files with 169063 additions and 30 deletions

View file

@ -0,0 +1,29 @@
<?php declare(strict_types=1);
namespace PhpParser\Node;
class IdentifierTest extends \PHPUnit\Framework\TestCase
{
public function testToString() {
$identifier = new Identifier('Foo');
$this->assertSame('Foo', (string) $identifier);
$this->assertSame('Foo', $identifier->toString());
$this->assertSame('foo', $identifier->toLowerString());
}
/** @dataProvider provideTestIsSpecialClassName */
public function testIsSpecialClassName($identifier, $expected) {
$identifier = new Identifier($identifier);
$this->assertSame($expected, $identifier->isSpecialClassName());
}
public function provideTestIsSpecialClassName() {
return [
['self', true],
['PARENT', true],
['Static', true],
['other', false],
];
}
}

View file

@ -0,0 +1,157 @@
<?php declare(strict_types=1);
namespace PhpParser\Node;
class NameTest extends \PHPUnit\Framework\TestCase
{
public function testConstruct() {
$name = new Name(['foo', 'bar']);
$this->assertSame(['foo', 'bar'], $name->parts);
$name = new Name('foo\bar');
$this->assertSame(['foo', 'bar'], $name->parts);
$name = new Name($name);
$this->assertSame(['foo', 'bar'], $name->parts);
}
public function testGet() {
$name = new Name('foo');
$this->assertSame('foo', $name->getFirst());
$this->assertSame('foo', $name->getLast());
$name = new Name('foo\bar');
$this->assertSame('foo', $name->getFirst());
$this->assertSame('bar', $name->getLast());
}
public function testToString() {
$name = new Name('Foo\Bar');
$this->assertSame('Foo\Bar', (string) $name);
$this->assertSame('Foo\Bar', $name->toString());
$this->assertSame('foo\bar', $name->toLowerString());
}
public function testSlice() {
$name = new Name('foo\bar\baz');
$this->assertEquals(new Name('foo\bar\baz'), $name->slice(0));
$this->assertEquals(new Name('bar\baz'), $name->slice(1));
$this->assertNull($name->slice(3));
$this->assertEquals(new Name('foo\bar\baz'), $name->slice(-3));
$this->assertEquals(new Name('bar\baz'), $name->slice(-2));
$this->assertEquals(new Name('foo\bar'), $name->slice(0, -1));
$this->assertNull($name->slice(0, -3));
$this->assertEquals(new Name('bar'), $name->slice(1, -1));
$this->assertNull($name->slice(1, -2));
$this->assertEquals(new Name('bar'), $name->slice(-2, 1));
$this->assertEquals(new Name('bar'), $name->slice(-2, -1));
$this->assertNull($name->slice(-2, -2));
}
public function testSliceOffsetTooLarge() {
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('Offset 4 is out of bounds');
(new Name('foo\bar\baz'))->slice(4);
}
public function testSliceOffsetTooSmall() {
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('Offset -4 is out of bounds');
(new Name('foo\bar\baz'))->slice(-4);
}
public function testSliceLengthTooLarge() {
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('Length 4 is out of bounds');
(new Name('foo\bar\baz'))->slice(0, 4);
}
public function testSliceLengthTooSmall() {
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('Length -4 is out of bounds');
(new Name('foo\bar\baz'))->slice(0, -4);
}
public function testConcat() {
$this->assertEquals(new Name('foo\bar\baz'), Name::concat('foo', 'bar\baz'));
$this->assertEquals(
new Name\FullyQualified('foo\bar'),
Name\FullyQualified::concat(['foo'], new Name('bar'))
);
$attributes = ['foo' => 'bar'];
$this->assertEquals(
new Name\Relative('foo\bar\baz', $attributes),
Name\Relative::concat(new Name\FullyQualified('foo\bar'), 'baz', $attributes)
);
$this->assertEquals(new Name('foo'), Name::concat(null, 'foo'));
$this->assertEquals(new Name('foo'), Name::concat('foo', null));
$this->assertNull(Name::concat(null, null));
}
public function testNameTypes() {
$name = new Name('foo');
$this->assertTrue($name->isUnqualified());
$this->assertFalse($name->isQualified());
$this->assertFalse($name->isFullyQualified());
$this->assertFalse($name->isRelative());
$this->assertSame('foo', $name->toCodeString());
$name = new Name('foo\bar');
$this->assertFalse($name->isUnqualified());
$this->assertTrue($name->isQualified());
$this->assertFalse($name->isFullyQualified());
$this->assertFalse($name->isRelative());
$this->assertSame('foo\bar', $name->toCodeString());
$name = new Name\FullyQualified('foo');
$this->assertFalse($name->isUnqualified());
$this->assertFalse($name->isQualified());
$this->assertTrue($name->isFullyQualified());
$this->assertFalse($name->isRelative());
$this->assertSame('\foo', $name->toCodeString());
$name = new Name\Relative('foo');
$this->assertFalse($name->isUnqualified());
$this->assertFalse($name->isQualified());
$this->assertFalse($name->isFullyQualified());
$this->assertTrue($name->isRelative());
$this->assertSame('namespace\foo', $name->toCodeString());
}
public function testInvalidArg() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Expected string, array of parts or Name instance');
Name::concat('foo', new \stdClass);
}
public function testInvalidEmptyString() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Name cannot be empty');
new Name('');
}
public function testInvalidEmptyArray() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Name cannot be empty');
new Name([]);
}
/** @dataProvider provideTestIsSpecialClassName */
public function testIsSpecialClassName($name, $expected) {
$name = new Name($name);
$this->assertSame($expected, $name->isSpecialClassName());
}
public function provideTestIsSpecialClassName() {
return [
['self', true],
['PARENT', true],
['Static', true],
['self\not', false],
['not\self', false],
];
}
}

View file

@ -0,0 +1,26 @@
<?php declare(strict_types=1);
namespace PhpParser\Node\Scalar;
class MagicConstTest extends \PHPUnit\Framework\TestCase
{
/**
* @dataProvider provideTestGetName
*/
public function testGetName(MagicConst $magicConst, $name) {
$this->assertSame($name, $magicConst->getName());
}
public function provideTestGetName() {
return [
[new MagicConst\Class_, '__CLASS__'],
[new MagicConst\Dir, '__DIR__'],
[new MagicConst\File, '__FILE__'],
[new MagicConst\Function_, '__FUNCTION__'],
[new MagicConst\Line, '__LINE__'],
[new MagicConst\Method, '__METHOD__'],
[new MagicConst\Namespace_, '__NAMESPACE__'],
[new MagicConst\Trait_, '__TRAIT__'],
];
}
}

View file

@ -0,0 +1,61 @@
<?php declare(strict_types=1);
namespace PhpParser\Node\Scalar;
class StringTest extends \PHPUnit\Framework\TestCase
{
/**
* @dataProvider provideTestParseEscapeSequences
*/
public function testParseEscapeSequences($expected, $string, $quote) {
$this->assertSame(
$expected,
String_::parseEscapeSequences($string, $quote)
);
}
/**
* @dataProvider provideTestParse
*/
public function testCreate($expected, $string) {
$this->assertSame(
$expected,
String_::parse($string)
);
}
public function provideTestParseEscapeSequences() {
return [
['"', '\\"', '"'],
['\\"', '\\"', '`'],
['\\"\\`', '\\"\\`', null],
["\\\$\n\r\t\f\v", '\\\\\$\n\r\t\f\v', null],
["\x1B", '\e', null],
[chr(255), '\xFF', null],
[chr(255), '\377', null],
[chr(0), '\400', null],
["\0", '\0', null],
['\xFF', '\\\\xFF', null],
];
}
public function provideTestParse() {
$tests = [
['A', '\'A\''],
['A', 'b\'A\''],
['A', '"A"'],
['A', 'b"A"'],
['\\', '\'\\\\\''],
['\'', '\'\\\'\''],
];
foreach ($this->provideTestParseEscapeSequences() as $i => $test) {
// skip second and third tests, they aren't for double quotes
if ($i !== 1 && $i !== 2) {
$tests[] = [$test[0], '"' . $test[1] . '"'];
}
}
return $tests;
}
}

View file

@ -0,0 +1,34 @@
<?php declare(strict_types=1);
namespace PhpParser\Node\Stmt;
class ClassConstTest extends \PHPUnit\Framework\TestCase
{
/**
* @dataProvider provideModifiers
*/
public function testModifiers($modifier) {
$node = new ClassConst(
[], // invalid
constant('PhpParser\Node\Stmt\Class_::MODIFIER_' . strtoupper($modifier))
);
$this->assertTrue($node->{'is' . $modifier}());
}
public function testNoModifiers() {
$node = new ClassConst([], 0);
$this->assertTrue($node->isPublic());
$this->assertFalse($node->isProtected());
$this->assertFalse($node->isPrivate());
}
public function provideModifiers() {
return [
['public'],
['protected'],
['private'],
];
}
}

View file

@ -0,0 +1,123 @@
<?php declare(strict_types=1);
namespace PhpParser\Node\Stmt;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
class ClassMethodTest extends \PHPUnit\Framework\TestCase
{
/**
* @dataProvider provideModifiers
*/
public function testModifiers($modifier) {
$node = new ClassMethod('foo', [
'type' => constant('PhpParser\Node\Stmt\Class_::MODIFIER_' . strtoupper($modifier))
]);
$this->assertTrue($node->{'is' . $modifier}());
}
public function testNoModifiers() {
$node = new ClassMethod('foo', ['type' => 0]);
$this->assertTrue($node->isPublic());
$this->assertFalse($node->isProtected());
$this->assertFalse($node->isPrivate());
$this->assertFalse($node->isAbstract());
$this->assertFalse($node->isFinal());
$this->assertFalse($node->isStatic());
$this->assertFalse($node->isMagic());
}
public function provideModifiers() {
return [
['public'],
['protected'],
['private'],
['abstract'],
['final'],
['static'],
];
}
/**
* Checks that implicit public modifier detection for method is working
*
* @dataProvider implicitPublicModifiers
*
* @param string $modifier Node type modifier
*/
public function testImplicitPublic(string $modifier)
{
$node = new ClassMethod('foo', [
'type' => constant('PhpParser\Node\Stmt\Class_::MODIFIER_' . strtoupper($modifier))
]);
$this->assertTrue($node->isPublic(), 'Node should be implicitly public');
}
public function implicitPublicModifiers() {
return [
['abstract'],
['final'],
['static'],
];
}
/**
* @dataProvider provideMagics
*
* @param string $name Node name
*/
public function testMagic(string $name) {
$node = new ClassMethod($name);
$this->assertTrue($node->isMagic(), 'Method should be magic');
}
public function provideMagics() {
return [
['__construct'],
['__DESTRUCT'],
['__caLL'],
['__callstatic'],
['__get'],
['__set'],
['__isset'],
['__unset'],
['__sleep'],
['__wakeup'],
['__tostring'],
['__set_state'],
['__clone'],
['__invoke'],
['__debuginfo'],
];
}
public function testFunctionLike() {
$param = new Param(new Variable('a'));
$type = new Name('Foo');
$return = new Return_(new Variable('a'));
$method = new ClassMethod('test', [
'byRef' => false,
'params' => [$param],
'returnType' => $type,
'stmts' => [$return],
]);
$this->assertFalse($method->returnsByRef());
$this->assertSame([$param], $method->getParams());
$this->assertSame($type, $method->getReturnType());
$this->assertSame([$return], $method->getStmts());
$method = new ClassMethod('test', [
'byRef' => true,
'stmts' => null,
]);
$this->assertTrue($method->returnsByRef());
$this->assertNull($method->getStmts());
}
}

View file

@ -0,0 +1,59 @@
<?php declare(strict_types=1);
namespace PhpParser\Node\Stmt;
class ClassTest extends \PHPUnit\Framework\TestCase
{
public function testIsAbstract() {
$class = new Class_('Foo', ['type' => Class_::MODIFIER_ABSTRACT]);
$this->assertTrue($class->isAbstract());
$class = new Class_('Foo');
$this->assertFalse($class->isAbstract());
}
public function testIsFinal() {
$class = new Class_('Foo', ['type' => Class_::MODIFIER_FINAL]);
$this->assertTrue($class->isFinal());
$class = new Class_('Foo');
$this->assertFalse($class->isFinal());
}
public function testGetMethods() {
$methods = [
new ClassMethod('foo'),
new ClassMethod('bar'),
new ClassMethod('fooBar'),
];
$class = new Class_('Foo', [
'stmts' => [
new TraitUse([]),
$methods[0],
new ClassConst([]),
$methods[1],
new Property(0, []),
$methods[2],
]
]);
$this->assertSame($methods, $class->getMethods());
}
public function testGetMethod() {
$methodConstruct = new ClassMethod('__CONSTRUCT');
$methodTest = new ClassMethod('test');
$class = new Class_('Foo', [
'stmts' => [
new ClassConst([]),
$methodConstruct,
new Property(0, []),
$methodTest,
]
]);
$this->assertSame($methodConstruct, $class->getMethod('__construct'));
$this->assertSame($methodTest, $class->getMethod('test'));
$this->assertNull($class->getMethod('nonExisting'));
}
}

View file

@ -0,0 +1,26 @@
<?php declare(strict_types=1);
namespace PhpParser\Node\Stmt;
use PhpParser\Node;
class InterfaceTest extends \PHPUnit\Framework\TestCase
{
public function testGetMethods() {
$methods = [
new ClassMethod('foo'),
new ClassMethod('bar'),
];
$interface = new Class_('Foo', [
'stmts' => [
new Node\Stmt\ClassConst([new Node\Const_('C1', new Node\Scalar\String_('C1'))]),
$methods[0],
new Node\Stmt\ClassConst([new Node\Const_('C2', new Node\Scalar\String_('C2'))]),
$methods[1],
new Node\Stmt\ClassConst([new Node\Const_('C3', new Node\Scalar\String_('C3'))]),
]
]);
$this->assertSame($methods, $interface->getMethods());
}
}

View file

@ -0,0 +1,44 @@
<?php declare(strict_types=1);
namespace PhpParser\Node\Stmt;
class PropertyTest extends \PHPUnit\Framework\TestCase
{
/**
* @dataProvider provideModifiers
*/
public function testModifiers($modifier) {
$node = new Property(
constant('PhpParser\Node\Stmt\Class_::MODIFIER_' . strtoupper($modifier)),
[] // invalid
);
$this->assertTrue($node->{'is' . $modifier}());
}
public function testNoModifiers() {
$node = new Property(0, []);
$this->assertTrue($node->isPublic());
$this->assertFalse($node->isProtected());
$this->assertFalse($node->isPrivate());
$this->assertFalse($node->isStatic());
}
public function testStaticImplicitlyPublic() {
$node = new Property(Class_::MODIFIER_STATIC, []);
$this->assertTrue($node->isPublic());
$this->assertFalse($node->isProtected());
$this->assertFalse($node->isPrivate());
$this->assertTrue($node->isStatic());
}
public function provideModifiers() {
return [
['public'],
['protected'],
['private'],
['static'],
];
}
}