Skip to content

Commit 7960553

Browse files
committed
Tests - bring back DBAL ArrayType
1 parent ef61898 commit 7960553

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

compatibility/ArrayType.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Doctrine\DBAL\Types;
4+
5+
use Doctrine\DBAL\Platforms\AbstractPlatform;
6+
7+
use function is_resource;
8+
use function restore_error_handler;
9+
use function serialize;
10+
use function set_error_handler;
11+
use function stream_get_contents;
12+
use function unserialize;
13+
14+
use const E_DEPRECATED;
15+
use const E_USER_DEPRECATED;
16+
17+
/**
18+
* Type that maps a PHP array to a clob SQL type.
19+
*
20+
* @deprecated Use {@link JsonType} instead.
21+
*/
22+
class ArrayType extends Type
23+
{
24+
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
25+
{
26+
return $platform->getClobTypeDeclarationSQL($column);
27+
}
28+
29+
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): mixed
30+
{
31+
// @todo 3.0 - $value === null check to save real NULL in database
32+
return serialize($value);
33+
}
34+
35+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed
36+
{
37+
if ($value === null) {
38+
return null;
39+
}
40+
41+
$value = is_resource($value) ? stream_get_contents($value) : $value;
42+
43+
set_error_handler(function (int $code, string $message): bool {
44+
if ($code === E_DEPRECATED || $code === E_USER_DEPRECATED) {
45+
return false;
46+
}
47+
48+
throw ConversionException::conversionFailedUnserialization($this->getName(), $message);
49+
});
50+
51+
try {
52+
return unserialize($value);
53+
} finally {
54+
restore_error_handler();
55+
}
56+
}
57+
}

tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ protected function getRule(): Rule
5454
if (!Type::hasType('carbon_immutable')) {
5555
Type::addType('carbon_immutable', CarbonImmutableType::class);
5656
}
57+
if (!Type::hasType('array')) {
58+
Type::addType('array', \Doctrine\DBAL\Types\ArrayType::class);
59+
}
5760

5861
return new EntityColumnRule(
5962
new ObjectMetadataResolver($this->objectManagerLoader, __DIR__ . '/../../../../tmp'),

tests/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
PHPStanTestCase::getContainer();
88

99
require_once __DIR__ . '/orm-3-bootstrap.php';
10+
require_once __DIR__ . '/dbal-4-bootstrap.php';

tests/dbal-4-bootstrap.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
if (
4+
!is_file(__DIR__ . '/../vendor/doctrine/dbal/src/Types/ArrayType.php')
5+
&& !is_file(__DIR__ . '/../vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/ArrayType.php')
6+
) {
7+
require_once __DIR__ . '/../compatibility/ArrayType.php';
8+
}

0 commit comments

Comments
 (0)