ValidFunctionNamePassTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2017 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. $stmts = $this->parse($code);
  25. $this->traverse($stmts);
  26. }
  27. public function getInvalidFunctions()
  28. {
  29. return array(
  30. // function declarations
  31. array('function array_merge() {}'),
  32. array('function Array_Merge() {}'),
  33. array('
  34. function psy_test_codecleaner_validfunctionnamepass_alpha() {}
  35. function psy_test_codecleaner_validfunctionnamepass_alpha() {}
  36. '),
  37. array('
  38. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  39. function beta() {}
  40. }
  41. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  42. function beta() {}
  43. }
  44. '),
  45. // function calls
  46. array('psy_test_codecleaner_validfunctionnamepass_gamma()'),
  47. array('
  48. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  49. delta();
  50. }
  51. '),
  52. // recursion
  53. array('function a() { a(); } function a() {}'),
  54. );
  55. }
  56. /**
  57. * @dataProvider getValidFunctions
  58. */
  59. public function testProcessValidFunctionCallsAndDeclarations($code)
  60. {
  61. $stmts = $this->parse($code);
  62. $this->traverse($stmts);
  63. }
  64. public function getValidFunctions()
  65. {
  66. return array(
  67. array('function psy_test_codecleaner_validfunctionnamepass_epsilon() {}'),
  68. array('
  69. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  70. function zeta() {}
  71. }
  72. '),
  73. array('
  74. namespace {
  75. function psy_test_codecleaner_validfunctionnamepass_eta() {}
  76. }
  77. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  78. function psy_test_codecleaner_validfunctionnamepass_eta() {}
  79. }
  80. '),
  81. array('
  82. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  83. function psy_test_codecleaner_validfunctionnamepass_eta() {}
  84. }
  85. namespace {
  86. function psy_test_codecleaner_validfunctionnamepass_eta() {}
  87. }
  88. '),
  89. array('
  90. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  91. function array_merge() {}
  92. }
  93. '),
  94. // function calls
  95. array('array_merge();'),
  96. array('
  97. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  98. function theta() {}
  99. }
  100. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  101. theta();
  102. }
  103. '),
  104. // closures
  105. array('$test = function(){};$test()'),
  106. array('
  107. namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
  108. function theta() {}
  109. }
  110. namespace {
  111. Psy\\Test\\CodeCleaner\\ValidFunctionNamePass\\theta();
  112. }
  113. '),
  114. // recursion
  115. array('function a() { a(); }'),
  116. // conditionally defined functions
  117. array('
  118. function a() {}
  119. if (false) {
  120. function a() {}
  121. }
  122. '),
  123. array('
  124. function a() {}
  125. if (true) {
  126. function a() {}
  127. } else if (false) {
  128. function a() {}
  129. } else {
  130. function a() {}
  131. }
  132. '),
  133. // ewww
  134. array('
  135. function a() {}
  136. if (true):
  137. function a() {}
  138. elseif (false):
  139. function a() {}
  140. else:
  141. function a() {}
  142. endif;
  143. '),
  144. array('
  145. function a() {}
  146. while (false) { function a() {} }
  147. '),
  148. array('
  149. function a() {}
  150. do { function a() {} } while (false);
  151. '),
  152. array('
  153. function a() {}
  154. switch (1) {
  155. case 0:
  156. function a() {}
  157. break;
  158. case 1:
  159. function a() {}
  160. break;
  161. case 2:
  162. function a() {}
  163. break;
  164. }
  165. '),
  166. );
  167. }
  168. }