ReflectionTest.php 3.7 KB

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