AbstractTimeConstraintsUtilsTest.php 5.0 KB

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