Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests to Pest #129

Merged
merged 17 commits into from
Nov 6, 2022
Merged
Prev Previous commit
Next Next commit
refactor LoadTest
  • Loading branch information
alexmanase committed Nov 6, 2022
commit a81554b7f3e4cae40abc0c341b723104011f2a81
226 changes: 101 additions & 125 deletions tests/Commands/LoadTest.php
Original file line number Diff line number Diff line change
@@ -1,169 +1,145 @@
<?php

namespace Spatie\DbSnapshots\Commands\Test;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Mockery as m;
use Spatie\DbSnapshots\Test\TestCase;

class LoadTest extends TestCase
use function Pest\Laravel\assertDatabaseCount;
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertNotEquals;

function assertSnapshotLoaded($snapshotName)
{
/** @var \Spatie\DbSnapshots\Commands\Delete|m\Mock */
protected $command;
$nameOfTheLoadedSnapshot = getNameOfLoadedSnapshot();

public function setUp(): void
{
parent::setUp();
assertEquals(
$snapshotName,
$nameOfTheLoadedSnapshot,
"Failed to assert that `{$snapshotName}` is loaded. Current snapshot: `{$nameOfTheLoadedSnapshot}`"
);
}

$this->command = m::mock('Spatie\DbSnapshots\Commands\Load[choice]');
function assertSnapshotNotLoaded($snapshotName): void
{
assertNotEquals(
$snapshotName,
getNameOfLoadedSnapshot(),
"Failed to assert that `{$snapshotName}` was not loaded."
);
}

$this->app->bind('command.snapshot:load', function () {
return $this->command;
});
}
function getNameOfLoadedSnapshot(): string
{
$result = DB::select('select `name` from models;');

return count($result) ? $result[0]->name : '';
}

/** @test */
public function it_can_load_a_snapshot()
{
$this->assertSnapshotNotLoaded('snapshot2');
beforeEach(function () {
$this->command = m::mock('Spatie\DbSnapshots\Commands\Load[choice]');

$this->command
->shouldReceive('choice')
->once()
->andReturn('snapshot2');
$this->app->bind('command.snapshot:load', function () {
return $this->command;
});
});

Artisan::call('snapshot:load');
it('can load a snapshot', function () {
assertSnapshotNotLoaded('snapshot2');

$this->assertSnapshotLoaded('snapshot2');
}
$this->command
->shouldReceive('choice')
->once()
->andReturn('snapshot2');

/** @test */
public function it_can_load_a_snapshot_via_streaming()
{
$this->assertSnapshotNotLoaded('snapshot2');
Artisan::call('snapshot:load');

$this->command
->shouldReceive('choice')
->once()
->andReturn('snapshot2');
assertSnapshotLoaded('snapshot2');
});

Artisan::call('snapshot:load', [
'--stream' => true,
]);
it('can load a snapshot via streaming', function () {
assertSnapshotNotLoaded('snapshot2');

$this->assertSnapshotLoaded('snapshot2');
}
$this->command
->shouldReceive('choice')
->once()
->andReturn('snapshot2');

/** @test */
public function it_can_load_a_compressed_snapshot_via_streaming()
{
$this->assertSnapshotNotLoaded('snapshot4');
Artisan::call('snapshot:load', [
'--stream' => true,
]);

$this->command
->shouldReceive('choice')
->once()
->andReturn('snapshot4');
assertSnapshotLoaded('snapshot2');
});

Artisan::call('snapshot:load', [
'--stream' => true,
]);
it('can load a compressed snapshot via streaming', function () {
assertSnapshotNotLoaded('snapshot4');

$this->assertSnapshotLoaded('snapshot4');
}
$this->command
->shouldReceive('choice')
->once()
->andReturn('snapshot4');

/** @test */
public function it_drops_tables_when_loading_a_snapshot()
{
DB::insert('insert into `users` (`id`, `name`) values (1, "test")');
Artisan::call('snapshot:load', [
'--stream' => true,
]);

$this->command
->shouldReceive('choice')
->once()
->andReturn('snapshot2');
assertSnapshotLoaded('snapshot4');
});

Artisan::call('snapshot:load');
it('drops tables when loading a snapshot', function () {
DB::insert('insert into `users` (`id`, `name`) values (1, "test")');

$this->assertTableNotExists('users');
}
$this->command
->shouldReceive('choice')
->once()
->andReturn('snapshot2');

/** @test */
public function it_can_load_a_snapshot_without_dropping_existing_tables()
{
DB::insert('insert into `users` (`id`, `name`) values (1, "test")');
Artisan::call('snapshot:load');

$this->command
->shouldReceive('choice')
->once()
->andReturn('snapshot2');
assertTableNotExists('users');
});

Artisan::call('snapshot:load', ['--drop-tables' => 0]);
it('can load a snapshot without dropping existing tables', function () {
DB::insert('insert into `users` (`id`, `name`) values (1, "test")');

$this->assertDatabaseCount('users', 1);
}
$this->command
->shouldReceive('choice')
->once()
->andReturn('snapshot2');

/** @test */
public function it_can_load_a_snapshot_with_a_given_name()
{
$this->assertSnapshotNotLoaded('snapshot2');
Artisan::call('snapshot:load', ['--drop-tables' => 0]);

Artisan::call('snapshot:load', ['name' => 'snapshot2']);
assertDatabaseCount('users', 1);
});

$this->assertSnapshotLoaded('snapshot2');
}
it('can load a snapshot with a given name', function () {
assertSnapshotNotLoaded('snapshot2');

/** @test */
public function it_can_load_the_latest_snapshot()
{
$this->assertSnapshotNotLoaded('snapshot4');
Artisan::call('snapshot:load', ['name' => 'snapshot2']);

Artisan::call('snapshot:load', ['--latest' => true]);

$this->assertSnapshotLoaded('snapshot4');
}
assertSnapshotLoaded('snapshot2');
});

/** @test */
public function it_can_load_a_snapshot_with_connection_option()
{
$this->assertSnapshotNotLoaded('snapshot2');
it('can load the latest snapshot', function () {
assertSnapshotNotLoaded('snapshot4');

Artisan::call('snapshot:load', ['name' => 'snapshot2', '--connection' => 'testing']);
Artisan::call('snapshot:load', ['--latest' => true]);

$this->assertSnapshotLoaded('snapshot2');
}
assertSnapshotLoaded('snapshot4');
});

/** @test */
public function it_can_load_a_compressed_snapshot()
{
$this->assertSnapshotNotLoaded('snapshot4');
it('can load a snapshot with connection option', function () {
assertSnapshotNotLoaded('snapshot2');

Artisan::call('snapshot:load', ['name' => 'snapshot4']);

$this->assertSnapshotLoaded('snapshot4');
}
Artisan::call('snapshot:load', ['name' => 'snapshot2', '--connection' => 'testing']);

protected function assertSnapshotLoaded($snapshotName)
{
$this->assertEquals(
$snapshotName,
$this->getNameOfLoadedSnapshot(),
"Failed to assert that `{$snapshotName}` is loaded. Current snapshot: `{$this->getNameOfLoadedSnapshot()}`"
);
}
assertSnapshotLoaded('snapshot2');
});

protected function assertSnapshotNotLoaded($snapshotName)
{
$this->assertNotEquals(
$snapshotName,
$this->getNameOfLoadedSnapshot(),
"Failed to assert that `{$snapshotName}` was not loaded."
);
}
it('can load a compressed snapshot', function () {
assertSnapshotNotLoaded('snapshot4');

protected function getNameOfLoadedSnapshot(): string
{
$result = DB::select('select `name` from models;');
Artisan::call('snapshot:load', ['name' => 'snapshot4']);

return count($result) ? $result[0]->name : '';
}
}
assertSnapshotLoaded('snapshot4');
});