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])); } }