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