ValidFunctionNamePassTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\ValidFunctionNamePass;
  12. class ValidFunctionNamePassTest extends CodeCleanerTestCase
  13. {
  14. public function setUp()
  15. {
  16. $this->setPass(new ValidFunctionNamePass());
  17. }
  18. /**
  19. * @dataProvider getInvalidFunctions
  20. * @expectedException \Psy\Exception\FatalErrorException
  21. */
  22. public function testProcessInvalidFunctionCallsAndDeclarations($code)
  23. {
  24. $this->parseAndTraverse($code);
  25. }
  26. public function getInvalidFunctions()
  27. {
  28. return [
  29. // function declarations
  30. ['function array_merge() {}'],
  31. ['function Array_Merge() {}'],
  32. ['
  33. function psy_test_codecleaner_validfunctionnamepass_alpha() {}
  34. function psy_test_codecleaner_validfunctionnamepass_alpha() {}
  35. '],
  36. ['
  37. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  38. function beta() {}
  39. }
  40. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  41. function beta() {}
  42. }
  43. '],
  44. // function calls
  45. ['psy_test_codecleaner_validfunctionnamepass_gamma()'],
  46. ['
  47. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  48. delta();
  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. // function calls
  94. ['array_merge();'],
  95. ['
  96. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  97. function theta() {}
  98. }
  99. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  100. theta();
  101. }
  102. '],
  103. // closures
  104. ['$test = function(){};$test()'],
  105. ['
  106. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  107. function theta() {}
  108. }
  109. namespace {
  110. Psy\\Test\\CodeCleaner\\ValidFunctionNamePass\\theta();
  111. }
  112. '],
  113. // recursion
  114. ['function a() { a(); }'],
  115. // conditionally defined functions
  116. ['
  117. function a() {}
  118. if (false) {
  119. function a() {}
  120. }
  121. '],
  122. ['
  123. function a() {}
  124. if (true) {
  125. function a() {}
  126. } else if (false) {
  127. function a() {}
  128. } else {
  129. function a() {}
  130. }
  131. '],
  132. // ewww
  133. ['
  134. function a() {}
  135. if (true):
  136. function a() {}
  137. elseif (false):
  138. function a() {}
  139. else:
  140. function a() {}
  141. endif;
  142. '],
  143. ['
  144. function a() {}
  145. while (false) { function a() {} }
  146. '],
  147. ['
  148. function a() {}
  149. do { function a() {} } while (false);
  150. '],
  151. ['
  152. function a() {}
  153. switch (1) {
  154. case 0:
  155. function a() {}
  156. break;
  157. case 1:
  158. function a() {}
  159. break;
  160. case 2:
  161. function a() {}
  162. break;
  163. }
  164. '],
  165. ];
  166. }
  167. }