SerializerPhp80Test.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. use Tests\Fixtures\RegularClass;
  3. test('named arguments', function () {
  4. $f1 = function (string $firstName, string $lastName) {
  5. return $firstName.' '.$lastName;
  6. };
  7. expect('Marco Deleu')->toBe(s($f1)(
  8. lastName: 'Deleu',
  9. firstName: 'Marco'
  10. ));
  11. })->with('serializers');
  12. test('single named argument within closures', function () {
  13. $f1 = function () {
  14. return (new SerializerPhp80NamedArguments)->publicMethod(
  15. namedArgument: 'string'
  16. );
  17. };
  18. expect('string')->toBe(s($f1)());
  19. })->with('serializers');
  20. test('multiple named arguments within closures', function () {
  21. $f1 = function () {
  22. return (new SerializerPhp80NamedArguments)->publicMethod(
  23. namedArgument: 'string', namedArgumentB: 1
  24. );
  25. };
  26. expect('string1')->toBe(s($f1)());
  27. })->with('serializers');
  28. test('multiple named arguments within nested closures', function () {
  29. $f1 = function () {
  30. $fn = fn ($namedArgument, $namedArgumentB) => (
  31. new SerializerPhp80NamedArguments
  32. )->publicMethod(namedArgument: $namedArgument, namedArgumentB: $namedArgumentB);
  33. return $fn(namedArgument: 'string', namedArgumentB: 1);
  34. };
  35. expect('string1')->toBe(s($f1)());
  36. })->with('serializers');
  37. test('named arguments with namespaced class const parameter', function () {
  38. $f1 = function () {
  39. return new RegularClass(a2: RegularClass::C);
  40. };
  41. $instance = s($f1)();
  42. expect($instance)->toBeInstanceOf(RegularClass::class)
  43. ->and($instance->a1)->toBeNull()
  44. ->and($instance->a2)->toBe('CONST');
  45. })->with('serializers');
  46. test('named arguments with match statements', function () {
  47. $f1 = function ($a) {
  48. return new RegularClass(a2: match ($a) {
  49. 1 => RegularClass::C,
  50. 2 => null,
  51. });
  52. };
  53. $instance = s($f1)(1);
  54. expect($instance)->toBeInstanceOf(RegularClass::class)
  55. ->and($instance->a1)->toBeNull()
  56. ->and($instance->a2)->toBe('CONST');
  57. $instance = s($f1)(2);
  58. expect($instance)->toBeInstanceOf(RegularClass::class)
  59. ->and($instance->a1)->toBeNull()
  60. ->and($instance->a2)->toBeNull();
  61. })->with('serializers');
  62. test('named arguments with switch cases and instanceof', function () {
  63. $f1 = function ($a) {
  64. switch (true) {
  65. case (new RegularClass(a2: $a))->a2 instanceof RegularClass:
  66. return (new RegularClass(a2: $a))->a2;
  67. default:
  68. return new DateTime();
  69. }
  70. };
  71. $instance = s($f1)('anything');
  72. expect($instance)->toBeInstanceOf(DateTime::class);
  73. $instance = s($f1)(new RegularClass());
  74. expect($instance)->toBeInstanceOf(RegularClass::class);
  75. })->with('serializers');
  76. test('named arguments with namespaced class instance parameter', function () {
  77. $f1 = function () {
  78. return new RegularClass(a2: new RegularClass());
  79. };
  80. $instance = s($f1)();
  81. expect($instance)->toBeInstanceOf(RegularClass::class)
  82. ->and($instance->a1)->toBeNull()
  83. ->and($instance->a2)->toBeInstanceOf(RegularClass::class);
  84. })->with('serializers');
  85. class SerializerPhp80NamedArguments
  86. {
  87. public function publicMethod(string $namedArgument, $namedArgumentB = null)
  88. {
  89. return $namedArgument.(string) $namedArgumentB;
  90. }
  91. }
  92. function serializer_php_80_switch_statement_test_is_two($a)
  93. {
  94. return $a === 2;
  95. }
  96. class SerializerPhp80SwitchStatementClass
  97. {
  98. public static function isThree($a)
  99. {
  100. return $a === 3;
  101. }
  102. public function isFour($a)
  103. {
  104. return $a === 4;
  105. }
  106. }
  107. class SerializerPhp80Class
  108. {
  109. }
  110. test('instanceof', function () {
  111. $closure = function (object $a): array {
  112. $b = $a instanceof DateTime || $a instanceof SerializerPhp80Class;
  113. return [
  114. $b,
  115. $a instanceof DateTime || $a instanceof SerializerPhp80Class,
  116. (function (object $a): bool {
  117. return ($a instanceof DateTime || $a instanceof SerializerPhp80Class) === true;
  118. })(a: $a),
  119. ];
  120. };
  121. $u = s($closure);
  122. expect($u(new DateTime))->toEqual([true, true, true])
  123. ->and($u(new SerializerPhp80Class))->toEqual([true, true, true])
  124. ->and($u(new stdClass))->toEqual([false, false, false]);
  125. })->with('serializers');
  126. test('switch statement', function () {
  127. $closure = function ($a) {
  128. switch (true) {
  129. case $a === 1:
  130. return 'one';
  131. case serializer_php_80_switch_statement_test_is_two(a: $a):
  132. return 'two';
  133. case SerializerPhp80SwitchStatementClass::isThree(a: $a):
  134. return 'three';
  135. case (new SerializerPhp80SwitchStatementClass)->isFour(a: $a):
  136. return 'four';
  137. case $a instanceof SerializerPhp80SwitchStatementClass:
  138. return 'five';
  139. case $a instanceof DateTime:
  140. return 'six';
  141. default:
  142. return 'other';
  143. }
  144. };
  145. $u = s($closure);
  146. expect($u(1))->toEqual('one')
  147. ->and($u(2))->toEqual('two')
  148. ->and($u(3))->toEqual('three')
  149. ->and($u(4))->toEqual('four')
  150. ->and($u(new SerializerPhp80SwitchStatementClass))->toEqual('five')
  151. ->and($u(new DateTime))->toEqual('six')
  152. ->and($u(999))->toEqual('other');
  153. })->with('serializers');
  154. function serializer_php_80_match_statement_test_is_two($a)
  155. {
  156. return $a === 2;
  157. }
  158. class SerializerPhp80MatchStatementTest
  159. {
  160. public static function isThree($a)
  161. {
  162. return $a === 3;
  163. }
  164. public function isFour($a)
  165. {
  166. return $a === 4;
  167. }
  168. }
  169. test('match statement', function () {
  170. $closure = function ($a) {
  171. return match (true) {
  172. $a === 1 => 'one',
  173. serializer_php_80_match_statement_test_is_two($a) => 'two',
  174. SerializerPhp80MatchStatementTest::isThree($a) => 'three',
  175. (new SerializerPhp80MatchStatementTest)->isFour(a: $a) => 'four',
  176. $a instanceof SerializerPhp80MatchStatementTest => 'five',
  177. $a instanceof DateTime => 'six',
  178. $a instanceof RegularClass => 'seven',
  179. default => 'other',
  180. };
  181. };
  182. $u = s($closure);
  183. expect($u(1))->toEqual('one')
  184. ->and($u(2))->toEqual('two')
  185. ->and($u(3))->toEqual('three')
  186. ->and($u(4))->toEqual('four')
  187. ->and($u(new SerializerPhp80MatchStatementTest))->toEqual('five')
  188. ->and($u(new DateTime))->toEqual('six')
  189. ->and($u(new RegularClass))->toEqual('seven')
  190. ->and($u(999))->toEqual('other');
  191. })->with('serializers');