DateTimeConstraintTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Tests\Service\Constraint;
  3. use App\Entity\Access\Access;
  4. use App\Entity\Organization\Organization;
  5. use App\Entity\Organization\Parameters;
  6. use App\Service\Constraint\DateTimeConstraint;
  7. use App\Service\Organization\Utils as OrganizationUtils;
  8. use App\Tests\TestToolsTrait;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use PHPUnit\Framework\MockObject\MockObject;
  11. use PHPUnit\Framework\TestCase;
  12. class DateTimeConstraintTest extends TestCase
  13. {
  14. use TestToolsTrait;
  15. private MockObject | OrganizationUtils $organizationUtils;
  16. private MockObject | EntityManagerInterface $em;
  17. private array $periods;
  18. public function setUp(): void
  19. {
  20. $this->em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  21. $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
  22. $this->periods = [
  23. 'dateStart' => '2021-12-20',
  24. 'dateEnd' => '2022-08-31'
  25. ];
  26. }
  27. /**
  28. * @see DateTimeConstraint::presentConstraint()
  29. */
  30. public function testPresentConstraint(): void
  31. {
  32. $expected = [
  33. 'start' => [
  34. '2022-08-31' => 4
  35. ],
  36. 'end' => [
  37. '2021-12-20' => 8,
  38. 'NULL' => 0
  39. ]
  40. ];
  41. $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
  42. ->setConstructorArgs([$this->em, $this->organizationUtils])
  43. ->setMethodsExcept(['presentConstraint'])
  44. ->getMock();
  45. $result = $this->invokeMethod($dateTimeConstraint, 'presentConstraint', [$this->periods]);
  46. $this->assertEquals($expected, $result);
  47. }
  48. /**
  49. * @see DateTimeConstraint::pastConstraint()
  50. */
  51. public function testPastConstraint(): void
  52. {
  53. $expected = [
  54. 'end' => [
  55. '2021-12-20' => 1
  56. ]
  57. ];
  58. $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
  59. ->setConstructorArgs([$this->em, $this->organizationUtils])
  60. ->setMethodsExcept(['pastConstraint'])
  61. ->getMock();
  62. $result = $this->invokeMethod($dateTimeConstraint, 'pastConstraint', [$this->periods]);
  63. $this->assertEquals($expected, $result);
  64. }
  65. /**
  66. * @see DateTimeConstraint::futurConstraint()
  67. */
  68. public function testFutureConstraint(): void
  69. {
  70. $expected = [
  71. 'start' => [
  72. '2022-08-31' => 5
  73. ]
  74. ];
  75. $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
  76. ->setConstructorArgs([$this->em, $this->organizationUtils])
  77. ->setMethodsExcept(['futureConstraint'])
  78. ->getMock();
  79. $result = $this->invokeMethod($dateTimeConstraint, 'futureConstraint', [$this->periods]);
  80. $this->assertEquals($expected, $result);
  81. }
  82. /**
  83. * Si l'année courante est l'année d'affichage choisie par l'utilisateur, alors la date de début est aujourd'hui
  84. *
  85. * @throws \ReflectionException
  86. * @see DateTimeConstraint::getPeriods()
  87. */
  88. public function testGetPeriodsToday(): void
  89. {
  90. $today = new \DateTime('now');
  91. $activityYear = (int)$today->format('Y');
  92. if ((int)$today->format('m') < 9) {
  93. $activityYear--;
  94. }
  95. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  96. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2000-09-01'));
  97. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  98. $organization->method('getParameters')->willReturn($parameters);
  99. $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
  100. $access->method('getOrganization')->willReturn($organization);
  101. $access->method('getActivityYear')->willReturn(2020);
  102. $this->organizationUtils->method('getOrganizationCurrentActivityYear')->with($organization)->willReturn(2020);
  103. $this->organizationUtils
  104. ->method('getActivityPeriodsSwitchYear')
  105. ->with($organization, 2020)
  106. ->willReturn(['dateStart' => 'YEAR-09-01', 'dateEnd' => ($activityYear + 1) . '-08-31']); // dateStart will be overwritten
  107. $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
  108. ->setConstructorArgs([$this->em, $this->organizationUtils])
  109. ->setMethodsExcept(['getPeriods'])
  110. ->getMock();
  111. $periodExpected = ['dateStart' => $today->format('Y-m-d'), 'dateEnd' => ($activityYear + 1) . '-08-31'];
  112. $result = $this->invokeMethod($dateTimeConstraint, 'getPeriods', [$access]);
  113. $this->assertEquals($periodExpected, $result);
  114. }
  115. /**
  116. * @throws \ReflectionException
  117. * @see DateTimeConstraint::getPeriods()
  118. */
  119. public function testGetPeriodsNotToday(): void
  120. {
  121. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  122. $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
  123. $access->method('getOrganization')->willReturn($organization);
  124. $access->method('getActivityYear')->willReturn(2020);
  125. $this->organizationUtils->method('getOrganizationCurrentActivityYear')->with($organization)->willReturn(2022);
  126. $this->organizationUtils
  127. ->method('getActivityPeriodsSwitchYear')
  128. ->with($organization, 2020)
  129. ->willReturn(['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31']);
  130. $dateTimeConstraint = $this->getMockBuilder(DateTimeConstraint::class)
  131. ->setConstructorArgs([$this->em, $this->organizationUtils])
  132. ->setMethodsExcept(['getPeriods'])
  133. ->getMock();
  134. $periodExpected = ['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31'];
  135. $this->assertEquals($periodExpected, $this->invokeMethod($dateTimeConstraint, 'getPeriods', [$access]));
  136. }
  137. }