-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Types; | ||
|
||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Types\Types; | ||
|
||
class EnumTest extends FunctionalTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$table = new Table('enum_table'); | ||
$table->addColumn('val', Types::ENUM, ['members' => ['a', 'b']]); | ||
|
||
$this->dropAndCreateTable($table); | ||
} | ||
|
||
public function testInsertAndSelect(): void | ||
{ | ||
$val = 'b'; | ||
|
||
$result = $this->connection->insert('enum_table', ['val' => $val]); | ||
self::assertSame(1, $result); | ||
|
||
$value = $this->connection->fetchOne('SELECT val FROM enum_table'); | ||
|
||
self::assertEquals($val, $value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
Check warning on line 1 in tests/Types/EnumTest.php GitHub Actions / Coding Standards / Coding Standards (8.3)
|
||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\EnumType; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use function anonymous_class; | ||
Check failure on line 12 in tests/Types/EnumTest.php GitHub Actions / Coding Standards / Coding Standards (8.3)
|
||
|
||
class EnumTest extends TestCase | ||
{ | ||
private AbstractPlatform&MockObject $platform; | ||
private EnumType $type; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->platform = $this->createMock(AbstractPlatform::class); | ||
$this->type = new EnumType(); | ||
} | ||
|
||
public function testReturnsSQLDeclaration(): void | ||
{ | ||
self::assertSame('ENUM(\'a\', \'b\')', $this->type->getSQLDeclaration(['members' => ['a', 'b']], $this->platform)); | ||
Check warning on line 27 in tests/Types/EnumTest.php GitHub Actions / Coding Standards / Coding Standards (8.3)
|
||
} | ||
|
||
public function testConvertToPHPValue(): void | ||
{ | ||
self::assertIsString($this->type->convertToPHPValue('foo', $this->platform)); | ||
self::assertIsString($this->type->convertToPHPValue('', $this->platform)); | ||
self::assertNull($this->type->convertToPHPValue(null, $this->platform)); | ||
} | ||
|
||
public function testConvertToPHPEnum(): void | ||
{ | ||
$this->type->enumClassname = EnumPhp::class; | ||
|
||
self::assertInstanceOf($this->type->enumClassname, $this->type->convertToPHPValue('A', $this->platform)); | ||
self::assertSame(EnumPhp::A, $this->type->convertToPHPValue('A', $this->platform)); | ||
self::assertNull($this->type->convertToPHPValue(null, $this->platform)); | ||
} | ||
|
||
public function testConvertToPHPEnumBacked(): void | ||
{ | ||
$this->type->enumClassname = EnumPhpBacked::class; | ||
|
||
self::assertInstanceOf($this->type->enumClassname, $this->type->convertToPHPValue('a', $this->platform)); | ||
self::assertSame(EnumPhpBacked::A, $this->type->convertToPHPValue('a', $this->platform)); | ||
self::assertNull($this->type->convertToPHPValue(null, $this->platform)); | ||
} | ||
|
||
public function testConvertToPHPObject(): void | ||
{ | ||
$this->type->enumClassname = EnumClass::class; | ||
|
||
self::assertInstanceOf($this->type->enumClassname, $this->type->convertToPHPValue('a', $this->platform)); | ||
self::assertEquals(new EnumClass('a'), $this->type->convertToPHPValue('a', $this->platform)); | ||
self::assertNull($this->type->convertToPHPValue(null, $this->platform)); | ||
} | ||
|
||
public function testConvertStringToDatabaseValue(): void | ||
{ | ||
self::assertSame('a', $this->type->convertToDatabaseValue('a', $this->platform)); | ||
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform)); | ||
} | ||
|
||
public function testConvertEnumToDatabaseValue(): void | ||
{ | ||
$this->type->enumClassname = EnumPhp::class; | ||
|
||
self::assertSame('A', $this->type->convertToDatabaseValue(EnumPhp::A, $this->platform)); | ||
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform)); | ||
} | ||
|
||
public function testConvertEnumBackedToDatabaseValue(): void | ||
{ | ||
$this->type->enumClassname = EnumPhpBacked::class; | ||
|
||
self::assertSame('a', $this->type->convertToDatabaseValue(EnumPhpBacked::A, $this->platform)); | ||
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform)); | ||
} | ||
|
||
public function testConvertObjectToDatabaseValue(): void | ||
{ | ||
$this->type->enumClassname = EnumClass::class; | ||
|
||
self::assertSame('a', $this->type->convertToDatabaseValue(new EnumClass('a'), $this->platform)); | ||
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform)); | ||
} | ||
} | ||
|
||
enum EnumPhp | ||
{ | ||
case A; | ||
case B; | ||
} | ||
|
||
|
||
enum EnumPhpBacked: string | ||
{ | ||
case A = 'a'; | ||
case B = 'b'; | ||
} | ||
|
||
final class EnumClass implements \Stringable { | ||
Check failure on line 108 in tests/Types/EnumTest.php GitHub Actions / Coding Standards / Coding Standards (8.3)
|
||
function __construct( | ||
private ?string $value | ||
) { | ||
} | ||
|
||
function __tostring(): string | ||
Check failure on line 114 in tests/Types/EnumTest.php GitHub Actions / Coding Standards / Coding Standards (8.3)
|
||
{ | ||
return $this->value; | ||
Check failure on line 116 in tests/Types/EnumTest.php GitHub Actions / Static Analysis with Psalm (8.3)NullableReturnStatement
|
||
} | ||
}; | ||
Check failure on line 118 in tests/Types/EnumTest.php GitHub Actions / Coding Standards / Coding Standards (8.3)
|