From 7f5298cd0070c5eef320cceed910a375af455203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Poirier=20Th=C3=A9or=C3=AAt?= Date: Wed, 20 Nov 2024 21:41:02 -0500 Subject: [PATCH] [TesterBundle] New AutowireServiceMock --- .../UserSetCommentNullMigration.php | 12 ---- .../SetUpAutowire/AutowireServiceMock.php | 59 +++++++++++++++++++ .../KernelTestCaseAutowireDependentTrait.php | 2 +- packages/tester-bundle/README.md | 7 +++ .../SetUpAutoWire/AutowireServiceMockTest.php | 40 +++++++++++++ 5 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 packages/tester-bundle/PHPUnit/Extension/SetUpAutowire/AutowireServiceMock.php create mode 100644 tests/TesterBundle/PHPUnit/Extension/SetUpAutoWire/AutowireServiceMockTest.php diff --git a/app/src/EntityMigration/UserSetCommentNullMigration.php b/app/src/EntityMigration/UserSetCommentNullMigration.php index 8d097262..c25424c9 100644 --- a/app/src/EntityMigration/UserSetCommentNullMigration.php +++ b/app/src/EntityMigration/UserSetCommentNullMigration.php @@ -38,18 +38,6 @@ public function needMigration(MigrationTargetEntityInterface $entity): bool return '' !== $entity->getComment(); } - public function countAllThatNeedMigration(): ?int - { - $manager = $this->managerRegistry->getManagerForClass(User::class); - \assert($manager instanceof EntityManagerInterface); - - return (int) $manager - ->createQuery('SELECT count(user) FROM '.User::class.' user WHERE user.comment != :comment') - ->setParameter('comment', '') - ->getSingleScalarResult() - ; - } - public function createSelectIdQueryBuilder(): QueryBuilder { $manager = $this->managerRegistry->getManagerForClass(User::class); diff --git a/packages/tester-bundle/PHPUnit/Extension/SetUpAutowire/AutowireServiceMock.php b/packages/tester-bundle/PHPUnit/Extension/SetUpAutowire/AutowireServiceMock.php new file mode 100644 index 00000000..3e15d348 --- /dev/null +++ b/packages/tester-bundle/PHPUnit/Extension/SetUpAutowire/AutowireServiceMock.php @@ -0,0 +1,59 @@ +getValue($testCase); + + $this->getContainer($testCase)->set( + $this->getServiceId($reflectionProperty), + $value + ); + } + + private function getServiceId(\ReflectionProperty $reflectionProperty): string + { + $serviceId = $this->serviceId; + + if ($serviceId) { + return $serviceId; + } + + $type = $reflectionProperty->getType(); + + \assert($type instanceof \ReflectionIntersectionType); + + foreach ($type->getTypes() as $type) { + \assert($type instanceof \ReflectionNamedType); + + if (MockObject::class === $type->getName()) { + continue; + } + + return $type->getName(); + } + + throw new \RuntimeException('Cannot load service id from property '.$reflectionProperty->getName().' of class '.$reflectionProperty->getDeclaringClass()->getName()); + } +} diff --git a/packages/tester-bundle/PHPUnit/Extension/SetUpAutowire/KernelTestCaseAutowireDependentTrait.php b/packages/tester-bundle/PHPUnit/Extension/SetUpAutowire/KernelTestCaseAutowireDependentTrait.php index 8a11f8c7..74eb5ff7 100644 --- a/packages/tester-bundle/PHPUnit/Extension/SetUpAutowire/KernelTestCaseAutowireDependentTrait.php +++ b/packages/tester-bundle/PHPUnit/Extension/SetUpAutowire/KernelTestCaseAutowireDependentTrait.php @@ -3,8 +3,8 @@ namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire; use PHPUnit\Framework\TestCase; -use Psr\Container\ContainerInterface; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\DependencyInjection\ContainerInterface; trait KernelTestCaseAutowireDependentTrait { diff --git a/packages/tester-bundle/README.md b/packages/tester-bundle/README.md index ba818512..f6290afd 100644 --- a/packages/tester-bundle/README.md +++ b/packages/tester-bundle/README.md @@ -108,11 +108,13 @@ namespace App\Tests; use App\AServiceInterface; use App\Entity\User; use App\MyService; +use App\MyOtherService; use Draw\Bundle\TesterBundle\Messenger\TransportTester; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireEntity; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireLoggerTester; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireParameter; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService; +use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireServiceMock; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireTransportTester; use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface; use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowireMock; @@ -148,6 +150,11 @@ class MyTest extends KernelTestCase implements AutowiredInterface #[AutowireEntity(['email' => 'test@example.com'])] private User $user; + + // Will create a mock object of MyOtherService and call container->set(MyOtherService::class, $mockObject) + // You can also set the service id to use in the container as the first parameter of the attribute. + #[AutowireServiceMock] + private MyOtherService&MockObject $myOtherService; } ``` diff --git a/tests/TesterBundle/PHPUnit/Extension/SetUpAutoWire/AutowireServiceMockTest.php b/tests/TesterBundle/PHPUnit/Extension/SetUpAutoWire/AutowireServiceMockTest.php new file mode 100644 index 00000000..8c892db0 --- /dev/null +++ b/tests/TesterBundle/PHPUnit/Extension/SetUpAutoWire/AutowireServiceMockTest.php @@ -0,0 +1,40 @@ +entityHandler + ->expects(static::once()) + ->method('findAll') + ->with(User::class) + ->willReturn([]) + ; + + $this->client + ->request('GET', '/api/users') + ; + + static::assertResponseIsSuccessful(); + } +}