|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Integration\MartinGeorgiev\Utils; |
| 6 | + |
| 7 | +use MartinGeorgiev\Utils\Exception\InvalidArrayFormatException; |
| 8 | +use MartinGeorgiev\Utils\PostgresArrayToPHPArrayTransformer; |
| 9 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 10 | +use Tests\Integration\MartinGeorgiev\TestCase; |
| 11 | + |
| 12 | +class PostgresArrayToPHPArrayTransformerTest extends TestCase |
| 13 | +{ |
| 14 | + private const TABLE_NAME = 'array_test_table'; |
| 15 | + |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + parent::setUp(); |
| 19 | + $this->createTestTable(); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @return array<int, array{0: array{description: string, input: array<int, string>}}> |
| 24 | + */ |
| 25 | + public static function provideArrayTestCases(): array |
| 26 | + { |
| 27 | + return [ |
| 28 | + [['description' => 'Simple array', 'input' => ['hello', 'world']]], |
| 29 | + [['description' => 'Empty array', 'input' => []]], |
| 30 | + [['description' => 'Single empty string', 'input' => ['']]], |
| 31 | + [['description' => 'Quotes and backslashes', 'input' => ['"quoted"', 'back\\slash']]], |
| 32 | + [['description' => 'Windows paths', 'input' => ['C:\\Windows\\System32']]], |
| 33 | + [['description' => 'Escaped quotes', 'input' => ['\"escaped\"']]], |
| 34 | + [['description' => 'Double backslashes', 'input' => ['\\\\double\\\\']]], |
| 35 | + [['description' => 'Unicode', 'input' => ['Hello 世界', '🌍 Earth']]], |
| 36 | + [['description' => 'Special chars', 'input' => ['!@#$%^&*()_+=-}{[]|":;\'?><,./']]], |
| 37 | + [['description' => 'GitHub #351 - regression #1', 'input' => ['⥀!@#$%^&*()_+=-}{[]|":;\'\?><,./']]], |
| 38 | + [['description' => 'GitHub #351 - regression #2', 'input' => ['⥀!@#$%^&*()_+=-}{[]|":;\'\?><,./', 'text']]], |
| 39 | + [['description' => 'Curly braces', 'input' => ['{foo,bar}']]], |
| 40 | + [['description' => 'Spaces', 'input' => [' spaces ']]], |
| 41 | + [['description' => 'Trailing backslash', 'input' => ['trailing\\']]], |
| 42 | + [['description' => 'Leading backslash', 'input' => ['\\leading']]], |
| 43 | + [['description' => 'Mixed', 'input' => ['simple', '"quoted"', 'back\\slash', '']]], |
| 44 | + ]; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param array{description: string, input: array<int, string>} $testCase |
| 49 | + */ |
| 50 | + #[DataProvider('provideArrayTestCases')] |
| 51 | + public function test_array_round_trip(array $testCase): void |
| 52 | + { |
| 53 | + $id = $this->insertArray($testCase['input']); |
| 54 | + |
| 55 | + $this->assertArrayRoundTrip($id, $testCase['input'], $testCase['description']); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return array<int, array{0: array{description: string, input: string}}> |
| 60 | + */ |
| 61 | + public static function provideInvalidArrayFormats(): array |
| 62 | + { |
| 63 | + return [ |
| 64 | + [['description' => 'Multi-dimensional', 'input' => '{{1,2},{3,4}}']], |
| 65 | + [['description' => 'Unclosed quote', 'input' => '{1,2,"unclosed']], |
| 66 | + [['description' => 'Invalid format', 'input' => '{invalid"format}']], |
| 67 | + [['description' => 'Malformed nesting', 'input' => '{1,{2,3},4}']], |
| 68 | + ]; |
| 69 | + } |
| 70 | + |
| 71 | + #[DataProvider('provideInvalidArrayFormats')] |
| 72 | + public function test_invalid_array_formats_throw_exceptions(array $testCase): void |
| 73 | + { |
| 74 | + $this->expectException(InvalidArrayFormatException::class); |
| 75 | + PostgresArrayToPHPArrayTransformer::transformPostgresArrayToPHPArray($testCase['input']); // @phpstan-ignore-line |
| 76 | + } |
| 77 | + |
| 78 | + private function createTestTable(): void |
| 79 | + { |
| 80 | + $this->dropTestTableIfItExists(self::TABLE_NAME); |
| 81 | + $this->connection->executeStatement(\sprintf(' |
| 82 | + CREATE TABLE %s ( |
| 83 | + id SERIAL PRIMARY KEY, |
| 84 | + test_array TEXT[] |
| 85 | + ) |
| 86 | + ', self::TABLE_NAME)); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @template T |
| 91 | + * |
| 92 | + * @param array<string, mixed> $params |
| 93 | + * @param callable(string): T $transform |
| 94 | + * |
| 95 | + * @return T |
| 96 | + */ |
| 97 | + private function retrieveFromDatabase(string $sql, array $params, callable $transform): mixed |
| 98 | + { |
| 99 | + $row = $this->connection->executeQuery($sql, $params)->fetchAssociative(); |
| 100 | + |
| 101 | + if ($row === false || !isset($row['test_array']) || !\is_string($row['test_array'])) { |
| 102 | + throw new \RuntimeException('Failed to retrieve array data'); |
| 103 | + } |
| 104 | + |
| 105 | + return $transform($row['test_array']); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return array<int, string> |
| 110 | + */ |
| 111 | + private function retrieveArray(int $id): array |
| 112 | + { |
| 113 | + $row = $this->connection->executeQuery( |
| 114 | + \sprintf('SELECT test_array FROM %s WHERE id = :id', self::TABLE_NAME), |
| 115 | + ['id' => $id] |
| 116 | + )->fetchAssociative(); |
| 117 | + |
| 118 | + if ($row === false || !isset($row['test_array'])) { |
| 119 | + throw new \RuntimeException(\sprintf('Failed to retrieve array data for ID %d', $id)); |
| 120 | + } |
| 121 | + |
| 122 | + if (!\is_string($row['test_array'])) { |
| 123 | + throw new \RuntimeException(\sprintf('Expected string for test_array, got %s', \gettype($row['test_array']))); |
| 124 | + } |
| 125 | + |
| 126 | + /** @var string $postgresArray */ |
| 127 | + $postgresArray = $row['test_array']; |
| 128 | + |
| 129 | + return PostgresArrayToPHPArrayTransformer::transformPostgresArrayToPHPArray($postgresArray); // @phpstan-ignore-line |
| 130 | + } |
| 131 | + |
| 132 | + private function retrieveArrayAsText(int $id): string |
| 133 | + { |
| 134 | + /** @var string $result */ |
| 135 | + $result = $this->retrieveFromDatabase( |
| 136 | + \sprintf('SELECT test_array::text FROM %s WHERE id = :id', self::TABLE_NAME), |
| 137 | + ['id' => $id], |
| 138 | + static fn (string $value): string => $value |
| 139 | + ); |
| 140 | + |
| 141 | + return $result; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @param array<int, string> $arrayData |
| 146 | + */ |
| 147 | + private function insertArray(array $arrayData): int |
| 148 | + { |
| 149 | + $result = $this->connection->executeQuery( |
| 150 | + \sprintf('INSERT INTO %s (test_array) VALUES (:arrayData) RETURNING id', self::TABLE_NAME), |
| 151 | + ['arrayData' => $arrayData], |
| 152 | + ['arrayData' => 'text[]'] |
| 153 | + ); |
| 154 | + |
| 155 | + $row = $result->fetchAssociative(); |
| 156 | + if ($row === false || !isset($row['id']) || !\is_numeric($row['id'])) { |
| 157 | + throw new \RuntimeException('Failed to insert array data'); |
| 158 | + } |
| 159 | + |
| 160 | + return (int) $row['id']; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * @param array<int, string> $expected |
| 165 | + */ |
| 166 | + private function assertArrayRoundTrip(int $id, array $expected, string $description): void |
| 167 | + { |
| 168 | + // Test direct retrieval |
| 169 | + $retrieved = $this->retrieveArray($id); |
| 170 | + self::assertEquals( |
| 171 | + $expected, |
| 172 | + $retrieved, |
| 173 | + \sprintf('Direct retrieval failed for %s', $description) |
| 174 | + ); |
| 175 | + |
| 176 | + // Test text representation |
| 177 | + $postgresText = $this->retrieveArrayAsText($id); |
| 178 | + $parsed = PostgresArrayToPHPArrayTransformer::transformPostgresArrayToPHPArray($postgresText); |
| 179 | + self::assertEquals( |
| 180 | + $expected, |
| 181 | + $parsed, |
| 182 | + \sprintf('Text representation parsing failed for %s', $description) |
| 183 | + ); |
| 184 | + } |
| 185 | +} |
0 commit comments