1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Service; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Schema\Column; |
8
|
|
|
use Doctrine\DBAL\Schema\Schema; |
9
|
|
|
use Doctrine\DBAL\Schema\Table; |
10
|
|
|
use Doctrine\DBAL\Types\StringType; |
11
|
|
|
use Doctrine\ORM\EntityManager; |
12
|
|
|
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs; |
13
|
|
|
use Doctrine\ORM\Tools\ToolEvents; |
14
|
|
|
use Ecodev\Felix\DBAL\Types\EnumType; |
15
|
|
|
use Ecodev\Felix\DBAL\Types\PhpEnumType; |
16
|
|
|
use Ecodev\Felix\Service\EnumAutoMigrator; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
|
19
|
|
|
class EnumAutoMigratorTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function testPostGenerateSchema(): void |
22
|
|
|
{ |
23
|
|
|
$enumAutoMigrator = new EnumAutoMigrator(); |
24
|
|
|
self::assertIsCallable([$enumAutoMigrator, ToolEvents::postGenerateSchema], 'must have method that can be called by Doctrine'); |
25
|
|
|
|
26
|
|
|
$enumType1 = new class() extends EnumType { |
27
|
|
|
protected function getPossibleValues(): array |
28
|
|
|
{ |
29
|
|
|
return ['key1' => 'val1']; |
30
|
|
|
} |
31
|
|
|
}; |
32
|
|
|
|
33
|
|
|
$enumType2 = new class() extends EnumType { |
34
|
|
|
protected function getPossibleValues(): array |
35
|
|
|
{ |
36
|
|
|
return ['key1' => 'val1']; |
37
|
|
|
} |
38
|
|
|
}; |
39
|
|
|
|
40
|
|
|
$phpEnumType = new class() extends PhpEnumType { |
41
|
|
|
protected function getEnumType(): string |
42
|
|
|
{ |
43
|
|
|
return TestEnum::class; |
44
|
|
|
} |
45
|
|
|
}; |
46
|
|
|
|
47
|
|
|
$col1 = new Column('col1', new StringType()); |
48
|
|
|
$col2 = new Column('col2', $enumType1); |
49
|
|
|
$col3 = new Column('col3', $enumType2); |
50
|
|
|
$col3bis = new Column('col3bis', $enumType2); |
51
|
|
|
$col4 = new Column('col4', $phpEnumType); |
52
|
|
|
|
53
|
|
|
$event = new GenerateSchemaEventArgs( |
54
|
|
|
$this->createMock(EntityManager::class), |
55
|
|
|
new Schema([ |
56
|
|
|
new Table('foo', [$col1, $col2, $col3, $col3bis, $col4]), |
57
|
|
|
]) |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$enumAutoMigrator->postGenerateSchema($event); |
61
|
|
|
|
62
|
|
|
self::assertNull($col1->getComment()); |
63
|
|
|
self::assertSame('(FelixEnum:59be1fe78104fed1c6b2e6aada4faf62)', $col2->getComment()); |
64
|
|
|
self::assertSame($col2->getComment(), $col3->getComment(), 'different enum that happen to have same definition have same hash, because it makes no difference for DB'); |
65
|
|
|
self::assertSame('(FelixEnum:59be1fe78104fed1c6b2e6aada4faf62)', $col3bis->getComment(), 'different column with exact same type must also have same hash'); |
66
|
|
|
self::assertSame('(FelixEnum:fa38e8669a8a21493a62a0d493a28ad0)', $col4->getComment(), 'native PHP enum are supported too'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|