From 61877e015717573ea7e40aefb0a16c272966da97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 7 Jun 2024 12:35:14 +0200 Subject: [PATCH] Refactor UserRenderTest and remove unused methods The UserRenderTest class has been refactored significantly. Redundant methods related to the booting kernel of Symfony have been removed. The approach of mocking objects has been changed, swapping from traditional mocking to prophecy mocking. --- .../Templating/Entity/UserRenderTest.php | 33 ++++--------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Tests/Templating/Entity/UserRenderTest.php b/src/Bundle/ChillMainBundle/Tests/Templating/Entity/UserRenderTest.php index 0efccc96c..1d6c91e13 100644 --- a/src/Bundle/ChillMainBundle/Tests/Templating/Entity/UserRenderTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Templating/Entity/UserRenderTest.php @@ -16,11 +16,9 @@ use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\UserJob; use Chill\MainBundle\Templating\Entity\UserRender; use Chill\MainBundle\Templating\TranslatableStringHelperInterface; -use Doctrine\ORM\EntityManagerInterface; -use JetBrains\PhpStorm\NoReturn; -use libphonenumber\PhoneNumberUtil; +use PHPUnit\Framework\TestCase; +use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; -use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\Clock\MockClock; use Symfony\Contracts\Translation\TranslatorInterface; use Twig\Environment; @@ -30,21 +28,12 @@ use Twig\Environment; * * @coversNothing */ -class UserRenderTest extends WebTestCase +class UserRenderTest extends TestCase { use ProphecyTrait; - public function setUp(): void - { - self::bootKernel(); - } - - #[NoReturn] public function testRenderUserWithJobAndScopeAtCertainDate(): void { - self::bootKernel(); - $em = self::$container->get(EntityManagerInterface::class); - assert($em instanceof EntityManagerInterface); // Create a user with a certain user job $user = new User(); @@ -55,7 +44,6 @@ class UserRenderTest extends WebTestCase ->setActive(true); $scopeA->setName(['fr' => 'service A']); $user->setLabel('BOB ISLA'); - // $user->setPhonenumber(PhoneNumberUtil::getInstance()->parse('+32475928635')); $userJobB = new UserJob(); $scopeB = new Scope(); @@ -92,29 +80,20 @@ class UserRenderTest extends WebTestCase $user->getUserJobHistories()->add($userJobHistoryB); $user->getUserScopeHistories()->add($userScopeHistoryB); - $em->persist($user); - $em->persist($userJobA); - $em->persist($userJobB); - $em->persist($scopeA); - $em->persist($scopeB); - - $em->flush(); - // Create renderer - $translatableStringHelperMock = $this->createMock(TranslatableStringHelperInterface::class); + $translatableStringHelperMock = $this->prophesize(TranslatableStringHelperInterface::class); + $translatableStringHelperMock->localize(Argument::type('array'))->will(fn($args) => $args[0]['fr']); $engineMock = $this->createMock(Environment::class); $translatorMock = $this->createMock(TranslatorInterface::class); $clock = new MockClock(new \DateTimeImmutable('2023-12-15 12:00:00')); - $renderer = new UserRender($translatableStringHelperMock, $engineMock, $translatorMock, $clock); + $renderer = new UserRender($translatableStringHelperMock->reveal(), $engineMock, $translatorMock, $clock); $optionsNoDate['at_date'] = null; $options['at_date'] = new \DateTime('2023-11-25 12:00:00'); $optionsTwo['at_date'] = new \DateTime('2024-01-30 12:00:00'); - // dd($user); - // Check that the user render for the first activity corresponds with the first user job $expectedStringA = 'BOB ISLA (assistant social) (service A)'; $this->assertEquals($expectedStringA, $renderer->renderString($user, $options));