SupportReflectsClosuresTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use Illuminate\Support\Traits\ReflectsClosures;
  4. use PHPUnit\Framework\TestCase;
  5. use RuntimeException;
  6. class SupportReflectsClosuresTest extends TestCase
  7. {
  8. public function testReflectsClosures()
  9. {
  10. $this->assertParameterTypes([ExampleParameter::class], function (ExampleParameter $one) {
  11. // assert the Closure isn't actually executed
  12. throw new RuntimeException;
  13. });
  14. $this->assertParameterTypes([], function () {
  15. //
  16. });
  17. $this->assertParameterTypes([null], function ($one) {
  18. //
  19. });
  20. $this->assertParameterTypes([null, ExampleParameter::class], function ($one, ExampleParameter $two = null) {
  21. //
  22. });
  23. $this->assertParameterTypes([null, ExampleParameter::class], function (string $one, ?ExampleParameter $two) {
  24. //
  25. });
  26. // Because the parameter is variadic, the closure will always receive an array.
  27. $this->assertParameterTypes([null], function (ExampleParameter ...$vars) {
  28. //
  29. });
  30. }
  31. public function testItReturnsTheFirstParameterType()
  32. {
  33. $type = ReflectsClosuresClass::reflectFirst(function (ExampleParameter $a) {
  34. //
  35. });
  36. $this->assertInstanceOf($type, new ExampleParameter);
  37. }
  38. public function testItThrowsWhenNoParameters()
  39. {
  40. $this->expectException(RuntimeException::class);
  41. ReflectsClosuresClass::reflectFirst(function () {
  42. //
  43. });
  44. }
  45. public function testItThrowsWhenNoFirstParameterType()
  46. {
  47. $this->expectException(RuntimeException::class);
  48. ReflectsClosuresClass::reflectFirst(function ($a, ExampleParameter $b) {
  49. //
  50. });
  51. }
  52. /**
  53. * @requires PHP >= 8
  54. */
  55. public function testItWorksWithUnionTypes()
  56. {
  57. $types = ReflectsClosuresClass::reflectFirstAll(function (ExampleParameter $a, $b) {
  58. //
  59. });
  60. $this->assertEquals([
  61. ExampleParameter::class,
  62. ], $types);
  63. $closure = require __DIR__.'/Fixtures/UnionTypesClosure.php';
  64. $types = ReflectsClosuresClass::reflectFirstAll($closure);
  65. $this->assertEquals([
  66. ExampleParameter::class,
  67. AnotherExampleParameter::class,
  68. ], $types);
  69. }
  70. public function testItWorksWithUnionTypesWithNoTypeHints()
  71. {
  72. $this->expectException(RuntimeException::class);
  73. $types = ReflectsClosuresClass::reflectFirstAll(function ($a, $b) {
  74. //
  75. });
  76. }
  77. public function testItWorksWithUnionTypesWithNoArguments()
  78. {
  79. $this->expectException(RuntimeException::class);
  80. $types = ReflectsClosuresClass::reflectFirstAll(function () {
  81. //
  82. });
  83. }
  84. private function assertParameterTypes($expected, $closure)
  85. {
  86. $types = ReflectsClosuresClass::reflect($closure);
  87. $this->assertSame($expected, $types);
  88. }
  89. }
  90. class ReflectsClosuresClass
  91. {
  92. use ReflectsClosures;
  93. public static function reflect($closure)
  94. {
  95. return array_values((new static)->closureParameterTypes($closure));
  96. }
  97. public static function reflectFirst($closure)
  98. {
  99. return (new static)->firstClosureParameterType($closure);
  100. }
  101. public static function reflectFirstAll($closure)
  102. {
  103. return (new static)->firstClosureParameterTypes($closure);
  104. }
  105. }
  106. class ExampleParameter
  107. {
  108. //
  109. }
  110. class AnotherExampleParameter
  111. {
  112. //
  113. }