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.
This commit is contained in:
Julien Fastré 2024-06-07 12:35:14 +02:00
parent 4c3f082163
commit 61877e0157
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB

View File

@ -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));