Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppBundle\Tests;
  4.  
  5. use Doctrine\DBAL\Connection;
  6. use Doctrine\ORM\Tools\SchemaTool;
  7. use Doctrine\DBAL\Driver\PDOMysql\Driver as MySQLDriver;
  8.  
  9. class DoctrineMigrationTest extends WebTestCase
  10. {
  11. protected function setUp()
  12. {
  13. // Since I use LiipFunctionalTestBundle I usually use SQLite for my functional tests
  14. // so I setup a 2nd environment for testing the Migrations with the actual target DB.
  15. // Note this environment uses a dedicated schema (ie. my_db vs my_db_test)
  16. $this->environment = 'mysql_test';
  17.  
  18. /* @var \Doctrine\ORM\EntityManager */
  19. $em = $this->getContainer()->get('doctrine')->getManager();
  20. /** @var Connection $connection */
  21. $connection = $em->getConnection();
  22. $driver = $connection->getDriver();
  23. if (!$driver instanceof MySQLDriver) {
  24. $this->markTestSkipped('This test requires MySQL.');
  25. }
  26.  
  27. try {
  28. $databaseName = $this->getContainer()->getParameter('database_name');
  29. if (in_array($databaseName, $connection->getSchemaManager()->listDatabases())) {
  30. $schemaTool = new SchemaTool($em);
  31. // Drop all tables, so we can test on a clean DB
  32. $schemaTool->dropDatabase();
  33. }
  34. } catch (\Exception $e) {
  35. $this->fail('Could not cleanup test database for migration test: ' . $e->getMessage());
  36. }
  37. }
  38.  
  39. /**
  40. * @group mysql
  41. */
  42. public function testMigrations()
  43. {
  44. // Test if all migrations run through
  45. $output = $this->runCommand('doctrine:migrations:migrate', ['--no-interaction']);
  46. $this->assertRegExp('/\d+ sql queries\n$/', $output);
  47.  
  48. // Validate that the mapping files are correct and in sync with the database.
  49. $output = $this->runCommand('doctrine:schema:validate');
  50. $this->assertContains('[Mapping] OK - The mapping files are correct.', $output);
  51. $this->assertContains('[Database] OK - The database schema is in sync with the mapping files.', $output);
  52.  
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement