ListPassTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\ListPass;
  12. use Psy\Exception\ParseErrorException;
  13. /**
  14. * @group isolation-fail
  15. */
  16. class ListPassTest extends CodeCleanerTestCase
  17. {
  18. /**
  19. * @before
  20. */
  21. public function getReady()
  22. {
  23. $this->setPass(new ListPass());
  24. }
  25. /**
  26. * @dataProvider invalidStatements
  27. */
  28. public function testProcessInvalidStatement($code, $expectedMessage)
  29. {
  30. $this->expectException(ParseErrorException::class);
  31. $this->expectExceptionMessage($expectedMessage);
  32. $stmts = $this->parse($code);
  33. $this->traverser->traverse($stmts);
  34. $this->fail();
  35. }
  36. public function invalidStatements()
  37. {
  38. // Not typo. It is ambiguous whether "Syntax" or "syntax".
  39. $errorShortListAssign = "yntax error, unexpected '='";
  40. $errorEmptyList = 'Cannot use empty list';
  41. $errorAssocListAssign = 'Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting \',\' or \')\'';
  42. $errorNonVariableAssign = 'Assignments can only happen to writable values';
  43. $errorPhpParserSyntax = 'PHP Parse error: ';
  44. $invalidExpr = [
  45. ['list() = []', $errorEmptyList],
  46. ['list("a") = [1]', $errorPhpParserSyntax],
  47. ];
  48. if (\version_compare(\PHP_VERSION, '7.1', '<')) {
  49. return \array_merge($invalidExpr, [
  50. ['list("a" => _) = ["a" => 1]', $errorPhpParserSyntax],
  51. ['[] = []', $errorShortListAssign],
  52. ['[$a] = [1]', $errorShortListAssign],
  53. ['list("a" => $a) = ["a" => 1]', $errorAssocListAssign],
  54. ['[$a[0], $a[1]] = [1, 2]', $errorShortListAssign],
  55. ['[$a->b, $a->c] = [1, 2]', $errorShortListAssign],
  56. ]);
  57. }
  58. return \array_merge($invalidExpr, [
  59. ['list("a" => _) = ["a" => 1]', $errorPhpParserSyntax],
  60. ['["a"] = [1]', $errorNonVariableAssign],
  61. ['[] = []', $errorEmptyList],
  62. ['[,] = [1,2]', $errorEmptyList],
  63. ['[,,] = [1,2,3]', $errorEmptyList],
  64. ]);
  65. }
  66. /**
  67. * @dataProvider validStatements
  68. */
  69. public function testProcessValidStatement($code)
  70. {
  71. $stmts = $this->parse($code);
  72. $this->traverser->traverse($stmts);
  73. $this->assertTrue(true);
  74. }
  75. public function validStatements()
  76. {
  77. $validExpr = [
  78. ['list($a) = [1]'],
  79. ['list($x, $y) = [1, 2]'],
  80. ];
  81. if (\version_compare(\PHP_VERSION, '7.1', '>=')) {
  82. return \array_merge($validExpr, [
  83. ['[$a] = [1]'],
  84. ['list($b) = [2]'],
  85. ['[$x, $y] = [1, 2]'],
  86. ['[$a] = [1]'],
  87. ['[$x, $y] = [1, 2]'],
  88. ['["_" => $v] = ["_" => 1]'],
  89. ['[$a,] = [1,2,3]'],
  90. ['[,$b] = [1,2,3]'],
  91. ['[$a,,$c] = [1,2,3]'],
  92. ['[$a,,,] = [1,2,3]'],
  93. ['[$a[0], $a[1]] = [1, 2]'],
  94. ['[$a[0][0][0], $a[0][0][1]] = [1, 2]'],
  95. ['[$a->b, $a->c] = [1, 2]'],
  96. ['[$a->b[0], $a->c[1]] = [1, 2]'],
  97. ['[$a[0]->b[0], $a[0]->c[1]] = [1, 2]'],
  98. ['[$a[$b->c + $b->d]] = [1]'],
  99. ['[$a->c()->d, $a->c()->e] = [1, 2]'],
  100. ['[x()->a, x()->b] = [1, 2]'],
  101. ]);
  102. }
  103. return $validExpr;
  104. }
  105. }