-
-
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 |
---|---|---|
|
@@ -5,7 +5,9 @@ | |
namespace Doctrine\DBAL\Tests\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\ConversionException; | ||
use Doctrine\DBAL\Types\EnumType; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Stringable; | ||
|
@@ -62,6 +64,8 @@ public function testConvertToPHPObject(): void | |
|
||
public function testConvertStringToDatabaseValue(): void | ||
{ | ||
$this->type->members = ['a', 'b']; | ||
|
||
self::assertSame('a', $this->type->convertToDatabaseValue('a', $this->platform)); | ||
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform)); | ||
} | ||
|
@@ -89,6 +93,107 @@ public function testConvertObjectToDatabaseValue(): void | |
self::assertSame('a', $this->type->convertToDatabaseValue(new EnumClass('a'), $this->platform)); | ||
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform)); | ||
} | ||
|
||
#[DataProvider('provideInvalidDataForEnumStringToDatabaseValueConversion')] | ||
public function testEnumStringDoesNotSupportInvalidValuesToDatabaseValueConversion($value): void | ||
Check failure on line 98 in tests/Types/EnumTest.php GitHub Actions / Static Analysis with Psalm (8.3)MissingParamType
Check failure on line 98 in tests/Types/EnumTest.php GitHub Actions / Coding Standards / Coding Standards (8.3)
|
||
{ | ||
$this->expectException(ConversionException::class); | ||
|
||
$this->type->convertToDatabaseValue($value, $this->platform); | ||
} | ||
|
||
public static function provideInvalidDataForEnumStringToDatabaseValueConversion() | ||
Check failure on line 105 in tests/Types/EnumTest.php GitHub Actions / Static Analysis with Psalm (8.3)MissingReturnType
Check failure on line 105 in tests/Types/EnumTest.php GitHub Actions / Static Analysis with PHPStan (8.3)
Check failure on line 105 in tests/Types/EnumTest.php GitHub Actions / Coding Standards / Coding Standards (8.3)
|
||
{ | ||
return [ | ||
'boolean true' => [true], | ||
'boolean false' => [false], | ||
'integer' => [17], | ||
'string' => ['not_in_members'], | ||
'array' => [['']], | ||
'enum' => [EnumPhp::A], | ||
'enum backed' => [EnumPhpBacked::A], | ||
'object' => [new \stdClass()], | ||
'stringable' => [new class() { function __toString() { return 'a'; }}], | ||
Check failure on line 116 in tests/Types/EnumTest.php GitHub Actions / Coding Standards / Coding Standards (8.3)
|
||
]; | ||
} | ||
|
||
#[DataProvider('provideInvalidDataForEnumPhpToDatabaseValueConversion')] | ||
public function testEnumPhpDoesNotSupportInvalidValuesToDatabaseValueConversion($value): void | ||
{ | ||
$this->expectException(ConversionException::class); | ||
|
||
$this->type->convertToDatabaseValue($value, $this->platform); | ||
} | ||
|
||
public static function provideInvalidDataForEnumPhpToDatabaseValueConversion() | ||
Check failure on line 128 in tests/Types/EnumTest.php GitHub Actions / Static Analysis with Psalm (8.3)MissingReturnType
|
||
{ | ||
return [ | ||
'boolean true' => [true], | ||
'boolean false' => [false], | ||
'integer' => [17], | ||
'string' => ['a'], | ||
'array' => [['']], | ||
'enum' => [WrongEnumPhp::WRONG], | ||
'enum backed' => [EnumPhpBacked::A], | ||
'object' => [new \stdClass()], | ||
'stringable' => [new class() { function __toString() { return 'a'; }}], | ||
]; | ||
} | ||
|
||
#[DataProvider('provideInvalidDataForEnumPhpBackedToDatabaseValueConversion')] | ||
public function testEnumPhpBackedDoesNotSupportInvalidValuesToDatabaseValueConversion($value): void | ||
{ | ||
$this->expectException(ConversionException::class); | ||
|
||
$this->type->convertToDatabaseValue($value, $this->platform); | ||
} | ||
|
||
public static function provideInvalidDataForEnumPhpBackedToDatabaseValueConversion() | ||
Check failure on line 151 in tests/Types/EnumTest.php GitHub Actions / Static Analysis with Psalm (8.3)MissingReturnType
|
||
{ | ||
return [ | ||
'boolean true' => [true], | ||
'boolean false' => [false], | ||
'integer' => [17], | ||
'string' => ['a'], | ||
'array' => [['']], | ||
'enum' => [EnumPhp::A], | ||
'enum backed' => [WrongEnumPhpBacked::WRONG], | ||
'object' => [new \stdClass()], | ||
'stringable' => [new class() { function __toString() { return 'a'; }}], | ||
]; | ||
} | ||
|
||
#[DataProvider('provideInvalidDataForEnumObjectToDatabaseValueConversion')] | ||
public function testEnumObjectDoesNotSupportInvalidValuesToDatabaseValueConversion($value): void | ||
{ | ||
$this->expectException(ConversionException::class); | ||
|
||
$this->type->convertToDatabaseValue($value, $this->platform); | ||
} | ||
|
||
public static function provideInvalidDataForEnumObjectToDatabaseValueConversion() | ||
{ | ||
return [ | ||
'boolean true' => [true], | ||
'boolean false' => [false], | ||
'integer' => [17], | ||
'string' => ['a'], | ||
'array' => [['']], | ||
'enum' => [EnumPhp::A], | ||
'enum backed' => [EnumPhpBacked::A], | ||
'object' => [new \stdClass()], | ||
'stringable' => [new class() { function __toString() { return 'a'; }}], | ||
]; | ||
} | ||
|
||
public function testInvalidValueForDatabaseValueToEnumStringConversion(): void | ||
{ | ||
$this->type->members = ['a', 'b']; | ||
|
||
$this->expectException(ConversionException::class); | ||
|
||
$this->type->convertToPHPValue('not_in_members', $this->platform); | ||
} | ||
} | ||
|
||
enum EnumPhp | ||
|
@@ -97,13 +202,22 @@ enum EnumPhp | |
case B; | ||
} | ||
|
||
enum WrongEnumPhp | ||
{ | ||
case WRONG; | ||
} | ||
|
||
enum EnumPhpBacked: string | ||
{ | ||
case A = 'a'; | ||
case B = 'b'; | ||
} | ||
|
||
enum WrongEnumPhpBacked: string | ||
{ | ||
case WRONG = 'wrong'; | ||
} | ||
|
||
final class EnumClass implements Stringable | ||
{ | ||
public function __construct( | ||
|