AbstractTimeConstraintsUtilsTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Tests\Unit\Service\Constraint;
  3. use App\Service\Constraint\AbstractTimeConstraintUtils;
  4. use App\Tests\Unit\TestToolsTrait;
  5. use PHPUnit\Framework\TestCase;
  6. class AbstractTimeConstraintsUtilsTest extends TestCase
  7. {
  8. use TestToolsTrait;
  9. /**
  10. * @throws \ReflectionException
  11. *
  12. * @see DateTimeConstraint::hasCustomPeriods()
  13. */
  14. public function testHasCustomPeriods(): void
  15. {
  16. $historical = ['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31'];
  17. $timeConstraintUtils = $this->getMockBuilder(AbstractTimeConstraintUtils::class)
  18. ->setConstructorArgs([])
  19. ->setMethodsExcept(['hasCustomPeriods'])
  20. ->getMock();
  21. $this->assertTrue($this->invokeMethod($timeConstraintUtils, 'hasCustomPeriods', [$historical]));
  22. }
  23. /**
  24. * @throws \ReflectionException
  25. *
  26. * @see DateTimeConstraint::hasCustomPeriods()
  27. */
  28. public function testHasNotCustomPeriods(): void
  29. {
  30. $historical = ['dateStart' => null, 'dateEnd' => null];
  31. $timeConstraintUtils = $this->getMockBuilder(AbstractTimeConstraintUtils::class)
  32. ->setConstructorArgs([])
  33. ->setMethodsExcept(['hasCustomPeriods'])
  34. ->getMock();
  35. $this->assertFalse($this->invokeMethod($timeConstraintUtils, 'hasCustomPeriods', [$historical]));
  36. }
  37. /**
  38. * @see DateTimeConstraint::addConstraint()
  39. */
  40. public function testAddConstraint(): void
  41. {
  42. $originalConstraint = [
  43. 'start' => [],
  44. 'end' => [],
  45. ];
  46. $presentConstraint = [
  47. 'start' => [
  48. '2022-08-31' => 4,
  49. ],
  50. 'end' => [
  51. '2021-12-20' => 8,
  52. 'NULL' => 0,
  53. ],
  54. ];
  55. $pastConstraint = [
  56. 'end' => [
  57. '2021-12-20' => 1,
  58. ],
  59. ];
  60. $constraintAfterStartExpected = [
  61. 'start' => [
  62. '2022-08-31' => [4],
  63. ],
  64. 'end' => [
  65. '2021-12-20' => [8],
  66. 'NULL' => [0],
  67. ],
  68. ];
  69. $constraintAfterEndExpected = [
  70. 'start' => [
  71. '2022-08-31' => [4],
  72. ],
  73. 'end' => [
  74. '2021-12-20' => [8, 1],
  75. 'NULL' => [0],
  76. ],
  77. ];
  78. $timeConstraintUtils = $this->getMockBuilder(AbstractTimeConstraintUtils::class)
  79. ->setConstructorArgs([])
  80. ->setMethodsExcept(['addConstraint'])
  81. ->getMock();
  82. $this->assertEquals($constraintAfterStartExpected, $this->invokeMethod($timeConstraintUtils, 'addConstraint', [$originalConstraint, $presentConstraint]));
  83. $this->assertEquals($constraintAfterEndExpected, $this->invokeMethod($timeConstraintUtils, 'addConstraint', [$constraintAfterStartExpected, $pastConstraint]));
  84. }
  85. /**
  86. * @see AbstractTimeConstraintUtils::cleanConstraints()
  87. */
  88. public function testCleanConstraints(): void
  89. {
  90. $originalConstraint = [
  91. 'start' => [
  92. '2022-08-31' => [5],
  93. ],
  94. 'end' => [
  95. '2021-12-20' => [8, 1],
  96. 'NULL' => [0],
  97. ],
  98. ];
  99. $constraintExpected = [
  100. 'start' => [
  101. '2022-08-31' => [5],
  102. ],
  103. 'end' => [],
  104. ];
  105. $timeConstraintUtils = $this->getMockBuilder(AbstractTimeConstraintUtils::class)
  106. ->setConstructorArgs([])
  107. ->setMethodsExcept(['cleanConstraints'])
  108. ->getMock();
  109. $this->assertEquals($constraintExpected, $this->invokeMethod($timeConstraintUtils, 'cleanConstraints', [$originalConstraint]));
  110. }
  111. /**
  112. * @see AbstractTimeConstraintUtils::filterConstraint()
  113. */
  114. public function testFilterConstraint(): void
  115. {
  116. $originalStartConstraint = [
  117. 'start' => [
  118. '2021-12-20' => [8, 1],
  119. '2022-01-01' => [5],
  120. ],
  121. ];
  122. $startConstraintExpected = [
  123. '2022-01-01' => [5],
  124. ];
  125. $timeConstraintUtils = $this->getMockBuilder(AbstractTimeConstraintUtils::class)
  126. ->setConstructorArgs([])
  127. ->setMethodsExcept(['filterConstraint'])
  128. ->getMock();
  129. $this->assertEquals(
  130. $startConstraintExpected,
  131. $this->invokeMethod($timeConstraintUtils, 'filterConstraint', [$originalStartConstraint, 'start'])
  132. );
  133. }
  134. /**
  135. * @see AbstractTimeConstraintUtils::clearNull()
  136. */
  137. public function testClearNull(): void
  138. {
  139. $originalEndConstraint = [
  140. 'end' => [
  141. 'NULL' => 0,
  142. ],
  143. ];
  144. $endConstraintExpected = [];
  145. $timeConstraintUtils = $this->getMockBuilder(AbstractTimeConstraintUtils::class)
  146. ->setConstructorArgs([])
  147. ->setMethodsExcept(['clearNull'])
  148. ->getMock();
  149. $this->assertEquals($endConstraintExpected, $this->invokeMethod($timeConstraintUtils, 'clearNull', [$originalEndConstraint, 'end']));
  150. }
  151. }