|
|
@@ -4,6 +4,7 @@ namespace App\Tests\Service\Constraint;
|
|
|
use App\Entity\Access\Access;
|
|
|
use App\Entity\Organization\Organization;
|
|
|
use App\Entity\Organization\Parameters;
|
|
|
+use App\Repository\Access\AccessRepository;
|
|
|
use App\Service\Constraint\DateTimeConstraint;
|
|
|
use App\Service\Organization\Utils as OrganizationUtils;
|
|
|
use App\Tests\TestToolsTrait;
|
|
|
@@ -11,19 +12,31 @@ use Doctrine\ORM\EntityManagerInterface;
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
+class TestableDateTimeConstraint extends DateTimeConstraint {
|
|
|
+ public function hasCustomPeriods($historical): bool { return parent::hasCustomPeriods($historical); }
|
|
|
+ public function addConstraint(array $contraints, array $newContraint): array { return parent::addConstraint($contraints, $newContraint); }
|
|
|
+ public function cleanConstraints(array $constraints): array { return parent::cleanConstraints($constraints); }
|
|
|
+
|
|
|
+ public function getCustomPeriods(string $dateStart, string $dateEnd): array { return parent::getCustomPeriods($dateStart, $dateEnd); }
|
|
|
+ public function getPeriods(Access $access): array { return parent::getPeriods($access); }
|
|
|
+ public function presentConstraint(array $periods): array { return parent::presentConstraint($periods); }
|
|
|
+ public function pastConstraint($periods): array { return parent::pastConstraint($periods); }
|
|
|
+ public function futureConstraint($periods): array { return parent::futureConstraint($periods); }
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
class DateTimeConstraintTest extends TestCase
|
|
|
{
|
|
|
use TestToolsTrait;
|
|
|
|
|
|
private MockObject | OrganizationUtils $organizationUtils;
|
|
|
- private MockObject | EntityManagerInterface $em;
|
|
|
+ private MockObject | EntityManagerInterface $entityManager;
|
|
|
|
|
|
private array $periods;
|
|
|
|
|
|
public function setUp(): void
|
|
|
{
|
|
|
- $this->em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
$this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
$this->periods = [
|
|
|
@@ -32,6 +45,224 @@ class DateTimeConstraintTest extends TestCase
|
|
|
];
|
|
|
}
|
|
|
|
|
|
+ public function testInvokePresentNoCustomPeriods(): void {
|
|
|
+ $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
|
|
|
+ ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
|
|
|
+ ->setMethodsExcept(['invoke'])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $access = $this->getMockBuilder(Access::class)->getMock();
|
|
|
+
|
|
|
+ $accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $accessRepository->method('find')->with(123)->willReturn($access);
|
|
|
+
|
|
|
+ $this->entityManager->method('getRepository')->with(Access::class)->willReturn($accessRepository);
|
|
|
+
|
|
|
+ $historical = ["future" => false,"past" => false,"present" => true,"dateStart" => null,"dateEnd" => null];
|
|
|
+ $access->method('getHistorical')->willReturn($historical);
|
|
|
+
|
|
|
+ $dateTimeConstraint->method('hasCustomPeriods')->with($historical)->willReturn(false);
|
|
|
+
|
|
|
+ $periods = ['dateStart' => '2020-01-01', 'dateEnd' => '2020-12-31'];
|
|
|
+ $dateTimeConstraint->method('getPeriods')->with($access)->willReturn($periods);
|
|
|
+
|
|
|
+ $constraint = ['foo'];
|
|
|
+ $dateTimeConstraint->method('presentConstraint')->with($periods)->willReturn($constraint);
|
|
|
+
|
|
|
+ $dateTimeConstraint
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('addConstraint')
|
|
|
+ ->with(['start' => [], 'end' => []] , $constraint)
|
|
|
+ ->willReturn(['bar']);
|
|
|
+
|
|
|
+ $dateTimeConstraint
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('cleanConstraints')
|
|
|
+ ->with(['bar']);
|
|
|
+
|
|
|
+ $dateTimeConstraint->invoke(123);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testInvokePastNoCustomPeriods(): void
|
|
|
+ {
|
|
|
+ $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
|
|
|
+ ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
|
|
|
+ ->setMethodsExcept(['invoke'])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $access = $this->getMockBuilder(Access::class)->getMock();
|
|
|
+
|
|
|
+ $accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $accessRepository->method('find')->with(123)->willReturn($access);
|
|
|
+
|
|
|
+ $this->entityManager->method('getRepository')->with(Access::class)->willReturn($accessRepository);
|
|
|
+
|
|
|
+ $historical = ["future" => false,"past" => true,"present" => false,"dateStart" => null,"dateEnd" => null];
|
|
|
+
|
|
|
+ $access->method('getHistorical')->willReturn($historical);
|
|
|
+
|
|
|
+ $dateTimeConstraint->method('hasCustomPeriods')->with($historical)->willReturn(false);
|
|
|
+
|
|
|
+ $periods = ['dateStart' => '2020-01-01', 'dateEnd' => '2020-12-31'];
|
|
|
+ $dateTimeConstraint->method('getPeriods')->with($access)->willReturn($periods);
|
|
|
+
|
|
|
+ $constraint = ['foo'];
|
|
|
+ $dateTimeConstraint->method('pastConstraint')->with($periods)->willReturn($constraint);
|
|
|
+
|
|
|
+ $dateTimeConstraint
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('addConstraint')
|
|
|
+ ->with(['start' => [], 'end' => []] , $constraint)
|
|
|
+ ->willReturn(['bar']);
|
|
|
+
|
|
|
+ $dateTimeConstraint
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('cleanConstraints')
|
|
|
+ ->with(['bar']);
|
|
|
+
|
|
|
+ $dateTimeConstraint->invoke(123);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testInvokeFutureNoCustomPeriods(): void
|
|
|
+ {
|
|
|
+ $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
|
|
|
+ ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
|
|
|
+ ->setMethodsExcept(['invoke'])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $access = $this->getMockBuilder(Access::class)->getMock();
|
|
|
+
|
|
|
+ $accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $accessRepository->method('find')->with(123)->willReturn($access);
|
|
|
+
|
|
|
+ $this->entityManager->method('getRepository')->with(Access::class)->willReturn($accessRepository);
|
|
|
+
|
|
|
+ $historical = ["future" => true,"past" => false,"present" => false,"dateStart" => null,"dateEnd" => null];
|
|
|
+
|
|
|
+ $access->method('getHistorical')->willReturn($historical);
|
|
|
+
|
|
|
+ $dateTimeConstraint->method('hasCustomPeriods')->with($historical)->willReturn(false);
|
|
|
+
|
|
|
+ $periods = ['dateStart' => '2020-01-01', 'dateEnd' => '2020-12-31'];
|
|
|
+ $dateTimeConstraint->method('getPeriods')->with($access)->willReturn($periods);
|
|
|
+
|
|
|
+ $constraint = ['foo'];
|
|
|
+ $dateTimeConstraint->method('futureConstraint')->with($periods)->willReturn($constraint);
|
|
|
+
|
|
|
+ $dateTimeConstraint
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('addConstraint')
|
|
|
+ ->with(['start' => [], 'end' => []] , $constraint)
|
|
|
+ ->willReturn(['bar']);
|
|
|
+
|
|
|
+ $dateTimeConstraint
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('cleanConstraints')
|
|
|
+ ->with(['bar']);
|
|
|
+
|
|
|
+ $dateTimeConstraint->invoke(123);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testInvokeMultiplePeriodsNoCustom(): void
|
|
|
+ {
|
|
|
+ $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
|
|
|
+ ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
|
|
|
+ ->setMethodsExcept(['invoke'])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $access = $this->getMockBuilder(Access::class)->getMock();
|
|
|
+
|
|
|
+ $accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $accessRepository->method('find')->with(123)->willReturn($access);
|
|
|
+
|
|
|
+ $this->entityManager->method('getRepository')->with(Access::class)->willReturn($accessRepository);
|
|
|
+
|
|
|
+ $historical = ["future" => true,"past" => true,"present" => true,"dateStart" => null,"dateEnd" => null];
|
|
|
+
|
|
|
+ $access->method('getHistorical')->willReturn($historical);
|
|
|
+ $dateTimeConstraint->method('getPeriods')->with($access)->willReturn(['dateStart' => '2020-01-01', 'dateEnd' => '2020-12-31']);
|
|
|
+
|
|
|
+ $dateTimeConstraint->method('hasCustomPeriods')->with($historical)->willReturn(false);
|
|
|
+
|
|
|
+ $dateTimeConstraint->method('pastConstraint')->willReturn(['pastConstraint']);
|
|
|
+ $dateTimeConstraint->method('presentConstraint')->willReturn(['presentConstraint']);
|
|
|
+ $dateTimeConstraint->method('futureConstraint')->willReturn(['futureConstraint']);
|
|
|
+
|
|
|
+ $dateTimeConstraint
|
|
|
+ ->expects(self::exactly(3))
|
|
|
+ ->method('addConstraint')
|
|
|
+ ->withConsecutive(
|
|
|
+ [['start' => [], 'end' => []] , ['presentConstraint']],
|
|
|
+ [['start' => [], 'end' => []] , ['pastConstraint']],
|
|
|
+ [['start' => [], 'end' => []] , ['futureConstraint']]
|
|
|
+ )->willReturn(['start' => [], 'end' => []]);
|
|
|
+
|
|
|
+ $dateTimeConstraint
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('cleanConstraints')
|
|
|
+ ->with(['start' => [], 'end' => []]);
|
|
|
+
|
|
|
+ $dateTimeConstraint->invoke(123);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testInvokeWithCustom(): void {
|
|
|
+ $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
|
|
|
+ ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
|
|
|
+ ->setMethodsExcept(['invoke'])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $access = $this->getMockBuilder(Access::class)->getMock();
|
|
|
+
|
|
|
+ $accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $accessRepository->method('find')->with(123)->willReturn($access);
|
|
|
+
|
|
|
+ $this->entityManager->method('getRepository')->with(Access::class)->willReturn($accessRepository);
|
|
|
+
|
|
|
+ $historical = ["future" => false,"past" => false,"present" => true,"dateStart" => '2020-01-01',"dateEnd" => '2020-02-01'];
|
|
|
+
|
|
|
+ $access->method('getHistorical')->willReturn($historical);
|
|
|
+
|
|
|
+ $dateTimeConstraint->method('hasCustomPeriods')->with($historical)->willReturn(true);
|
|
|
+
|
|
|
+ $dateTimeConstraint->expects(self::never())->method('pastConstraint');
|
|
|
+ $dateTimeConstraint->expects(self::never())->method('futureConstraint');
|
|
|
+
|
|
|
+ $dateTimeConstraint
|
|
|
+ ->method('getCustomPeriods')
|
|
|
+ ->with('2020-01-01', '2020-02-01')
|
|
|
+ ->willReturn(['dateStart' => 2020, 'dateEnd' => 2020]);
|
|
|
+
|
|
|
+ $dateTimeConstraint->expects(self::once())
|
|
|
+ ->method('presentConstraint')
|
|
|
+ ->with(['dateStart' => 2020, 'dateEnd' => 2020])
|
|
|
+ ->willReturn(['bar']);
|
|
|
+
|
|
|
+ $dateTimeConstraint
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('addConstraint')
|
|
|
+ ->with(['start' => [], 'end' => []], ['bar'])
|
|
|
+ ->willReturn(['start' => [], 'end' => []]);
|
|
|
+
|
|
|
+ $dateTimeConstraint
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('cleanConstraints')
|
|
|
+ ->with(['start' => [], 'end' => []]);
|
|
|
+
|
|
|
+ $dateTimeConstraint->invoke(123);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testGetCustomPeriods(): void {
|
|
|
+ $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
|
|
|
+ ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
|
|
|
+ ->setMethodsExcept(['getCustomPeriods'])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $this->assertEquals(
|
|
|
+ ['dateStart' => '2020-01-01', 'dateEnd' => '2020-12-31'],
|
|
|
+ $dateTimeConstraint->getCustomPeriods('2020-01-01', '2020-12-31')
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @see DateTimeConstraint::presentConstraint()
|
|
|
*/
|
|
|
@@ -47,8 +278,8 @@ class DateTimeConstraintTest extends TestCase
|
|
|
]
|
|
|
];
|
|
|
|
|
|
- $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
|
|
|
- ->setConstructorArgs([$this->em, $this->organizationUtils])
|
|
|
+ $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
|
|
|
+ ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
|
|
|
->setMethodsExcept(['presentConstraint'])
|
|
|
->getMock();
|
|
|
|
|
|
@@ -68,8 +299,8 @@ class DateTimeConstraintTest extends TestCase
|
|
|
]
|
|
|
];
|
|
|
|
|
|
- $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
|
|
|
- ->setConstructorArgs([$this->em, $this->organizationUtils])
|
|
|
+ $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
|
|
|
+ ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
|
|
|
->setMethodsExcept(['pastConstraint'])
|
|
|
->getMock();
|
|
|
|
|
|
@@ -89,8 +320,8 @@ class DateTimeConstraintTest extends TestCase
|
|
|
]
|
|
|
];
|
|
|
|
|
|
- $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
|
|
|
- ->setConstructorArgs([$this->em, $this->organizationUtils])
|
|
|
+ $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
|
|
|
+ ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
|
|
|
->setMethodsExcept(['futureConstraint'])
|
|
|
->getMock();
|
|
|
|
|
|
@@ -130,8 +361,8 @@ class DateTimeConstraintTest extends TestCase
|
|
|
->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])
|
|
|
+ $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
|
|
|
+ ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
|
|
|
->setMethodsExcept(['getPeriods'])
|
|
|
->getMock();
|
|
|
|
|
|
@@ -160,8 +391,8 @@ class DateTimeConstraintTest extends TestCase
|
|
|
->with($organization, 2020)
|
|
|
->willReturn(['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31']);
|
|
|
|
|
|
- $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
|
|
|
- ->setConstructorArgs([$this->em, $this->organizationUtils])
|
|
|
+ $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
|
|
|
+ ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
|
|
|
->setMethodsExcept(['getPeriods'])
|
|
|
->getMock();
|
|
|
|