AbstractTimeConstraintsUtilsTest.php 5.0 KB

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