NamespacePassTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2018 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Test\CodeCleaner;
  11. use Psy\CodeCleaner;
  12. use Psy\CodeCleaner\NamespacePass;
  13. class NamespacePassTest extends CodeCleanerTestCase
  14. {
  15. private $cleaner;
  16. public function setUp()
  17. {
  18. $this->cleaner = new CodeCleaner();
  19. $this->setPass(new NamespacePass($this->cleaner));
  20. }
  21. public function testProcess()
  22. {
  23. $this->parseAndTraverse('');
  24. $this->assertNull($this->cleaner->getNamespace());
  25. $this->parseAndTraverse('array_merge()');
  26. $this->assertNull($this->cleaner->getNamespace());
  27. // A non-block namespace statement should set the current namespace.
  28. $this->parseAndTraverse('namespace Alpha');
  29. $this->assertSame(['Alpha'], $this->cleaner->getNamespace());
  30. // A new non-block namespace statement should override the current namespace.
  31. $this->parseAndTraverse('namespace Beta; class B {}');
  32. $this->assertSame(['Beta'], $this->cleaner->getNamespace());
  33. // A new block namespace clears out the current namespace...
  34. $this->parseAndTraverse('namespace Gamma { array_merge(); }');
  35. if (\defined('PhpParser\\Node\\Stmt\\Namespace_::KIND_SEMICOLON')) {
  36. $this->assertNull($this->cleaner->getNamespace());
  37. } else {
  38. // But not for PHP-Parser < v3.1.2 :(
  39. $this->assertSame(['Gamma'], $this->cleaner->getNamespace());
  40. }
  41. $this->parseAndTraverse('namespace Delta');
  42. // A null namespace clears out the current namespace.
  43. $this->parseAndTraverse('namespace { array_merge(); }');
  44. $this->assertNull($this->cleaner->getNamespace());
  45. }
  46. }