UseTest.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser\Builder;
  4. use PhpParser\Node\Name;
  5. use PhpParser\Node\Stmt;
  6. class UseTest extends \PHPUnit\Framework\TestCase
  7. {
  8. protected function createUseBuilder($name, $type = Stmt\Use_::TYPE_NORMAL) {
  9. return new Builder\Use_($name, $type);
  10. }
  11. public function testCreation() {
  12. $node = $this->createUseBuilder('Foo\Bar')->getNode();
  13. $this->assertEquals(new Stmt\Use_([
  14. new Stmt\UseUse(new Name('Foo\Bar'), null)
  15. ]), $node);
  16. $node = $this->createUseBuilder(new Name('Foo\Bar'))->as('XYZ')->getNode();
  17. $this->assertEquals(new Stmt\Use_([
  18. new Stmt\UseUse(new Name('Foo\Bar'), 'XYZ')
  19. ]), $node);
  20. $node = $this->createUseBuilder('foo\bar', Stmt\Use_::TYPE_FUNCTION)->as('foo')->getNode();
  21. $this->assertEquals(new Stmt\Use_([
  22. new Stmt\UseUse(new Name('foo\bar'), 'foo')
  23. ], Stmt\Use_::TYPE_FUNCTION), $node);
  24. $node = $this->createUseBuilder('foo\BAR', Stmt\Use_::TYPE_CONSTANT)->as('FOO')->getNode();
  25. $this->assertEquals(new Stmt\Use_([
  26. new Stmt\UseUse(new Name('foo\BAR'), 'FOO')
  27. ], Stmt\Use_::TYPE_CONSTANT), $node);
  28. }
  29. }