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