InstanceOfPassTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\InstanceOfPass;
  12. /**
  13. * @group isolation-fail
  14. */
  15. class InstanceOfPassTest extends CodeCleanerTestCase
  16. {
  17. /**
  18. * @before
  19. */
  20. public function getReady()
  21. {
  22. $this->setPass(new InstanceOfPass());
  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. if (\version_compare(\PHP_VERSION, '7.3', '>=')) {
  36. return [];
  37. }
  38. return [
  39. ['null instanceof stdClass'],
  40. ['true instanceof stdClass'],
  41. ['9 instanceof stdClass'],
  42. ['1.0 instanceof stdClass'],
  43. ['"foo" instanceof stdClass'],
  44. ['__DIR__ instanceof stdClass'],
  45. ['PHP_SAPI instanceof stdClass'],
  46. ['1+1 instanceof stdClass'],
  47. ['true && false instanceof stdClass'],
  48. ['"a"."b" instanceof stdClass'],
  49. ['!5 instanceof stdClass'],
  50. ['[1] instanceof stdClass'],
  51. ['(1+1) instanceof stdClass'],
  52. ['DateTime::ISO8601 instanceof stdClass'],
  53. ];
  54. }
  55. /**
  56. * @dataProvider validStatements
  57. */
  58. public function testProcessValidStatement($code)
  59. {
  60. $this->parseAndTraverse($code);
  61. $this->assertTrue(true);
  62. }
  63. public function validStatements()
  64. {
  65. $data = [
  66. ['$a instanceof stdClass'],
  67. ['strtolower("foo") instanceof stdClass'],
  68. ['(string) "foo" instanceof stdClass'],
  69. ['"foo ${foo} $bar" instanceof stdClass'],
  70. ];
  71. if (\version_compare(\PHP_VERSION, '7.3', '>=')) {
  72. return \array_merge($data, [
  73. ['null instanceof stdClass'],
  74. ['true instanceof stdClass'],
  75. ['9 instanceof stdClass'],
  76. ['1.0 instanceof stdClass'],
  77. ['"foo" instanceof stdClass'],
  78. ['__DIR__ instanceof stdClass'],
  79. ['PHP_SAPI instanceof stdClass'],
  80. ['1+1 instanceof stdClass'],
  81. ['true && false instanceof stdClass'],
  82. ['"a"."b" instanceof stdClass'],
  83. ['!5 instanceof stdClass'],
  84. ['[1] instanceof stdClass'],
  85. ['(1+1) instanceof stdClass'],
  86. ['DateTime::ISO8601 instanceof stdClass'],
  87. ]);
  88. }
  89. return $data;
  90. }
  91. }