FinalClassPassTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 PhpParser\NodeTraverser;
  12. use Psy\CodeCleaner\FinalClassPass;
  13. class FinalClassPassTest extends CodeCleanerTestCase
  14. {
  15. public function setUp()
  16. {
  17. $this->pass = new FinalClassPass();
  18. $this->traverser = new NodeTraverser();
  19. $this->traverser->addVisitor($this->pass);
  20. }
  21. /**
  22. * @dataProvider invalidStatements
  23. * @expectedException \Psy\Exception\FatalErrorException
  24. */
  25. public function testProcessStatementFails($code)
  26. {
  27. $stmts = $this->parse($code);
  28. $this->traverser->traverse($stmts);
  29. }
  30. public function invalidStatements()
  31. {
  32. $stmts = array(
  33. array('final class A {} class B extends A {}'),
  34. array('class A {} final class B extends A {} class C extends B {}'),
  35. // array('namespace A { final class B {} } namespace C { class D extends \\A\\B {} }'),
  36. );
  37. if (!defined('HHVM_VERSION')) {
  38. // For some reason Closure isn't final in HHVM?
  39. $stmts[] = array('class A extends \\Closure {}');
  40. }
  41. return $stmts;
  42. }
  43. /**
  44. * @dataProvider validStatements
  45. */
  46. public function testProcessStatementPasses($code)
  47. {
  48. $stmts = $this->parse($code);
  49. $this->traverser->traverse($stmts);
  50. }
  51. public function validStatements()
  52. {
  53. return array(
  54. array('class A extends \\stdClass {}'),
  55. array('final class A extends \\stdClass {}'),
  56. array('class A {} class B extends A {}'),
  57. );
  58. }
  59. }