3 namespace Tests\Wallabag\CoreBundle\Subscriber
;
5 use Doctrine\Common\EventManager
;
6 use Doctrine\ORM\Event\LoadClassMetadataEventArgs
;
7 use Doctrine\ORM\Mapping\ClassMetadata
;
8 use Wallabag\CoreBundle\Subscriber\TablePrefixSubscriber
;
10 class TablePrefixSubscriberTest
extends \PHPUnit_Framework_TestCase
12 public function dataForPrefix()
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()],
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()],
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()],
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()],
34 * @dataProvider dataForPrefix
36 public function testPrefix($prefix, $entityName, $tableName, $tableNameExpected, $finalTableName, $finalTableNameQuoted, $platform)
38 $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
39 ->disableOriginalConstructor()
42 $subscriber = new TablePrefixSubscriber($prefix);
44 $metaClass = new ClassMetadata($entityName);
45 $metaClass->setPrimaryTable(['name' => $tableName]);
47 $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em);
49 $this->assertEquals($tableNameExpected, $metaDataEvent->getClassMetadata()->getTableName());
51 $subscriber->loadClassMetadata($metaDataEvent);
53 $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName());
54 $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform));
58 * @dataProvider dataForPrefix
60 public function testSubscribedEvents($prefix, $entityName, $tableName, $tableNameExpected, $finalTableName, $finalTableNameQuoted, $platform)
62 $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
63 ->disableOriginalConstructor()
66 $metaClass = new ClassMetadata($entityName);
67 $metaClass->setPrimaryTable(['name' => $tableName]);
69 $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em);
71 $subscriber = new TablePrefixSubscriber($prefix);
73 $evm = new EventManager();
74 $evm->addEventSubscriber($subscriber);
76 $evm->dispatchEvent('loadClassMetadata', $metaDataEvent);
78 $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName());
79 $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform));
82 public function testPrefixManyToMany()
84 $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
85 ->disableOriginalConstructor()
88 $subscriber = new TablePrefixSubscriber('yo_');
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',
97 'inversedBy' => 'entries',
98 'cascade' => ['persist'],
100 'orphanRemoval' => false,
104 $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em);
106 $this->assertEquals('entry', $metaDataEvent->getClassMetadata()->getTableName());
108 $subscriber->loadClassMetadata($metaDataEvent);
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()));