EmptyArrayDimFetchPassTest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\EmptyArrayDimFetchPass;
  12. /**
  13. * @group isolation-fail
  14. */
  15. class EmptyArrayDimFetchPassTest extends CodeCleanerTestCase
  16. {
  17. /**
  18. * @before
  19. */
  20. public function getReady()
  21. {
  22. $this->setPass(new EmptyArrayDimFetchPass());
  23. }
  24. /**
  25. * @dataProvider invalidStatements
  26. */
  27. public function testProcessInvalidStatement($code)
  28. {
  29. $this->expectException(\Psy\Exception\FatalErrorException::class);
  30. $this->parseAndTraverse($code);
  31. $this->fail();
  32. }
  33. public function invalidStatements()
  34. {
  35. return [
  36. ['$foo[]'],
  37. ['echo $foo[]'],
  38. ['${$foo}[]'],
  39. ['array_pop($this->foo[])'],
  40. ['$foo[] = $bar[]'],
  41. ];
  42. }
  43. /**
  44. * @dataProvider validStatements
  45. */
  46. public function testProcessValidStatement($code)
  47. {
  48. $this->parseAndTraverse($code);
  49. $this->assertTrue(true);
  50. }
  51. public function validStatements()
  52. {
  53. $data = [
  54. ['$foo[] = "bar"'],
  55. ['$this->foo[] = 1'],
  56. ['$foo->{$bar}[] = 1'],
  57. ['foreach ($bar as $foo[]) {}'],
  58. ['$bar = &$foo[]'],
  59. ['$foo[]["bar"] = "baz"'],
  60. ];
  61. return $data;
  62. }
  63. }