From 735068d1814a4b7a688e1d19cd17b13e8c5da3eb Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 25 Oct 2015 15:55:19 +0100 Subject: [PATCH] Add tests on TablePrefixSubscriber --- .../Subscriber/TablePrefixSubscriber.php | 8 +- .../Subscriber/TablePrefixSubscriberTest.php | 115 ++++++++++++++++++ 2 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Tests/Subscriber/TablePrefixSubscriberTest.php diff --git a/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php b/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php index 8ec85e643..0019eead2 100644 --- a/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php +++ b/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php @@ -8,6 +8,9 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo; /** * Puts a prefix to each table. + * This way were used instead of using the built-in strategy from Doctrine, using `naming_strategy` + * Because it conflicts with the DefaultQuoteStrategy (that espace table name, like user for Postgres) + * see #1498 for more detail. * * Solution from : * - http://stackoverflow.com/a/23860613/569101 @@ -30,17 +33,18 @@ class TablePrefixSubscriber implements EventSubscriber public function loadClassMetadata(LoadClassMetadataEventArgs $args) { $classMetadata = $args->getClassMetadata(); + // if we are in an inheritance hierarchy, only apply this once if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) { return; } - $classMetadata->setTableName($this->prefix . $classMetadata->getTableName()); + $classMetadata->setTableName($this->prefix.$classMetadata->getTableName()); foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) { if ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY && isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])) { $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name']; - $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName; + $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix.$mappedTableName; } } } diff --git a/src/Wallabag/CoreBundle/Tests/Subscriber/TablePrefixSubscriberTest.php b/src/Wallabag/CoreBundle/Tests/Subscriber/TablePrefixSubscriberTest.php new file mode 100644 index 000000000..4c1386109 --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/Subscriber/TablePrefixSubscriberTest.php @@ -0,0 +1,115 @@ +getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $subscriber = new TablePrefixSubscriber($prefix); + + $metaClass = new ClassMetadata($entityName); + $metaClass->setPrimaryTable(array('name' => $tableName)); + + $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); + + $this->assertEquals($tableNameExpected, $metaDataEvent->getClassMetadata()->getTableName()); + + $subscriber->loadClassMetadata($metaDataEvent); + + $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName()); + $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform)); + } + + /** + * @dataProvider dataForPrefix + */ + public function testSubscribedEvents($prefix, $entityName, $tableName, $tableNameExpected, $finalTableName, $finalTableNameQuoted, $platform) + { + $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $metaClass = new ClassMetadata($entityName); + $metaClass->setPrimaryTable(array('name' => $tableName)); + + $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); + + $subscriber = new TablePrefixSubscriber($prefix); + + $evm = new EventManager(); + $evm->addEventSubscriber($subscriber); + + $evm->dispatchEvent('loadClassMetadata', $metaDataEvent); + + $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName()); + $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform)); + } + + public function testPrefixManyToMany() + { + $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $subscriber = new TablePrefixSubscriber('yo_'); + + $metaClass = new ClassMetadata('Wallabag\UserBundle\Entity\Entry'); + $metaClass->setPrimaryTable(array('name' => 'entry')); + $metaClass->mapManyToMany(array( + 'fieldName' => 'tags', + 'joinTable' => array('name' => null, 'schema' => null), + 'targetEntity' => 'Tag', + 'mappedBy' => null, + 'inversedBy' => 'entries', + 'cascade' => array('persist'), + 'indexBy' => null, + 'orphanRemoval' => false, + 'fetch' => 2, + )); + + $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); + + $this->assertEquals('entry', $metaDataEvent->getClassMetadata()->getTableName()); + + $subscriber->loadClassMetadata($metaDataEvent); + + $this->assertEquals('yo_entry', $metaDataEvent->getClassMetadata()->getTableName()); + $this->assertEquals('yo_entry_tag', $metaDataEvent->getClassMetadata()->associationMappings['tags']['joinTable']['name']); + $this->assertEquals('yo_entry', $metaDataEvent->getClassMetadata()->getQuotedTableName(new \Doctrine\DBAL\Platforms\MySqlPlatform())); + } +}