]>
Commit | Line | Data |
---|---|---|
735068d1 JB |
1 | <?php |
2 | ||
48656e0e | 3 | namespace Tests\Wallabag\CoreBundle\Event\Subscriber; |
735068d1 | 4 | |
735068d1 | 5 | use Doctrine\Common\EventManager; |
619cc453 JB |
6 | use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
7 | use Doctrine\ORM\Mapping\ClassMetadata; | |
4b6942d2 | 8 | use PHPUnit\Framework\TestCase; |
48656e0e | 9 | use Wallabag\CoreBundle\Event\Subscriber\TablePrefixSubscriber; |
735068d1 | 10 | |
4b6942d2 | 11 | class TablePrefixSubscriberTest extends TestCase |
735068d1 JB |
12 | { |
13 | public function dataForPrefix() | |
14 | { | |
4094ea47 JB |
15 | return [ |
16 | ['wallabag_', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'wallabag_user', '"wallabag_user"', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()], | |
17 | ['wallabag_', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'wallabag_user', '`wallabag_user`', new \Doctrine\DBAL\Platforms\MySqlPlatform()], | |
18 | ['wallabag_', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'wallabag_user', '"wallabag_user"', new \Doctrine\DBAL\Platforms\SqlitePlatform()], | |
19 | ||
20 | ['wallabag_', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'wallabag_user', 'wallabag_user', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()], | |
21 | ['wallabag_', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'wallabag_user', 'wallabag_user', new \Doctrine\DBAL\Platforms\MySqlPlatform()], | |
22 | ['wallabag_', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'wallabag_user', 'wallabag_user', new \Doctrine\DBAL\Platforms\SqlitePlatform()], | |
23 | ||
24 | ['', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'user', '"user"', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()], | |
25 | ['', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'user', '`user`', new \Doctrine\DBAL\Platforms\MySqlPlatform()], | |
26 | ['', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'user', '"user"', new \Doctrine\DBAL\Platforms\SqlitePlatform()], | |
27 | ||
28 | ['', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'user', 'user', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()], | |
29 | ['', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'user', 'user', new \Doctrine\DBAL\Platforms\MySqlPlatform()], | |
30 | ['', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'user', 'user', new \Doctrine\DBAL\Platforms\SqlitePlatform()], | |
31 | ]; | |
735068d1 JB |
32 | } |
33 | ||
34 | /** | |
35 | * @dataProvider dataForPrefix | |
36 | */ | |
37 | public function testPrefix($prefix, $entityName, $tableName, $tableNameExpected, $finalTableName, $finalTableNameQuoted, $platform) | |
38 | { | |
39 | $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | |
40 | ->disableOriginalConstructor() | |
41 | ->getMock(); | |
42 | ||
43 | $subscriber = new TablePrefixSubscriber($prefix); | |
44 | ||
45 | $metaClass = new ClassMetadata($entityName); | |
4094ea47 | 46 | $metaClass->setPrimaryTable(['name' => $tableName]); |
735068d1 JB |
47 | |
48 | $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); | |
49 | ||
f808b016 | 50 | $this->assertSame($tableNameExpected, $metaDataEvent->getClassMetadata()->getTableName()); |
735068d1 JB |
51 | |
52 | $subscriber->loadClassMetadata($metaDataEvent); | |
53 | ||
f808b016 JB |
54 | $this->assertSame($finalTableName, $metaDataEvent->getClassMetadata()->getTableName()); |
55 | $this->assertSame($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform)); | |
735068d1 JB |
56 | } |
57 | ||
58 | /** | |
59 | * @dataProvider dataForPrefix | |
60 | */ | |
61 | public function testSubscribedEvents($prefix, $entityName, $tableName, $tableNameExpected, $finalTableName, $finalTableNameQuoted, $platform) | |
62 | { | |
63 | $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | |
64 | ->disableOriginalConstructor() | |
65 | ->getMock(); | |
66 | ||
67 | $metaClass = new ClassMetadata($entityName); | |
4094ea47 | 68 | $metaClass->setPrimaryTable(['name' => $tableName]); |
735068d1 JB |
69 | |
70 | $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); | |
71 | ||
72 | $subscriber = new TablePrefixSubscriber($prefix); | |
73 | ||
74 | $evm = new EventManager(); | |
75 | $evm->addEventSubscriber($subscriber); | |
76 | ||
77 | $evm->dispatchEvent('loadClassMetadata', $metaDataEvent); | |
78 | ||
f808b016 JB |
79 | $this->assertSame($finalTableName, $metaDataEvent->getClassMetadata()->getTableName()); |
80 | $this->assertSame($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform)); | |
735068d1 JB |
81 | } |
82 | ||
83 | public function testPrefixManyToMany() | |
84 | { | |
85 | $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | |
86 | ->disableOriginalConstructor() | |
87 | ->getMock(); | |
88 | ||
89 | $subscriber = new TablePrefixSubscriber('yo_'); | |
90 | ||
91 | $metaClass = new ClassMetadata('Wallabag\UserBundle\Entity\Entry'); | |
4094ea47 JB |
92 | $metaClass->setPrimaryTable(['name' => 'entry']); |
93 | $metaClass->mapManyToMany([ | |
735068d1 | 94 | 'fieldName' => 'tags', |
4094ea47 | 95 | 'joinTable' => ['name' => null, 'schema' => null], |
735068d1 JB |
96 | 'targetEntity' => 'Tag', |
97 | 'mappedBy' => null, | |
98 | 'inversedBy' => 'entries', | |
4094ea47 | 99 | 'cascade' => ['persist'], |
735068d1 JB |
100 | 'indexBy' => null, |
101 | 'orphanRemoval' => false, | |
102 | 'fetch' => 2, | |
4094ea47 | 103 | ]); |
735068d1 JB |
104 | |
105 | $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); | |
106 | ||
f808b016 | 107 | $this->assertSame('entry', $metaDataEvent->getClassMetadata()->getTableName()); |
735068d1 JB |
108 | |
109 | $subscriber->loadClassMetadata($metaDataEvent); | |
110 | ||
f808b016 JB |
111 | $this->assertSame('yo_entry', $metaDataEvent->getClassMetadata()->getTableName()); |
112 | $this->assertSame('yo_entry_tag', $metaDataEvent->getClassMetadata()->associationMappings['tags']['joinTable']['name']); | |
113 | $this->assertSame('yo_entry', $metaDataEvent->getClassMetadata()->getQuotedTableName(new \Doctrine\DBAL\Platforms\MySqlPlatform())); | |
735068d1 JB |
114 | } |
115 | } |