|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\MartinGeorgiev\Doctrine\DBAL\Types; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 8 | +use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidPointArrayItemForPHPException; |
| 9 | +use MartinGeorgiev\Doctrine\DBAL\Types\PointArray; |
| 10 | +use MartinGeorgiev\ValueObject\Point as PointValueObject; |
| 11 | +use PHPUnit\Framework\MockObject\MockObject; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +class PointArrayTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var AbstractPlatform&MockObject |
| 18 | + */ |
| 19 | + private AbstractPlatform $platform; |
| 20 | + |
| 21 | + private PointArray $fixture; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + $this->platform = $this->createMock(AbstractPlatform::class); |
| 26 | + $this->fixture = new PointArray(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @test |
| 31 | + */ |
| 32 | + public function has_name(): void |
| 33 | + { |
| 34 | + self::assertEquals('point[]', $this->fixture->getName()); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @test |
| 39 | + * |
| 40 | + * @dataProvider provideValidTransformations |
| 41 | + */ |
| 42 | + public function can_transform_from_php_value(?array $phpValue, ?string $postgresValue): void |
| 43 | + { |
| 44 | + self::assertEquals($postgresValue, $this->fixture->convertToDatabaseValue($phpValue, $this->platform)); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @test |
| 49 | + * |
| 50 | + * @dataProvider provideValidTransformations |
| 51 | + */ |
| 52 | + public function can_transform_to_php_value(?array $phpValue, ?string $postgresValue): void |
| 53 | + { |
| 54 | + self::assertEquals($phpValue, $this->fixture->convertToPHPValue($postgresValue, $this->platform)); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return array<string, array{phpValue: array<PointValueObject>|null, postgresValue: string|null}> |
| 59 | + */ |
| 60 | + public static function provideValidTransformations(): array |
| 61 | + { |
| 62 | + return [ |
| 63 | + 'null' => [ |
| 64 | + 'phpValue' => null, |
| 65 | + 'postgresValue' => null, |
| 66 | + ], |
| 67 | + 'empty array' => [ |
| 68 | + 'phpValue' => [], |
| 69 | + 'postgresValue' => '{}', |
| 70 | + ], |
| 71 | + 'single point' => [ |
| 72 | + 'phpValue' => [new PointValueObject(1.23, 4.56)], |
| 73 | + 'postgresValue' => '{"(1.230000, 4.560000)"}', |
| 74 | + ], |
| 75 | + 'multiple points' => [ |
| 76 | + 'phpValue' => [ |
| 77 | + new PointValueObject(1.23, 4.56), |
| 78 | + new PointValueObject(-7.89, 0.12), |
| 79 | + ], |
| 80 | + 'postgresValue' => '{"(1.230000, 4.560000)","(-7.890000, 0.120000)"}', |
| 81 | + ], |
| 82 | + 'points with zero values' => [ |
| 83 | + 'phpValue' => [ |
| 84 | + new PointValueObject(0.0, 0.0), |
| 85 | + new PointValueObject(10.5, -3.7), |
| 86 | + ], |
| 87 | + 'postgresValue' => '{"(0.000000, 0.000000)","(10.500000, -3.700000)"}', |
| 88 | + ], |
| 89 | + ]; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @test |
| 94 | + * |
| 95 | + * @dataProvider provideInvalidTransformations |
| 96 | + */ |
| 97 | + public function throws_exception_when_invalid_data_provided_to_convert_to_database_value(mixed $phpValue): void |
| 98 | + { |
| 99 | + $this->expectException(InvalidPointArrayItemForPHPException::class); |
| 100 | + $this->fixture->convertToDatabaseValue($phpValue, $this->platform); // @phpstan-ignore-line |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @return array<string, array{mixed}> |
| 105 | + */ |
| 106 | + public static function provideInvalidTransformations(): array |
| 107 | + { |
| 108 | + return [ |
| 109 | + 'not an array' => ['string value'], |
| 110 | + 'array containing non-Point items' => [[1, 2, 3]], |
| 111 | + 'invalid nested point' => [['(1.23,4.56)']], |
| 112 | + 'mixed array (valid and invalid points)' => [ |
| 113 | + [ |
| 114 | + new PointValueObject(1.23, 4.56), |
| 115 | + 'invalid', |
| 116 | + ], |
| 117 | + ], |
| 118 | + ]; |
| 119 | + } |
| 120 | +} |
0 commit comments