Skip to content

Commit

Permalink
Fix database persistance between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Nov 10, 2023
1 parent cfb3e2f commit 67a638f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/src/Database/Migrator/DatabaseMigrationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function create(): void
*/
public function delete(): void
{
$this->getSchemaBuilder()->drop($this->model->getTable());
$this->getSchemaBuilder()->dropIfExists($this->model->getTable());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -102,6 +109,9 @@ public function testRepository(): void
'bar',
'barfoo',
], $repository->list());

// Delete repository for next test
$repository->delete();
}

public function testMigrationNotFound(): void
Expand All @@ -111,6 +121,9 @@ public function testMigrationNotFound(): void

$this->expectException(MigrationNotFoundException::class);
$repository->get('foo');

// Delete repository for next test
$repository->delete();
}

/**
Expand Down Expand Up @@ -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();
}
}

Expand Down
29 changes: 29 additions & 0 deletions app/tests/Integration/Database/Migrator/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 67a638f

Please sign in to comment.