aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2015-10-25 15:55:19 +0100
committerJeremy Benoist <jeremy.benoist@gmail.com>2015-11-07 14:15:33 +0100
commit735068d1814a4b7a688e1d19cd17b13e8c5da3eb (patch)
tree2f97dd9e9e11fa481c80273827f953b02c76b888 /src
parentbd0f3d32c9ccb8f7a1409edb960b909a5e6a096d (diff)
downloadwallabag-735068d1814a4b7a688e1d19cd17b13e8c5da3eb.tar.gz
wallabag-735068d1814a4b7a688e1d19cd17b13e8c5da3eb.tar.zst
wallabag-735068d1814a4b7a688e1d19cd17b13e8c5da3eb.zip
Add tests on TablePrefixSubscriber
Diffstat (limited to 'src')
-rw-r--r--src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php8
-rw-r--r--src/Wallabag/CoreBundle/Tests/Subscriber/TablePrefixSubscriberTest.php115
2 files changed, 121 insertions, 2 deletions
diff --git a/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php b/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php
index 8ec85e64..0019eead 100644
--- a/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php
+++ b/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php
@@ -8,6 +8,9 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo;
8 8
9/** 9/**
10 * Puts a prefix to each table. 10 * Puts a prefix to each table.
11 * This way were used instead of using the built-in strategy from Doctrine, using `naming_strategy`
12 * Because it conflicts with the DefaultQuoteStrategy (that espace table name, like user for Postgres)
13 * see #1498 for more detail.
11 * 14 *
12 * Solution from : 15 * Solution from :
13 * - http://stackoverflow.com/a/23860613/569101 16 * - http://stackoverflow.com/a/23860613/569101
@@ -30,17 +33,18 @@ class TablePrefixSubscriber implements EventSubscriber
30 public function loadClassMetadata(LoadClassMetadataEventArgs $args) 33 public function loadClassMetadata(LoadClassMetadataEventArgs $args)
31 { 34 {
32 $classMetadata = $args->getClassMetadata(); 35 $classMetadata = $args->getClassMetadata();
36
33 // if we are in an inheritance hierarchy, only apply this once 37 // if we are in an inheritance hierarchy, only apply this once
34 if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) { 38 if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
35 return; 39 return;
36 } 40 }
37 41
38 $classMetadata->setTableName($this->prefix . $classMetadata->getTableName()); 42 $classMetadata->setTableName($this->prefix.$classMetadata->getTableName());
39 43
40 foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) { 44 foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
41 if ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY && isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])) { 45 if ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY && isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])) {
42 $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name']; 46 $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
43 $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName; 47 $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix.$mappedTableName;
44 } 48 }
45 } 49 }
46 } 50 }
diff --git a/src/Wallabag/CoreBundle/Tests/Subscriber/TablePrefixSubscriberTest.php b/src/Wallabag/CoreBundle/Tests/Subscriber/TablePrefixSubscriberTest.php
new file mode 100644
index 00000000..4c138610
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Tests/Subscriber/TablePrefixSubscriberTest.php
@@ -0,0 +1,115 @@
1<?php
2
3namespace Wallabag\CoreBundle\Tests\Subscriber;
4
5use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6use Doctrine\ORM\Mapping\ClassMetadata;
7use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
8use Doctrine\Common\EventManager;
9use Wallabag\CoreBundle\Subscriber\TablePrefixSubscriber;
10
11class TablePrefixSubscriberTest extends KernelTestCase
12{
13 public function dataForPrefix()
14 {
15 return array(
16 array('wallabag_', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'wallabag_user', '"wallabag_user"', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()),
17 array('wallabag_', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'wallabag_user', '`wallabag_user`', new \Doctrine\DBAL\Platforms\MySqlPlatform()),
18 array('wallabag_', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'wallabag_user', '"wallabag_user"', new \Doctrine\DBAL\Platforms\SqlitePlatform()),
19
20 array('wallabag_', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'wallabag_user', 'wallabag_user', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()),
21 array('wallabag_', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'wallabag_user', 'wallabag_user', new \Doctrine\DBAL\Platforms\MySqlPlatform()),
22 array('wallabag_', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'wallabag_user', 'wallabag_user', new \Doctrine\DBAL\Platforms\SqlitePlatform()),
23
24 array('', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'user', '"user"', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()),
25 array('', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'user', '`user`', new \Doctrine\DBAL\Platforms\MySqlPlatform()),
26 array('', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'user', '"user"', new \Doctrine\DBAL\Platforms\SqlitePlatform()),
27
28 array('', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'user', 'user', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()),
29 array('', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'user', 'user', new \Doctrine\DBAL\Platforms\MySqlPlatform()),
30 array('', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'user', 'user', new \Doctrine\DBAL\Platforms\SqlitePlatform()),
31 );
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);
46 $metaClass->setPrimaryTable(array('name' => $tableName));
47
48 $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em);
49
50 $this->assertEquals($tableNameExpected, $metaDataEvent->getClassMetadata()->getTableName());
51
52 $subscriber->loadClassMetadata($metaDataEvent);
53
54 $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName());
55 $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform));
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);
68 $metaClass->setPrimaryTable(array('name' => $tableName));
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
79 $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName());
80 $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform));
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');
92 $metaClass->setPrimaryTable(array('name' => 'entry'));
93 $metaClass->mapManyToMany(array(
94 'fieldName' => 'tags',
95 'joinTable' => array('name' => null, 'schema' => null),
96 'targetEntity' => 'Tag',
97 'mappedBy' => null,
98 'inversedBy' => 'entries',
99 'cascade' => array('persist'),
100 'indexBy' => null,
101 'orphanRemoval' => false,
102 'fetch' => 2,
103 ));
104
105 $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em);
106
107 $this->assertEquals('entry', $metaDataEvent->getClassMetadata()->getTableName());
108
109 $subscriber->loadClassMetadata($metaDataEvent);
110
111 $this->assertEquals('yo_entry', $metaDataEvent->getClassMetadata()->getTableName());
112 $this->assertEquals('yo_entry_tag', $metaDataEvent->getClassMetadata()->associationMappings['tags']['joinTable']['name']);
113 $this->assertEquals('yo_entry', $metaDataEvent->getClassMetadata()->getQuotedTableName(new \Doctrine\DBAL\Platforms\MySqlPlatform()));
114 }
115}