SudoVisitorTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\Sudo;
  11. use PhpParser\NodeTraverser;
  12. use Psy\Sudo\SudoVisitor;
  13. use Psy\Test\ParserTestCase;
  14. /**
  15. * @group isolation-fail
  16. */
  17. class SudoVisitorTest extends ParserTestCase
  18. {
  19. /**
  20. * @before
  21. */
  22. public function getReady()
  23. {
  24. $this->traverser = new NodeTraverser();
  25. $this->traverser->addVisitor(new SudoVisitor());
  26. }
  27. /**
  28. * @dataProvider propertyFetches
  29. */
  30. public function testPropertyFetch($from, $to)
  31. {
  32. $this->assertProcessesAs($from, $to);
  33. }
  34. public function propertyFetches()
  35. {
  36. return [
  37. ['$a->b', "\Psy\Sudo::fetchProperty(\$a, 'b');"],
  38. ['$a->$b', '\Psy\Sudo::fetchProperty($a, $b);'],
  39. ["\$a->{'b'}", "\Psy\Sudo::fetchProperty(\$a, 'b');"],
  40. ];
  41. }
  42. /**
  43. * @dataProvider propertyAssigns
  44. */
  45. public function testPropertyAssign($from, $to)
  46. {
  47. $this->assertProcessesAs($from, $to);
  48. }
  49. public function propertyAssigns()
  50. {
  51. return [
  52. ['$a->b = $c', "\Psy\Sudo::assignProperty(\$a, 'b', \$c);"],
  53. ['$a->$b = $c', '\Psy\Sudo::assignProperty($a, $b, $c);'],
  54. ["\$a->{'b'} = \$c", "\Psy\Sudo::assignProperty(\$a, 'b', \$c);"],
  55. ];
  56. }
  57. /**
  58. * @dataProvider methodCalls
  59. */
  60. public function testMethodCall($from, $to)
  61. {
  62. $this->assertProcessesAs($from, $to);
  63. }
  64. public function methodCalls()
  65. {
  66. return [
  67. ['$a->b()', "\Psy\Sudo::callMethod(\$a, 'b');"],
  68. ['$a->$b()', '\Psy\Sudo::callMethod($a, $b);'],
  69. ["\$a->b(\$c, 'd')", "\Psy\Sudo::callMethod(\$a, 'b', \$c, 'd');"],
  70. ["\$a->\$b(\$c, 'd')", "\Psy\Sudo::callMethod(\$a, \$b, \$c, 'd');"],
  71. ];
  72. }
  73. /**
  74. * @dataProvider staticPropertyFetches
  75. */
  76. public function testStaticPropertyFetch($from, $to)
  77. {
  78. $this->assertProcessesAs($from, $to);
  79. }
  80. public function staticPropertyFetches()
  81. {
  82. return [
  83. ['A::$b', "\Psy\Sudo::fetchStaticProperty('A', 'b');"],
  84. ['$a::$b', "\Psy\Sudo::fetchStaticProperty(\$a, 'b');"],
  85. ];
  86. }
  87. /**
  88. * @dataProvider staticPropertyAssigns
  89. */
  90. public function testStaticPropertyAssign($from, $to)
  91. {
  92. $this->assertProcessesAs($from, $to);
  93. }
  94. public function staticPropertyAssigns()
  95. {
  96. return [
  97. ['A::$b = $c', "\Psy\Sudo::assignStaticProperty('A', 'b', \$c);"],
  98. ['$a::$b = $c', "\Psy\Sudo::assignStaticProperty(\$a, 'b', \$c);"],
  99. ];
  100. }
  101. /**
  102. * @dataProvider staticCalls
  103. */
  104. public function testStaticCall($from, $to)
  105. {
  106. $this->assertProcessesAs($from, $to);
  107. }
  108. public function staticCalls()
  109. {
  110. return [
  111. ['A::b()', "\Psy\Sudo::callStatic('A', 'b');"],
  112. ['A::$b()', "\Psy\Sudo::callStatic('A', \$b);"],
  113. ["A::b(\$c, 'd')", "\Psy\Sudo::callStatic('A', 'b', \$c, 'd');"],
  114. ["A::\$b(\$c, 'd')", "\Psy\Sudo::callStatic('A', \$b, \$c, 'd');"],
  115. ];
  116. }
  117. /**
  118. * @dataProvider classConstFetches
  119. */
  120. public function testClassConstFetch($from, $to)
  121. {
  122. $this->assertProcessesAs($from, $to);
  123. }
  124. public function classConstFetches()
  125. {
  126. return [
  127. ['A::B', "\Psy\Sudo::fetchClassConst('A', 'B');"],
  128. ];
  129. }
  130. /**
  131. * @dataProvider newInstances
  132. */
  133. public function testNewInstance($from, $to)
  134. {
  135. $this->assertProcessesAs($from, $to);
  136. }
  137. public function newInstances()
  138. {
  139. return [
  140. ['new A', "\Psy\Sudo::newInstance('A');"],
  141. ['new A($b)', "\Psy\Sudo::newInstance('A', \$b);"],
  142. ["new A(\$b, 'c')", "\Psy\Sudo::newInstance('A', \$b, 'c');"],
  143. ["new \$a(\$b, 'c')", "\Psy\Sudo::newInstance(\$a, \$b, 'c');"],
  144. ];
  145. }
  146. }