Skip to content

Commit ed9319c

Browse files
no message
1 parent 0fab327 commit ed9319c

File tree

9 files changed

+200
-67
lines changed

9 files changed

+200
-67
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public static function provideValidTransformations(): array
7979
];
8080
}
8181

82-
#[DataProvider('provideInvalidPHPValuesForDatabaseTransformation')]
82+
#[DataProvider('provideInvalidDatabaseValueInputs')]
8383
#[Test]
84-
public function throws_exception_when_invalid_data_provided_to_convert_to_database_value(mixed $phpValue): void
84+
public function throws_exception_for_invalid_database_value_inputs(mixed $phpValue): void
8585
{
8686
$this->expectException(InvalidCidrArrayItemForPHPException::class);
8787
$this->fixture->convertToDatabaseValue($phpValue, $this->platform); // @phpstan-ignore-line
@@ -90,7 +90,7 @@ public function throws_exception_when_invalid_data_provided_to_convert_to_databa
9090
/**
9191
* @return array<string, array{mixed}>
9292
*/
93-
public static function provideInvalidPHPValuesForDatabaseTransformation(): array
93+
public static function provideInvalidDatabaseValueInputs(): array
9494
{
9595
return [
9696
'invalid type' => ['not-an-array'],
@@ -103,12 +103,16 @@ public static function provideInvalidPHPValuesForDatabaseTransformation(): array
103103
'empty string' => [['']], // Empty string in array
104104
'whitespace only' => [[' ']], // Whitespace string in array
105105
'malformed CIDR with spaces' => [['192.168.1.0 / 24']], // Space in CIDR notation
106+
'valid value mixed with null array item' => [['192.168.1.0/24', null]],
107+
'valid value mixed with integer array item' => [['192.168.1.0/24', 123]],
108+
'valid value mixed with boolean array item' => [['192.168.1.0/24', true]],
109+
'valid value mixed with object array item' => [['192.168.1.0/24', new \stdClass()]],
106110
];
107111
}
108112

109-
#[DataProvider('provideInvalidDatabaseValuesForPHPTransformation')]
113+
#[DataProvider('provideInvalidPHPValueInputs')]
110114
#[Test]
111-
public function throws_exception_when_invalid_data_provided_to_convert_to_php_value(string $postgresValue): void
115+
public function throws_exception_for_invalid_php_value_inputs(string $postgresValue): void
112116
{
113117
$this->expectException(InvalidCidrArrayItemForPHPException::class);
114118
$this->fixture->convertToPHPValue($postgresValue, $this->platform);
@@ -117,7 +121,7 @@ public function throws_exception_when_invalid_data_provided_to_convert_to_php_va
117121
/**
118122
* @return array<string, array{string}>
119123
*/
120-
public static function provideInvalidDatabaseValuesForPHPTransformation(): array
124+
public static function provideInvalidPHPValueInputs(): array
121125
{
122126
return [
123127
'invalid format' => ['{"invalid-cidr"}'],

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ public static function provideValidTransformations(): array
9393
];
9494
}
9595

96-
#[DataProvider('provideInvalidPHPValuesForDatabaseTransformation')]
96+
#[DataProvider('provideInvalidDatabaseValueInputs')]
9797
#[Test]
98-
public function throws_exception_when_invalid_data_provided_to_convert_to_database_value(mixed $phpValue): void
98+
public function throws_exception_for_invalid_database_value_inputs(mixed $phpValue): void
9999
{
100100
$this->expectException(InvalidCidrForPHPException::class);
101101
$this->fixture->convertToDatabaseValue($phpValue, $this->platform);
@@ -104,7 +104,7 @@ public function throws_exception_when_invalid_data_provided_to_convert_to_databa
104104
/**
105105
* @return array<string, array{mixed}>
106106
*/
107-
public static function provideInvalidPHPValuesForDatabaseTransformation(): array
107+
public static function provideInvalidDatabaseValueInputs(): array
108108
{
109109
return [
110110
'empty string' => [''],
@@ -124,9 +124,9 @@ public static function provideInvalidPHPValuesForDatabaseTransformation(): array
124124
];
125125
}
126126

127-
#[DataProvider('provideInvalidDatabaseValuesForPHPTransformation')]
127+
#[DataProvider('provideInvalidPHPValueInputs')]
128128
#[Test]
129-
public function throws_exception_when_invalid_data_provided_to_convert_to_php_value(mixed $dbValue): void
129+
public function throws_exception_for_invalid_php_value_inputs(mixed $dbValue): void
130130
{
131131
$this->expectException(InvalidCidrForDatabaseException::class);
132132
$this->fixture->convertToPHPValue($dbValue, $this->platform);
@@ -135,7 +135,7 @@ public function throws_exception_when_invalid_data_provided_to_convert_to_php_va
135135
/**
136136
* @return array<string, array{mixed}>
137137
*/
138-
public static function provideInvalidDatabaseValuesForPHPTransformation(): array
138+
public static function provideInvalidPHPValueInputs(): array
139139
{
140140
return [
141141
'invalid type' => [123],

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ public static function provideValidTransformations(): array
9999
];
100100
}
101101

102-
#[DataProvider('provideInvalidPHPValuesForDatabaseTransformation')]
102+
#[DataProvider('provideInvalidDatabaseValueInputs')]
103103
#[Test]
104-
public function throws_exception_when_invalid_data_provided_to_convert_to_database_value(mixed $phpValue): void
104+
public function throws_exception_for_invalid_database_value_inputs(mixed $phpValue): void
105105
{
106106
$this->expectException(InvalidInetArrayItemForPHPException::class);
107107
$this->fixture->convertToDatabaseValue($phpValue, $this->platform); // @phpstan-ignore-line
@@ -110,7 +110,7 @@ public function throws_exception_when_invalid_data_provided_to_convert_to_databa
110110
/**
111111
* @return array<string, array{mixed}>
112112
*/
113-
public static function provideInvalidPHPValuesForDatabaseTransformation(): array
113+
public static function provideInvalidDatabaseValueInputs(): array
114114
{
115115
return [
116116
'invalid type' => ['not-an-array'],
@@ -131,9 +131,9 @@ public static function provideInvalidPHPValuesForDatabaseTransformation(): array
131131
];
132132
}
133133

134-
#[DataProvider('provideInvalidDatabaseValuesForPHPTransformation')]
134+
#[DataProvider('provideInvalidPHPValueInputs')]
135135
#[Test]
136-
public function throws_exception_when_invalid_data_provided_to_convert_to_php_value(string $postgresValue): void
136+
public function throws_exception_for_invalid_php_value_inputs(string $postgresValue): void
137137
{
138138
$this->expectException(InvalidInetArrayItemForPHPException::class);
139139

@@ -143,7 +143,7 @@ public function throws_exception_when_invalid_data_provided_to_convert_to_php_va
143143
/**
144144
* @return array<string, array{string}>
145145
*/
146-
public static function provideInvalidDatabaseValuesForPHPTransformation(): array
146+
public static function provideInvalidPHPValueInputs(): array
147147
{
148148
return [
149149
'invalid format' => ['{"invalid-ip"}'],

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ public static function provideValidTransformations(): array
101101
];
102102
}
103103

104-
#[DataProvider('provideInvalidPHPValuesForDatabaseTransformation')]
104+
#[DataProvider('provideInvalidDatabaseValueInputs')]
105105
#[Test]
106-
public function throws_exception_when_invalid_data_provided_to_convert_to_database_value(mixed $phpValue): void
106+
public function throws_exception_for_invalid_database_value_inputs(mixed $phpValue): void
107107
{
108108
$this->expectException(InvalidInetForPHPException::class);
109109
$this->fixture->convertToDatabaseValue($phpValue, $this->platform);
@@ -112,7 +112,7 @@ public function throws_exception_when_invalid_data_provided_to_convert_to_databa
112112
/**
113113
* @return array<string, array{mixed}>
114114
*/
115-
public static function provideInvalidPHPValuesForDatabaseTransformation(): array
115+
public static function provideInvalidDatabaseValueInputs(): array
116116
{
117117
return [
118118
'non-string value' => [123],
@@ -130,12 +130,15 @@ public static function provideInvalidPHPValuesForDatabaseTransformation(): array
130130
'IPv4 with invalid octet count' => ['192.168.1'],
131131
'IPv4 with character suffix' => ['192.168.1.1x'],
132132
'malformed IPv4-mapped IPv6' => ['::ffff:256.256.256.256'],
133+
'array input' => [['not', 'ip']],
134+
'boolean input' => [true],
135+
'object input' => [new \stdClass()],
133136
];
134137
}
135138

136-
#[DataProvider('provideInvalidDatabaseValuesForPHPTransformation')]
139+
#[DataProvider('provideInvalidPHPValueInputs')]
137140
#[Test]
138-
public function throws_exception_when_invalid_data_provided_to_convert_to_php_value(mixed $postgresValue): void
141+
public function throws_exception_for_invalid_php_value_inputs(mixed $postgresValue): void
139142
{
140143
$this->expectException(InvalidInetForDatabaseException::class);
141144
$this->fixture->convertToPHPValue($postgresValue, $this->platform);
@@ -144,7 +147,7 @@ public function throws_exception_when_invalid_data_provided_to_convert_to_php_va
144147
/**
145148
* @return array<string, array{mixed}>
146149
*/
147-
public static function provideInvalidDatabaseValuesForPHPTransformation(): array
150+
public static function provideInvalidPHPValueInputs(): array
148151
{
149152
return [
150153
'non-string value' => [123],
@@ -161,6 +164,9 @@ public static function provideInvalidDatabaseValuesForPHPTransformation(): array
161164
'IPv4 with invalid octet count' => ['192.168.1'],
162165
'IPv4 with character suffix' => ['192.168.1.1x'],
163166
'malformed IPv4-mapped IPv6' => ['::ffff:256.256.256.256'],
167+
'array input' => [['not', 'ip']],
168+
'boolean input' => [false],
169+
'object input' => [new \stdClass()],
164170
];
165171
}
166172
}

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

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Tests\Unit\MartinGeorgiev\Doctrine\DBAL\Types;
66

77
use Doctrine\DBAL\Platforms\AbstractPlatform;
8+
use Doctrine\DBAL\Types\ConversionException;
9+
use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidJsonItemForPHPException;
810
use MartinGeorgiev\Doctrine\DBAL\Types\Jsonb;
911
use PHPUnit\Framework\Attributes\DataProvider;
1012
use PHPUnit\Framework\Attributes\Test;
@@ -48,43 +50,43 @@ public function can_transform_to_php_value(null|array|bool|float|int|string $php
4850
}
4951

5052
/**
51-
* @return list<array{
53+
* @return array<string, array{
5254
* phpValue: array|bool|float|int|string|null,
5355
* postgresValue: string|null
5456
* }>
5557
*/
5658
public static function provideValidTransformations(): array
5759
{
5860
return [
59-
[
61+
'null value' => [
6062
'phpValue' => null,
6163
'postgresValue' => null,
6264
],
63-
[
65+
'boolean true' => [
6466
'phpValue' => true,
6567
'postgresValue' => 'true',
6668
],
67-
[
69+
'empty array' => [
6870
'phpValue' => [],
6971
'postgresValue' => '[]',
7072
],
71-
[
73+
'integer value' => [
7274
'phpValue' => 13,
7375
'postgresValue' => '13',
7476
],
75-
[
77+
'float value' => [
7678
'phpValue' => 13.93,
7779
'postgresValue' => '13.93',
7880
],
79-
[
81+
'string value' => [
8082
'phpValue' => 'a string value',
8183
'postgresValue' => '"a string value"',
8284
],
83-
[
85+
'numeric array' => [
8486
'phpValue' => [681, 1185, 1878, 1989],
8587
'postgresValue' => '[681,1185,1878,1989]',
8688
],
87-
[
89+
'complex object' => [
8890
'phpValue' => [
8991
'key1' => 'value1',
9092
'key2' => false,
@@ -96,4 +98,78 @@ public static function provideValidTransformations(): array
9698
],
9799
];
98100
}
101+
102+
#[Test]
103+
public function throws_exception_for_non_encodable_value(): void
104+
{
105+
$resourceThatCannotBeJsonEncoded = \fopen('php://memory', 'r');
106+
107+
$this->expectException(ConversionException::class);
108+
$this->expectExceptionMessage("can't be resolved to valid JSON");
109+
110+
try {
111+
$this->fixture->convertToDatabaseValue($resourceThatCannotBeJsonEncoded, $this->platform);
112+
} finally {
113+
\fclose($resourceThatCannotBeJsonEncoded);
114+
}
115+
}
116+
117+
#[Test]
118+
public function throws_exception_for_circular_reference(): void
119+
{
120+
// Create a circular reference
121+
$object1 = new \stdClass();
122+
$object2 = new \stdClass();
123+
$object1->reference = $object2;
124+
$object2->reference = $object1;
125+
126+
// Set up a custom error handler to expect the warning
127+
$warningTriggered = false;
128+
\set_error_handler(static function ($error, $errorText) use (&$warningTriggered): bool {
129+
if ($error === E_WARNING && \str_contains($errorText, 'var_export does not handle circular references')) {
130+
$warningTriggered = true;
131+
132+
return true; // Suppress the circular reference warning
133+
}
134+
135+
return false; // Let other errors/warnings through
136+
});
137+
138+
$this->expectException(ConversionException::class);
139+
$this->expectExceptionMessage("can't be resolved to valid JSON");
140+
141+
try {
142+
$this->fixture->convertToDatabaseValue($object1, $this->platform);
143+
} finally {
144+
\restore_error_handler();
145+
}
146+
147+
$this->assertTrue($warningTriggered, 'Expected warning about circular references was not triggered');
148+
}
149+
150+
#[DataProvider('provideInvalidJsonStrings')]
151+
#[Test]
152+
public function throws_exception_for_invalid_json_strings(string $invalidJson): void
153+
{
154+
$this->expectException(InvalidJsonItemForPHPException::class);
155+
$this->expectExceptionMessage('Postgres value must be single, valid JSON object');
156+
157+
$this->fixture->convertToPHPValue($invalidJson, $this->platform);
158+
}
159+
160+
/**
161+
* @return array<string, array{string}>
162+
*/
163+
public static function provideInvalidJsonStrings(): array
164+
{
165+
return [
166+
'invalid json syntax' => ['{invalid json}'],
167+
'empty string' => [''],
168+
'malformed json' => ['{"key": value}'],
169+
'incomplete json' => ['{"key":'],
170+
'trailing comma' => ['{"key": "value",}'],
171+
'unquoted keys' => ['{key: "value"}'],
172+
'single quotes' => ["{'key': 'value'}"],
173+
];
174+
}
99175
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public static function provideValidTransformations(): array
8383
];
8484
}
8585

86-
#[DataProvider('provideInvalidPHPValuesForDatabaseTransformation')]
86+
#[DataProvider('provideInvalidDatabaseValueInputs')]
8787
#[Test]
88-
public function throws_exception_when_invalid_data_provided_to_convert_to_database_value(mixed $phpValue): void
88+
public function throws_exception_for_invalid_database_value_inputs(mixed $phpValue): void
8989
{
9090
$this->expectException(InvalidMacaddrArrayItemForPHPException::class);
9191
$this->fixture->convertToDatabaseValue($phpValue, $this->platform); // @phpstan-ignore-line
@@ -94,7 +94,7 @@ public function throws_exception_when_invalid_data_provided_to_convert_to_databa
9494
/**
9595
* @return array<string, array{mixed}>
9696
*/
97-
public static function provideInvalidPHPValuesForDatabaseTransformation(): array
97+
public static function provideInvalidDatabaseValueInputs(): array
9898
{
9999
return [
100100
'invalid type' => ['not-an-array'],
@@ -112,9 +112,9 @@ public static function provideInvalidPHPValuesForDatabaseTransformation(): array
112112
];
113113
}
114114

115-
#[DataProvider('provideInvalidDatabaseValuesForPHPTransformationForPHPTransformation')]
115+
#[DataProvider('provideInvalidPHPValueInputs')]
116116
#[Test]
117-
public function throws_exception_when_invalid_data_provided_to_convert_to_php_value(string $postgresValue): void
117+
public function throws_exception_for_invalid_php_value_inputs(string $postgresValue): void
118118
{
119119
$this->expectException(InvalidMacaddrArrayItemForPHPException::class);
120120

@@ -124,7 +124,7 @@ public function throws_exception_when_invalid_data_provided_to_convert_to_php_va
124124
/**
125125
* @return array<string, array{string}>
126126
*/
127-
public static function provideInvalidDatabaseValuesForPHPTransformationForPHPTransformation(): array
127+
public static function provideInvalidPHPValueInputs(): array
128128
{
129129
return [
130130
'invalid format' => ['{"invalid-mac"}'],

0 commit comments

Comments
 (0)