Skip to content

Commit e89b8c4

Browse files
chore: improve test coverage in unit tests
1 parent 8679690 commit e89b8c4

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

tests/Unit/MartinGeorgiev/Doctrine/DBAL/Types/BaseFloatArrayTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Tests\Unit\MartinGeorgiev\Doctrine\DBAL\Types;
66

7-
use Doctrine\DBAL\Types\ConversionException;
87
use MartinGeorgiev\Doctrine\DBAL\Types\BaseFloatArray;
8+
use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidFloatArrayItemForPHPException;
99
use PHPUnit\Framework\Attributes\DataProvider;
1010
use PHPUnit\Framework\Attributes\Test;
1111
use PHPUnit\Framework\TestCase;
@@ -64,7 +64,7 @@ abstract public static function provideValidTransformations(): array;
6464
#[Test]
6565
public function throws_domain_exception_when_invalid_array_item_value(): void
6666
{
67-
$this->expectException(ConversionException::class);
67+
$this->expectException(InvalidFloatArrayItemForPHPException::class);
6868
$this->expectExceptionMessage('cannot be transformed to valid PHP float');
6969

7070
$this->fixture->transformArrayItemForPHP('1.e234');

tests/Unit/MartinGeorgiev/Doctrine/DBAL/Types/PointArrayTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,37 @@ public function returns_empty_array_for_non_standard_postgres_array_format(): vo
191191
self::assertEquals([], $result1);
192192
self::assertEquals([], $result2);
193193
}
194+
195+
#[Test]
196+
public function transform_array_item_for_php_returns_null_for_null(): void
197+
{
198+
$this->assertNull($this->fixture->transformArrayItemForPHP(null));
199+
}
200+
201+
#[Test]
202+
#[DataProvider('provideInvalidTypes')]
203+
public function transform_array_item_for_php_throws_for_invalid_type(mixed $value): void
204+
{
205+
$this->expectException(InvalidPointArrayItemForPHPException::class);
206+
$this->fixture->transformArrayItemForPHP($value);
207+
}
208+
209+
#[Test]
210+
#[DataProvider('provideInvalidTypes')]
211+
public function transform_postgres_array_to_php_array_returns_empty_for_invalid_format(mixed $value): void
212+
{
213+
$reflectionObject = new \ReflectionObject($this->fixture);
214+
$reflectionMethod = $reflectionObject->getMethod('transformPostgresArrayToPHPArray');
215+
$reflectionMethod->setAccessible(true);
216+
$this->assertSame([], $reflectionMethod->invoke($this->fixture, $value));
217+
}
218+
219+
public static function provideInvalidTypes(): array
220+
{
221+
return [
222+
[123],
223+
['not-a-point-instance'],
224+
['{invalid}'],
225+
];
226+
}
194227
}

tests/Unit/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/BaseVariadicFunctionTestCase.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use Doctrine\ORM\Configuration;
88
use Doctrine\ORM\EntityManager;
99
use Doctrine\ORM\Query;
10+
use Doctrine\ORM\Query\AST\Node;
1011
use Doctrine\ORM\Query\Parser;
1112
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\BaseVariadicFunction;
13+
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\InvalidArgumentForVariadicFunctionException;
1214
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\ParserException;
1315
use PHPUnit\Framework\Attributes\Test;
1416

@@ -38,4 +40,70 @@ public function throws_an_exception_when_lexer_is_not_populated_with_a_lookahead
3840
$reflectionMethod->setAccessible(true);
3941
$reflectionMethod->invoke($baseVariadicFunction, $parser, 'ArithmeticPrimary');
4042
}
43+
44+
#[Test]
45+
public function throws_exception_when_argument_count_is_too_low(): void
46+
{
47+
$function = new class('TEST') extends BaseVariadicFunction {
48+
protected function getFunctionName(): string
49+
{
50+
return 'TEST';
51+
}
52+
53+
protected function getNodeMappingPattern(): array
54+
{
55+
return ['StringPrimary'];
56+
}
57+
58+
protected function getMinArgumentCount(): int
59+
{
60+
return 2;
61+
}
62+
63+
protected function getMaxArgumentCount(): int
64+
{
65+
return 3;
66+
}
67+
};
68+
$this->expectException(InvalidArgumentForVariadicFunctionException::class);
69+
$reflectionClass = new \ReflectionClass($function);
70+
$reflectionMethod = $reflectionClass->getMethod('validateArguments');
71+
$reflectionMethod->setAccessible(true);
72+
73+
$node = $this->createMock(Node::class);
74+
$reflectionMethod->invoke($function, $node); // No arguments
75+
}
76+
77+
#[Test]
78+
public function throws_exception_when_argument_count_is_too_high(): void
79+
{
80+
$function = new class('TEST') extends BaseVariadicFunction {
81+
protected function getFunctionName(): string
82+
{
83+
return 'TEST';
84+
}
85+
86+
protected function getNodeMappingPattern(): array
87+
{
88+
return ['StringPrimary'];
89+
}
90+
91+
protected function getMinArgumentCount(): int
92+
{
93+
return 1;
94+
}
95+
96+
protected function getMaxArgumentCount(): int
97+
{
98+
return 2;
99+
}
100+
};
101+
$this->expectException(InvalidArgumentForVariadicFunctionException::class);
102+
$reflectionClass = new \ReflectionClass($function);
103+
$reflectionMethod = $reflectionClass->getMethod('validateArguments');
104+
$reflectionMethod->setAccessible(true);
105+
106+
$node = $this->createMock(Node::class);
107+
$reflectionMethod->invoke($function, $node, $node, $node);
108+
}
41109
}

0 commit comments

Comments
 (0)