DateTimeConstraintTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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\Repository\Access\AccessRepository;
  7. use App\Service\Constraint\DateTimeConstraint;
  8. use App\Service\Organization\Utils as OrganizationUtils;
  9. use App\Tests\TestToolsTrait;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use PHPUnit\Framework\MockObject\MockObject;
  12. use PHPUnit\Framework\TestCase;
  13. class TestableDateTimeConstraint extends DateTimeConstraint {
  14. public function hasCustomPeriods($historical): bool { return parent::hasCustomPeriods($historical); }
  15. public function addConstraint(array $contraints, array $newContraint): array { return parent::addConstraint($contraints, $newContraint); }
  16. public function cleanConstraints(array $constraints): array { return parent::cleanConstraints($constraints); }
  17. public function getCustomPeriods(string $dateStart, string $dateEnd): array { return parent::getCustomPeriods($dateStart, $dateEnd); }
  18. public function getPeriods(Access $access): array { return parent::getPeriods($access); }
  19. public function presentConstraint(array $periods): array { return parent::presentConstraint($periods); }
  20. public function pastConstraint($periods): array { return parent::pastConstraint($periods); }
  21. public function futureConstraint($periods): array { return parent::futureConstraint($periods); }
  22. }
  23. class DateTimeConstraintTest extends TestCase
  24. {
  25. use TestToolsTrait;
  26. private MockObject | OrganizationUtils $organizationUtils;
  27. private MockObject | EntityManagerInterface $entityManager;
  28. private array $periods;
  29. public function setUp(): void
  30. {
  31. $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  32. $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
  33. $this->periods = [
  34. 'dateStart' => '2021-12-20',
  35. 'dateEnd' => '2022-08-31'
  36. ];
  37. }
  38. /**
  39. * @see DateTimeConstraint::invoke()
  40. */
  41. public function testInvokePresentNoCustomPeriods(): void {
  42. $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
  43. ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
  44. ->setMethodsExcept(['invoke'])
  45. ->getMock();
  46. $access = $this->getMockBuilder(Access::class)->getMock();
  47. $accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  48. $accessRepository->method('find')->with(123)->willReturn($access);
  49. $this->entityManager->method('getRepository')->with(Access::class)->willReturn($accessRepository);
  50. $historical = ["future" => false,"past" => false,"present" => true,"dateStart" => null,"dateEnd" => null];
  51. $access->method('getHistorical')->willReturn($historical);
  52. $dateTimeConstraint->method('hasCustomPeriods')->with($historical)->willReturn(false);
  53. $periods = ['dateStart' => '2020-01-01', 'dateEnd' => '2020-12-31'];
  54. $dateTimeConstraint->method('getPeriods')->with($access)->willReturn($periods);
  55. $constraint = ['foo'];
  56. $dateTimeConstraint->method('presentConstraint')->with($periods)->willReturn($constraint);
  57. $dateTimeConstraint
  58. ->expects(self::once())
  59. ->method('addConstraint')
  60. ->with(['start' => [], 'end' => []] , $constraint)
  61. ->willReturn(['bar']);
  62. $dateTimeConstraint
  63. ->expects(self::once())
  64. ->method('cleanConstraints')
  65. ->with(['bar']);
  66. $dateTimeConstraint->invoke(123);
  67. }
  68. /**
  69. * @see DateTimeConstraint::invoke()
  70. */
  71. public function testInvokePastNoCustomPeriods(): void
  72. {
  73. $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
  74. ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
  75. ->setMethodsExcept(['invoke'])
  76. ->getMock();
  77. $access = $this->getMockBuilder(Access::class)->getMock();
  78. $accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  79. $accessRepository->method('find')->with(123)->willReturn($access);
  80. $this->entityManager->method('getRepository')->with(Access::class)->willReturn($accessRepository);
  81. $historical = ["future" => false,"past" => true,"present" => false,"dateStart" => null,"dateEnd" => null];
  82. $access->method('getHistorical')->willReturn($historical);
  83. $dateTimeConstraint->method('hasCustomPeriods')->with($historical)->willReturn(false);
  84. $periods = ['dateStart' => '2020-01-01', 'dateEnd' => '2020-12-31'];
  85. $dateTimeConstraint->method('getPeriods')->with($access)->willReturn($periods);
  86. $constraint = ['foo'];
  87. $dateTimeConstraint->method('pastConstraint')->with($periods)->willReturn($constraint);
  88. $dateTimeConstraint
  89. ->expects(self::once())
  90. ->method('addConstraint')
  91. ->with(['start' => [], 'end' => []] , $constraint)
  92. ->willReturn(['bar']);
  93. $dateTimeConstraint
  94. ->expects(self::once())
  95. ->method('cleanConstraints')
  96. ->with(['bar']);
  97. $dateTimeConstraint->invoke(123);
  98. }
  99. /**
  100. * @see DateTimeConstraint::invoke()
  101. */
  102. public function testInvokeFutureNoCustomPeriods(): void
  103. {
  104. $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
  105. ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
  106. ->setMethodsExcept(['invoke'])
  107. ->getMock();
  108. $access = $this->getMockBuilder(Access::class)->getMock();
  109. $accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  110. $accessRepository->method('find')->with(123)->willReturn($access);
  111. $this->entityManager->method('getRepository')->with(Access::class)->willReturn($accessRepository);
  112. $historical = ["future" => true,"past" => false,"present" => false,"dateStart" => null,"dateEnd" => null];
  113. $access->method('getHistorical')->willReturn($historical);
  114. $dateTimeConstraint->method('hasCustomPeriods')->with($historical)->willReturn(false);
  115. $periods = ['dateStart' => '2020-01-01', 'dateEnd' => '2020-12-31'];
  116. $dateTimeConstraint->method('getPeriods')->with($access)->willReturn($periods);
  117. $constraint = ['foo'];
  118. $dateTimeConstraint->method('futureConstraint')->with($periods)->willReturn($constraint);
  119. $dateTimeConstraint
  120. ->expects(self::once())
  121. ->method('addConstraint')
  122. ->with(['start' => [], 'end' => []] , $constraint)
  123. ->willReturn(['bar']);
  124. $dateTimeConstraint
  125. ->expects(self::once())
  126. ->method('cleanConstraints')
  127. ->with(['bar']);
  128. $dateTimeConstraint->invoke(123);
  129. }
  130. /**
  131. * @see DateTimeConstraint::invoke()
  132. */
  133. public function testInvokeMultiplePeriodsNoCustom(): void
  134. {
  135. $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
  136. ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
  137. ->setMethodsExcept(['invoke'])
  138. ->getMock();
  139. $access = $this->getMockBuilder(Access::class)->getMock();
  140. $accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  141. $accessRepository->method('find')->with(123)->willReturn($access);
  142. $this->entityManager->method('getRepository')->with(Access::class)->willReturn($accessRepository);
  143. $historical = ["future" => true,"past" => true,"present" => true,"dateStart" => null,"dateEnd" => null];
  144. $access->method('getHistorical')->willReturn($historical);
  145. $dateTimeConstraint->method('getPeriods')->with($access)->willReturn(['dateStart' => '2020-01-01', 'dateEnd' => '2020-12-31']);
  146. $dateTimeConstraint->method('hasCustomPeriods')->with($historical)->willReturn(false);
  147. $dateTimeConstraint->method('pastConstraint')->willReturn(['pastConstraint']);
  148. $dateTimeConstraint->method('presentConstraint')->willReturn(['presentConstraint']);
  149. $dateTimeConstraint->method('futureConstraint')->willReturn(['futureConstraint']);
  150. $dateTimeConstraint
  151. ->expects(self::exactly(3))
  152. ->method('addConstraint')
  153. ->withConsecutive(
  154. [['start' => [], 'end' => []] , ['presentConstraint']],
  155. [['start' => [], 'end' => []] , ['pastConstraint']],
  156. [['start' => [], 'end' => []] , ['futureConstraint']]
  157. )->willReturn(['start' => [], 'end' => []]);
  158. $dateTimeConstraint
  159. ->expects(self::once())
  160. ->method('cleanConstraints')
  161. ->with(['start' => [], 'end' => []]);
  162. $dateTimeConstraint->invoke(123);
  163. }
  164. /**
  165. * @see DateTimeConstraint::invoke()
  166. */
  167. public function testInvokeWithCustom(): void {
  168. $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
  169. ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
  170. ->setMethodsExcept(['invoke'])
  171. ->getMock();
  172. $access = $this->getMockBuilder(Access::class)->getMock();
  173. $accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  174. $accessRepository->method('find')->with(123)->willReturn($access);
  175. $this->entityManager->method('getRepository')->with(Access::class)->willReturn($accessRepository);
  176. $historical = ["future" => false,"past" => false,"present" => true,"dateStart" => '2020-01-01',"dateEnd" => '2020-02-01'];
  177. $access->method('getHistorical')->willReturn($historical);
  178. $dateTimeConstraint->method('hasCustomPeriods')->with($historical)->willReturn(true);
  179. $dateTimeConstraint->expects(self::never())->method('pastConstraint');
  180. $dateTimeConstraint->expects(self::never())->method('futureConstraint');
  181. $dateTimeConstraint
  182. ->method('getCustomPeriods')
  183. ->with('2020-01-01', '2020-02-01')
  184. ->willReturn(['dateStart' => 2020, 'dateEnd' => 2020]);
  185. $dateTimeConstraint->expects(self::once())
  186. ->method('presentConstraint')
  187. ->with(['dateStart' => 2020, 'dateEnd' => 2020])
  188. ->willReturn(['bar']);
  189. $dateTimeConstraint
  190. ->expects(self::once())
  191. ->method('addConstraint')
  192. ->with(['start' => [], 'end' => []], ['bar'])
  193. ->willReturn(['start' => [], 'end' => []]);
  194. $dateTimeConstraint
  195. ->expects(self::once())
  196. ->method('cleanConstraints')
  197. ->with(['start' => [], 'end' => []]);
  198. $dateTimeConstraint->invoke(123);
  199. }
  200. /**
  201. * @see DateTimeConstraint::getCustomPeriods()
  202. */
  203. public function testGetCustomPeriods(): void {
  204. $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
  205. ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
  206. ->setMethodsExcept(['getCustomPeriods'])
  207. ->getMock();
  208. $this->assertEquals(
  209. ['dateStart' => '2020-01-01', 'dateEnd' => '2020-12-31'],
  210. $dateTimeConstraint->getCustomPeriods('2020-01-01', '2020-12-31')
  211. );
  212. }
  213. /**
  214. * @see DateTimeConstraint::presentConstraint()
  215. */
  216. public function testPresentConstraint(): void
  217. {
  218. $expected = [
  219. 'start' => [
  220. '2022-08-31' => 4
  221. ],
  222. 'end' => [
  223. '2021-12-20' => 8,
  224. 'NULL' => 0
  225. ]
  226. ];
  227. $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
  228. ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
  229. ->setMethodsExcept(['presentConstraint'])
  230. ->getMock();
  231. $result = $this->invokeMethod($dateTimeConstraint, 'presentConstraint', [$this->periods]);
  232. $this->assertEquals($expected, $result);
  233. }
  234. /**
  235. * @see DateTimeConstraint::pastConstraint()
  236. */
  237. public function testPastConstraint(): void
  238. {
  239. $expected = [
  240. 'end' => [
  241. '2021-12-20' => 1
  242. ]
  243. ];
  244. $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
  245. ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
  246. ->setMethodsExcept(['pastConstraint'])
  247. ->getMock();
  248. $result = $this->invokeMethod($dateTimeConstraint, 'pastConstraint', [$this->periods]);
  249. $this->assertEquals($expected, $result);
  250. }
  251. /**
  252. * @see DateTimeConstraint::futurConstraint()
  253. */
  254. public function testFutureConstraint(): void
  255. {
  256. $expected = [
  257. 'start' => [
  258. '2022-08-31' => 5
  259. ]
  260. ];
  261. $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
  262. ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
  263. ->setMethodsExcept(['futureConstraint'])
  264. ->getMock();
  265. $result = $this->invokeMethod($dateTimeConstraint, 'futureConstraint', [$this->periods]);
  266. $this->assertEquals($expected, $result);
  267. }
  268. /**
  269. * Si l'année courante est l'année d'affichage choisie par l'utilisateur, alors la date de début est aujourd'hui
  270. *
  271. * @throws \ReflectionException
  272. * @see DateTimeConstraint::getPeriods()
  273. */
  274. public function testGetPeriodsToday(): void
  275. {
  276. $today = new \DateTime('now');
  277. $activityYear = (int)$today->format('Y');
  278. if ((int)$today->format('m') < 9) {
  279. $activityYear--;
  280. }
  281. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  282. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2000-09-01'));
  283. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  284. $organization->method('getParameters')->willReturn($parameters);
  285. $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
  286. $access->method('getOrganization')->willReturn($organization);
  287. $access->method('getActivityYear')->willReturn(2020);
  288. $this->organizationUtils->method('getOrganizationCurrentActivityYear')->with($organization)->willReturn(2020);
  289. $this->organizationUtils
  290. ->method('getActivityPeriodsSwitchYear')
  291. ->with($organization, 2020)
  292. ->willReturn(['dateStart' => 'YEAR-09-01', 'dateEnd' => ($activityYear + 1) . '-08-31']); // dateStart will be overwritten
  293. $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
  294. ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
  295. ->setMethodsExcept(['getPeriods'])
  296. ->getMock();
  297. $periodExpected = ['dateStart' => $today->format('Y-m-d'), 'dateEnd' => ($activityYear + 1) . '-08-31'];
  298. $result = $this->invokeMethod($dateTimeConstraint, 'getPeriods', [$access]);
  299. $this->assertEquals($periodExpected, $result);
  300. }
  301. /**
  302. * @throws \ReflectionException
  303. * @see DateTimeConstraint::getPeriods()
  304. */
  305. public function testGetPeriodsNotToday(): void
  306. {
  307. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  308. $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
  309. $access->method('getOrganization')->willReturn($organization);
  310. $access->method('getActivityYear')->willReturn(2020);
  311. $this->organizationUtils->method('getOrganizationCurrentActivityYear')->with($organization)->willReturn(2022);
  312. $this->organizationUtils
  313. ->method('getActivityPeriodsSwitchYear')
  314. ->with($organization, 2020)
  315. ->willReturn(['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31']);
  316. $dateTimeConstraint = $this->getMockBuilder(TestableDateTimeConstraint::class)
  317. ->setConstructorArgs([$this->entityManager, $this->organizationUtils])
  318. ->setMethodsExcept(['getPeriods'])
  319. ->getMock();
  320. $periodExpected = ['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31'];
  321. $this->assertEquals($periodExpected, $this->invokeMethod($dateTimeConstraint, 'getPeriods', [$access]));
  322. }
  323. }