|
|
@@ -0,0 +1,212 @@
|
|
|
+<?php
|
|
|
+namespace App\Tests\Service\Utils;
|
|
|
+
|
|
|
+use App\Entity\Access\Access;
|
|
|
+use App\Entity\Organization\Organization;
|
|
|
+use App\Service\Utils\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();
|
|
|
+ $this->dateTimeConstraint = new DateTimeConstraint($em);
|
|
|
+
|
|
|
+ $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();
|
|
|
+
|
|
|
+ $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $access->method('getOrganization')->willReturn($organization);
|
|
|
+ $access->method('getActivityYear')->willReturn(intval($today->format('Y')));
|
|
|
+
|
|
|
+ $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]));
|
|
|
+ }
|
|
|
+}
|