| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace App\Tests\Service\Constraint;
- use App\Entity\Access\Access;
- use App\Entity\Organization\Organization;
- use App\Entity\Organization\Parameters;
- use App\Service\Constraint\DateTimeConstraint;
- use App\Service\Organization\Utils as OrganizationUtils;
- use App\Tests\TestToolsTrait;
- use Doctrine\ORM\EntityManagerInterface;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- class DateTimeConstraintTest extends TestCase
- {
- use TestToolsTrait;
- private MockObject | OrganizationUtils $organizationUtils;
- private MockObject | EntityManagerInterface $em;
- private array $periods;
- public function setUp(): void
- {
- $this->em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
- $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
- $this->periods = [
- 'dateStart' => '2021-12-20',
- 'dateEnd' => '2022-08-31'
- ];
- }
- /**
- * @see DateTimeConstraint::presentConstraint()
- */
- public function testPresentConstraint(): void
- {
- $expected = [
- 'start' => [
- '2022-08-31' => 4
- ],
- 'end' => [
- '2021-12-20' => 8,
- 'NULL' => 0
- ]
- ];
- $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
- ->setConstructorArgs([$this->em, $this->organizationUtils])
- ->setMethodsExcept(['presentConstraint'])
- ->getMock();
- $result = $this->invokeMethod($dateTimeConstraint, 'presentConstraint', [$this->periods]);
- $this->assertEquals($expected, $result);
- }
- /**
- * @see DateTimeConstraint::pastConstraint()
- */
- public function testPastConstraint(): void
- {
- $expected = [
- 'end' => [
- '2021-12-20' => 1
- ]
- ];
- $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
- ->setConstructorArgs([$this->em, $this->organizationUtils])
- ->setMethodsExcept(['pastConstraint'])
- ->getMock();
- $result = $this->invokeMethod($dateTimeConstraint, 'pastConstraint', [$this->periods]);
- $this->assertEquals($expected, $result);
- }
- /**
- * @see DateTimeConstraint::futurConstraint()
- */
- public function testFutureConstraint(): void
- {
- $expected = [
- 'start' => [
- '2022-08-31' => 5
- ]
- ];
- $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
- ->setConstructorArgs([$this->em, $this->organizationUtils])
- ->setMethodsExcept(['futureConstraint'])
- ->getMock();
- $result = $this->invokeMethod($dateTimeConstraint, 'futureConstraint', [$this->periods]);
- $this->assertEquals($expected, $result);
- }
- /**
- * Si l'année courante est l'année d'affichage choisie par l'utilisateur, alors la date de début est aujourd'hui
- *
- * @throws \ReflectionException
- * @see DateTimeConstraint::getPeriods()
- */
- public function testGetPeriodsToday(): void
- {
- $today = new \DateTime('now');
- $activityYear = (int)$today->format('Y');
- if ((int)$today->format('m') < 9) {
- $activityYear--;
- }
- $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
- $parameters->method('getMusicalDate')->willReturn(new \DateTime('2000-09-01'));
- $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
- $organization->method('getParameters')->willReturn($parameters);
- $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
- $access->method('getOrganization')->willReturn($organization);
- $access->method('getActivityYear')->willReturn(2020);
- $this->organizationUtils->method('getOrganizationCurrentActivityYear')->with($organization)->willReturn(2020);
- $this->organizationUtils
- ->method('getActivityPeriodsSwitchYear')
- ->with($organization, 2020)
- ->willReturn(['dateStart' => 'YEAR-09-01', 'dateEnd' => ($activityYear + 1) . '-08-31']); // dateStart will be overwritten
- $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
- ->setConstructorArgs([$this->em, $this->organizationUtils])
- ->setMethodsExcept(['getPeriods'])
- ->getMock();
- $periodExpected = ['dateStart' => $today->format('Y-m-d'), 'dateEnd' => ($activityYear + 1) . '-08-31'];
- $result = $this->invokeMethod($dateTimeConstraint, 'getPeriods', [$access]);
- $this->assertEquals($periodExpected, $result);
- }
- /**
- * @throws \ReflectionException
- * @see DateTimeConstraint::getPeriods()
- */
- public function testGetPeriodsNotToday(): void
- {
- $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
- $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
- $access->method('getOrganization')->willReturn($organization);
- $access->method('getActivityYear')->willReturn(2020);
- $this->organizationUtils->method('getOrganizationCurrentActivityYear')->with($organization)->willReturn(2022);
- $this->organizationUtils
- ->method('getActivityPeriodsSwitchYear')
- ->with($organization, 2020)
- ->willReturn(['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31']);
- $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
- ->setConstructorArgs([$this->em, $this->organizationUtils])
- ->setMethodsExcept(['getPeriods'])
- ->getMock();
- $periodExpected = ['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31'];
- $this->assertEquals($periodExpected, $this->invokeMethod($dateTimeConstraint, 'getPeriods', [$access]));
- }
- }
|