Skip to content
Merged
26 changes: 21 additions & 5 deletions src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/Daterange.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
class Daterange extends BaseFunction
class Daterange extends BaseVariadicFunction
{
protected function customizeFunction(): void
protected function getFunctionName(): string
{
$this->setFunctionPrototype('daterange(%s, %s)');
$this->addNodeMapping('StringPrimary');
$this->addNodeMapping('StringPrimary');
return 'daterange';
}

/**
* @return array<string>
*/
protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function getMinArgumentCount(): int
{
return 2;
}

protected function getMaxArgumentCount(): int
{
return 3;
}
}
26 changes: 21 additions & 5 deletions src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/Int4range.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
class Int4range extends BaseFunction
class Int4range extends BaseVariadicFunction
{
protected function customizeFunction(): void
protected function getFunctionName(): string
{
$this->setFunctionPrototype('int4range(%s, %s)');
$this->addNodeMapping('StringPrimary');
$this->addNodeMapping('StringPrimary');
return 'int4range';
}

/**
* @return array<string>
*/
protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function getMinArgumentCount(): int
{
return 2;
}

protected function getMaxArgumentCount(): int
{
return 3;
}
}
26 changes: 21 additions & 5 deletions src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/Int8range.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
class Int8range extends BaseFunction
class Int8range extends BaseVariadicFunction
{
protected function customizeFunction(): void
protected function getFunctionName(): string
{
$this->setFunctionPrototype('int8range(%s, %s)');
$this->addNodeMapping('StringPrimary');
$this->addNodeMapping('StringPrimary');
return 'int8range';
}

/**
* @return array<string>
*/
protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function getMinArgumentCount(): int
{
return 2;
}

protected function getMaxArgumentCount(): int
{
return 3;
}
}
26 changes: 21 additions & 5 deletions src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/Numrange.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
class Numrange extends BaseFunction
class Numrange extends BaseVariadicFunction
{
protected function customizeFunction(): void
protected function getFunctionName(): string
{
$this->setFunctionPrototype('numrange(%s, %s)');
$this->addNodeMapping('StringPrimary');
$this->addNodeMapping('StringPrimary');
return 'numrange';
}

/**
* @return array<string>
*/
protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function getMinArgumentCount(): int
{
return 2;
}

protected function getMaxArgumentCount(): int
{
return 3;
}
}
26 changes: 21 additions & 5 deletions src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/Tsrange.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
class Tsrange extends BaseFunction
class Tsrange extends BaseVariadicFunction
{
protected function customizeFunction(): void
protected function getFunctionName(): string
{
$this->setFunctionPrototype('tsrange(%s, %s)');
$this->addNodeMapping('StringPrimary');
$this->addNodeMapping('StringPrimary');
return 'tsrange';
}

/**
* @return array<string>
*/
protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function getMinArgumentCount(): int
{
return 2;
}

protected function getMaxArgumentCount(): int
{
return 3;
}
}
26 changes: 21 additions & 5 deletions src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/Tstzrange.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
class Tstzrange extends BaseFunction
class Tstzrange extends BaseVariadicFunction
{
protected function customizeFunction(): void
protected function getFunctionName(): string
{
$this->setFunctionPrototype('tstzrange(%s, %s)');
$this->addNodeMapping('StringPrimary');
$this->addNodeMapping('StringPrimary');
return 'tstzrange';
}

/**
* @return array<string>
*/
protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function getMinArgumentCount(): int
{
return 2;
}

protected function getMaxArgumentCount(): int
{
return 3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;

use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Daterange;

class DaterangeTest extends DateTestCase
{
protected function getStringFunctions(): array
{
return [
'DATERANGE' => Daterange::class,
];
}

public function test_daterange(): void
{
$dql = 'SELECT DATERANGE(t.date1, t.date2) as result FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsDates t WHERE t.id = 1';
$result = $this->executeDqlQuery($dql);
$this->assertIsArray($result);
$this->assertNotEmpty($result[0]['result']);
$this->assertIsString($result[0]['result']);
$this->assertSame('[2023-06-15,2023-06-16)', $result[0]['result']);
}

public function test_daterange_with_bounds(): void
{
$dql = 'SELECT DATERANGE(t.date1, t.date2, "[)") as result FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsDates t WHERE t.id = 1';
$result = $this->executeDqlQuery($dql);
$this->assertIsArray($result);
$this->assertNotEmpty($result[0]['result']);
$this->assertIsString($result[0]['result']);
$this->assertSame('[2023-06-15,2023-06-16)', $result[0]['result']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;

use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Int4range;

class Int4rangeTest extends JsonTestCase
{
protected function getStringFunctions(): array
{
return [
'INT4RANGE' => Int4range::class,
];
}

public function test_int4range(): void
{
$dql = 'SELECT INT4RANGE(:start, :end) as result FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t WHERE t.id = 1';
$result = $this->executeDqlQuery($dql, [
'start' => 10,
'end' => 20,
]);
$this->assertIsArray($result);
$this->assertNotEmpty($result[0]['result']);
$this->assertIsString($result[0]['result']);
$this->assertSame('[10,20)', $result[0]['result']);
}

public function test_int4range_with_bounds(): void
{
$dql = 'SELECT INT4RANGE(:start, :end, "[)") as result FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t WHERE t.id = 1';
$result = $this->executeDqlQuery($dql, [
'start' => 10,
'end' => 20,
]);
$this->assertIsArray($result);
$this->assertNotEmpty($result[0]['result']);
$this->assertIsString($result[0]['result']);
$this->assertSame('[10,20)', $result[0]['result']);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Test for explicit bounds doesn't effectively verify the bounds parameter.

Both test methods use the same expected result ('[10,20)'), even though the second test explicitly specifies "[)" bounds. Since "[)" is the default bounds format anyway (as shown in the first test), this doesn't effectively verify that specifying explicit bounds works as expected.

Add a test case that uses different explicit bounds (e.g., '[]' for inclusive-inclusive) and verify that the result changes accordingly:

 public function test_int4range_with_bounds(): void
 {
-    $dql = 'SELECT INT4RANGE(:start, :end, "[)") as result FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t WHERE t.id = 1';
+    $dql = 'SELECT INT4RANGE(:start, :end, "[]") as result FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t WHERE t.id = 1';
     $result = $this->executeDqlQuery($dql, [
         'start' => 10,
         'end' => 20,
     ]);
     $this->assertIsArray($result);
     $this->assertNotEmpty($result[0]['result']);
     $this->assertIsString($result[0]['result']);
-    $this->assertSame('[10,20)', $result[0]['result']);
+    $this->assertSame('[10,20]', $result[0]['result']);
 }
🤖 Prompt for AI Agents
In
tests/Integration/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/Int4rangeTest.php
around lines 31 to 42, the test for explicit bounds uses the default "[)" bounds
and expects the same result as the implicit default, which does not verify the
bounds parameter effectively. Add a new test method that uses different explicit
bounds such as "[]" (inclusive-inclusive) and assert that the result string
reflects these bounds correctly, for example '[10,20]'. This will ensure the
bounds parameter is properly tested.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;

use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Int8range;

class Int8rangeTest extends JsonTestCase
{
protected function getStringFunctions(): array
{
return [
'INT8RANGE' => Int8range::class,
];
}

public function test_int8range(): void
{
$dql = 'SELECT INT8RANGE(:start, :end) as result FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t WHERE t.id = 1';
$result = $this->executeDqlQuery($dql, [
'start' => 100,
'end' => 200,
]);
$this->assertIsArray($result);
$this->assertNotEmpty($result[0]['result']);
$this->assertIsString($result[0]['result']);
$this->assertSame('[100,200)', $result[0]['result']);
}

public function test_int8range_with_bounds(): void
{
$dql = 'SELECT INT8RANGE(:start, :end, "[)") as result FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t WHERE t.id = 1';
$result = $this->executeDqlQuery($dql, [
'start' => 1000,
'end' => 2000,
]);
$this->assertIsArray($result);
$this->assertNotEmpty($result[0]['result']);
$this->assertIsString($result[0]['result']);
$this->assertSame('[1000,2000)', $result[0]['result']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;

use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Tsrange;

class TsrangeTest extends DateTestCase
{
protected function getStringFunctions(): array
{
return [
'TSRANGE' => Tsrange::class,
];
}

public function test_tsrange(): void
{
$dql = 'SELECT TSRANGE(t.datetime1, t.datetime2) as result FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsDates t WHERE t.id = 1';
$result = $this->executeDqlQuery($dql);
$this->assertIsArray($result);
$this->assertNotEmpty($result[0]['result']);
$this->assertIsString($result[0]['result']);
$this->assertSame('["2023-06-15 10:30:00","2023-06-16 11:45:00")', $result[0]['result']);
}

public function test_tsrange_with_bounds(): void
{
$dql = 'SELECT TSRANGE(t.datetime1, t.datetime2, "[)") as result FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsDates t WHERE t.id = 1';
$result = $this->executeDqlQuery($dql);
$this->assertIsArray($result);
$this->assertNotEmpty($result[0]['result']);
$this->assertIsString($result[0]['result']);
$this->assertSame('["2023-06-15 10:30:00","2023-06-16 11:45:00")', $result[0]['result']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;

use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Tstzrange;

class TstzrangeTest extends JsonTestCase
{
protected function getStringFunctions(): array
{
return [
'TSTZRANGE' => Tstzrange::class,
];
}

public function test_tstzrange(): void
{
$dql = 'SELECT TSTZRANGE(\'2024-01-01 00:00:00+00\', \'2024-12-31 23:59:59+00\') as result FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t WHERE t.id = 1';
$result = $this->executeDqlQuery($dql);
$this->assertIsArray($result);
$this->assertNotEmpty($result[0]['result']);
$this->assertIsString($result[0]['result']);
$this->assertSame('["2024-01-01 00:00:00+00","2024-12-31 23:59:59+00")', $result[0]['result']);
}
}
Loading
Loading