Fix test on CalendarType: use the real IdToEntityDataTransformer, mocking the repository

This commit is contained in:
Julien Fastré 2023-08-28 15:22:08 +02:00
parent 2c83b4c912
commit 667104a595
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
4 changed files with 38 additions and 15 deletions

View File

@ -32,8 +32,14 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
class CalendarType extends AbstractType class CalendarType extends AbstractType
{ {
public function __construct(private readonly PersonsToIdDataTransformer $personsToIdDataTransformer, private readonly IdToUserDataTransformer $idToUserDataTransformer, private readonly IdToUsersDataTransformer $idToUsersDataTransformer, private readonly IdToLocationDataTransformer $idToLocationDataTransformer, private readonly ThirdPartiesToIdDataTransformer $partiesToIdDataTransformer, private readonly IdToCalendarRangeDataTransformer $calendarRangeDataTransformer) public function __construct(
{ private readonly PersonsToIdDataTransformer $personsToIdDataTransformer,
private readonly IdToUserDataTransformer $idToUserDataTransformer,
private readonly IdToUsersDataTransformer $idToUsersDataTransformer,
private readonly IdToLocationDataTransformer $idToLocationDataTransformer,
private readonly ThirdPartiesToIdDataTransformer $partiesToIdDataTransformer,
private readonly IdToCalendarRangeDataTransformer $calendarRangeDataTransformer
) {
} }
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)

View File

@ -24,14 +24,17 @@ use Chill\CalendarBundle\Form\CalendarType;
use Chill\CalendarBundle\Form\DataTransformer\IdToCalendarRangeDataTransformer; use Chill\CalendarBundle\Form\DataTransformer\IdToCalendarRangeDataTransformer;
use Chill\MainBundle\Entity\Location; use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\DataMapper\PrivateCommentDataMapper;
use Chill\MainBundle\Form\DataTransformer\IdToLocationDataTransformer; use Chill\MainBundle\Form\DataTransformer\IdToLocationDataTransformer;
use Chill\MainBundle\Form\DataTransformer\IdToUserDataTransformer; use Chill\MainBundle\Form\DataTransformer\IdToUserDataTransformer;
use Chill\MainBundle\Form\DataTransformer\IdToUsersDataTransformer; use Chill\MainBundle\Form\DataTransformer\IdToUsersDataTransformer;
use Chill\MainBundle\Form\Type\CommentType; use Chill\MainBundle\Form\Type\CommentType;
use Chill\MainBundle\Form\Type\PrivateCommentType;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Form\DataTransformer\PersonsToIdDataTransformer; use Chill\PersonBundle\Form\DataTransformer\PersonsToIdDataTransformer;
use Chill\ThirdPartyBundle\Entity\ThirdParty; use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\ThirdPartyBundle\Form\DataTransformer\ThirdPartiesToIdDataTransformer; use Chill\ThirdPartyBundle\Form\DataTransformer\ThirdPartiesToIdDataTransformer;
use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository;
use DateTimeImmutable; use DateTimeImmutable;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Prophecy\Argument; use Prophecy\Argument;
@ -41,6 +44,7 @@ use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase; use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Security;
/** /**
* @internal * @internal
@ -70,7 +74,7 @@ final class CalendarTypeTest extends TypeTestCase
$this->idToUserDataTransformer = $this->buildSingleToIdDataTransformer(IdToUserDataTransformer::class, User::class); $this->idToUserDataTransformer = $this->buildSingleToIdDataTransformer(IdToUserDataTransformer::class, User::class);
$this->idToUsersDataTransformer = $this->buildMultiToIdDataTransformer(IdToUsersDataTransformer::class, User::class); $this->idToUsersDataTransformer = $this->buildMultiToIdDataTransformer(IdToUsersDataTransformer::class, User::class);
$this->idToLocationDataTransformer = $this->buildSingleToIdDataTransformer(IdToLocationDataTransformer::class, Location::class); $this->idToLocationDataTransformer = $this->buildSingleToIdDataTransformer(IdToLocationDataTransformer::class, Location::class);
$this->partiesToIdDataTransformer = $this->buildMultiToIdDataTransformer(ThirdPartiesToIdDataTransformer::class, ThirdParty::class); $this->partiesToIdDataTransformer = $this->buildMultiToIdDataTransformerThirdParties();
$this->calendarRangeDataTransformer = $this->buildSingleToIdDataTransformer(IdToCalendarRangeDataTransformer::class, CalendarRange::class); $this->calendarRangeDataTransformer = $this->buildSingleToIdDataTransformer(IdToCalendarRangeDataTransformer::class, CalendarRange::class);
$tokenStorage = $this->prophesize(TokenStorageInterface::class); $tokenStorage = $this->prophesize(TokenStorageInterface::class);
$token = $this->prophesize(TokenInterface::class); $token = $this->prophesize(TokenInterface::class);
@ -86,9 +90,9 @@ final class CalendarTypeTest extends TypeTestCase
$formData = [ $formData = [
'mainUser' => '1', 'mainUser' => '1',
'users' => '2,3', 'users' => '2,3',
'professionnals' => '4,5', 'professionnals' => '646',
'startDate' => '2022-05-05 14:00:00', 'startDate' => '2022-05-05T14:00:00+0200',
'endDate' => '2022-05-05 14:30:00', 'endDate' => '2022-05-05T14:30:00+0200',
'persons' => '7', 'persons' => '7',
'calendarRange' => '8', 'calendarRange' => '8',
'location' => '9', 'location' => '9',
@ -120,8 +124,6 @@ final class CalendarTypeTest extends TypeTestCase
protected function getExtensions() protected function getExtensions()
{ {
$parents = parent::getExtensions();
$calendarType = new CalendarType( $calendarType = new CalendarType(
$this->personsToIdDataTransformer, $this->personsToIdDataTransformer,
$this->idToUserDataTransformer, $this->idToUserDataTransformer,
@ -132,12 +134,27 @@ final class CalendarTypeTest extends TypeTestCase
); );
$commentType = new CommentType($this->tokenStorage); $commentType = new CommentType($this->tokenStorage);
$user = $this->prophesize(User::class);
$user->getId()->willReturn(1);
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn($user->reveal());
$privateCommentDataMapper = new PrivateCommentDataMapper($security->reveal());
$privateCommentType = new PrivateCommentType($privateCommentDataMapper);
return array_merge( return array_merge(
parent::getExtensions(), parent::getExtensions(),
[new PreloadedExtension([$calendarType, $commentType], [])] [new PreloadedExtension([$calendarType, $commentType, $privateCommentType], [])]
); );
} }
private function buildMultiToIdDataTransformerThirdParties(): ThirdPartiesToIdDataTransformer
{
$repository = $this->prophesize(ThirdPartyRepository::class);
$repository->findOneBy(Argument::type('array'))->willReturn(new ThirdParty());
return new ThirdPartiesToIdDataTransformer($repository->reveal());
}
private function buildMultiToIdDataTransformer( private function buildMultiToIdDataTransformer(
string $classTransformer, string $classTransformer,
string $objClass string $objClass

View File

@ -34,8 +34,11 @@ class IdToEntityDataTransformer implements DataTransformerInterface
/** /**
* @param Closure $getId * @param Closure $getId
*/ */
public function __construct(private readonly ObjectRepository $repository, private readonly bool $multiple = false, ?callable $getId = null) public function __construct(
{ private readonly ObjectRepository $repository,
private readonly bool $multiple = false,
?callable $getId = null
) {
$this->getId = $getId ?? static fn (object $o) => $o->getId(); $this->getId = $getId ?? static fn (object $o) => $o->getId();
} }

View File

@ -24,11 +24,8 @@ use Symfony\Component\Security\Core\User\UserInterface;
class PrivateCommentType extends AbstractType class PrivateCommentType extends AbstractType
{ {
protected UserInterface $user; public function __construct(protected PrivateCommentDataMapper $dataMapper)
public function __construct(TokenStorageInterface $tokenStorage, protected PrivateCommentDataMapper $dataMapper)
{ {
$this->user = $tokenStorage->getToken()->getUser();
} }
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)