SudoVisitorTest.php 3.5 KB

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