ValidFunctionNamePassTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\ValidFunctionNamePass;
  12. /**
  13. * @group isolation-fail
  14. */
  15. class ValidFunctionNamePassTest extends CodeCleanerTestCase
  16. {
  17. /**
  18. * @before
  19. */
  20. public function getReady()
  21. {
  22. $this->setPass(new ValidFunctionNamePass());
  23. }
  24. /**
  25. * @dataProvider getInvalidFunctions
  26. */
  27. public function testProcessInvalidFunctionCallsAndDeclarations($code)
  28. {
  29. $this->expectException(\Psy\Exception\FatalErrorException::class);
  30. $this->parseAndTraverse($code);
  31. $this->fail();
  32. }
  33. public function getInvalidFunctions()
  34. {
  35. return [
  36. // function declarations
  37. ['function array_merge() {}'],
  38. ['function Array_Merge() {}'],
  39. ['
  40. function psy_test_codecleaner_validfunctionnamepass_alpha() {}
  41. function psy_test_codecleaner_validfunctionnamepass_alpha() {}
  42. '],
  43. ['
  44. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  45. function beta() {}
  46. }
  47. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  48. function beta() {}
  49. }
  50. '],
  51. // recursion
  52. ['function a() { a(); } function a() {}'],
  53. ];
  54. }
  55. /**
  56. * @dataProvider getValidFunctions
  57. */
  58. public function testProcessValidFunctionCallsAndDeclarations($code)
  59. {
  60. $this->parseAndTraverse($code);
  61. $this->assertTrue(true);
  62. }
  63. public function getValidFunctions()
  64. {
  65. return [
  66. ['function psy_test_codecleaner_validfunctionnamepass_epsilon() {}'],
  67. ['
  68. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  69. function zeta() {}
  70. }
  71. '],
  72. ['
  73. namespace {
  74. function psy_test_codecleaner_validfunctionnamepass_eta() {}
  75. }
  76. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  77. function psy_test_codecleaner_validfunctionnamepass_eta() {}
  78. }
  79. '],
  80. ['
  81. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  82. function psy_test_codecleaner_validfunctionnamepass_eta() {}
  83. }
  84. namespace {
  85. function psy_test_codecleaner_validfunctionnamepass_eta() {}
  86. }
  87. '],
  88. ['
  89. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  90. function array_merge() {}
  91. }
  92. '],
  93. // closures
  94. ['$test = function(){};$test()'],
  95. ['
  96. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  97. function theta() {}
  98. }
  99. namespace {
  100. Psy\\Test\\CodeCleaner\\ValidFunctionNamePass\\theta();
  101. }
  102. '],
  103. // recursion
  104. ['function a() { a(); }'],
  105. // conditionally defined functions
  106. ['
  107. function a() {}
  108. if (false) {
  109. function a() {}
  110. }
  111. '],
  112. ['
  113. function a() {}
  114. if (true) {
  115. function a() {}
  116. } else if (false) {
  117. function a() {}
  118. } else {
  119. function a() {}
  120. }
  121. '],
  122. // ewww
  123. ['
  124. function a() {}
  125. if (true):
  126. function a() {}
  127. elseif (false):
  128. function a() {}
  129. else:
  130. function a() {}
  131. endif;
  132. '],
  133. ['
  134. function a() {}
  135. while (false) { function a() {} }
  136. '],
  137. ['
  138. function a() {}
  139. do { function a() {} } while (false);
  140. '],
  141. ['
  142. function a() {}
  143. switch (1) {
  144. case 0:
  145. function a() {}
  146. break;
  147. case 1:
  148. function a() {}
  149. break;
  150. case 2:
  151. function a() {}
  152. break;
  153. }
  154. '],
  155. ];
  156. }
  157. }