From 67a638f01c839a7f8b8637e4ace59168ad6a4c37 Mon Sep 17 00:00:00 2001 From: Louis Charette Date: Fri, 10 Nov 2023 16:57:04 -0500 Subject: [PATCH] Fix database persistance between tests --- .../Migrator/DatabaseMigrationRepository.php | 2 +- .../DatabaseMigrationRepositoryTest.php | 18 +++++++++++- .../Database/Migrator/MigratorTest.php | 29 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/app/src/Database/Migrator/DatabaseMigrationRepository.php b/app/src/Database/Migrator/DatabaseMigrationRepository.php index e3c0578a..42cbef89 100644 --- a/app/src/Database/Migrator/DatabaseMigrationRepository.php +++ b/app/src/Database/Migrator/DatabaseMigrationRepository.php @@ -167,7 +167,7 @@ public function create(): void */ public function delete(): void { - $this->getSchemaBuilder()->drop($this->model->getTable()); + $this->getSchemaBuilder()->dropIfExists($this->model->getTable()); } /** diff --git a/app/tests/Integration/Database/Migrator/DatabaseMigrationRepositoryTest.php b/app/tests/Integration/Database/Migrator/DatabaseMigrationRepositoryTest.php index f588fc9b..19543ce1 100644 --- a/app/tests/Integration/Database/Migrator/DatabaseMigrationRepositoryTest.php +++ b/app/tests/Integration/Database/Migrator/DatabaseMigrationRepositoryTest.php @@ -21,6 +21,9 @@ /** * DatabaseMigrationRepository Test + * + * N.B.: This can can't use the refresh database trait, since the trait uses + * part of the code we are testing. */ class DatabaseMigrationRepositoryTest extends TestCase { @@ -31,9 +34,10 @@ public function testRepositoryCreation(): void // Repository should not exists before migration is instantiated /** @var Builder */ $builder = $this->ci->get(Builder::class); - $this->assertFalse($builder->hasTable('migrationTest')); + $builder->dropIfExists('migrationTest'); // Replace the default model with a custom one for testing + $this->assertFalse($builder->hasTable('migrationTest')); $this->ci->set(MigrationTable::class, new TestMigration()); $repository = $this->ci->get(DatabaseMigrationRepository::class); @@ -52,6 +56,9 @@ public function testRepositoryCreation(): void $repository->delete(); $this->assertFalse($builder->hasTable('migrationTest')); $this->assertFalse($repository->exists()); + + // Assert deleting when table doesn't exist doesn't throw an error + $repository->delete(); } public function testRepository(): void @@ -102,6 +109,9 @@ public function testRepository(): void 'bar', 'barfoo', ], $repository->list()); + + // Delete repository for next test + $repository->delete(); } public function testMigrationNotFound(): void @@ -111,6 +121,9 @@ public function testMigrationNotFound(): void $this->expectException(MigrationNotFoundException::class); $repository->get('foo'); + + // Delete repository for next test + $repository->delete(); } /** @@ -150,6 +163,9 @@ public function testLegacyMigrations(): void // Delete $repository->remove(MigrationClassStub::class); $this->assertFalse($repository->has(MigrationClassStub::class)); + + // Delete repository for next test + $repository->delete(); } } diff --git a/app/tests/Integration/Database/Migrator/MigratorTest.php b/app/tests/Integration/Database/Migrator/MigratorTest.php index 345fd1e1..d8368f9d 100644 --- a/app/tests/Integration/Database/Migrator/MigratorTest.php +++ b/app/tests/Integration/Database/Migrator/MigratorTest.php @@ -23,11 +23,40 @@ /** * Migrator Tests + * + * N.B.: This can can't use the refresh database trait, since the trait uses + * part of the code we are testing. */ class MigratorTest extends TestCase { protected string $mainSprinkle = TestMigrateSprinkle::class; + public function setUp(): void + { + parent::setUp(); + + // Manually reset the repository and database between test + $this->resetDatabase(); + } + + public function tearDown(): void + { + // Manually reset the repository and database between test + $this->resetDatabase(); + + parent::tearDown(); + } + + protected function resetDatabase(): void + { + $repo = $this->ci->get(MigrationRepositoryInterface::class); + $repo->delete(); + $repo->create(); + + $builder = $this->ci->get(Builder::class); + $builder->dropIfExists('test'); + } + public function testGetters(): void { /** @var Migrator */