ReflectionClosure1Test.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. // Fake
  3. use Foo\Bar;
  4. use Foo\Baz as Qux;
  5. use Tests\Fixtures\RegularClass;
  6. test('new instance', function () {
  7. $f = function () {
  8. $c = '\A';
  9. new $c();
  10. };
  11. $e = 'function () {
  12. $c = \'\A\';
  13. new $c();
  14. }';
  15. expect($f)->toBeCode($e);
  16. });
  17. test('new instance2', function () {
  18. $f = function () {
  19. new A();
  20. };
  21. $e = 'function () {
  22. new \A();
  23. }';
  24. expect($f)->toBeCode($e);
  25. $f = function () {
  26. new A\B();
  27. };
  28. $e = 'function () {
  29. new \A\B();
  30. }';
  31. expect($f)->toBeCode($e);
  32. $f = function () {
  33. new \A();
  34. };
  35. $e = 'function () {
  36. new \A();
  37. }';
  38. expect($f)->toBeCode($e);
  39. $f = function () {
  40. new A(new B(), [new C()]);
  41. };
  42. $e = 'function () {
  43. new \A(new \B(), [new \C()]);
  44. }';
  45. expect($f)->toBeCode($e);
  46. $f = function () {
  47. new self();
  48. new static();
  49. new parent();
  50. };
  51. $e = 'function () {
  52. new self();
  53. new static();
  54. new parent();
  55. }';
  56. expect($f)->toBeCode($e);
  57. });
  58. test('instance of', function () {
  59. $f = function () {
  60. $c = null;
  61. $b = '\X\y';
  62. v($c instanceof $b);
  63. };
  64. $e = 'function () {
  65. $c = null;
  66. $b = \'\X\y\';
  67. v($c instanceof $b);
  68. }';
  69. expect($f)->toBeCode($e);
  70. });
  71. test('closure resolve arguments', function () {
  72. $f1 = function (Bar $p) {
  73. };
  74. $e1 = 'function (\Foo\Bar $p) {
  75. }';
  76. $f2 = function (Bar\Test $p) {
  77. };
  78. $e2 = 'function (\Foo\Bar\Test $p) {
  79. }';
  80. $f3 = function (Qux $p) {
  81. };
  82. $e3 = 'function (\Foo\Baz $p) {
  83. }';
  84. $f4 = function (Qux\Test $p) {
  85. };
  86. $e4 = 'function (\Foo\Baz\Test $p) {
  87. }';
  88. $f5 = function (\Foo $p) {
  89. };
  90. $e5 = 'function (\Foo $p) {
  91. }';
  92. $f6 = function (Foo $p) {
  93. };
  94. $e6 = 'function (\Foo $p) {
  95. }';
  96. expect($f1)->toBeCode($e1);
  97. expect($f2)->toBeCode($e2);
  98. expect($f3)->toBeCode($e3);
  99. expect($f4)->toBeCode($e4);
  100. expect($f5)->toBeCode($e5);
  101. expect($f6)->toBeCode($e6);
  102. });
  103. test('cloure resolve in body', function () {
  104. $f1 = function () {
  105. return new Bar();
  106. };
  107. $e1 = 'function () {
  108. return new \Foo\Bar();
  109. }';
  110. $f2 = function () {
  111. return new Bar\Test();
  112. };
  113. $e2 = 'function () {
  114. return new \Foo\Bar\Test();
  115. }';
  116. $f3 = function () {
  117. return new Qux();
  118. };
  119. $e3 = 'function () {
  120. return new \Foo\Baz();
  121. }';
  122. $f4 = function () {
  123. return new Qux\Test();
  124. };
  125. $e4 = 'function () {
  126. return new \Foo\Baz\Test();
  127. }';
  128. $f5 = function () {
  129. return new \Foo();
  130. };
  131. $e5 = 'function () {
  132. return new \Foo();
  133. }';
  134. $f6 = function () {
  135. return new Foo();
  136. };
  137. $e6 = 'function () {
  138. return new \Foo();
  139. }';
  140. expect($f1)->toBeCode($e1);
  141. expect($f2)->toBeCode($e2);
  142. expect($f3)->toBeCode($e3);
  143. expect($f4)->toBeCode($e4);
  144. expect($f5)->toBeCode($e5);
  145. expect($f6)->toBeCode($e6);
  146. });
  147. test('closure resolve static method', function () {
  148. $f1 = function () {
  149. return Bar::test();
  150. };
  151. $e1 = 'function () {
  152. return \Foo\Bar::test();
  153. }';
  154. $f2 = function () {
  155. return Bar\Test::test();
  156. };
  157. $e2 = 'function () {
  158. return \Foo\Bar\Test::test();
  159. }';
  160. $f3 = function () {
  161. return Qux::test();
  162. };
  163. $e3 = 'function () {
  164. return \Foo\Baz::test();
  165. }';
  166. $f4 = function () {
  167. return Qux\Test::test();
  168. };
  169. $e4 = 'function () {
  170. return \Foo\Baz\Test::test();
  171. }';
  172. $f5 = function () {
  173. return Foo::test();
  174. };
  175. $e5 = 'function () {
  176. return \Foo::test();
  177. }';
  178. $f6 = function () {
  179. return Foo::test();
  180. };
  181. $e6 = 'function () {
  182. return \Foo::test();
  183. }';
  184. expect($f1)->toBeCode($e1);
  185. expect($f2)->toBeCode($e2);
  186. expect($f3)->toBeCode($e3);
  187. expect($f4)->toBeCode($e4);
  188. expect($f5)->toBeCode($e5);
  189. expect($f6)->toBeCode($e6);
  190. });
  191. test('static inside closure', function () {
  192. $f1 = function () {
  193. return static::foo();
  194. };
  195. $e1 = 'function () {
  196. return static::foo();
  197. }';
  198. $f2 = function ($a) {
  199. return $a instanceof static;
  200. };
  201. $e2 = 'function ($a) {
  202. return $a instanceof static;
  203. }';
  204. expect($f1)->toBeCode($e1);
  205. expect($f2)->toBeCode($e2);
  206. });
  207. test('self inside closure', function () {
  208. $f1 = function () {
  209. return self::foo();
  210. };
  211. $e1 = 'function () {
  212. return self::foo();
  213. }';
  214. $f2 = function ($a) {
  215. return $a instanceof self;
  216. };
  217. $e2 = 'function ($a) {
  218. return $a instanceof self;
  219. }';
  220. expect($f1)->toBeCode($e1);
  221. expect($f2)->toBeCode($e2);
  222. });
  223. test('parent inside closure', function () {
  224. $f1 = function () {
  225. return parent::foo();
  226. };
  227. $e1 = 'function () {
  228. return parent::foo();
  229. }';
  230. $f2 = function ($a) {
  231. return $a instanceof parent;
  232. };
  233. $e2 = 'function ($a) {
  234. return $a instanceof parent;
  235. }';
  236. expect($f1)->toBeCode($e1);
  237. expect($f2)->toBeCode($e2);
  238. });
  239. test('interpolation1', function () {
  240. $f1 = function () {
  241. return "{$foo}{$bar}{$foobar}";
  242. };
  243. $e1 = 'function () {
  244. return "{$foo}{$bar}{$foobar}";
  245. }';
  246. expect($f1)->toBeCode($e1);
  247. });
  248. test('consts', function () {
  249. $f1 = function () {
  250. return RegularClass::C;
  251. };
  252. $e1 = 'function () {
  253. return \Tests\Fixtures\RegularClass::C;
  254. }';
  255. expect($f1)->toBeCode($e1);
  256. });
  257. function reflection_closure_php_74_switch_statement_test_is_two($a)
  258. {
  259. return $a === 2;
  260. }
  261. class ReflectionClosurePhp74InstanceOfTest
  262. {
  263. }
  264. class ReflectionClosurePhp74SwitchStatementTest
  265. {
  266. }
  267. test('instanceof', function () {
  268. $f1 = function ($a) {
  269. $b = $a instanceof DateTime || $a instanceof ReflectionClosurePhp74InstanceOfTest || $a instanceof RegularClass;
  270. return [
  271. $b,
  272. $a instanceof DateTime || $a instanceof ReflectionClosurePhp74InstanceOfTest || $a instanceof RegularClass,
  273. (function ($a) {
  274. return ($a instanceof DateTime || $a instanceof ReflectionClosurePhp74InstanceOfTest || $a instanceof RegularClass) === true;
  275. })($a),
  276. ];
  277. };
  278. $e1 = 'function ($a) {
  279. $b = $a instanceof \DateTime || $a instanceof \ReflectionClosurePhp74InstanceOfTest || $a instanceof \Tests\Fixtures\RegularClass;
  280. return [
  281. $b,
  282. $a instanceof \DateTime || $a instanceof \ReflectionClosurePhp74InstanceOfTest || $a instanceof \Tests\Fixtures\RegularClass,
  283. (function ($a) {
  284. return ($a instanceof \DateTime || $a instanceof \ReflectionClosurePhp74InstanceOfTest || $a instanceof \Tests\Fixtures\RegularClass) === true;
  285. })($a),
  286. ];
  287. }';
  288. expect($f1)->toBeCode($e1);
  289. });
  290. test('switch statement', function () {
  291. $f1 = function ($a) {
  292. switch (true) {
  293. case $a === 1:
  294. return 'one';
  295. case reflection_closure_php_74_switch_statement_test_is_two($a):
  296. return 'two';
  297. case ReflectionClosurePhp74SwitchStatementTest::isThree($a):
  298. return 'three';
  299. case (new ReflectionClosurePhp74SwitchStatementTest)->isFour($a):
  300. return 'four';
  301. case $a instanceof ReflectionClosurePhp74SwitchStatementTest:
  302. return 'five';
  303. case $a instanceof DateTime:
  304. return 'six';
  305. case $a instanceof RegularClass:
  306. return 'seven';
  307. default:
  308. return 'other';
  309. }
  310. };
  311. $e1 = 'function ($a) {
  312. switch (true) {
  313. case $a === 1:
  314. return \'one\';
  315. case \reflection_closure_php_74_switch_statement_test_is_two($a):
  316. return \'two\';
  317. case \ReflectionClosurePhp74SwitchStatementTest::isThree($a):
  318. return \'three\';
  319. case (new \ReflectionClosurePhp74SwitchStatementTest)->isFour($a):
  320. return \'four\';
  321. case $a instanceof \ReflectionClosurePhp74SwitchStatementTest:
  322. return \'five\';
  323. case $a instanceof \DateTime:
  324. return \'six\';
  325. case $a instanceof \Tests\Fixtures\RegularClass:
  326. return \'seven\';
  327. default:
  328. return \'other\';
  329. }
  330. }';
  331. expect($f1)->toBeCode($e1);
  332. });