Skip to content

Commit 9fb0419

Browse files
chore: test functions against a real PostgreSQL server (#382)
1 parent 5b4f975 commit 9fb0419

File tree

5 files changed

+128
-2
lines changed

5 files changed

+128
-2
lines changed

.scrutinizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ build:
88
tests:
99
override:
1010
- php-scrutinizer-run
11-
- command: composer run-tests-with-clover
11+
- command: composer run-unit-tests-with-clover
1212
coverage:
1313
file: var/logs/test-coverage/clover.xml
1414
format: clover
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;
6+
7+
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ArrayAgg;
8+
9+
class ArrayAggTest extends ArrayTestCase
10+
{
11+
protected function getStringFunctions(): array
12+
{
13+
return ['ARRAY_AGG' => ArrayAgg::class];
14+
}
15+
16+
public function test_array_agg_with_text_array(): void
17+
{
18+
$dql = 'SELECT ARRAY_AGG(t.textArray) as result
19+
FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsArrays t
20+
WHERE t.id = 1';
21+
$result = $this->executeDqlQuery($dql);
22+
$this->assertSame('{{apple,banana,orange}}', $result[0]['result']);
23+
}
24+
25+
public function test_array_agg_with_integer_array(): void
26+
{
27+
$dql = 'SELECT ARRAY_AGG(t.integerArray) as result
28+
FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsArrays t
29+
WHERE t.id = 1';
30+
$result = $this->executeDqlQuery($dql);
31+
$this->assertSame('{{1,2,3}}', $result[0]['result']);
32+
}
33+
34+
public function test_array_agg_with_boolean_array(): void
35+
{
36+
$dql = 'SELECT ARRAY_AGG(t.boolArray) as result
37+
FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsArrays t
38+
WHERE t.id = 1';
39+
$result = $this->executeDqlQuery($dql);
40+
$this->assertSame('{{t,f,t}}', $result[0]['result']);
41+
}
42+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;
6+
7+
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonObjectAgg;
8+
9+
class JsonObjectAggTest extends JsonTestCase
10+
{
11+
protected function getStringFunctions(): array
12+
{
13+
return [
14+
'JSON_OBJECT_AGG' => JsonObjectAgg::class,
15+
];
16+
}
17+
18+
public function test_json_object_agg_simple(): void
19+
{
20+
$dql = "SELECT JSON_OBJECT_AGG('key', t.object1) as result
21+
FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsJsons t
22+
WHERE t.id = 3";
23+
$result = $this->executeDqlQuery($dql);
24+
$this->assertIsString($result[0]['result']);
25+
$decoded = \json_decode($result[0]['result'], true);
26+
$this->assertIsArray($decoded);
27+
$this->assertSame([
28+
'key' => [
29+
'age' => 30,
30+
'name' => 'Micky',
31+
'tags' => [],
32+
'address' => ['city' => 'New York'],
33+
],
34+
], $decoded);
35+
}
36+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;
6+
7+
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\StringAgg;
8+
9+
class StringAggTest extends TextTestCase
10+
{
11+
protected function getStringFunctions(): array
12+
{
13+
return ['STRING_AGG' => StringAgg::class];
14+
}
15+
16+
public function test_string_agg_with_for_all_rows(): void
17+
{
18+
$dql = "SELECT STRING_AGG(t.text1, ',') as result
19+
FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsTexts t";
20+
$result = $this->executeDqlQuery($dql);
21+
$this->assertSame('this is a test string,lorem ipsum dolor,foo,special,chars;test', $result[0]['result']);
22+
}
23+
24+
public function test_string_agg_with_for_all_rows_and_semicolon_delimiter(): void
25+
{
26+
$dql = "SELECT STRING_AGG(t.text2, ';') as result
27+
FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsTexts t";
28+
$result = $this->executeDqlQuery($dql);
29+
$this->assertSame('another test string;sit amet;bar;multi;delimiter,case', $result[0]['result']);
30+
}
31+
32+
public function test_string_agg_with_for_all_rows_and_space_delimiter(): void
33+
{
34+
$dql = "SELECT STRING_AGG(t.text1, ' ') as result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsTexts t";
35+
$result = $this->executeDqlQuery($dql);
36+
$this->assertSame('this is a test string lorem ipsum dolor foo special,chars;test', $result[0]['result']);
37+
}
38+
39+
public function test_string_agg_with_where_clause(): void
40+
{
41+
$dql = "SELECT STRING_AGG(t.text1, ',') as result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsTexts t WHERE t.id in (1, 4)";
42+
$result = $this->executeDqlQuery($dql);
43+
$this->assertSame('this is a test string,special,chars;test', $result[0]['result']);
44+
}
45+
}

tests/Integration/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/TextTestCase.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ protected function insertTestDataForTextFixture(): void
3838
{
3939
$sql = \sprintf('
4040
INSERT INTO %s.containstexts (text1, text2) VALUES
41-
(\'this is a test string\', \'another test string\')
41+
(\'this is a test string\', \'another test string\'),
42+
(\'lorem ipsum dolor\', \'sit amet\'),
43+
(\'foo\', \'bar\'),
44+
(\'special,chars;test\', \'multi;delimiter,case\')
4245
', self::DATABASE_SCHEMA);
4346
$this->connection->executeStatement($sql);
4447
}

0 commit comments

Comments
 (0)