CalledClassPassTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\CalledClassPass;
  13. class CalledClassPassTest extends CodeCleanerTestCase
  14. {
  15. public function setUp()
  16. {
  17. $this->pass = new CalledClassPass();
  18. $this->traverser = new NodeTraverser();
  19. $this->traverser->addVisitor($this->pass);
  20. }
  21. /**
  22. * @dataProvider invalidStatements
  23. * @expectedException \Psy\Exception\ErrorException
  24. */
  25. public function testProcessStatementFails($code)
  26. {
  27. $stmts = $this->parse($code);
  28. $this->traverser->traverse($stmts);
  29. }
  30. public function invalidStatements()
  31. {
  32. return array(
  33. array('get_class()'),
  34. array('get_class(null)'),
  35. array('get_called_class()'),
  36. array('get_called_class(null)'),
  37. array('function foo() { return get_class(); }'),
  38. array('function foo() { return get_class(null); }'),
  39. array('function foo() { return get_called_class(); }'),
  40. array('function foo() { return get_called_class(null); }'),
  41. );
  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('get_class($foo)'),
  55. array('get_class(bar())'),
  56. array('get_called_class($foo)'),
  57. array('get_called_class(bar())'),
  58. array('function foo($bar) { return get_class($bar); }'),
  59. array('function foo($bar) { return get_called_class($bar); }'),
  60. array('class Foo { function bar() { return get_class(); } }'),
  61. array('class Foo { function bar() { return get_class(null); } }'),
  62. array('class Foo { function bar() { return get_called_class(); } }'),
  63. array('class Foo { function bar() { return get_called_class(null); } }'),
  64. array('$foo = function () {}; $foo()'),
  65. );
  66. }
  67. /**
  68. * @dataProvider validTraitStatements
  69. */
  70. public function testProcessTraitStatementPasses($code)
  71. {
  72. if (version_compare(PHP_VERSION, '5.4', '<')) {
  73. $this->markTestSkipped();
  74. }
  75. $stmts = $this->parse($code);
  76. $this->traverser->traverse($stmts);
  77. }
  78. public function validTraitStatements()
  79. {
  80. return array(
  81. array('trait Foo { function bar() { return get_class(); } }'),
  82. array('trait Foo { function bar() { return get_class(null); } }'),
  83. array('trait Foo { function bar() { return get_called_class(); } }'),
  84. array('trait Foo { function bar() { return get_called_class(null); } }'),
  85. );
  86. }
  87. }