1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Thunder\Platenum\Tests; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\DriverManager; |
6
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
7
|
|
|
use Doctrine\ORM\Configuration; |
8
|
|
|
use Doctrine\ORM\EntityManager; |
9
|
|
|
use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver; |
10
|
|
|
use Thunder\Platenum\Doctrine\PlatenumDoctrineType; |
11
|
|
|
use Thunder\Platenum\Tests\Fake\DoctrineEntity; |
12
|
|
|
use Thunder\Platenum\Tests\Fake\DoctrineIntEnum; |
13
|
|
|
use Thunder\Platenum\Tests\Fake\DoctrineStringEnum; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
final class DoctrineTest extends AbstractTestCase |
19
|
|
|
{ |
20
|
|
|
public function testCreateFromMember(): void |
21
|
|
|
{ |
22
|
|
|
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'dbname' => ':memory:']); |
23
|
|
|
$connection->exec('CREATE TABLE doctrine_entity ( |
|
|
|
|
24
|
|
|
id INTEGER NOT NULL PRIMARY KEY, |
25
|
|
|
int_value INTEGER NOT NULL, |
26
|
|
|
string_value VARCHAR(20) NOT NULL, |
27
|
|
|
nullable_value VARCHAR(20) NULL |
28
|
|
|
)'); |
29
|
|
|
$configuration = new Configuration(); |
30
|
|
|
$configuration->setMetadataDriverImpl(new StaticPHPDriver([__DIR__.'/Fake'])); |
31
|
|
|
$configuration->setProxyDir(__DIR__.'/../var/doctrine'); |
32
|
|
|
$configuration->setProxyNamespace('Platenum\\Doctrine'); |
33
|
|
|
|
34
|
|
|
PlatenumDoctrineType::registerInteger('intEnum', DoctrineIntEnum::class); |
35
|
|
|
PlatenumDoctrineType::registerString('stringEnum', DoctrineStringEnum::class); |
36
|
|
|
|
37
|
|
|
$entity = new DoctrineEntity(1337, DoctrineIntEnum::FIRST(), DoctrineStringEnum::TWO()); |
38
|
|
|
$em = EntityManager::create($connection, $configuration); |
39
|
|
|
$em->persist($entity); |
40
|
|
|
$em->flush(); |
41
|
|
|
$em->clear(); |
42
|
|
|
|
43
|
|
|
$foundEntity = $em->find(DoctrineEntity::class, 1337); |
44
|
|
|
$this->assertInstanceOf(DoctrineEntity::class, $foundEntity); |
45
|
|
|
$this->assertSame($entity->getId(), $foundEntity->getId()); |
46
|
|
|
$this->assertSame($entity->getIntValue(), $foundEntity->getIntValue()); |
47
|
|
|
$this->assertSame($entity->getStringValue(), $foundEntity->getStringValue()); |
48
|
|
|
$this->assertNull($foundEntity->getNullableValue()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testDoctrineType(): void |
52
|
|
|
{ |
53
|
|
|
PlatenumDoctrineType::registerInteger('intEnum0', DoctrineIntEnum::class); |
54
|
|
|
$intType = PlatenumDoctrineType::getType('intEnum0'); |
55
|
|
|
|
56
|
|
|
$platform = new MySqlPlatform(); |
57
|
|
|
$this->assertTrue($intType->requiresSQLCommentHint($platform)); |
58
|
|
|
$this->assertSame('intEnum0', $intType->getName()); |
59
|
|
|
$this->assertSame('INT', $intType->getSQLDeclaration([], $platform)); |
60
|
|
|
|
61
|
|
|
PlatenumDoctrineType::registerString('stringEnum0', DoctrineStringEnum::class); |
62
|
|
|
$stringType = PlatenumDoctrineType::getType('stringEnum0'); |
63
|
|
|
$this->assertSame('VARCHAR(255)', $stringType->getSQLDeclaration([], $platform)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testInvalidClass(): void |
67
|
|
|
{ |
68
|
|
|
$this->expectException(\LogicException::class); |
69
|
|
|
$this->expectExceptionMessage('PlatenumDoctrineType allows only Platenum enumerations, `stdClass` given.'); |
70
|
|
|
PlatenumDoctrineType::registerInteger('invalid', \stdClass::class); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testDuplicateAlias(): void |
74
|
|
|
{ |
75
|
|
|
PlatenumDoctrineType::registerString('enumX', DoctrineIntEnum::class); |
76
|
|
|
|
77
|
|
|
$this->expectException(\LogicException::class); |
78
|
|
|
$this->expectExceptionMessage('Alias `'.DoctrineIntEnum::class.'` was already registered in PlatenumDoctrineType.'); |
79
|
|
|
PlatenumDoctrineType::registerString('enumX', DoctrineIntEnum::class); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testImpossibleDatabaseConversionWithUnsupportedValue(): void |
83
|
|
|
{ |
84
|
|
|
PlatenumDoctrineType::registerString('impossibleEnumConvert', DoctrineIntEnum::class); |
85
|
|
|
$this->expectException(\LogicException::class); |
86
|
|
|
PlatenumDoctrineType::getType('impossibleEnumConvert')->convertToDatabaseValue('not an object', new MySqlPlatform()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testImpossibleValueConversionCast(): void |
90
|
|
|
{ |
91
|
|
|
PlatenumDoctrineType::registerString('impossibleEnumCast', DoctrineIntEnum::class); |
92
|
|
|
$result = PlatenumDoctrineType::getType('impossibleEnumCast')->convertToDatabaseValue(DoctrineIntEnum::FIRST(), new MySqlPlatform()); |
93
|
|
|
$this->assertSame('1', $result, $result); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.