diff options
Diffstat (limited to 'tests/Wallabag/CoreBundle/Subscriber')
-rw-r--r-- | tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php b/tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php new file mode 100644 index 00000000..4ae76703 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php | |||
@@ -0,0 +1,114 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\CoreBundle\Subscriber; | ||
4 | |||
5 | use Doctrine\Common\EventManager; | ||
6 | use Doctrine\ORM\Event\LoadClassMetadataEventArgs; | ||
7 | use Doctrine\ORM\Mapping\ClassMetadata; | ||
8 | use Wallabag\CoreBundle\Subscriber\TablePrefixSubscriber; | ||
9 | |||
10 | class TablePrefixSubscriberTest extends \PHPUnit_Framework_TestCase | ||
11 | { | ||
12 | public function dataForPrefix() | ||
13 | { | ||
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 | ]; | ||
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); | ||
45 | $metaClass->setPrimaryTable(['name' => $tableName]); | ||
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); | ||
67 | $metaClass->setPrimaryTable(['name' => $tableName]); | ||
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'); | ||
91 | $metaClass->setPrimaryTable(['name' => 'entry']); | ||
92 | $metaClass->mapManyToMany([ | ||
93 | 'fieldName' => 'tags', | ||
94 | 'joinTable' => ['name' => null, 'schema' => null], | ||
95 | 'targetEntity' => 'Tag', | ||
96 | 'mappedBy' => null, | ||
97 | 'inversedBy' => 'entries', | ||
98 | 'cascade' => ['persist'], | ||
99 | 'indexBy' => null, | ||
100 | 'orphanRemoval' => false, | ||
101 | 'fetch' => 2, | ||
102 | ]); | ||
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 | } | ||