NamespacePassTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2023 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. /**
  14. * @group isolation-fail
  15. */
  16. class NamespacePassTest extends CodeCleanerTestCase
  17. {
  18. private $cleaner;
  19. /**
  20. * @before
  21. */
  22. public function getReady()
  23. {
  24. $this->cleaner = new CodeCleaner();
  25. $this->setPass(new NamespacePass($this->cleaner));
  26. }
  27. public function testProcess()
  28. {
  29. $this->parseAndTraverse('');
  30. $this->assertNull($this->cleaner->getNamespace());
  31. $this->parseAndTraverse('array_merge()');
  32. $this->assertNull($this->cleaner->getNamespace());
  33. // A non-block namespace statement should set the current namespace.
  34. $this->parseAndTraverse('namespace Alpha');
  35. $this->assertSame(['Alpha'], $this->cleaner->getNamespace());
  36. // A new non-block namespace statement should override the current namespace.
  37. $this->parseAndTraverse('namespace Beta; class B {}');
  38. $this->assertSame(['Beta'], $this->cleaner->getNamespace());
  39. // A new block namespace clears out the current namespace...
  40. $this->parseAndTraverse('namespace Gamma { array_merge(); }');
  41. if (\defined('PhpParser\\Node\\Stmt\\Namespace_::KIND_SEMICOLON')) {
  42. $this->assertNull($this->cleaner->getNamespace());
  43. } else {
  44. // But not for PHP-Parser < v3.1.2 :(
  45. $this->assertSame(['Gamma'], $this->cleaner->getNamespace());
  46. }
  47. $this->parseAndTraverse('namespace Delta');
  48. // A null namespace clears out the current namespace.
  49. $this->parseAndTraverse('namespace { array_merge(); }');
  50. $this->assertNull($this->cleaner->getNamespace());
  51. }
  52. }