Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

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

use Doctrine\DBAL\Types\ConversionException;
use MartinGeorgiev\Doctrine\DBAL\Types\BaseFloatArray;
use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidFloatArrayItemForPHPException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -64,7 +64,7 @@ abstract public static function provideValidTransformations(): array;
#[Test]
public function throws_domain_exception_when_invalid_array_item_value(): void
{
$this->expectException(ConversionException::class);
$this->expectException(InvalidFloatArrayItemForPHPException::class);
$this->expectExceptionMessage('cannot be transformed to valid PHP float');

$this->fixture->transformArrayItemForPHP('1.e234');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Platforms\AbstractPlatform;
use MartinGeorgiev\Doctrine\DBAL\Types\BaseNetworkTypeArray;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -55,24 +56,28 @@ public function has_name(): void
}

#[Test]
public function can_convert_null_to_database_value(): void
#[DataProvider('provideValidTransformations')]
public function can_convert_to_php_value(?array $phpValue, ?string $postgresValue): void
{
self::assertNull($this->fixture->convertToDatabaseValue(null, $this->platform));
self::assertEquals($phpValue, $this->fixture->convertToPHPValue($postgresValue, $this->platform));
}

#[Test]
public function can_convert_empty_array_to_database_value(): void
public static function provideValidTransformations(): array
{
self::assertEquals('{}', $this->fixture->convertToDatabaseValue([], $this->platform));
}

#[Test]
public function can_convert_valid_array_to_database_value(): void
{
self::assertEquals(
'{"valid_address","valid_address"}',
$this->fixture->convertToDatabaseValue(['valid_address', 'valid_address'], $this->platform)
);
return [
'null' => [
'phpValue' => null,
'postgresValue' => null,
],
'empty array' => [
'phpValue' => [],
'postgresValue' => '{}',
],
'valid array' => [
'phpValue' => ['valid_address', 'valid_address'],
'postgresValue' => '{"valid_address","valid_address"}',
],
];
}

#[Test]
Expand All @@ -84,23 +89,31 @@ public function throws_exception_for_invalid_type(): void
}

#[Test]
public function can_convert_null_to_php_value(): void
#[DataProvider('provideInvalidValues')]
public function throws_exception_for_invalid_values(mixed $arrayItem, string $exceptionMessage): void
{
self::assertNull($this->fixture->convertToPHPValue(null, $this->platform));
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($exceptionMessage);
$this->fixture->transformArrayItemForPHP($arrayItem);
}

#[Test]
public function can_convert_empty_array_to_php_value(): void
public static function provideInvalidValues(): array
{
self::assertEquals([], $this->fixture->convertToPHPValue('{}', $this->platform));
return [
'invalid type' => [
'arrayItem' => [],
'exceptionMessage' => 'Invalid type',
],
'invalid format' => [
'arrayItem' => '"invalid_address"',
'exceptionMessage' => 'Invalid format',
],
];
}

#[Test]
public function can_convert_valid_string_to_php_value(): void
public function transform_array_item_for_php_handles_valid_string(): void
{
self::assertEquals(
['valid_address', 'valid_address'],
$this->fixture->convertToPHPValue('{"valid_address","valid_address"}', $this->platform)
);
$this->assertSame('valid_address', $this->fixture->transformArrayItemForPHP('"valid_address"'));
}
}
33 changes: 33 additions & 0 deletions tests/Unit/MartinGeorgiev/Doctrine/DBAL/Types/PointArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,37 @@ public function returns_empty_array_for_non_standard_postgres_array_format(): vo
self::assertEquals([], $result1);
self::assertEquals([], $result2);
}

#[Test]
public function transform_array_item_for_php_returns_null_for_null(): void
{
$this->assertNull($this->fixture->transformArrayItemForPHP(null));
}

#[Test]
#[DataProvider('provideInvalidTypes')]
public function transform_array_item_for_php_throws_for_invalid_type(mixed $value): void
{
$this->expectException(InvalidPointArrayItemForPHPException::class);
$this->fixture->transformArrayItemForPHP($value);
}

#[Test]
#[DataProvider('provideInvalidTypes')]
public function transform_postgres_array_to_php_array_returns_empty_for_invalid_format(mixed $value): void
{
$reflectionObject = new \ReflectionObject($this->fixture);
$reflectionMethod = $reflectionObject->getMethod('transformPostgresArrayToPHPArray');
$reflectionMethod->setAccessible(true);
$this->assertSame([], $reflectionMethod->invoke($this->fixture, $value));
}

public static function provideInvalidTypes(): array
{
return [
[123],
['not-a-point-instance'],
['{invalid}'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Query\Parser;
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\BaseVariadicFunction;
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\InvalidArgumentForVariadicFunctionException;
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\ParserException;
use PHPUnit\Framework\Attributes\Test;

Expand Down Expand Up @@ -38,4 +40,70 @@ public function throws_an_exception_when_lexer_is_not_populated_with_a_lookahead
$reflectionMethod->setAccessible(true);
$reflectionMethod->invoke($baseVariadicFunction, $parser, 'ArithmeticPrimary');
}

#[Test]
public function throws_exception_when_argument_count_is_too_low(): void
{
$function = new class('TEST') extends BaseVariadicFunction {
protected function getFunctionName(): string
{
return 'TEST';
}

protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

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

protected function getMaxArgumentCount(): int
{
return 3;
}
};
$this->expectException(InvalidArgumentForVariadicFunctionException::class);
$reflectionClass = new \ReflectionClass($function);
$reflectionMethod = $reflectionClass->getMethod('validateArguments');
$reflectionMethod->setAccessible(true);

$node = $this->createMock(Node::class);
$reflectionMethod->invoke($function, $node); // 1 argument when min 2 are required
}

#[Test]
public function throws_exception_when_argument_count_is_too_high(): void
{
$function = new class('TEST') extends BaseVariadicFunction {
protected function getFunctionName(): string
{
return 'TEST';
}

protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

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

protected function getMaxArgumentCount(): int
{
return 2;
}
};
$this->expectException(InvalidArgumentForVariadicFunctionException::class);
$reflectionClass = new \ReflectionClass($function);
$reflectionMethod = $reflectionClass->getMethod('validateArguments');
$reflectionMethod->setAccessible(true);

$node = $this->createMock(Node::class);
$reflectionMethod->invoke($function, $node, $node, $node);; // 3 arguments when max 2 are required
}
}
Loading