Skip to content

Commit

Permalink
Tests - bring back DBAL ArrayType
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 9, 2024
1 parent ef61898 commit 7960553
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
57 changes: 57 additions & 0 deletions compatibility/ArrayType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Doctrine\DBAL\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;

use function is_resource;
use function restore_error_handler;
use function serialize;
use function set_error_handler;
use function stream_get_contents;
use function unserialize;

use const E_DEPRECATED;
use const E_USER_DEPRECATED;

/**
* Type that maps a PHP array to a clob SQL type.
*
* @deprecated Use {@link JsonType} instead.
*/
class ArrayType extends Type
{
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getClobTypeDeclarationSQL($column);
}

public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): mixed
{
// @todo 3.0 - $value === null check to save real NULL in database
return serialize($value);
}

public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed
{
if ($value === null) {
return null;
}

$value = is_resource($value) ? stream_get_contents($value) : $value;

set_error_handler(function (int $code, string $message): bool {
if ($code === E_DEPRECATED || $code === E_USER_DEPRECATED) {
return false;
}

throw ConversionException::conversionFailedUnserialization($this->getName(), $message);
});

try {
return unserialize($value);
} finally {
restore_error_handler();
}
}
}
3 changes: 3 additions & 0 deletions tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ protected function getRule(): Rule
if (!Type::hasType('carbon_immutable')) {
Type::addType('carbon_immutable', CarbonImmutableType::class);
}
if (!Type::hasType('array')) {
Type::addType('array', \Doctrine\DBAL\Types\ArrayType::class);
}

return new EntityColumnRule(
new ObjectMetadataResolver($this->objectManagerLoader, __DIR__ . '/../../../../tmp'),
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
PHPStanTestCase::getContainer();

require_once __DIR__ . '/orm-3-bootstrap.php';
require_once __DIR__ . '/dbal-4-bootstrap.php';
8 changes: 8 additions & 0 deletions tests/dbal-4-bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

if (
!is_file(__DIR__ . '/../vendor/doctrine/dbal/src/Types/ArrayType.php')
&& !is_file(__DIR__ . '/../vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/ArrayType.php')
) {
require_once __DIR__ . '/../compatibility/ArrayType.php';
}

0 comments on commit 7960553

Please sign in to comment.