ReflectionTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. $this->container->method('get')->with('subject')->willReturn(ReflectionTestSubject::class);
  54. $this->expectException(\LogicException::class);
  55. $reflection->dynamicInvokeServiceWithArgsAndMethod('subject', 'invalid');
  56. }
  57. /**
  58. * @see Reflection::dynamicInvokeClassWithArgsAndMethod()
  59. */
  60. public function testDynamicInvokeClassWithArgsAndMethod(): void {
  61. $reflection = $this->getMockBuilder(Reflection::class)
  62. ->setConstructorArgs([$this->container])
  63. ->setMethodsExcept(['dynamicInvokeClassWithArgsAndMethod'])
  64. ->getMock();
  65. $this->assertEquals(
  66. 6,
  67. $reflection->dynamicInvokeClassWithArgsAndMethod(
  68. ReflectionTestSubject::class,
  69. 'multiply',
  70. [2, 3],
  71. [1]
  72. )
  73. );
  74. }
  75. /**
  76. * @see Reflection::dynamicInvokeClassWithArgsAndMethod()
  77. */
  78. public function testDynamicInvokeClassWithArgsAndMethodWithStatic(): void {
  79. $reflection = $this->getMockBuilder(Reflection::class)
  80. ->setConstructorArgs([$this->container])
  81. ->setMethodsExcept(['dynamicInvokeClassWithArgsAndMethod'])
  82. ->getMock();
  83. $this->assertEquals(
  84. 5,
  85. $reflection->dynamicInvokeClassWithArgsAndMethod(ReflectionTestSubject::class, 'add', [2, 3])
  86. );
  87. }
  88. }