wallabag/tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php

115 lines
5.3 KiB
PHP
Raw Normal View History

2015-10-25 15:55:19 +01:00
<?php
2016-10-30 11:27:09 +01:00
namespace Tests\Wallabag\CoreBundle\Event\Subscriber;
2015-10-25 15:55:19 +01:00
use Doctrine\Common\EventManager;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Mapping\ClassMetadata;
2016-10-30 11:27:09 +01:00
use Wallabag\CoreBundle\Event\Subscriber\TablePrefixSubscriber;
2015-10-25 15:55:19 +01:00
class TablePrefixSubscriberTest extends \PHPUnit_Framework_TestCase
2015-10-25 15:55:19 +01:00
{
public function dataForPrefix()
{
return [
['wallabag_', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'wallabag_user', '"wallabag_user"', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()],
['wallabag_', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'wallabag_user', '`wallabag_user`', new \Doctrine\DBAL\Platforms\MySqlPlatform()],
['wallabag_', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'wallabag_user', '"wallabag_user"', new \Doctrine\DBAL\Platforms\SqlitePlatform()],
['wallabag_', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'wallabag_user', 'wallabag_user', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()],
['wallabag_', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'wallabag_user', 'wallabag_user', new \Doctrine\DBAL\Platforms\MySqlPlatform()],
['wallabag_', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'wallabag_user', 'wallabag_user', new \Doctrine\DBAL\Platforms\SqlitePlatform()],
['', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'user', '"user"', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()],
['', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'user', '`user`', new \Doctrine\DBAL\Platforms\MySqlPlatform()],
['', 'Wallabag\UserBundle\Entity\User', '`user`', 'user', 'user', '"user"', new \Doctrine\DBAL\Platforms\SqlitePlatform()],
['', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'user', 'user', new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()],
['', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'user', 'user', new \Doctrine\DBAL\Platforms\MySqlPlatform()],
['', 'Wallabag\UserBundle\Entity\User', 'user', 'user', 'user', 'user', new \Doctrine\DBAL\Platforms\SqlitePlatform()],
];
2015-10-25 15:55:19 +01:00
}
/**
* @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(['name' => $tableName]);
2015-10-25 15:55:19 +01:00
$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(['name' => $tableName]);
2015-10-25 15:55:19 +01:00
$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(['name' => 'entry']);
$metaClass->mapManyToMany([
2015-10-25 15:55:19 +01:00
'fieldName' => 'tags',
'joinTable' => ['name' => null, 'schema' => null],
2015-10-25 15:55:19 +01:00
'targetEntity' => 'Tag',
'mappedBy' => null,
'inversedBy' => 'entries',
'cascade' => ['persist'],
2015-10-25 15:55:19 +01:00
'indexBy' => null,
'orphanRemoval' => false,
'fetch' => 2,
]);
2015-10-25 15:55:19 +01:00
$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()));
}
}