Skip to content

Instantly share code, notes, and snippets.

@mablae
Forked from lsmith77/DoctrineMigrationTest.php
Created July 24, 2017 18:06
Show Gist options
  • Select an option

  • Save mablae/0372f478b92cbfed92f4b6553c92d074 to your computer and use it in GitHub Desktop.

Select an option

Save mablae/0372f478b92cbfed92f4b6553c92d074 to your computer and use it in GitHub Desktop.

Revisions

  1. @lsmith77 lsmith77 revised this gist Jul 24, 2017. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions DoctrineMigrationTest.php
    Original file line number Diff line number Diff line change
    @@ -44,8 +44,11 @@ public function testMigrations()
    // Test if all migrations run through
    $output = $this->runCommand('doctrine:migrations:migrate', ['--no-interaction']);
    $this->assertRegExp('/\d+ sql queries\n$/', $output);
    // Test if any migrations are missing
    $output = $this->runCommand('doctrine:migrations:diff');
    $this->assertContains('No changes detected in your mapping information.', $output);

    // Validate that the mapping files are correct and in sync with the database.
    $output = $this->runCommand('doctrine:schema:validate');
    $this->assertContains('[Mapping] OK - The mapping files are correct.', $output);
    $this->assertContains('[Database] OK - The database schema is in sync with the mapping files.', $output);

    }
    }
  2. @lsmith77 lsmith77 revised this gist Jul 24, 2017. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion DoctrineMigrationTest.php
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,9 @@ class DoctrineMigrationTest extends WebTestCase
    {
    protected function setUp()
    {
    // Since I use LiipFunctionalTestBundle I usually use SQLite for my functional tests
    // so I setup a 2nd environment for testing the Migrations with the actual target DB.
    // Note this environment uses a dedicated schema (ie. my_db vs my_db_test)
    $this->environment = 'mysql_test';

    /* @var \Doctrine\ORM\EntityManager */
    @@ -18,13 +21,14 @@ protected function setUp()
    $connection = $em->getConnection();
    $driver = $connection->getDriver();
    if (!$driver instanceof MySQLDriver) {
    $this->markTestSkipped('This test requires a MySQL.');
    $this->markTestSkipped('This test requires MySQL.');
    }

    try {
    $databaseName = $this->getContainer()->getParameter('database_name');
    if (in_array($databaseName, $connection->getSchemaManager()->listDatabases())) {
    $schemaTool = new SchemaTool($em);
    // Drop all tables, so we can test on a clean DB
    $schemaTool->dropDatabase();
    }
    } catch (\Exception $e) {
    @@ -37,8 +41,10 @@ protected function setUp()
    */
    public function testMigrations()
    {
    // Test if all migrations run through
    $output = $this->runCommand('doctrine:migrations:migrate', ['--no-interaction']);
    $this->assertRegExp('/\d+ sql queries\n$/', $output);
    // Test if any migrations are missing
    $output = $this->runCommand('doctrine:migrations:diff');
    $this->assertContains('No changes detected in your mapping information.', $output);
    }
  3. @lsmith77 lsmith77 created this gist Jul 24, 2017.
    45 changes: 45 additions & 0 deletions DoctrineMigrationTest.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?php

    namespace AppBundle\Tests;

    use Doctrine\DBAL\Connection;
    use Doctrine\ORM\Tools\SchemaTool;
    use Doctrine\DBAL\Driver\PDOMysql\Driver as MySQLDriver;

    class DoctrineMigrationTest extends WebTestCase
    {
    protected function setUp()
    {
    $this->environment = 'mysql_test';

    /* @var \Doctrine\ORM\EntityManager */
    $em = $this->getContainer()->get('doctrine')->getManager();
    /** @var Connection $connection */
    $connection = $em->getConnection();
    $driver = $connection->getDriver();
    if (!$driver instanceof MySQLDriver) {
    $this->markTestSkipped('This test requires a MySQL.');
    }

    try {
    $databaseName = $this->getContainer()->getParameter('database_name');
    if (in_array($databaseName, $connection->getSchemaManager()->listDatabases())) {
    $schemaTool = new SchemaTool($em);
    $schemaTool->dropDatabase();
    }
    } catch (\Exception $e) {
    $this->fail('Could not cleanup test database for migration test: ' . $e->getMessage());
    }
    }

    /**
    * @group mysql
    */
    public function testMigrations()
    {
    $output = $this->runCommand('doctrine:migrations:migrate', ['--no-interaction']);
    $this->assertRegExp('/\d+ sql queries\n$/', $output);
    $output = $this->runCommand('doctrine:migrations:diff');
    $this->assertContains('No changes detected in your mapping information.', $output);
    }
    }