ReflectionTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Tests\Unit\Service\Utils;
  3. use App\Service\Utils\Reflection;
  4. use PHPUnit\Framework\MockObject\MockObject;
  5. use PHPUnit\Framework\TestCase;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. class ReflectionTestSubject
  8. {
  9. public function __construct($c)
  10. {
  11. }
  12. public function multiply(int $a, int $b): int
  13. {
  14. return $a * $b;
  15. }
  16. public static function add(int $a, int $b): int
  17. {
  18. return $a + $b;
  19. }
  20. }
  21. class ReflectionTest extends TestCase
  22. {
  23. private ContainerInterface|MockObject $container;
  24. public function setUp(): void
  25. {
  26. $this->container = $this->getMockBuilder(ContainerInterface::class)->disableOriginalConstructor()->getMock();
  27. }
  28. /**
  29. * @see Reflection::dynamicInvokeServiceWithArgsAndMethod()
  30. */
  31. public function testDynamicInvokeServiceWithArgsAndMethod(): void
  32. {
  33. $reflection = $this->getMockBuilder(Reflection::class)
  34. ->setConstructorArgs([$this->container])
  35. ->setMethodsExcept(['dynamicInvokeServiceWithArgsAndMethod'])
  36. ->getMock();
  37. $subject = new ReflectionTestSubject(1);
  38. $this->container->method('get')->with('subject')->willReturn($subject);
  39. $this->assertEquals(
  40. 6,
  41. $reflection->dynamicInvokeServiceWithArgsAndMethod('subject', 'multiply', [2, 3])
  42. );
  43. }
  44. /**
  45. * @see Reflection::dynamicInvokeServiceWithArgsAndMethod()
  46. */
  47. public function testDynamicInvokeServiceWithArgsAndClassNotFound(): void
  48. {
  49. $reflection = $this->getMockBuilder(Reflection::class)
  50. ->setConstructorArgs([$this->container])
  51. ->setMethodsExcept(['dynamicInvokeServiceWithArgsAndMethod'])
  52. ->getMock();
  53. $this->container->method('get')->with('subject')->willReturn(null);
  54. $this->expectException(\LogicException::class);
  55. $reflection->dynamicInvokeServiceWithArgsAndMethod('subject', 'invalid');
  56. }
  57. /**
  58. * @see Reflection::dynamicInvokeServiceWithArgsAndMethod()
  59. */
  60. public function testDynamicInvokeServiceWithArgsAndMethodInexistant(): void
  61. {
  62. $reflection = $this->getMockBuilder(Reflection::class)
  63. ->setConstructorArgs([$this->container])
  64. ->setMethodsExcept(['dynamicInvokeServiceWithArgsAndMethod'])
  65. ->getMock();
  66. $subject = $this->getMockBuilder(ReflectionTestSubject::class)->disableOriginalConstructor()->getMock();
  67. $this->container->method('get')->with('subject')->willReturn($subject);
  68. $this->expectException(\LogicException::class);
  69. $reflection->dynamicInvokeServiceWithArgsAndMethod('subject', 'invalid');
  70. }
  71. /**
  72. * @see Reflection::dynamicInvokeClassWithArgsAndMethod()
  73. */
  74. public function testDynamicInvokeClassWithArgsAndMethod(): void
  75. {
  76. $reflection = $this->getMockBuilder(Reflection::class)
  77. ->setConstructorArgs([$this->container])
  78. ->setMethodsExcept(['dynamicInvokeClassWithArgsAndMethod'])
  79. ->getMock();
  80. $this->assertEquals(
  81. 6,
  82. $reflection->dynamicInvokeClassWithArgsAndMethod(
  83. ReflectionTestSubject::class,
  84. 'multiply',
  85. [2, 3],
  86. [1]
  87. )
  88. );
  89. }
  90. /**
  91. * @see Reflection::dynamicInvokeClassWithArgsAndMethod()
  92. */
  93. public function testDynamicInvokeClassWithArgsAndMethodWithStatic(): void
  94. {
  95. $reflection = $this->getMockBuilder(Reflection::class)
  96. ->setConstructorArgs([$this->container])
  97. ->setMethodsExcept(['dynamicInvokeClassWithArgsAndMethod'])
  98. ->getMock();
  99. $this->assertEquals(
  100. 5,
  101. $reflection->dynamicInvokeClassWithArgsAndMethod(ReflectionTestSubject::class, 'add', [2, 3])
  102. );
  103. }
  104. }