Add tests on TablePrefixSubscriber

This commit is contained in:
Jeremy Benoist 2015-10-25 15:55:19 +01:00
parent bd0f3d32c9
commit 735068d181
2 changed files with 121 additions and 2 deletions

View file

@ -8,6 +8,9 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo;
/** /**
* Puts a prefix to each table. * 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 : * Solution from :
* - http://stackoverflow.com/a/23860613/569101 * - http://stackoverflow.com/a/23860613/569101
@ -30,6 +33,7 @@ class TablePrefixSubscriber implements EventSubscriber
public function loadClassMetadata(LoadClassMetadataEventArgs $args) public function loadClassMetadata(LoadClassMetadataEventArgs $args)
{ {
$classMetadata = $args->getClassMetadata(); $classMetadata = $args->getClassMetadata();
// if we are in an inheritance hierarchy, only apply this once // if we are in an inheritance hierarchy, only apply this once
if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) { if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
return; return;

View file

@ -0,0 +1,115 @@
<?php
namespace Wallabag\CoreBundle\Tests\Subscriber;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\Common\EventManager;
use Wallabag\CoreBundle\Subscriber\TablePrefixSubscriber;
class TablePrefixSubscriberTest extends KernelTestCase
{
public function dataForPrefix()
{
return array(
array('wallabag_', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'wallabag_user', '"wallabag_user"', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()),
array('wallabag_', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'wallabag_user', '`wallabag_user`', new \Doctrine\DBAL\Platforms\MySqlPlatform()),
array('wallabag_', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'wallabag_user', '"wallabag_user"', new \Doctrine\DBAL\Platforms\SqlitePlatform()),
array('wallabag_', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'wallabag_user', 'wallabag_user', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()),
array('wallabag_', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'wallabag_user', 'wallabag_user', new \Doctrine\DBAL\Platforms\MySqlPlatform()),
array('wallabag_', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'wallabag_user', 'wallabag_user', new \Doctrine\DBAL\Platforms\SqlitePlatform()),
array('', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'user', '"user"', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()),
array('', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'user', '`user`', new \Doctrine\DBAL\Platforms\MySqlPlatform()),
array('', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'user', '"user"', new \Doctrine\DBAL\Platforms\SqlitePlatform()),
array('', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'user', 'user', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()),
array('', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'user', 'user', new \Doctrine\DBAL\Platforms\MySqlPlatform()),
array('', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'user', 'user', new \Doctrine\DBAL\Platforms\SqlitePlatform()),
);
}
/**
* @dataProvider dataForPrefix
*/
public function testPrefix($prefix, $entityName, $tableName, $tableNameExpected, $finalTableName, $finalTableNameQuoted, $platform)
{
$em = $this->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()));
}
}