DateTimeConstraintTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace App\Tests\Service\Utils;
  3. use App\Entity\Access\Access;
  4. use App\Entity\Organization\Organization;
  5. use App\Entity\Organization\Parameters;
  6. use App\Service\Utils\DateTimeConstraint;
  7. use App\Tests\TestToolsTrait;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use PHPUnit\Framework\TestCase;
  10. class DateTimeConstraintTest extends TestCase
  11. {
  12. use TestToolsTrait;
  13. private DateTimeConstraint $dateTimeConstraint;
  14. private array $periods;
  15. public function setUp(): void
  16. {
  17. $em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  18. $this->dateTimeConstraint = new DateTimeConstraint($em);
  19. $this->periods = [
  20. 'dateStart' => '2021-12-20',
  21. 'dateEnd' => '2022-08-31'
  22. ];
  23. }
  24. /**
  25. * @see DateTimeConstraint::presentConstraint()
  26. */
  27. public function testPresentConstrain(){
  28. $constraintExpected = [
  29. 'start' => [
  30. '2022-08-31' => 4
  31. ],
  32. 'end' => [
  33. '2021-12-20' => 8,
  34. 'NULL' => 0
  35. ]
  36. ];
  37. $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'presentConstraint', [$this->periods]));
  38. }
  39. /**
  40. * @see DateTimeConstraint::pastConstraint()
  41. */
  42. public function testPastConstrain(){
  43. $constraintExpected = [
  44. 'end' => [
  45. '2021-12-20' => 1
  46. ]
  47. ];
  48. $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'pastConstraint', [$this->periods]));
  49. }
  50. /**
  51. * @see DateTimeConstraint::futurConstraint()
  52. */
  53. public function testFuturConstrain(){
  54. $constraintExpected = [
  55. 'start' => [
  56. '2022-08-31' => 5
  57. ]
  58. ];
  59. $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'futurConstraint', [$this->periods]));
  60. }
  61. /**
  62. * @see DateTimeConstraint::clearNull()
  63. */
  64. public function testClearNull(){
  65. $originalEndConstraint= [
  66. 'end' => [
  67. 'NULL' => 0
  68. ]
  69. ];
  70. $endConstraintExpected = [];
  71. $this->assertEquals($endConstraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'clearNull', [$originalEndConstraint, 'end']));
  72. }
  73. /**
  74. * @see DateTimeConstraint::filterConstraint()
  75. */
  76. public function testFilterConstraint(){
  77. $originalStartConstraint= [
  78. 'start' => [
  79. '2021-12-20' => [8, 1],
  80. '2022-01-01' => [5]
  81. ]
  82. ];
  83. $startConstraintExpected = [
  84. '2022-01-01' => [5]
  85. ];
  86. $this->assertEquals($startConstraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'filterConstraint', [$originalStartConstraint, 'start']));
  87. }
  88. /**
  89. * @see DateTimeConstraint::cleanConstraints()
  90. */
  91. public function testCleanConstraints(){
  92. $originalConstraint= [
  93. 'start' => [
  94. '2022-08-31' => [5]
  95. ],
  96. 'end' => [
  97. '2021-12-20' => [8, 1],
  98. 'NULL' => [0]
  99. ]
  100. ];
  101. $constraintExpected= [
  102. 'start' => [
  103. '2022-08-31' => [5]
  104. ],
  105. 'end' => []
  106. ];
  107. $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'cleanConstraints', [$originalConstraint]));
  108. }
  109. /**
  110. * @see DateTimeConstraint::addConstraint()
  111. */
  112. public function testAddConstraint(){
  113. $originalConstraint = [
  114. 'start' => [],
  115. 'end' => []
  116. ];
  117. $presentConstraint = [
  118. 'start' => [
  119. '2022-08-31' => 4
  120. ],
  121. 'end' => [
  122. '2021-12-20' => 8,
  123. 'NULL' => 0
  124. ]
  125. ];
  126. $pastConstraint = [
  127. 'end' => [
  128. '2021-12-20' => 1
  129. ]
  130. ];
  131. $constraintAfterStartExpected = [
  132. 'start' => [
  133. '2022-08-31' => [4]
  134. ],
  135. 'end' => [
  136. '2021-12-20' => [8],
  137. 'NULL' => [0]
  138. ]
  139. ];
  140. $constraintAfterEndExpected = [
  141. 'start' => [
  142. '2022-08-31' => [4]
  143. ],
  144. 'end' => [
  145. '2021-12-20' => [8, 1],
  146. 'NULL' => [0]
  147. ]
  148. ];
  149. $this->assertEquals($constraintAfterStartExpected, $this->invokeMethod($this->dateTimeConstraint, 'addConstraint', [$originalConstraint, $presentConstraint]));
  150. $this->assertEquals($constraintAfterEndExpected, $this->invokeMethod($this->dateTimeConstraint, 'addConstraint', [$constraintAfterStartExpected, $pastConstraint]));
  151. }
  152. /**
  153. * @throws \ReflectionException
  154. * @see DateTimeConstraint::getPeriods()
  155. */
  156. public function testGetPeriodsToday(){
  157. $today = new \DateTime('now');
  158. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  159. $parameters = new Parameters();
  160. $parameters->setMusicalDate(new \DateTime('2000-09-01'));
  161. $organization->method('getParameters')->willReturn($parameters);
  162. $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
  163. $access->method('getOrganization')->willReturn($organization);
  164. $activityYear = $today->format('Y');
  165. if($today->format('m') < 9) $activityYear--;
  166. $access->method('getActivityYear')->willReturn(intval($activityYear));
  167. $periodExpected = ['dateStart' => $today->format('Y-m-d'), 'dateEnd' => '2022-08-31'];
  168. $this->assertEquals($periodExpected, $this->invokeMethod($this->dateTimeConstraint, 'getPeriods', [$access]));
  169. }
  170. /**
  171. * @throws \ReflectionException
  172. * @see DateTimeConstraint::getPeriods()
  173. */
  174. public function testGetPeriodsNotToday(){
  175. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  176. $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
  177. $access->method('getOrganization')->willReturn($organization);
  178. $access->method('getActivityYear')->willReturn(2020);
  179. $periodExpected = ['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31'];
  180. $this->assertEquals($periodExpected, $this->invokeMethod($this->dateTimeConstraint, 'getPeriods', [$access]));
  181. }
  182. /**
  183. * @throws \ReflectionException
  184. * @see DateTimeConstraint::hasCustomPeriods()
  185. */
  186. public function testHasCustomPeriods(){
  187. $historical = ['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31'];
  188. $this->assertTrue($this->invokeMethod($this->dateTimeConstraint, 'hasCustomPeriods', [$historical]));
  189. }
  190. /**
  191. * @throws \ReflectionException
  192. * @see DateTimeConstraint::hasCustomPeriods()
  193. */
  194. public function testHasNotCustomPeriods(){
  195. $historical = ['dateStart' => null, 'dateEnd' => null];
  196. $this->assertFalse($this->invokeMethod($this->dateTimeConstraint, 'hasCustomPeriods', [$historical]));
  197. }
  198. }