Skip to content

Commit c95f312

Browse files
chore: reorganize the integration tests for DBAL\Types (#417)
1 parent d8eb766 commit c95f312

31 files changed

+1768
-456
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Integration\MartinGeorgiev\Doctrine\DBAL\Types;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
9+
abstract class ArrayTypeTestCase extends TestCase
10+
{
11+
#[Test]
12+
public function can_handle_empty_array(): void
13+
{
14+
$typeName = $this->getTypeName();
15+
$columnType = $this->getPostgresTypeName();
16+
17+
$this->runTypeTest($typeName, $columnType, []);
18+
}
19+
20+
#[Test]
21+
public function can_handle_null_values(): void
22+
{
23+
$typeName = $this->getTypeName();
24+
$columnType = $this->getPostgresTypeName();
25+
26+
$this->runTypeTest($typeName, $columnType, null);
27+
}
28+
29+
/**
30+
* Data-driven test for array values.
31+
* Subclasses should add #[DataProvider('provideValidTransformations')].
32+
*/
33+
public function can_handle_array_values(string $testName, array $arrayValue): void
34+
{
35+
$typeName = $this->getTypeName();
36+
$columnType = $this->getPostgresTypeName();
37+
38+
$this->runTypeTest($typeName, $columnType, $arrayValue);
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Integration\MartinGeorgiev\Doctrine\DBAL\Types;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Test;
9+
10+
class BigIntArrayTypeTest extends ArrayTypeTestCase
11+
{
12+
protected function getTypeName(): string
13+
{
14+
return 'bigint[]';
15+
}
16+
17+
protected function getPostgresTypeName(): string
18+
{
19+
return 'BIGINT[]';
20+
}
21+
22+
#[DataProvider('provideValidTransformations')]
23+
#[Test]
24+
public function can_handle_array_values(string $testName, array $arrayValue): void
25+
{
26+
parent::can_handle_array_values($testName, $arrayValue);
27+
}
28+
29+
/**
30+
* @return array<string, array{string, array<int, int>}>
31+
*/
32+
public static function provideValidTransformations(): array
33+
{
34+
return [
35+
'simple bigint array' => ['simple bigint array', [9223372036854775807, 1, -9223372036854775807]],
36+
'bigint array with zeros' => ['bigint array with zeros', [0, 0, 0, 1, 0]],
37+
'bigint array with large numbers' => ['bigint array with large numbers', [1000000000000, 2000000000000, 3000000000000]],
38+
'bigint array with negative numbers' => ['bigint array with negative numbers', [-1000000000000, -2000000000000, -3000000000000]],
39+
'bigint array with PHP max and min integer constants' => ['bigint array with PHP max and min integer constants', [PHP_INT_MAX, PHP_INT_MIN, 0]],
40+
];
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Integration\MartinGeorgiev\Doctrine\DBAL\Types;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Test;
9+
10+
class BooleanArrayTypeTest extends ArrayTypeTestCase
11+
{
12+
protected function getTypeName(): string
13+
{
14+
return 'bool[]';
15+
}
16+
17+
protected function getPostgresTypeName(): string
18+
{
19+
return 'BOOL[]';
20+
}
21+
22+
#[DataProvider('provideValidTransformations')]
23+
#[Test]
24+
public function can_handle_array_values(string $testName, array $arrayValue): void
25+
{
26+
parent::can_handle_array_values($testName, $arrayValue);
27+
}
28+
29+
/**
30+
* @return array<string, array{string, array<int, bool>}>
31+
*/
32+
public static function provideValidTransformations(): array
33+
{
34+
return [
35+
'simple boolean array' => ['simple boolean array', [true, false, true]],
36+
'boolean array with all true' => ['boolean array with all true', [true, true, true]],
37+
'boolean array with all false' => ['boolean array with all false', [false, false, false]],
38+
'boolean array mixed' => ['boolean array mixed', [true, false, true, false, true]],
39+
];
40+
}
41+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Integration\MartinGeorgiev\Doctrine\DBAL\Types;
6+
7+
use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidCidrArrayItemForPHPException;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\Attributes\Test;
10+
11+
class CidrArrayTypeTest extends ArrayTypeTestCase
12+
{
13+
protected function getTypeName(): string
14+
{
15+
return 'cidr[]';
16+
}
17+
18+
protected function getPostgresTypeName(): string
19+
{
20+
return 'CIDR[]';
21+
}
22+
23+
#[DataProvider('provideValidTransformations')]
24+
#[Test]
25+
public function can_handle_array_values(string $testName, array $arrayValue): void
26+
{
27+
parent::can_handle_array_values($testName, $arrayValue);
28+
}
29+
30+
/**
31+
* @return array<string, array{string, array<int, string>}>
32+
*/
33+
public static function provideValidTransformations(): array
34+
{
35+
return [
36+
'simple cidr array' => ['simple cidr array', ['192.168.1.0/24', '10.0.0.0/8']],
37+
'cidr array with IPv6' => ['cidr array with IPv6', ['2001:db8::/32', '2001:db8::/64']],
38+
'cidr array with mixed networks' => ['cidr array with mixed networks', [
39+
'192.168.1.0/24',
40+
'172.16.0.0/16',
41+
'10.0.0.0/8',
42+
'2001:db8::/32',
43+
]],
44+
'cidr array with single hosts' => ['cidr array with single hosts', [
45+
'192.168.1.1/32',
46+
'10.0.0.1/32',
47+
'2001:db8::1/128',
48+
]],
49+
'empty cidr array' => ['empty cidr array', []],
50+
];
51+
}
52+
53+
#[Test]
54+
public function can_handle_invalid_networks(): void
55+
{
56+
$this->expectException(InvalidCidrArrayItemForPHPException::class);
57+
58+
$typeName = $this->getTypeName();
59+
$columnType = $this->getPostgresTypeName();
60+
61+
$this->runTypeTest($typeName, $columnType, ['invalid-network', '192.168.1.0/24']);
62+
}
63+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Integration\MartinGeorgiev\Doctrine\DBAL\Types;
6+
7+
use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidCidrForPHPException;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\Attributes\Test;
10+
11+
class CidrTypeTest extends ScalarTypeTestCase
12+
{
13+
protected function getTypeName(): string
14+
{
15+
return 'cidr';
16+
}
17+
18+
protected function getPostgresTypeName(): string
19+
{
20+
return 'CIDR';
21+
}
22+
23+
#[DataProvider('provideValidTransformations')]
24+
#[Test]
25+
public function can_transform_from_php_value(string $testValue): void
26+
{
27+
$typeName = $this->getTypeName();
28+
$columnType = $this->getPostgresTypeName();
29+
30+
$this->runTypeTest($typeName, $columnType, $testValue);
31+
}
32+
33+
/**
34+
* @return array<string, array{string}>
35+
*/
36+
public static function provideValidTransformations(): array
37+
{
38+
return [
39+
'IPv4 CIDR' => ['192.168.1.0/24'],
40+
'IPv4 CIDR /8' => ['10.0.0.0/8'],
41+
'IPv4 CIDR /16' => ['172.16.0.0/16'],
42+
'IPv6 CIDR' => ['2001:db8::/32'],
43+
'IPv6 CIDR /64' => ['2001:db8::/64'],
44+
'IPv6 CIDR /128' => ['2001:db8::1/128'],
45+
];
46+
}
47+
48+
#[Test]
49+
public function can_handle_invalid_networks(): void
50+
{
51+
$this->expectException(InvalidCidrForPHPException::class);
52+
53+
$typeName = $this->getTypeName();
54+
$columnType = $this->getPostgresTypeName();
55+
56+
$this->runTypeTest($typeName, $columnType, 'invalid-network');
57+
}
58+
}

0 commit comments

Comments
 (0)