NamespacePassTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2017 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->process('array_merge()');
  24. $this->assertNull($this->cleaner->getNamespace());
  25. // A non-block namespace statement should set the current namespace.
  26. $this->process('namespace Alpha');
  27. $this->assertEquals(array('Alpha'), $this->cleaner->getNamespace());
  28. // A new non-block namespace statement should override the current namespace.
  29. $this->process('namespace Beta; class B {}');
  30. $this->assertEquals(array('Beta'), $this->cleaner->getNamespace());
  31. // @todo Figure out if we can detect when the last namespace block is
  32. // bracketed or unbracketed, because this should really clear the
  33. // namespace at the end...
  34. $this->process('namespace Gamma { array_merge(); }');
  35. $this->assertEquals(array('Gamma'), $this->cleaner->getNamespace());
  36. // A null namespace clears out the current namespace.
  37. $this->process('namespace { array_merge(); }');
  38. $this->assertNull($this->cleaner->getNamespace());
  39. }
  40. private function process($code)
  41. {
  42. $stmts = $this->parse($code);
  43. $this->traverse($stmts);
  44. }
  45. }