|
|
@@ -5,22 +5,26 @@ 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 DateTimeConstraint $dateTimeConstraint;
|
|
|
+ private MockObject | OrganizationUtils $organizationUtils;
|
|
|
+ private MockObject | EntityManagerInterface $em;
|
|
|
+
|
|
|
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->em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
$this->periods = [
|
|
|
'dateStart' => '2021-12-20',
|
|
|
@@ -31,8 +35,9 @@ class DateTimeConstraintTest extends TestCase
|
|
|
/**
|
|
|
* @see DateTimeConstraint::presentConstraint()
|
|
|
*/
|
|
|
- public function testPresentConstrain(){
|
|
|
- $constraintExpected = [
|
|
|
+ public function testPresentConstraint(): void
|
|
|
+ {
|
|
|
+ $expected = [
|
|
|
'start' => [
|
|
|
'2022-08-31' => 4
|
|
|
],
|
|
|
@@ -41,181 +46,126 @@ class DateTimeConstraintTest extends TestCase
|
|
|
'NULL' => 0
|
|
|
]
|
|
|
];
|
|
|
- $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'presentConstraint', [$this->periods]));
|
|
|
+
|
|
|
+ $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 testPastConstrain(){
|
|
|
- $constraintExpected = [
|
|
|
+ public function testPastConstraint(): void
|
|
|
+ {
|
|
|
+ $expected = [
|
|
|
'end' => [
|
|
|
'2021-12-20' => 1
|
|
|
]
|
|
|
];
|
|
|
- $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'pastConstraint', [$this->periods]));
|
|
|
+
|
|
|
+ $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 testFuturConstrain(){
|
|
|
- $constraintExpected = [
|
|
|
+ public function testFutureConstraint(): void
|
|
|
+ {
|
|
|
+ $expected = [
|
|
|
'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']));
|
|
|
- }
|
|
|
+ $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
|
|
|
+ ->setConstructorArgs([$this->em, $this->organizationUtils])
|
|
|
+ ->setMethodsExcept(['futureConstraint'])
|
|
|
+ ->getMock();
|
|
|
|
|
|
- /**
|
|
|
- * @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]));
|
|
|
- }
|
|
|
+ $result = $this->invokeMethod($dateTimeConstraint, 'futureConstraint', [$this->periods]);
|
|
|
|
|
|
- /**
|
|
|
- * @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]));
|
|
|
+ $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(){
|
|
|
+ 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();
|
|
|
- $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);
|
|
|
+ $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
|
|
|
|
|
|
- $activityYear = $today->format('Y');
|
|
|
- if($today->format('m') < 9) $activityYear--;
|
|
|
+ $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
|
|
|
+ ->setConstructorArgs([$this->em, $this->organizationUtils])
|
|
|
+ ->setMethodsExcept(['getPeriods'])
|
|
|
+ ->getMock();
|
|
|
|
|
|
- $access->method('getActivityYear')->willReturn(intval($activityYear));
|
|
|
+ $periodExpected = ['dateStart' => $today->format('Y-m-d'), 'dateEnd' => ($activityYear + 1) . '-08-31'];
|
|
|
|
|
|
- $periodExpected = ['dateStart' => $today->format('Y-m-d'), 'dateEnd' => '2022-08-31'];
|
|
|
- $this->assertEquals($periodExpected, $this->invokeMethod($this->dateTimeConstraint, 'getPeriods', [$access]));
|
|
|
+ $result = $this->invokeMethod($dateTimeConstraint, 'getPeriods', [$access]);
|
|
|
+
|
|
|
+ $this->assertEquals($periodExpected, $result);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @throws \ReflectionException
|
|
|
* @see DateTimeConstraint::getPeriods()
|
|
|
*/
|
|
|
- public function testGetPeriodsNotToday(){
|
|
|
+ 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);
|
|
|
- $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]));
|
|
|
- }
|
|
|
+ $this->organizationUtils->method('getOrganizationCurrentActivityYear')->with($organization)->willReturn(2022);
|
|
|
+ $this->organizationUtils
|
|
|
+ ->method('getActivityPeriodsSwitchYear')
|
|
|
+ ->with($organization, 2020)
|
|
|
+ ->willReturn(['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31']);
|
|
|
|
|
|
- /**
|
|
|
- * @throws \ReflectionException
|
|
|
- * @see DateTimeConstraint::hasCustomPeriods()
|
|
|
- */
|
|
|
- public function testHasNotCustomPeriods(){
|
|
|
- $historical = ['dateStart' => null, 'dateEnd' => null];
|
|
|
- $this->assertFalse($this->invokeMethod($this->dateTimeConstraint, 'hasCustomPeriods', [$historical]));
|
|
|
+ $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]));
|
|
|
}
|
|
|
}
|