2017-11-02 18:56:01 +01:00
|
|
|
<?php declare(strict_types=1);
|
2011-09-28 18:11:28 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Node;
|
|
|
|
|
2019-01-19 11:18:00 +01:00
|
|
|
class NameTest extends \PHPUnit\Framework\TestCase
|
2011-09-28 18:11:28 +02:00
|
|
|
{
|
|
|
|
public function testConstruct() {
|
2017-08-13 14:35:03 +02:00
|
|
|
$name = new Name(['foo', 'bar']);
|
|
|
|
$this->assertSame(['foo', 'bar'], $name->parts);
|
2011-09-28 18:11:28 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
$name = new Name('foo\bar');
|
2017-08-13 14:35:03 +02:00
|
|
|
$this->assertSame(['foo', 'bar'], $name->parts);
|
2016-10-22 16:41:58 +02:00
|
|
|
|
|
|
|
$name = new Name($name);
|
2017-08-13 14:35:03 +02:00
|
|
|
$this->assertSame(['foo', 'bar'], $name->parts);
|
2011-09-28 18:11:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGet() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$name = new Name('foo');
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('foo', $name->getFirst());
|
|
|
|
$this->assertSame('foo', $name->getLast());
|
2011-09-28 18:11:28 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
$name = new Name('foo\bar');
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('foo', $name->getFirst());
|
|
|
|
$this->assertSame('bar', $name->getLast());
|
2011-09-28 18:11:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testToString() {
|
2017-10-03 20:57:48 +02:00
|
|
|
$name = new Name('Foo\Bar');
|
2011-09-28 18:11:28 +02:00
|
|
|
|
2017-10-03 20:57:48 +02:00
|
|
|
$this->assertSame('Foo\Bar', (string) $name);
|
|
|
|
$this->assertSame('Foo\Bar', $name->toString());
|
|
|
|
$this->assertSame('foo\bar', $name->toLowerString());
|
2011-09-28 18:11:28 +02:00
|
|
|
}
|
|
|
|
|
2015-07-12 23:55:57 +02:00
|
|
|
public function testSlice() {
|
2016-07-25 14:47:24 +02:00
|
|
|
$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));
|
2016-10-22 17:02:38 +02:00
|
|
|
$this->assertNull($name->slice(3));
|
2016-07-25 14:47:24 +02:00
|
|
|
$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));
|
2016-10-22 17:02:38 +02:00
|
|
|
$this->assertNull($name->slice(0, -3));
|
2016-07-25 14:47:24 +02:00
|
|
|
$this->assertEquals(new Name('bar'), $name->slice(1, -1));
|
2016-10-22 17:02:38 +02:00
|
|
|
$this->assertNull($name->slice(1, -2));
|
2016-07-25 14:47:24 +02:00
|
|
|
$this->assertEquals(new Name('bar'), $name->slice(-2, 1));
|
|
|
|
$this->assertEquals(new Name('bar'), $name->slice(-2, -1));
|
2016-10-22 17:02:38 +02:00
|
|
|
$this->assertNull($name->slice(-2, -2));
|
2015-07-12 23:55:57 +02:00
|
|
|
}
|
|
|
|
|
2016-07-25 14:47:24 +02:00
|
|
|
public function testSliceOffsetTooLarge() {
|
2018-09-22 10:43:54 +02:00
|
|
|
$this->expectException(\OutOfBoundsException::class);
|
|
|
|
$this->expectExceptionMessage('Offset 4 is out of bounds');
|
2015-07-12 23:55:57 +02:00
|
|
|
(new Name('foo\bar\baz'))->slice(4);
|
|
|
|
}
|
|
|
|
|
2016-07-25 14:47:24 +02:00
|
|
|
public function testSliceOffsetTooSmall() {
|
2018-09-22 10:43:54 +02:00
|
|
|
$this->expectException(\OutOfBoundsException::class);
|
|
|
|
$this->expectExceptionMessage('Offset -4 is out of bounds');
|
2016-07-25 14:47:24 +02:00
|
|
|
(new Name('foo\bar\baz'))->slice(-4);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSliceLengthTooLarge() {
|
2018-09-22 10:43:54 +02:00
|
|
|
$this->expectException(\OutOfBoundsException::class);
|
|
|
|
$this->expectExceptionMessage('Length 4 is out of bounds');
|
2016-07-25 14:47:24 +02:00
|
|
|
(new Name('foo\bar\baz'))->slice(0, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSliceLengthTooSmall() {
|
2018-09-22 10:43:54 +02:00
|
|
|
$this->expectException(\OutOfBoundsException::class);
|
|
|
|
$this->expectExceptionMessage('Length -4 is out of bounds');
|
2016-07-25 14:47:24 +02:00
|
|
|
(new Name('foo\bar\baz'))->slice(0, -4);
|
|
|
|
}
|
|
|
|
|
2015-07-12 23:55:57 +02:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
|
2016-10-22 17:02:38 +02:00
|
|
|
$this->assertEquals(new Name('foo'), Name::concat(null, 'foo'));
|
|
|
|
$this->assertEquals(new Name('foo'), Name::concat('foo', null));
|
|
|
|
$this->assertNull(Name::concat(null, null));
|
2015-07-12 23:55:57 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 17:10:30 +02:00
|
|
|
public function testNameTypes() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$name = new Name('foo');
|
2018-01-10 17:59:52 +01:00
|
|
|
$this->assertTrue($name->isUnqualified());
|
2011-09-28 18:11:28 +02:00
|
|
|
$this->assertFalse($name->isQualified());
|
|
|
|
$this->assertFalse($name->isFullyQualified());
|
|
|
|
$this->assertFalse($name->isRelative());
|
2017-04-28 17:10:30 +02:00
|
|
|
$this->assertSame('foo', $name->toCodeString());
|
2011-09-28 18:11:28 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
$name = new Name('foo\bar');
|
2011-09-28 18:11:28 +02:00
|
|
|
$this->assertFalse($name->isUnqualified());
|
2018-01-10 17:59:52 +01:00
|
|
|
$this->assertTrue($name->isQualified());
|
2011-09-28 18:11:28 +02:00
|
|
|
$this->assertFalse($name->isFullyQualified());
|
|
|
|
$this->assertFalse($name->isRelative());
|
2017-04-28 17:10:30 +02:00
|
|
|
$this->assertSame('foo\bar', $name->toCodeString());
|
2011-09-28 18:11:28 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
$name = new Name\FullyQualified('foo');
|
2011-09-28 18:11:28 +02:00
|
|
|
$this->assertFalse($name->isUnqualified());
|
|
|
|
$this->assertFalse($name->isQualified());
|
2018-01-10 17:59:52 +01:00
|
|
|
$this->assertTrue($name->isFullyQualified());
|
2011-09-28 18:11:28 +02:00
|
|
|
$this->assertFalse($name->isRelative());
|
2017-04-28 17:10:30 +02:00
|
|
|
$this->assertSame('\foo', $name->toCodeString());
|
2011-09-28 18:11:28 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
$name = new Name\Relative('foo');
|
2011-09-28 18:11:28 +02:00
|
|
|
$this->assertFalse($name->isUnqualified());
|
|
|
|
$this->assertFalse($name->isQualified());
|
|
|
|
$this->assertFalse($name->isFullyQualified());
|
2018-01-10 17:59:52 +01:00
|
|
|
$this->assertTrue($name->isRelative());
|
2017-04-28 17:10:30 +02:00
|
|
|
$this->assertSame('namespace\foo', $name->toCodeString());
|
2011-09-28 18:11:28 +02:00
|
|
|
}
|
2011-11-27 12:53:48 +01:00
|
|
|
|
|
|
|
public function testInvalidArg() {
|
2018-09-22 10:43:54 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('Expected string, array of parts or Name instance');
|
2016-10-22 00:25:15 +02:00
|
|
|
Name::concat('foo', new \stdClass);
|
2011-11-27 12:53:48 +01:00
|
|
|
}
|
2017-10-03 20:57:48 +02:00
|
|
|
|
2017-12-01 18:13:55 +01:00
|
|
|
public function testInvalidEmptyString() {
|
2018-09-22 10:43:54 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('Name cannot be empty');
|
2017-12-01 18:13:55 +01:00
|
|
|
new Name('');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidEmptyArray() {
|
2018-09-22 10:43:54 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('Name cannot be empty');
|
2017-12-01 18:13:55 +01:00
|
|
|
new Name([]);
|
|
|
|
}
|
|
|
|
|
2017-10-03 20:57:48 +02:00
|
|
|
/** @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],
|
|
|
|
];
|
|
|
|
}
|
2017-04-27 18:14:07 +02:00
|
|
|
}
|