DateTimeFilterTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Tests\Filter\DoctrineFilter;
  3. use App\Filter\DoctrineFilter\DateTimeFilter;
  4. use App\Tests\TestToolsTrait;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use PHPUnit\Framework\TestCase;
  7. class DateTimeFilterTest extends TestCase
  8. {
  9. use TestToolsTrait;
  10. private DateTimeFilter $dateTimeFilter;
  11. public function setUp(): void
  12. {
  13. $em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  14. $this->dateTimeFilter = new DateTimeFilter($em);
  15. }
  16. /**
  17. * @see DateTimeFilter::constructQuery()
  18. */
  19. public function testConstructQuery():void
  20. {
  21. $queryExpected = "(o.startDate <= '2021-12-20') AND (o.endDate > '2021-12-20' OR o.endDate IS NULL)";
  22. $this->assertEquals($queryExpected, $this->invokeMethod($this->dateTimeFilter, 'constructQuery', [
  23. [
  24. 'start' => [
  25. '2021-12-20' => [4]
  26. ],
  27. 'end' => [
  28. '2021-12-20' => [5],
  29. 'NULL' => [0]
  30. ]
  31. ],
  32. 'o',
  33. [
  34. 'start' => 'startDate',
  35. 'end' => 'endDate'
  36. ]
  37. ]));
  38. $queryExpected = "(o.startDate < '2021-12-20' OR o.startDate > '2021-12-30') AND (o.endDate < '2022-01-20')";
  39. $this->assertEquals($queryExpected, $this->invokeMethod($this->dateTimeFilter, 'constructQuery', [
  40. [
  41. 'start' => [
  42. '2021-12-20' => [1],
  43. '2021-12-30' => [5]
  44. ],
  45. 'end' => [
  46. '2022-01-20' => [1]
  47. ]
  48. ],
  49. 'o',
  50. [
  51. 'start' => 'startDate',
  52. 'end' => 'endDate'
  53. ]
  54. ]));
  55. }
  56. /**
  57. * @see DateTimeFilter::getArithmeticValue()
  58. */
  59. public function testGetArithmeticValue():void
  60. {
  61. $this->assertEquals('<', $this->invokeMethod($this->dateTimeFilter, 'getArithmeticValue', [1]));
  62. $this->assertEquals('<=', $this->invokeMethod($this->dateTimeFilter, 'getArithmeticValue', [4]));
  63. $this->assertEquals('=', $this->invokeMethod($this->dateTimeFilter, 'getArithmeticValue', [3]));
  64. $this->assertEquals('>=', $this->invokeMethod($this->dateTimeFilter, 'getArithmeticValue', [8]));
  65. $this->assertEquals('>', $this->invokeMethod($this->dateTimeFilter, 'getArithmeticValue', [5]));
  66. $this->assertEquals(null, $this->invokeMethod($this->dateTimeFilter, 'getArithmeticValue', [0]));
  67. }
  68. }