Skip to content

Instantly share code, notes, and snippets.

@tarlepp
Created May 16, 2024 16:09
Show Gist options
  • Save tarlepp/4ad89805123636624699426339953cd6 to your computer and use it in GitHub Desktop.
Save tarlepp/4ad89805123636624699426339953cd6 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types = 1);
namespace App\Tests\Integration;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaValidator;
use Doctrine\Persistence\ManagerRegistry;
use PHPUnit\Framework\Attributes\TestDox;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use function array_walk;
use function implode;
class SchemaTest extends KernelTestCase
{
#[TestDox('Test that entity mappings are valid')]
public function testThatMappingsAreValid(): void
{
$errors = $this->getValidator()->validateMapping();
$messages = [];
$formatter = static function (array $errors, string $className) use (&$messages): void {
$messages[] = $className . ': ' . implode(', ', $errors);
};
array_walk($errors, $formatter);
self::assertEmpty($errors, implode("\n", $messages));
}
#[TestDox('Test that database schema is sync with entity metadata')]
public function testThatSchemaInSyncWithMetadata(): void
{
self::assertTrue(
$this->getValidator()->schemaInSyncWithMetadata(),
'The database schema is not in sync with the current mapping file.'
);
}
private function getValidator(): SchemaValidator
{
self::bootKernel();
/** @var ManagerRegistry $managerRegistry */
$managerRegistry = self::$kernel->getContainer()->get('doctrine');
/** @var EntityManagerInterface $entityManager */
$entityManager = $managerRegistry->getManager();
return new SchemaValidator($entityManager);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment