1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional\SchemaTool;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\Tests\Models;
use Doctrine\Tests\OrmFunctionalTestCase;
use function array_filter;
use function implode;
use function method_exists;
use function str_contains;
use const PHP_EOL;
/**
* WARNING: This test should be run as last test! It can affect others very easily!
*/
class DDC214Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();
$conn = $this->_em->getConnection();
if ($conn->getDatabasePlatform() instanceof SqlitePlatform) {
self::markTestSkipped('SQLite does not support ALTER TABLE statements.');
}
}
/** @group DDC-214 */
public function testCmsAddressModel(): void
{
$this->assertCreatedSchemaNeedsNoUpdates(
Models\CMS\CmsUser::class,
Models\CMS\CmsPhonenumber::class,
Models\CMS\CmsAddress::class,
Models\CMS\CmsGroup::class,
Models\CMS\CmsArticle::class,
Models\CMS\CmsEmail::class
);
}
/** @group DDC-214 */
public function testCompanyModel(): void
{
$this->assertCreatedSchemaNeedsNoUpdates(
Models\Company\CompanyPerson::class,
Models\Company\CompanyEmployee::class,
Models\Company\CompanyManager::class,
Models\Company\CompanyOrganization::class,
Models\Company\CompanyEvent::class,
Models\Company\CompanyAuction::class,
Models\Company\CompanyRaffle::class,
Models\Company\CompanyCar::class
);
}
/** @param class-string ...$classes */
public function assertCreatedSchemaNeedsNoUpdates(string ...$classes): void
{
$this->createSchemaForModels(...$classes);
$sm = $this->createSchemaManager();
$method = method_exists(AbstractSchemaManager::class, 'introspectSchema') ?
'introspectSchema' :
'createSchema';
$fromSchema = $sm->$method();
$toSchema = $this->getSchemaForModels(...$classes);
if (method_exists($sm, 'createComparator')) {
$comparator = $sm->createComparator();
} else {
$comparator = new Comparator();
}
$schemaDiff = $comparator->compareSchemas($fromSchema, $toSchema);
$sql = method_exists(AbstractPlatform::class, 'getAlterSchemaSQL') ?
$this->_em->getConnection()->getDatabasePlatform()->getAlterSchemaSQL($schemaDiff) :
$schemaDiff->toSql($this->_em->getConnection()->getDatabasePlatform());
$sql = array_filter($sql, static function ($sql) {
return ! str_contains($sql, 'DROP');
});
self::assertCount(0, $sql, 'SQL: ' . implode(PHP_EOL, $sql));
}
}
|