wallabag/tests/Event/Subscriber/TablePrefixSubscriberTest.php

121 lines
4.9 KiB
PHP
Raw Normal View History

2015-10-25 14:55:19 +00:00
<?php
2024-02-19 00:30:12 +00:00
namespace Tests\Wallabag\Event\Subscriber;
2015-10-25 14:55:19 +00:00
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
2022-08-28 14:59:43 +00:00
use Doctrine\DBAL\Platforms\SqlitePlatform;
2022-09-01 18:54:56 +00:00
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Mapping\ClassMetadata;
2017-12-16 21:38:07 +00:00
use PHPUnit\Framework\TestCase;
2024-02-19 00:30:12 +00:00
use Wallabag\Entity\User;
use Wallabag\Event\Subscriber\TablePrefixSubscriber;
2015-10-25 14:55:19 +00:00
2017-12-16 21:38:07 +00:00
class TablePrefixSubscriberTest extends TestCase
2015-10-25 14:55:19 +00:00
{
public function dataForPrefix()
{
return [
['wallabag_', User::class, '`user`', 'user', 'wallabag_user', '"wallabag_user"', new PostgreSQLPlatform()],
['wallabag_', User::class, '`user`', 'user', 'wallabag_user', '`wallabag_user`', new MySQLPlatform()],
2022-09-01 18:54:56 +00:00
['wallabag_', User::class, '`user`', 'user', 'wallabag_user', '"wallabag_user"', new SqlitePlatform()],
['wallabag_', User::class, 'user', 'user', 'wallabag_user', 'wallabag_user', new PostgreSQLPlatform()],
['wallabag_', User::class, 'user', 'user', 'wallabag_user', 'wallabag_user', new MySQLPlatform()],
2022-09-01 18:54:56 +00:00
['wallabag_', User::class, 'user', 'user', 'wallabag_user', 'wallabag_user', new SqlitePlatform()],
['', User::class, '`user`', 'user', 'user', '"user"', new PostgreSQLPlatform()],
['', User::class, '`user`', 'user', 'user', '`user`', new MySQLPlatform()],
2022-09-01 18:54:56 +00:00
['', User::class, '`user`', 'user', 'user', '"user"', new SqlitePlatform()],
['', User::class, 'user', 'user', 'user', 'user', new PostgreSQLPlatform()],
['', User::class, 'user', 'user', 'user', 'user', new MySQLPlatform()],
2022-09-01 18:54:56 +00:00
['', User::class, 'user', 'user', 'user', 'user', new SqlitePlatform()],
];
2015-10-25 14:55:19 +00:00
}
/**
* @dataProvider dataForPrefix
*/
public function testPrefix($prefix, $entityName, $tableName, $tableNameExpected, $finalTableName, $finalTableNameQuoted, $platform)
{
2022-09-01 18:54:56 +00:00
$em = $this->getMockBuilder(EntityManager::class)
2015-10-25 14:55:19 +00:00
->disableOriginalConstructor()
->getMock();
$subscriber = new TablePrefixSubscriber($prefix);
$metaClass = new ClassMetadata($entityName);
$metaClass->setPrimaryTable(['name' => $tableName]);
2015-10-25 14:55:19 +00:00
$metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em);
2017-07-01 07:52:38 +00:00
$this->assertSame($tableNameExpected, $metaDataEvent->getClassMetadata()->getTableName());
2015-10-25 14:55:19 +00:00
$subscriber->loadClassMetadata($metaDataEvent);
2017-07-01 07:52:38 +00:00
$this->assertSame($finalTableName, $metaDataEvent->getClassMetadata()->getTableName());
$this->assertSame($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform));
2015-10-25 14:55:19 +00:00
}
/**
* @dataProvider dataForPrefix
*/
public function testSubscribedEvents($prefix, $entityName, $tableName, $tableNameExpected, $finalTableName, $finalTableNameQuoted, $platform)
{
2022-09-01 18:54:56 +00:00
$em = $this->getMockBuilder(EntityManager::class)
2015-10-25 14:55:19 +00:00
->disableOriginalConstructor()
->getMock();
$metaClass = new ClassMetadata($entityName);
$metaClass->setPrimaryTable(['name' => $tableName]);
2015-10-25 14:55:19 +00:00
$metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em);
$subscriber = new TablePrefixSubscriber($prefix);
$evm = new EventManager();
$evm->addEventSubscriber($subscriber);
$evm->dispatchEvent('loadClassMetadata', $metaDataEvent);
2017-07-01 07:52:38 +00:00
$this->assertSame($finalTableName, $metaDataEvent->getClassMetadata()->getTableName());
$this->assertSame($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform));
2015-10-25 14:55:19 +00:00
}
public function testPrefixManyToMany()
{
2022-09-01 18:54:56 +00:00
$em = $this->getMockBuilder(EntityManager::class)
2015-10-25 14:55:19 +00:00
->disableOriginalConstructor()
->getMock();
$subscriber = new TablePrefixSubscriber('yo_');
2024-02-19 00:30:12 +00:00
$metaClass = new ClassMetadata('Wallabag\Entity\Entry');
$metaClass->setPrimaryTable(['name' => 'entry']);
$metaClass->mapManyToMany([
2015-10-25 14:55:19 +00:00
'fieldName' => 'tags',
'joinTable' => ['name' => null, 'schema' => null],
2015-10-25 14:55:19 +00:00
'targetEntity' => 'Tag',
'mappedBy' => null,
'inversedBy' => 'entries',
'cascade' => ['persist'],
2015-10-25 14:55:19 +00:00
'indexBy' => null,
'orphanRemoval' => false,
'fetch' => 2,
]);
2015-10-25 14:55:19 +00:00
$metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em);
2017-07-01 07:52:38 +00:00
$this->assertSame('entry', $metaDataEvent->getClassMetadata()->getTableName());
2015-10-25 14:55:19 +00:00
$subscriber->loadClassMetadata($metaDataEvent);
2017-07-01 07:52:38 +00:00
$this->assertSame('yo_entry', $metaDataEvent->getClassMetadata()->getTableName());
$this->assertSame('yo_entry_tag', $metaDataEvent->getClassMetadata()->associationMappings['tags']['joinTable']['name']);
$this->assertSame('yo_entry', $metaDataEvent->getClassMetadata()->getQuotedTableName(new MySQLPlatform()));
2015-10-25 14:55:19 +00:00
}
}