-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related to issue doctrine/orm#9021 Related to PR ORM doctrine/orm#9149
- Loading branch information
Showing
6 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
|
||
final class EnumType extends Type | ||
{ | ||
public function getSQLDeclaration(array $column, AbstractPlatform $platform) | ||
{ | ||
return $platform->getVarcharTypeDeclarationSQL($column); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convertToDatabaseValue($value, AbstractPlatform $platform) | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if ($value instanceof \UnitEnum) { | ||
return serialize($value); | ||
} | ||
|
||
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'UnitEnum']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convertToPHPValue($value, AbstractPlatform $platform) | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
set_error_handler(function (int $code, string $message): bool { | ||
throw ConversionException::conversionFailedUnserialization($this->getName(), $message); | ||
}); | ||
|
||
try { | ||
return unserialize($value); | ||
} finally { | ||
restore_error_handler(); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return Types::ENUM; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function requiresSQLCommentHint(AbstractPlatform $platform) | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Types; | ||
|
||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Tests\Tools\TestAsset\SimpleEnum; | ||
use Doctrine\DBAL\Types\Types; | ||
|
||
/** | ||
* @requires PHP >= 8.1 | ||
*/ | ||
class EnumTest extends FunctionalTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$table = new Table('enum_table'); | ||
$table->addColumn('enum', 'enum'); | ||
|
||
$this->connection->createSchemaManager()->dropAndCreateTable($table); | ||
} | ||
|
||
public function testInsertAndSelect(): void | ||
{ | ||
$draft = SimpleEnum::DRAFT; | ||
|
||
$result = $this->connection->insert('enum_table', ['enum' => $draft], [Types::ENUM]); | ||
self::assertSame(1, $result); | ||
|
||
$value = $this->connection->fetchOne('SELECT enum FROM enum_table'); | ||
|
||
self::assertSame($draft, $this->connection->convertToPHPValue($value, Types::ENUM)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Tests\Tools\TestAsset; | ||
|
||
enum SimpleEnum | ||
{ | ||
case DRAFT; | ||
case PUBLISHED; | ||
case UNPUBLISHED; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Tests\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\ConversionException; | ||
use Doctrine\DBAL\Types\EnumType; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Doctrine\DBAL\Tests\Tools\TestAsset\SimpleEnum; | ||
|
||
/** | ||
* @requires PHP >= 8.1 | ||
*/ | ||
class EnumTest extends TestCase | ||
{ | ||
/** @var AbstractPlatform&MockObject */ | ||
private $platform; | ||
|
||
/** @var EnumType */ | ||
private $type; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->platform = $this->createMock(AbstractPlatform::class); | ||
$this->type = new EnumType(); | ||
} | ||
|
||
public function testObjectConvertsToDatabaseValue(): void | ||
{ | ||
self::assertIsString($this->type->convertToDatabaseValue(SimpleEnum::DRAFT, $this->platform)); | ||
} | ||
|
||
public function testObjectConvertsToPHPValue(): void | ||
{ | ||
self::assertTrue(is_a($this->type->convertToPHPValue(serialize(SimpleEnum::DRAFT), $this->platform), SimpleEnum::class)); | ||
} | ||
|
||
public function testConversionToDatabaseValueFailure(): void | ||
{ | ||
$this->expectException(ConversionException::class); | ||
$this->expectExceptionMessage( | ||
"Could not convert PHP value 'abcdefg' to type enum. Expected one of the following types: null, UnitEnum" | ||
); | ||
$this->type->convertToDatabaseValue('abcdefg', $this->platform); | ||
} | ||
|
||
public function testConversionToPHPValueFailure(): void | ||
{ | ||
$this->expectException(ConversionException::class); | ||
$this->expectExceptionMessage( | ||
"Could not convert database value to 'enum' as an error was triggered by the unserialization:" | ||
. " 'unserialize(): Error at offset 0 of 7 bytes'" | ||
); | ||
$this->type->convertToPHPValue('abcdefg', $this->platform); | ||
} | ||
|
||
public function testNullConversion(): void | ||
{ | ||
self::assertNull($this->type->convertToPHPValue(null, $this->platform)); | ||
} | ||
} |