| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?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\Tests\TestToolsTrait;
- use Doctrine\ORM\EntityManagerInterface;
- use PHPUnit\Framework\TestCase;
- class DateTimeConstraintTest extends TestCase
- {
- use TestToolsTrait;
- private DateTimeConstraint $dateTimeConstraint;
- private array $periods;
- public function setUp(): void
- {
- $em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
- $organizationUtils = new \App\Service\Organization\Utils();
- $this->dateTimeConstraint = new DateTimeConstraint($em, $organizationUtils);
- $this->periods = [
- 'dateStart' => '2021-12-20',
- 'dateEnd' => '2022-08-31'
- ];
- }
- /**
- * @see DateTimeConstraint::presentConstraint()
- */
- public function testPresentConstrain(){
- $constraintExpected = [
- 'start' => [
- '2022-08-31' => 4
- ],
- 'end' => [
- '2021-12-20' => 8,
- 'NULL' => 0
- ]
- ];
- $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'presentConstraint', [$this->periods]));
- }
- /**
- * @see DateTimeConstraint::pastConstraint()
- */
- public function testPastConstrain(){
- $constraintExpected = [
- 'end' => [
- '2021-12-20' => 1
- ]
- ];
- $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'pastConstraint', [$this->periods]));
- }
- /**
- * @see DateTimeConstraint::futurConstraint()
- */
- public function testFuturConstrain(){
- $constraintExpected = [
- 'start' => [
- '2022-08-31' => 5
- ]
- ];
- $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'futurConstraint', [$this->periods]));
- }
- /**
- * @see DateTimeConstraint::clearNull()
- */
- public function testClearNull(){
- $originalEndConstraint= [
- 'end' => [
- 'NULL' => 0
- ]
- ];
- $endConstraintExpected = [];
- $this->assertEquals($endConstraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'clearNull', [$originalEndConstraint, 'end']));
- }
- /**
- * @see DateTimeConstraint::filterConstraint()
- */
- public function testFilterConstraint(){
- $originalStartConstraint= [
- 'start' => [
- '2021-12-20' => [8, 1],
- '2022-01-01' => [5]
- ]
- ];
- $startConstraintExpected = [
- '2022-01-01' => [5]
- ];
- $this->assertEquals($startConstraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'filterConstraint', [$originalStartConstraint, 'start']));
- }
- /**
- * @see DateTimeConstraint::cleanConstraints()
- */
- public function testCleanConstraints(){
- $originalConstraint= [
- 'start' => [
- '2022-08-31' => [5]
- ],
- 'end' => [
- '2021-12-20' => [8, 1],
- 'NULL' => [0]
- ]
- ];
- $constraintExpected= [
- 'start' => [
- '2022-08-31' => [5]
- ],
- 'end' => []
- ];
- $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'cleanConstraints', [$originalConstraint]));
- }
- /**
- * @see DateTimeConstraint::addConstraint()
- */
- public function testAddConstraint(){
- $originalConstraint = [
- 'start' => [],
- 'end' => []
- ];
- $presentConstraint = [
- 'start' => [
- '2022-08-31' => 4
- ],
- 'end' => [
- '2021-12-20' => 8,
- 'NULL' => 0
- ]
- ];
- $pastConstraint = [
- 'end' => [
- '2021-12-20' => 1
- ]
- ];
- $constraintAfterStartExpected = [
- 'start' => [
- '2022-08-31' => [4]
- ],
- 'end' => [
- '2021-12-20' => [8],
- 'NULL' => [0]
- ]
- ];
- $constraintAfterEndExpected = [
- 'start' => [
- '2022-08-31' => [4]
- ],
- 'end' => [
- '2021-12-20' => [8, 1],
- 'NULL' => [0]
- ]
- ];
- $this->assertEquals($constraintAfterStartExpected, $this->invokeMethod($this->dateTimeConstraint, 'addConstraint', [$originalConstraint, $presentConstraint]));
- $this->assertEquals($constraintAfterEndExpected, $this->invokeMethod($this->dateTimeConstraint, 'addConstraint', [$constraintAfterStartExpected, $pastConstraint]));
- }
- /**
- * @throws \ReflectionException
- * @see DateTimeConstraint::getPeriods()
- */
- public function testGetPeriodsToday(){
- $today = new \DateTime('now');
- $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
- $parameters = new Parameters();
- $parameters->setMusicalDate(new \DateTime('2000-09-01'));
- $organization->method('getParameters')->willReturn($parameters);
- $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
- $access->method('getOrganization')->willReturn($organization);
- $activityYear = $today->format('Y');
- if($today->format('m') < 9) $activityYear--;
- $access->method('getActivityYear')->willReturn(intval($activityYear));
- $periodExpected = ['dateStart' => $today->format('Y-m-d'), 'dateEnd' => '2022-08-31'];
- $this->assertEquals($periodExpected, $this->invokeMethod($this->dateTimeConstraint, 'getPeriods', [$access]));
- }
- /**
- * @throws \ReflectionException
- * @see DateTimeConstraint::getPeriods()
- */
- public function testGetPeriodsNotToday(){
- $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
- $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
- $access->method('getOrganization')->willReturn($organization);
- $access->method('getActivityYear')->willReturn(2020);
- $periodExpected = ['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31'];
- $this->assertEquals($periodExpected, $this->invokeMethod($this->dateTimeConstraint, 'getPeriods', [$access]));
- }
- /**
- * @throws \ReflectionException
- * @see DateTimeConstraint::hasCustomPeriods()
- */
- public function testHasCustomPeriods(){
- $historical = ['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31'];
- $this->assertTrue($this->invokeMethod($this->dateTimeConstraint, 'hasCustomPeriods', [$historical]));
- }
- /**
- * @throws \ReflectionException
- * @see DateTimeConstraint::hasCustomPeriods()
- */
- public function testHasNotCustomPeriods(){
- $historical = ['dateStart' => null, 'dateEnd' => null];
- $this->assertFalse($this->invokeMethod($this->dateTimeConstraint, 'hasCustomPeriods', [$historical]));
- }
- }
|