2011-09-28 18:11:28 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Node;
|
|
|
|
|
|
|
|
class NameTest extends \PHPUnit_Framework_TestCase
|
2011-09-28 18:11:28 +02:00
|
|
|
{
|
|
|
|
public function testConstruct() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$name = new Name(array('foo', 'bar'));
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame(array('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');
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame(array('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() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$name = new Name('foo\bar');
|
2011-09-28 18:11:28 +02:00
|
|
|
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('foo\bar', (string) $name);
|
|
|
|
$this->assertSame('foo\bar', $name->toString());
|
|
|
|
$this->assertSame('foo_bar', $name->toString('_'));
|
2011-09-28 18:11:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAppend() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$name = new Name('foo');
|
2011-09-28 18:11:28 +02:00
|
|
|
|
|
|
|
$name->append('bar');
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('foo\bar', $name->toString());
|
2011-09-28 18:11:28 +02:00
|
|
|
|
|
|
|
$name->append('bar\foo');
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('foo\bar\bar\foo', $name->toString());
|
2011-09-28 18:11:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrepend() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$name = new Name('foo');
|
2011-09-28 18:11:28 +02:00
|
|
|
|
|
|
|
$name->prepend('bar');
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('bar\foo', $name->toString());
|
2011-09-28 18:11:28 +02:00
|
|
|
|
|
|
|
$name->prepend('foo\bar');
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('foo\bar\bar\foo', $name->toString());
|
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));
|
|
|
|
$this->assertEquals(new Name([]), $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->assertEquals(new Name([]), $name->slice(0, -3));
|
|
|
|
$this->assertEquals(new Name('bar'), $name->slice(1, -1));
|
|
|
|
$this->assertEquals(new Name([]), $name->slice(1, -2));
|
|
|
|
$this->assertEquals(new Name('bar'), $name->slice(-2, 1));
|
|
|
|
$this->assertEquals(new Name('bar'), $name->slice(-2, -1));
|
|
|
|
$this->assertEquals(new Name([]), $name->slice(-2, -2));
|
2015-07-12 23:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OutOfBoundsException
|
|
|
|
* @expectedExceptionMessage Offset 4 is out of bounds
|
|
|
|
*/
|
2016-07-25 14:47:24 +02:00
|
|
|
public function testSliceOffsetTooLarge() {
|
2015-07-12 23:55:57 +02:00
|
|
|
(new Name('foo\bar\baz'))->slice(4);
|
|
|
|
}
|
|
|
|
|
2016-07-25 14:47:24 +02:00
|
|
|
/**
|
|
|
|
* @expectedException \OutOfBoundsException
|
|
|
|
* @expectedExceptionMessage Offset -4 is out of bounds
|
|
|
|
*/
|
|
|
|
public function testSliceOffsetTooSmall() {
|
|
|
|
(new Name('foo\bar\baz'))->slice(-4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OutOfBoundsException
|
|
|
|
* @expectedExceptionMessage Length 4 is out of bounds
|
|
|
|
*/
|
|
|
|
public function testSliceLengthTooLarge() {
|
|
|
|
(new Name('foo\bar\baz'))->slice(0, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OutOfBoundsException
|
|
|
|
* @expectedExceptionMessage Length -4 is out of bounds
|
|
|
|
*/
|
|
|
|
public function testSliceLengthTooSmall() {
|
|
|
|
(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)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(new Name('foo'), Name::concat([], 'foo'));
|
|
|
|
$this->assertEquals(new Name([]), Name::concat([], []));
|
|
|
|
}
|
|
|
|
|
2011-09-28 18:11:28 +02:00
|
|
|
public function testIs() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$name = new Name('foo');
|
2011-09-28 18:11:28 +02:00
|
|
|
$this->assertTrue ($name->isUnqualified());
|
|
|
|
$this->assertFalse($name->isQualified());
|
|
|
|
$this->assertFalse($name->isFullyQualified());
|
|
|
|
$this->assertFalse($name->isRelative());
|
|
|
|
|
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());
|
|
|
|
$this->assertTrue ($name->isQualified());
|
|
|
|
$this->assertFalse($name->isFullyQualified());
|
|
|
|
$this->assertFalse($name->isRelative());
|
|
|
|
|
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());
|
|
|
|
$this->assertTrue ($name->isFullyQualified());
|
|
|
|
$this->assertFalse($name->isRelative());
|
|
|
|
|
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());
|
|
|
|
$this->assertTrue ($name->isRelative());
|
|
|
|
}
|
2011-11-27 12:53:48 +01:00
|
|
|
|
|
|
|
/**
|
2014-02-06 14:44:16 +01:00
|
|
|
* @expectedException \InvalidArgumentException
|
2011-11-27 12:53:48 +01:00
|
|
|
* @expectedExceptionMessage When changing a name you need to pass either a string, an array or a Name node
|
|
|
|
*/
|
|
|
|
public function testInvalidArg() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$name = new Name('foo');
|
2016-07-25 14:50:37 +02:00
|
|
|
$name->append(new \stdClass);
|
2011-11-27 12:53:48 +01:00
|
|
|
}
|
2011-09-28 18:11:28 +02:00
|
|
|
}
|