|
| 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\BaseNetworkTypeArray; |
| 9 | +use PHPUnit\Framework\MockObject\MockObject; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +class BaseNetworkTypeArrayTest extends TestCase |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var AbstractPlatform&MockObject |
| 16 | + */ |
| 17 | + private AbstractPlatform $platform; |
| 18 | + |
| 19 | + private BaseNetworkTypeArray $fixture; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + $this->platform = $this->createMock(AbstractPlatform::class); |
| 24 | + // Create a concrete implementation of the abstract class for testing |
| 25 | + $this->fixture = new class extends BaseNetworkTypeArray { |
| 26 | + protected const TYPE_NAME = 'test_network_array'; |
| 27 | + |
| 28 | + protected function isValidNetworkAddress(string $value): bool |
| 29 | + { |
| 30 | + return $value === 'valid_address'; |
| 31 | + } |
| 32 | + |
| 33 | + protected function throwInvalidTypeException(mixed $value): never |
| 34 | + { |
| 35 | + throw new \InvalidArgumentException('Invalid type'); |
| 36 | + } |
| 37 | + |
| 38 | + protected function throwInvalidFormatException(mixed $value): never |
| 39 | + { |
| 40 | + throw new \InvalidArgumentException('Invalid format'); |
| 41 | + } |
| 42 | + |
| 43 | + protected function throwInvalidItemException(): never |
| 44 | + { |
| 45 | + throw new \InvalidArgumentException('Invalid item'); |
| 46 | + } |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @test |
| 52 | + */ |
| 53 | + public function has_name(): void |
| 54 | + { |
| 55 | + self::assertEquals('test_network_array', $this->fixture->getName()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @test |
| 60 | + */ |
| 61 | + public function can_convert_null_to_database_value(): void |
| 62 | + { |
| 63 | + self::assertNull($this->fixture->convertToDatabaseValue(null, $this->platform)); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @test |
| 68 | + */ |
| 69 | + public function can_convert_empty_array_to_database_value(): void |
| 70 | + { |
| 71 | + self::assertEquals('{}', $this->fixture->convertToDatabaseValue([], $this->platform)); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @test |
| 76 | + */ |
| 77 | + public function can_convert_valid_array_to_database_value(): void |
| 78 | + { |
| 79 | + self::assertEquals( |
| 80 | + '{"valid_address","valid_address"}', |
| 81 | + $this->fixture->convertToDatabaseValue(['valid_address', 'valid_address'], $this->platform) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @test |
| 87 | + */ |
| 88 | + public function throws_exception_for_invalid_type(): void |
| 89 | + { |
| 90 | + $this->expectException(\InvalidArgumentException::class); |
| 91 | + $this->expectExceptionMessage('Invalid type'); |
| 92 | + $this->fixture->convertToDatabaseValue('not_an_array', $this->platform); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @test |
| 97 | + */ |
| 98 | + public function can_convert_null_to_php_value(): void |
| 99 | + { |
| 100 | + self::assertNull($this->fixture->convertToPHPValue(null, $this->platform)); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @test |
| 105 | + */ |
| 106 | + public function can_convert_empty_array_to_php_value(): void |
| 107 | + { |
| 108 | + self::assertEquals([], $this->fixture->convertToPHPValue('{}', $this->platform)); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @test |
| 113 | + */ |
| 114 | + public function can_convert_valid_string_to_php_value(): void |
| 115 | + { |
| 116 | + self::assertEquals( |
| 117 | + ['valid_address', 'valid_address'], |
| 118 | + $this->fixture->convertToPHPValue('{"valid_address","valid_address"}', $this->platform) |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
0 commit comments