Skip to content

Commit 5dc36dc

Browse files
chore: improve test coverage and standardize style of unit tests in DBAL\Types namespace (#404)
1 parent 8d18b35 commit 5dc36dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+462
-184
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function provideValidTransformations(): array
7777
}
7878

7979
#[Test]
80-
public function throws_invalid_argument_exception_when_php_value_is_not_array(): void
80+
public function throws_exception_when_php_value_is_not_array(): void
8181
{
8282
$this->expectException(\InvalidArgumentException::class);
8383
$this->expectExceptionMessageMatches('/Given PHP value content type is not PHP array. Instead it is "\w+"./');

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract class BaseFloatArrayTestCase extends TestCase
1414
{
1515
protected BaseFloatArray $fixture;
1616

17-
#[DataProvider('provideInvalidPHPValuesForDatabaseTransformation')]
17+
#[DataProvider('provideInvalidDatabaseValueInputs')]
1818
#[Test]
1919
public function can_detect_invalid_for_transformation_php_value(mixed $phpValue): void
2020
{
@@ -24,7 +24,7 @@ public function can_detect_invalid_for_transformation_php_value(mixed $phpValue)
2424
/**
2525
* @return list<mixed>
2626
*/
27-
public static function provideInvalidPHPValuesForDatabaseTransformation(): array
27+
public static function provideInvalidDatabaseValueInputs(): array
2828
{
2929
return [
3030
[true],

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract class BaseIntegerArrayTestCase extends TestCase
1414
{
1515
protected BaseIntegerArray $fixture;
1616

17-
#[DataProvider('provideInvalidPHPValuesForDatabaseTransformation')]
17+
#[DataProvider('provideInvalidDatabaseValueInputs')]
1818
#[Test]
1919
public function can_detect_invalid_for_transformation_php_value(mixed $phpValue): void
2020
{
@@ -24,7 +24,7 @@ public function can_detect_invalid_for_transformation_php_value(mixed $phpValue)
2424
/**
2525
* @return list<mixed>
2626
*/
27-
public static function provideInvalidPHPValuesForDatabaseTransformation(): array
27+
public static function provideInvalidDatabaseValueInputs(): array
2828
{
2929
return [
3030
[true],

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function has_name(): void
5757

5858
#[Test]
5959
#[DataProvider('provideValidTransformations')]
60-
public function can_convert_to_php_value(?array $phpValue, ?string $postgresValue): void
60+
public function can_transform_to_php_value(?array $phpValue, ?string $postgresValue): void
6161
{
6262
self::assertEquals($phpValue, $this->fixture->convertToPHPValue($postgresValue, $this->platform));
6363
}
@@ -112,7 +112,7 @@ public static function provideInvalidValues(): array
112112
}
113113

114114
#[Test]
115-
public function transform_array_item_for_php_handles_valid_string(): void
115+
public function can_transform_array_item_for_php_with_valid_string(): void
116116
{
117117
$this->assertSame('valid_address', $this->fixture->transformArrayItemForPHP('"valid_address"'));
118118
}

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,48 @@ public function can_handle_empty_string_from_sql(): void
108108
self::assertNull($result);
109109
}
110110

111+
#[DataProvider('provideInvalidDatabaseValues')]
111112
#[Test]
112-
public function throws_exception_for_invalid_php_value_type(): void
113+
public function throws_exception_for_invalid_database_value_inputs(mixed $invalidValue): void
113114
{
114115
$this->expectException(InvalidRangeForDatabaseException::class);
115116
$this->expectExceptionMessage('Invalid type for range');
116117

117-
$this->fixture->convertToDatabaseValue('invalid', $this->platform); // @phpstan-ignore-line argument.type
118+
$this->fixture->convertToDatabaseValue($invalidValue, $this->platform); // @phpstan-ignore-line argument.type
118119
}
119120

121+
/**
122+
* @return array<string, array{mixed}>
123+
*/
124+
public static function provideInvalidDatabaseValues(): array
125+
{
126+
return [
127+
'string value' => ['not_a_range'],
128+
'integer value' => [123],
129+
'array value' => [[1, 2, 3]],
130+
];
131+
}
132+
133+
#[DataProvider('provideInvalidPHPValues')]
120134
#[Test]
121-
public function throws_exception_for_invalid_sql_value_type(): void
135+
public function throws_exception_for_invalid_php_value_inputs(mixed $invalidValue): void
122136
{
123137
$this->expectException(InvalidRangeForPHPException::class);
124138
$this->expectExceptionMessage('Invalid database value type for range conversion');
125139

126-
$this->fixture->convertToPHPValue([1, 2], $this->platform); // @phpstan-ignore-line argument.type
140+
$this->fixture->convertToPHPValue($invalidValue, $this->platform); // @phpstan-ignore-line argument.type
141+
}
142+
143+
/**
144+
* @return array<string, array{mixed}>
145+
*/
146+
public static function provideInvalidPHPValues(): array
147+
{
148+
return [
149+
'integer value' => [123],
150+
'array value' => [['invalid']],
151+
'boolean value' => [true],
152+
];
127153
}
128154

129155
#[Test]

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function throws_exception_when_getting_sql_declaration_with_no_type_name(
4949
}
5050

5151
#[Test]
52-
public function returns_correct_type_name(): void
52+
public function can_return_correct_type_name(): void
5353
{
5454
$type = new class extends BaseType {
5555
protected const TYPE_NAME = 'custom_type';
@@ -59,7 +59,7 @@ public function returns_correct_type_name(): void
5959
}
6060

6161
#[Test]
62-
public function gets_correct_sql_declaration(): void
62+
public function can_get_correct_sql_declaration(): void
6363
{
6464
$type = new class extends BaseType {
6565
protected const TYPE_NAME = 'custom_type';
@@ -76,7 +76,7 @@ public function gets_correct_sql_declaration(): void
7676
}
7777

7878
#[Test]
79-
public function requires_sql_comment_hint_returns_false(): void
79+
public function can_return_false_for_sql_comment_hint_requirement(): void
8080
{
8181
$type = new class extends BaseType {
8282
protected const TYPE_NAME = 'custom_type';

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public function has_name(): void
2222
self::assertEquals('bigint[]', $this->fixture->getName());
2323
}
2424

25-
public static function provideInvalidPHPValuesForDatabaseTransformation(): array
25+
public static function provideInvalidDatabaseValueInputs(): array
2626
{
27-
return \array_merge(parent::provideInvalidPHPValuesForDatabaseTransformation(), [
27+
return \array_merge(parent::provideInvalidDatabaseValueInputs(), [
2828
['9223372036854775808'], // Greater than PHP_INT_MAX
2929
['-9223372036854775809'], // Less than PHP_INT_MIN
3030
['1.23e10'], // Scientific notation
@@ -33,7 +33,10 @@ public static function provideInvalidPHPValuesForDatabaseTransformation(): array
3333
}
3434

3535
/**
36-
* @return array<int, array{phpValue: int, postgresValue: string}>
36+
* @return list<array{
37+
* phpValue: int,
38+
* postgresValue: string
39+
* }>
3740
*/
3841
public static function provideValidTransformations(): array
3942
{
@@ -76,7 +79,7 @@ public function throws_domain_exception_when_value_exceeds_range(string $outOfRa
7679
}
7780

7881
/**
79-
* @return array<array{string}>
82+
* @return list<array{string}>
8083
*/
8184
public static function provideOutOfRangeValues(): array
8285
{

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ public function can_transform_to_php_value(?array $phpValue, ?string $postgresVa
4848
}
4949

5050
/**
51-
* @return array<string, array{phpValue: array|null, postgresValue: string|null}>
51+
* @return array<string, array{
52+
* phpValue: array|null,
53+
* postgresValue: string|null
54+
* }>
5255
*/
5356
public static function provideValidTransformations(): array
5457
{
@@ -76,9 +79,9 @@ public static function provideValidTransformations(): array
7679
];
7780
}
7881

79-
#[DataProvider('provideInvalidPHPValuesForDatabaseTransformation')]
82+
#[DataProvider('provideInvalidDatabaseValueInputs')]
8083
#[Test]
81-
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
8285
{
8386
$this->expectException(InvalidCidrArrayItemForPHPException::class);
8487
$this->fixture->convertToDatabaseValue($phpValue, $this->platform); // @phpstan-ignore-line
@@ -87,7 +90,7 @@ public function throws_exception_when_invalid_data_provided_to_convert_to_databa
8790
/**
8891
* @return array<string, array{mixed}>
8992
*/
90-
public static function provideInvalidPHPValuesForDatabaseTransformation(): array
93+
public static function provideInvalidDatabaseValueInputs(): array
9194
{
9295
return [
9396
'invalid type' => ['not-an-array'],
@@ -100,12 +103,16 @@ public static function provideInvalidPHPValuesForDatabaseTransformation(): array
100103
'empty string' => [['']], // Empty string in array
101104
'whitespace only' => [[' ']], // Whitespace string in array
102105
'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()]],
103110
];
104111
}
105112

106-
#[DataProvider('provideInvalidDatabaseValuesForPHPTransformationForPHPTransformation')]
113+
#[DataProvider('provideInvalidPHPValueInputs')]
107114
#[Test]
108-
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
109116
{
110117
$this->expectException(InvalidCidrArrayItemForPHPException::class);
111118
$this->fixture->convertToPHPValue($postgresValue, $this->platform);
@@ -114,7 +121,7 @@ public function throws_exception_when_invalid_data_provided_to_convert_to_php_va
114121
/**
115122
* @return array<string, array{string}>
116123
*/
117-
public static function provideInvalidDatabaseValuesForPHPTransformationForPHPTransformation(): array
124+
public static function provideInvalidPHPValueInputs(): array
118125
{
119126
return [
120127
'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/DoublePrecisionArrayTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public function has_name(): void
2323
self::assertEquals('double precision[]', $this->fixture->getName());
2424
}
2525

26-
public static function provideInvalidPHPValuesForDatabaseTransformation(): array
26+
public static function provideInvalidDatabaseValueInputs(): array
2727
{
28-
return \array_merge(parent::provideInvalidPHPValuesForDatabaseTransformation(), [
28+
return \array_merge(parent::provideInvalidDatabaseValueInputs(), [
2929
['1.7976931348623157E+309'], // Too large
3030
['-1.7976931348623157E+309'], // Too small
3131
['1.123456789012345678'], // Too many decimal places (>15)
@@ -35,7 +35,10 @@ public static function provideInvalidPHPValuesForDatabaseTransformation(): array
3535
}
3636

3737
/**
38-
* @return array<int, array{phpValue: float, postgresValue: string}>
38+
* @return list<array{
39+
* phpValue: float,
40+
* postgresValue: string
41+
* }>
3942
*/
4043
public static function provideValidTransformations(): array
4144
{

0 commit comments

Comments
 (0)