Skip to content

Commit 5a8382e

Browse files
committedJun 17, 2023
add basic tests on types and tools
add ci
1 parent c45663b commit 5a8382e

19 files changed

+446
-34
lines changed
 

‎.github/workflows/ci.yaml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: CI
2+
on:
3+
push:
4+
branches: [ dev, master ]
5+
pull_request:
6+
branches: [ dev, master ]
7+
jobs:
8+
tests:
9+
runs-on: 'Ubuntu-20.04'
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
php: ['8.1', '8.2']
14+
15+
name: Tests PHP ${{ matrix.php }}
16+
steps:
17+
- uses: actions/checkout@v2
18+
- uses: shivammathur/setup-php@v2
19+
with:
20+
php-version: '${{ matrix.php }}'
21+
coverage: none
22+
ini-values: 'memory_limit=-1'
23+
- uses: "ramsey/composer-install@v2"
24+
- name: Run Unit tests
25+
run: vendor/bin/phpunit
26+
style:
27+
runs-on: 'Ubuntu-20.04'
28+
strategy:
29+
fail-fast: false
30+
matrix:
31+
php: [ '8.2' ]
32+
name: Style PHP ${{ matrix.php }}
33+
steps:
34+
- uses: actions/checkout@v2
35+
- uses: shivammathur/setup-php@v2
36+
with:
37+
php-version: '${{ matrix.php }}'
38+
coverage: none
39+
ini-values: 'memory_limit=-1'
40+
- uses: "ramsey/composer-install@v2"
41+
- name: Run Style checks
42+
run: vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php -v --dry-run --stop-on-violation

‎composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"doctrine/orm": "^2.13",
4040
"symfony/serializer": ">=5.4",
4141
"symfony/property-info": ">=5.4",
42-
"phpunit/phpunit": "^10.0"
42+
"phpunit/phpunit": "^10.0",
43+
"symfony/property-access": ">=5.4"
4344
}
4445
}

‎phpunit.xml.dist

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd">
3-
<testsuites>
4-
<testsuite name="PostgreSQLDoctrine">
5-
<directory suffix="Test.php">./tests</directory>
6-
</testsuite>
7-
</testsuites>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd">
3+
<testsuites>
4+
<testsuite name="PostgreSQLDoctrine">
5+
<directory suffix="Test.php">./tests</directory>
6+
</testsuite>
7+
</testsuites>
88

9-
<coverage>
10-
<include>
11-
<directory>./src</directory>
12-
</include>
13-
</coverage>
14-
</phpunit>
9+
<source>
10+
<include>
11+
<directory>./src</directory>
12+
</include>
13+
</source>
14+
</phpunit>

‎src/DBAL/Platform/PostgreSQLPlatform.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Pfilsx\PostgreSQLDoctrine\DBAL\Type\EnumType;
1515
use Pfilsx\PostgreSQLDoctrine\DBAL\Type\JsonModelType;
1616

17-
final class PostgreSQLPlatform extends BasePlatform
17+
class PostgreSQLPlatform extends BasePlatform
1818
{
1919
public function createSchemaManager(Connection $connection): PostgreSQLSchemaManager
2020
{

‎src/DBAL/Schema/EnumTypeUsageAsset.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class EnumTypeUsageAsset
1212

1313
private ?string $default;
1414

15-
public function __construct(string $table, string $column, ?string $default = null)
15+
public function __construct(string $table, string $column, string $default = null)
1616
{
1717
$this->table = $table;
1818
$this->column = $column;

‎src/DBAL/Schema/PostgreSQLSchemaManager.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function createComparator(): Comparator
6161
return new Comparator($this->_platform);
6262
}
6363

64-
protected function selectTableColumns(string $databaseName, ?string $tableName = null): Result
64+
protected function selectTableColumns(string $databaseName, string $tableName = null): Result
6565
{
6666
$sql = 'SELECT';
6767

‎src/DBAL/Schema/Schema.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class Schema extends BaseSchema
1818
public function __construct(
1919
array $tables = [],
2020
array $sequences = [],
21-
?SchemaConfig $schemaConfig = null,
21+
SchemaConfig $schemaConfig = null,
2222
array $namespaces = [],
2323
array $types = []
2424
) {

‎src/DBAL/Schema/SchemaDiff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222
array $createdTables = [],
2323
array $alteredTables = [],
2424
array $droppedTables = [],
25-
?Schema $fromSchema = null,
25+
Schema $fromSchema = null,
2626
array $createdSchemas = [],
2727
array $droppedSchemas = [],
2828
array $createdSequences = [],

‎src/DBAL/Schema/Table.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function addColumn($name, $typeName, array $options = []): Column
2727
*
2828
* @return self
2929
*/
30-
public function modifyColumn($name, array $options, ?string $enumClass = null): self
30+
public function modifyColumn($name, array $options, string $enumClass = null): self
3131
{
3232
$column = $this->getColumn($name);
3333
$column->setOptions($options);

‎src/DBAL/Type/JsonModelType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform)
3232
return null;
3333
}
3434

35-
if (!\is_object($value)) {
35+
if (!\is_object($value) || get_class($value) !== static::getModelClass()) {
3636
throw ConversionException::conversionFailed($value, $this->getName());
3737
}
3838

‎src/Tools/ArrayTypeTool.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class ArrayTypeTool
4545
*
4646
* @return string
4747
*/
48-
public static function convertPHPArrayToDatabaseArrayString(array $array, ArrayTypeEnum $type, ?AbstractPlatform $platform = null): string
48+
public static function convertPHPArrayToDatabaseArrayString(array $array, ArrayTypeEnum $type, AbstractPlatform $platform = null): string
4949
{
5050
if ($array === []) {
5151
return '{}';
@@ -55,7 +55,7 @@ public static function convertPHPArrayToDatabaseArrayString(array $array, ArrayT
5555
ArrayTypeEnum::SmallIntArray, ArrayTypeEnum::IntArray, ArrayTypeEnum::BigIntArray => self::convertIntPHPArrayToDatabaseArray($array, $type),
5656
ArrayTypeEnum::TextArray => self::convertStringPHPArrayToDatabaseArray($array),
5757
ArrayTypeEnum::BooleanArray => self::convertBooleanPHPArrayToDatabaseArray($array, $platform),
58-
ArrayTypeEnum::JsonArray => array_map(static fn ($row) => '"' . json_encode($row) . '"', $array),
58+
ArrayTypeEnum::JsonArray => array_map(static fn ($row) => '"' . addcslashes(json_encode($row), '"') . '"', $array),
5959
};
6060

6161
return '{' . implode(',', $preparedArray) . '}';
@@ -70,7 +70,7 @@ public static function convertPHPArrayToDatabaseArrayString(array $array, ArrayT
7070
*
7171
* @return bool[]|int[]|string[]
7272
*/
73-
public static function convertDatabaseArrayStringToPHPArray(string $value, ArrayTypeEnum $type, ?AbstractPlatform $platform = null): array
73+
public static function convertDatabaseArrayStringToPHPArray(string $value, ArrayTypeEnum $type, AbstractPlatform $platform = null): array
7474
{
7575
if ($value === '{}') {
7676
return [];
@@ -153,7 +153,7 @@ private static function convertBooleanPHPArrayToDatabaseArray(array $array, ?Abs
153153
continue;
154154
}
155155

156-
throw new \InvalidArgumentException(\sprintf('Item at key %s has invalid type. Expected boolean, 0, 1 or boolean string literal, "%s" provided.', $key, \var_export($value, true)));
156+
throw new \InvalidArgumentException(\sprintf('Item at key %s has invalid type. Expected boolean, 0, 1 or boolean string literal, %s provided.', $key, \var_export($value, true)));
157157
}
158158

159159
return $array;
@@ -193,10 +193,10 @@ private static function convertDatabaseArrayStringToStringPHPArray(string $value
193193
$array = \str_getcsv(\trim($value, '{}'));
194194

195195
foreach ($array as $key => $item) {
196-
if ($item === null) {
197-
unset($array[$key]);
196+
if ($item === 'null') {
197+
$array[$key] = null;
198198

199-
break;
199+
continue;
200200
}
201201

202202
$array[$key] = \stripslashes($item);

‎src/Tools/SchemaTool.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,11 @@ private function processingNotRequired(
294294
ClassMetadata $class,
295295
array $processedClasses
296296
): bool {
297-
return isset($processedClasses[$class->name]) ||
298-
$class->isMappedSuperclass ||
299-
$class->isEmbeddedClass ||
300-
($class->isInheritanceTypeSingleTable() && $class->name !== $class->rootEntityName) ||
301-
in_array($class->name, $this->em->getConfiguration()->getSchemaIgnoreClasses());
297+
return isset($processedClasses[$class->name])
298+
|| $class->isMappedSuperclass
299+
|| $class->isEmbeddedClass
300+
|| ($class->isInheritanceTypeSingleTable() && $class->name !== $class->rootEntityName)
301+
|| in_array($class->name, $this->em->getConfiguration()->getSchemaIgnoreClasses());
302302
}
303303

304304
private function gatherColumns(ClassMetadata $class, Table $table): void
@@ -673,8 +673,8 @@ private function addDiscriminatorColumnDefinition(ClassMetadata $class, Table $t
673673
$discrColumn = $class->discriminatorColumn;
674674

675675
if (
676-
!isset($discrColumn['type']) ||
677-
(strtolower($discrColumn['type']) === 'string' && !isset($discrColumn['length']))
676+
!isset($discrColumn['type'])
677+
|| (strtolower($discrColumn['type']) === 'string' && !isset($discrColumn['length']))
678678
) {
679679
$discrColumn['type'] = 'string';
680680
$discrColumn['length'] = 255;

‎tests/Fixtures/Model/TestModel.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Model;
6+
7+
8+
final class TestModel
9+
{
10+
public int $id;
11+
12+
private string $text;
13+
14+
public function getText(): string
15+
{
16+
return $this->text;
17+
}
18+
19+
public function setText(string $text): self
20+
{
21+
$this->text = $text;
22+
return $this;
23+
}
24+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Type;
6+
7+
8+
use Pfilsx\PostgreSQLDoctrine\DBAL\Type\JsonModelType;
9+
use Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Model\TestModel;
10+
11+
final class TestJsonModelType extends JsonModelType
12+
{
13+
public static function getTypeName(): string
14+
{
15+
return 'test_json_model_type';
16+
}
17+
18+
protected static function getModelClass(): string
19+
{
20+
return TestModel::class;
21+
}
22+
}
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\Tests\Unit\DBAL\Type;
6+
7+
8+
use Pfilsx\PostgreSQLDoctrine\DBAL\Platform\PostgreSQLPlatform;
9+
use Pfilsx\PostgreSQLDoctrine\DBAL\Type\BigIntArrayType;
10+
use Pfilsx\PostgreSQLDoctrine\DBAL\Type\BooleanArrayType;
11+
use Pfilsx\PostgreSQLDoctrine\DBAL\Type\IntegerArrayType;
12+
use Pfilsx\PostgreSQLDoctrine\DBAL\Type\JsonArrayType;
13+
use Pfilsx\PostgreSQLDoctrine\DBAL\Type\SmallIntArrayType;
14+
use Pfilsx\PostgreSQLDoctrine\DBAL\Type\TextArrayType;
15+
use PHPUnit\Framework\TestCase;
16+
17+
final class ArrayTypesTest extends TestCase
18+
{
19+
private PostgreSQLPlatform $platform;
20+
21+
public function setUp(): void
22+
{
23+
$this->platform = $this->createMock(PostgreSQLPlatform::class);
24+
}
25+
26+
/**
27+
* @see SmallIntArrayType
28+
*/
29+
public function testSmallIntArrayType(): void
30+
{
31+
$type = new SmallIntArrayType();
32+
self::assertSame('smallint[]', $type->getName());
33+
self::assertSame('{-32768,0,32767}', $type->convertToDatabaseValue([-32768, 0, 32767], $this->platform));
34+
self::assertSame([-32768, 0, 32767], $type->convertToPHPValue('{-32768,0,32767}', $this->platform));
35+
}
36+
37+
/**
38+
* @see IntegerArrayType
39+
*/
40+
public function testIntArrayType(): void
41+
{
42+
$type = new IntegerArrayType();
43+
self::assertSame('integer[]', $type->getName());
44+
self::assertSame('{-2147483648,0,2147483647}', $type->convertToDatabaseValue([-2147483648, 0, 2147483647], $this->platform));
45+
self::assertSame([-2147483648, 0, 2147483647], $type->convertToPHPValue('{-2147483648,0,2147483647}', $this->platform));
46+
}
47+
48+
/**
49+
* @see BigIntArrayType
50+
*/
51+
public function testBigIntArrayType(): void
52+
{
53+
$type = new BigIntArrayType();
54+
self::assertSame('bigint[]', $type->getName());
55+
self::assertSame('{-9223372036854775807,0,9223372036854775807}', $type->convertToDatabaseValue([-9223372036854775807, 0, 9223372036854775807], $this->platform));
56+
self::assertSame([-9223372036854775807, 0, 9223372036854775807], $type->convertToPHPValue('{-9223372036854775807,0,9223372036854775807}', $this->platform));
57+
}
58+
59+
/**
60+
* @see BooleanArrayType
61+
*/
62+
public function testBooleanArrayType(): void
63+
{
64+
$type = new BooleanArrayType();
65+
self::assertSame('bool[]', $type->getName());
66+
self::assertSame('{true,false,true,false}', $type->convertToDatabaseValue([1, false, 'yes', 'off'], $this->platform));
67+
self::assertSame([true, false], $type->convertToPHPValue('{true,false}', $this->platform));
68+
}
69+
70+
/**
71+
* @see TextArrayType
72+
*/
73+
public function testTextArrayType(): void
74+
{
75+
$type = new TextArrayType();
76+
self::assertSame('text[]', $type->getName());
77+
self::assertSame('{1,"text"}', $type->convertToDatabaseValue(['1', 'text'], $this->platform));
78+
self::assertSame(['1', 'text'], $type->convertToPHPValue('{1,"text"}', $this->platform));
79+
}
80+
81+
/**
82+
* @see JsonArrayType
83+
*/
84+
public function testJsonArrayType(): void
85+
{
86+
$type = new JsonArrayType();
87+
self::assertSame('json[]', $type->getName());
88+
self::assertSame('{"{\"key\":1}","{\"key\":2}"}', $type->convertToDatabaseValue([['key' => 1], ['key' => 2]], $this->platform));
89+
self::assertSame([['key' => 1], ['key' => 2]], $type->convertToPHPValue('{"{\"key\":1}","{\"key\":2}"}', $this->platform));
90+
}
91+
}

‎tests/Unit/DBAL/Type/EnumTypeTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Pfilsx\PostgreSQLDoctrine\Tests\Unit\DBAL\Type;
4+
5+
use Pfilsx\PostgreSQLDoctrine\DBAL\Platform\PostgreSQLPlatform;
6+
use Pfilsx\PostgreSQLDoctrine\DBAL\Type\EnumType;
7+
use Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Enum\TestStringBackedEnum;
8+
use Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Enum\TestUnitEnum;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* @see EnumType
13+
*/
14+
final class EnumTypeTest extends TestCase
15+
{
16+
private PostgreSQLPlatform $platform;
17+
18+
private EnumType $type;
19+
20+
public function setUp(): void
21+
{
22+
$this->platform = $this->createMock(PostgreSQLPlatform::class);
23+
$this->type = new EnumType();
24+
}
25+
26+
public function testGetName(): void
27+
{
28+
self::assertEquals('enum', $this->type->getName());
29+
}
30+
31+
public function testGetSqlDeclaration(): void
32+
{
33+
self::assertSame('test_string_backed_enum_type', $this->type->getSQLDeclaration([
34+
'enumType' => TestStringBackedEnum::class
35+
], $this->platform));
36+
}
37+
38+
public function testConvertToDatabaseValue(): void
39+
{
40+
self::assertSame('Case1', $this->type->convertToDatabaseValue(TestStringBackedEnum::Case1, $this->platform));
41+
self::assertSame('Case1', $this->type->convertToDatabaseValue(TestUnitEnum::Case1, $this->platform));
42+
self::assertSame(null, $this->type->convertToDatabaseValue(null, $this->platform));
43+
}
44+
}
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\Tests\Unit\DBAL\Type;
6+
7+
8+
use Pfilsx\PostgreSQLDoctrine\DBAL\Platform\PostgreSQLPlatform;
9+
use Pfilsx\PostgreSQLDoctrine\DBAL\Type\JsonModelType;
10+
use Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Model\TestModel;
11+
use Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Type\TestJsonModelType;
12+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
14+
15+
/**
16+
* @see JsonModelType
17+
*/
18+
final class JsonModelTypeTest extends TestCase
19+
{
20+
private PostgreSQLPlatform $platform;
21+
private TestJsonModelType $type;
22+
23+
public function setUp(): void
24+
{
25+
$this->platform = $this->createMock(PostgreSQLPlatform::class);
26+
$this->type = new TestJsonModelType();
27+
$this->type->setObjectNormalizer(new ObjectNormalizer());
28+
}
29+
30+
public function testGetName(): void
31+
{
32+
self::assertSame('test_json_model_type', $this->type->getName());
33+
}
34+
35+
public function testConvertToDatabaseValue(): void
36+
{
37+
$model = new TestModel();
38+
$model->id = 1;
39+
$model->setText('test');
40+
self::assertSame('{"text":"test","id":1}', $this->type->convertToDatabaseValue($model, $this->platform));
41+
}
42+
43+
public function testConvertToPHPValue(): void
44+
{
45+
$model = $this->type->convertToPHPValue('{"id":1,"text":"test"}', $this->platform);
46+
self::assertInstanceOf(TestModel::class, $model);
47+
48+
self::assertSame(1, $model->id);
49+
self::assertSame('test', $model->getText());
50+
}
51+
}
+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\Tests\Unit\Tools;
6+
7+
8+
use Pfilsx\PostgreSQLDoctrine\Enum\ArrayTypeEnum;
9+
use Pfilsx\PostgreSQLDoctrine\Tools\ArrayTypeTool;
10+
use PHPUnit\Framework\TestCase;
11+
12+
/**
13+
* @see ArrayTypeTool
14+
*/
15+
final class ArrayTypeToolTest extends TestCase
16+
{
17+
/**
18+
* @dataProvider providerTestConvertPHPArrayToDatabaseArrayString
19+
* @see ArrayTypeTool::convertPHPArrayToDatabaseArrayString()
20+
*/
21+
public function testConvertPHPArrayToDatabaseArrayString(array $in, ArrayTypeEnum $type, string $expectedOut): void
22+
{
23+
self::assertSame($expectedOut, ArrayTypeTool::convertPHPArrayToDatabaseArrayString($in, $type));
24+
}
25+
26+
public static function providerTestConvertPHPArrayToDatabaseArrayString(): array
27+
{
28+
return [
29+
'empty array' => [
30+
[],
31+
ArrayTypeEnum::IntArray,
32+
'{}',
33+
],
34+
'int array' => [
35+
[1,2,3],
36+
ArrayTypeEnum::SmallIntArray,
37+
'{1,2,3}',
38+
],
39+
'text array' => [
40+
['1', 'text', 'text with whitespace and quote "'],
41+
ArrayTypeEnum::TextArray,
42+
'{1,"text","text with whitespace and quote \""}'
43+
],
44+
'boolean array without platform' => [
45+
[1, true, false, 'yes', 'off'],
46+
ArrayTypeEnum::BooleanArray,
47+
'{true,true,false,true,false}',
48+
],
49+
'json array' => [
50+
[['key1' => 1, 'key2' => null, 'key3' => ['sub_key1' => false]], ['key1' => 2]],
51+
ArrayTypeEnum::JsonArray,
52+
'{"{\"key1\":1,\"key2\":null,\"key3\":{\"sub_key1\":false}}","{\"key1\":2}"}'
53+
],
54+
];
55+
}
56+
57+
/**
58+
* @dataProvider providerTestConvertPHPArrayToDatabaseArrayStringOnInvalidData
59+
* @see ArrayTypeTool::convertPHPArrayToDatabaseArrayString()
60+
*/
61+
public function testConvertPHPArrayToDatabaseArrayStringOnInvalidData(array $in, ArrayTypeEnum $type, string $exMessage): void
62+
{
63+
self::expectException(\InvalidArgumentException::class);
64+
self::expectExceptionMessage($exMessage);
65+
ArrayTypeTool::convertPHPArrayToDatabaseArrayString($in, $type);
66+
}
67+
68+
public static function providerTestConvertPHPArrayToDatabaseArrayStringOnInvalidData(): array
69+
{
70+
return [
71+
'int array with not int value' => [
72+
['test'],
73+
ArrayTypeEnum::IntArray,
74+
'Item at key 0 has invalid type. Expected type "int", "string" provided.'
75+
],
76+
'int array with not in range int value' => [
77+
[32768],
78+
ArrayTypeEnum::SmallIntArray,
79+
'Item at key 0 is invalid for "smallint". Expected integer between -32768 and 32767.'
80+
],
81+
'text array' => [
82+
[false],
83+
ArrayTypeEnum::TextArray,
84+
'Item at key 0 has invalid type. Expected type "string", "bool" provided.'
85+
],
86+
'boolean array' => [
87+
['test'],
88+
ArrayTypeEnum::BooleanArray,
89+
'Item at key 0 has invalid type. Expected boolean, 0, 1 or boolean string literal, \'test\' provided.'
90+
],
91+
];
92+
}
93+
94+
95+
/**
96+
* @dataProvider providerTestConvertDatabaseArrayStringToPHPArray
97+
* @see ArrayTypeTool::convertDatabaseArrayStringToPHPArray()
98+
*/
99+
public function testConvertDatabaseArrayStringToPHPArray(string $in, ArrayTypeEnum $type, array $expectedOut): void
100+
{
101+
self::assertSame($expectedOut, ArrayTypeTool::convertDatabaseArrayStringToPHPArray($in, $type));
102+
}
103+
104+
public static function providerTestConvertDatabaseArrayStringToPHPArray(): array
105+
{
106+
return [
107+
'empty array' => [
108+
'{}',
109+
ArrayTypeEnum::IntArray,
110+
[],
111+
],
112+
'int array' => [
113+
'{1,2,3}',
114+
ArrayTypeEnum::SmallIntArray,
115+
[1,2,3],
116+
],
117+
'text array' => [
118+
'{1,text,"text with whitespace and quote \"",null}',
119+
ArrayTypeEnum::TextArray,
120+
['1', 'text', 'text with whitespace and quote "', null],
121+
],
122+
'boolean array without platform' => [
123+
'{true,false}',
124+
ArrayTypeEnum::BooleanArray,
125+
[true, false],
126+
],
127+
'json array' => [
128+
'{"{\"key1\":1,\"key2\":null,\"key3\":{\"sub_key1\":false}}","{\"key1\":2}"}',
129+
ArrayTypeEnum::JsonArray,
130+
[['key1' => 1, 'key2' => null, 'key3' => ['sub_key1' => false]], ['key1' => 2]],
131+
],
132+
];
133+
}
134+
}

‎tests/Unit/Tools/EnumToolTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Pfilsx\PostgreSQLDoctrine\Tools\EnumTool;
1111
use PHPUnit\Framework\TestCase;
1212

13+
/**
14+
* @see EnumTool
15+
*/
1316
final class EnumToolTest extends TestCase
1417
{
1518
/**

0 commit comments

Comments
 (0)
Please sign in to comment.