-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from alexmanase/refactor-tests-to-pest
Refactor tests to Pest
- Loading branch information
Showing
18 changed files
with
519 additions
and
674 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,41 @@ | ||
<?php | ||
|
||
namespace Spatie\DbSnapshots\Commands\Test; | ||
|
||
use Illuminate\Support\Facades\Artisan; | ||
use Spatie\DbSnapshots\Test\TestCase; | ||
|
||
class CleanupTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_can_delete_old_snapshots_keeping_the_desired_number_of_snapshots() | ||
{ | ||
// Add sleep to make sure files do not have the same modified time. | ||
// They may not sort properly if all have the same timestamp. | ||
$this->clearDisk(); | ||
it('can delete old snapshots keeping the desired number of snapshots', function () { | ||
// Add sleep to make sure files do not have the same modified time. | ||
// They may not sort properly if all have the same timestamp. | ||
clearDisk(); | ||
|
||
$this->disk->put('snapshot1.sql', 'new content'); | ||
$this->disk->put('snapshot1.sql', 'new content'); | ||
|
||
sleep(1); | ||
sleep(1); | ||
|
||
$this->disk->put('snapshot2.sql', 'new content'); | ||
$this->disk->put('snapshot2.sql', 'new content'); | ||
|
||
Artisan::call('snapshot:cleanup', ['--keep' => 1]); | ||
Artisan::call('snapshot:cleanup', ['--keep' => 1]); | ||
|
||
$this->disk->assertMissing('snapshot1.sql'); | ||
$this->disk->assertExists('snapshot2.sql'); | ||
} | ||
$this->disk->assertMissing('snapshot1.sql'); | ||
$this->disk->assertExists('snapshot2.sql'); | ||
}); | ||
|
||
/** @test */ | ||
public function it_can_delete_all_snapshots_if_keep_is_zero() | ||
{ | ||
$this->clearDisk(); | ||
it('can delete all snapshots if keep is zero', function () { | ||
clearDisk(); | ||
|
||
$this->disk->put('snapshot.sql', 'new content'); | ||
$this->disk->put('snapshot.sql', 'new content'); | ||
|
||
Artisan::call('snapshot:cleanup --keep=0'); | ||
Artisan::call('snapshot:cleanup --keep=0'); | ||
|
||
$this->disk->assertMissing('snapshot.sql'); | ||
} | ||
$this->disk->assertMissing('snapshot.sql'); | ||
}); | ||
|
||
/** @test */ | ||
public function it_warns_if_keep_is_not_specified() | ||
{ | ||
$this->clearDisk(); | ||
it('warns if keep is not specified', function () { | ||
clearDisk(); | ||
|
||
$this->disk->put('snapshot.sql', 'new content'); | ||
$this->disk->put('snapshot.sql', 'new content'); | ||
|
||
Artisan::call('snapshot:cleanup'); | ||
Artisan::call('snapshot:cleanup'); | ||
|
||
$this->disk->assertExists('snapshot.sql'); | ||
$this->seeInConsoleOutput('No value for option --keep.'); | ||
} | ||
} | ||
$this->disk->assertExists('snapshot.sql'); | ||
seeInConsoleOutput('No value for option --keep.'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,132 +1,114 @@ | ||
<?php | ||
|
||
namespace Spatie\DbSnapshots\Commands\Test; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Support\Facades\Artisan; | ||
use Spatie\DbSnapshots\Test\TestCase; | ||
|
||
class CreateTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_can_create_a_snapshot_without_a_specific_name() | ||
{ | ||
Artisan::call('snapshot:create'); | ||
|
||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
it('can create a snapshot without a specific', function () { | ||
Artisan::call('snapshot:create'); | ||
|
||
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/'); | ||
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/'); | ||
} | ||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
|
||
/** @test */ | ||
public function it_can_create_a_snapshot_with_specific_name() | ||
{ | ||
Artisan::call('snapshot:create', ['name' => 'test']); | ||
expect($fileName) | ||
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/') | ||
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/') | ||
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/'); | ||
}); | ||
|
||
$this->assertFileOnDiskPassesRegex('test.sql', '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
} | ||
it('can create a snapshot with specific name') | ||
->tap(fn () => Artisan::call('snapshot:create', ['name' => 'test'])) | ||
->expect('test.sql') | ||
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
|
||
/** @test */ | ||
public function it_can_create_a_compressed_snapshot_from_cli_param() | ||
{ | ||
Artisan::call('snapshot:create', ['--compress' => true]); | ||
it('can create a compressed snapshot from CLI param', function () { | ||
Artisan::call('snapshot:create', ['--compress' => true]); | ||
|
||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql.gz'; | ||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql.gz'; | ||
|
||
$this->disk->assertExists($fileName); | ||
$this->disk->assertExists($fileName); | ||
|
||
$this->assertNotEmpty(gzdecode($this->disk->get($fileName))); | ||
} | ||
expect( | ||
gzdecode($this->disk->get($fileName)) | ||
)->not->toBeEmpty(); | ||
}); | ||
|
||
/** @test */ | ||
public function it_can_create_a_compressed_snapshot_from_config() | ||
{ | ||
$this->app['config']->set('db-snapshots.compress', true); | ||
it('can create a compressed snapshot from config', function () { | ||
$this->app['config']->set('db-snapshots.compress', true); | ||
|
||
Artisan::call('snapshot:create'); | ||
Artisan::call('snapshot:create'); | ||
|
||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql.gz'; | ||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql.gz'; | ||
|
||
$this->disk->assertExists($fileName); | ||
$this->disk->assertExists($fileName); | ||
|
||
$this->assertNotEmpty(gzdecode($this->disk->get($fileName))); | ||
} | ||
expect(gzdecode($this->disk->get($fileName)))->not->toBeEmpty(); | ||
}); | ||
|
||
/** @test */ | ||
public function it_can_create_a_snapshot_with_specific_tables_specified_in_the_command_options() | ||
{ | ||
Artisan::call('snapshot:create', ['--table' => ['users', 'posts']]); | ||
it('can create a snapshot with specific tables specified in the command options', function () { | ||
Artisan::call('snapshot:create', ['--table' => ['users', 'posts']]); | ||
|
||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
|
||
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/'); | ||
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/'); | ||
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
} | ||
expect($fileName) | ||
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/') | ||
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/') | ||
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
}); | ||
|
||
/** @test */ | ||
public function it_can_create_a_snapshot_with_specific_tables_specified_in_the_command_options_as_a_string() | ||
{ | ||
Artisan::call('snapshot:create', ['--table' => 'users,posts']); | ||
it('can create a snapshot with specific tables specified in the command options as a string', function () { | ||
Artisan::call('snapshot:create', ['--table' => 'users,posts']); | ||
|
||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
|
||
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/'); | ||
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/'); | ||
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
} | ||
expect($fileName) | ||
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/') | ||
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/') | ||
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
}); | ||
|
||
/** @test */ | ||
public function it_can_create_a_snapshot_with_specific_tables_specified_in_the_config() | ||
{ | ||
$this->app['config']->set('db-snapshots.tables', ['users', 'posts']); | ||
it('can create a snapshot with specific tables specified in the config', function () { | ||
$this->app['config']->set('db-snapshots.tables', ['users', 'posts']); | ||
|
||
Artisan::call('snapshot:create'); | ||
Artisan::call('snapshot:create'); | ||
|
||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
|
||
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/'); | ||
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/'); | ||
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
} | ||
expect($fileName) | ||
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/') | ||
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/') | ||
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
}); | ||
|
||
/** @test */ | ||
public function it_can_create_a_snapshot_without_excluded_tables_specified_in_the_command_options() | ||
{ | ||
Artisan::call('snapshot:create', ['--exclude' => ['users', 'posts']]); | ||
it('can create a snapshot without excluded tables specified in the command options', function () { | ||
Artisan::call('snapshot:create', ['--exclude' => ['users', 'posts']]); | ||
|
||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
|
||
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/'); | ||
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/'); | ||
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
} | ||
expect($fileName) | ||
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/') | ||
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/') | ||
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
}); | ||
|
||
/** @test */ | ||
public function it_can_create_a_snapshot_without_excluded_tables_specified_in_the_command_options_as_a_string() | ||
{ | ||
Artisan::call('snapshot:create', ['--exclude' => 'users,posts']); | ||
it('can create a snapshot without excluded tables specified in the command options as a string', function () { | ||
Artisan::call('snapshot:create', ['--exclude' => 'users,posts']); | ||
|
||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
|
||
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/'); | ||
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/'); | ||
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
} | ||
expect($fileName) | ||
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/') | ||
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/') | ||
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
}); | ||
|
||
/** @test */ | ||
public function it_can_create_a_snapshot_without_excluded_tables_specified_in_the_config() | ||
{ | ||
$this->app['config']->set('db-snapshots.exclude', ['users', 'posts']); | ||
it('can create a snapshot without excluded tables specified in the config', function () { | ||
$this->app['config']->set('db-snapshots.exclude', ['users', 'posts']); | ||
|
||
Artisan::call('snapshot:create'); | ||
Artisan::call('snapshot:create'); | ||
|
||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; | ||
|
||
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/'); | ||
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/'); | ||
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
} | ||
} | ||
expect($fileName) | ||
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/') | ||
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/') | ||
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,33 @@ | ||
<?php | ||
|
||
namespace Spatie\DbSnapshots\Commands\Test; | ||
|
||
use Illuminate\Support\Facades\Artisan; | ||
use Mockery as m; | ||
use Spatie\DbSnapshots\Test\TestCase; | ||
|
||
class DeleteTest extends TestCase | ||
{ | ||
/** @var \Spatie\DbSnapshots\Commands\Delete|m\Mock */ | ||
protected $command; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->command = m::mock('Spatie\DbSnapshots\Commands\Delete[choice]'); | ||
beforeEach(function () { | ||
$this->command = m::mock('Spatie\DbSnapshots\Commands\Delete[choice]'); | ||
|
||
$this->app->bind('command.snapshot:delete', function () { | ||
return $this->command; | ||
}); | ||
} | ||
$this->app->bind('command.snapshot:delete', function () { | ||
return $this->command; | ||
}); | ||
}); | ||
|
||
/** @test */ | ||
public function it_can_delete_a_snapshot() | ||
{ | ||
$this->disk->assertExists('snapshot2.sql'); | ||
it('can delete a snapshot', function () { | ||
$this->disk->assertExists('snapshot2.sql'); | ||
|
||
$this->command | ||
->shouldReceive('choice') | ||
->once() | ||
->andReturn('snapshot2'); | ||
$this->command | ||
->shouldReceive('choice') | ||
->once() | ||
->andReturn('snapshot2'); | ||
|
||
Artisan::call('snapshot:delete'); | ||
Artisan::call('snapshot:delete'); | ||
|
||
$this->disk->assertMissing('snapshot2.sql'); | ||
} | ||
$this->disk->assertMissing('snapshot2.sql'); | ||
}); | ||
|
||
/** @test */ | ||
public function it_can_delete_a_snapshot_with_a_specific_name() | ||
{ | ||
$this->disk->assertExists('snapshot2.sql'); | ||
it('can delete a snapshot with a specific name', function () { | ||
$this->disk->assertExists('snapshot2.sql'); | ||
|
||
Artisan::call('snapshot:delete', ['name' => 'snapshot2']); | ||
Artisan::call('snapshot:delete', ['name' => 'snapshot2']); | ||
|
||
$this->disk->assertMissing('snapshot2.sql'); | ||
} | ||
} | ||
$this->disk->assertMissing('snapshot2.sql'); | ||
}); |
Oops, something went wrong.