ListPassTest.php 3.7 KB

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