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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ jobs:
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/phpunit --no-coverage
run: vendor/bin/pest --no-coverage
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"require-dev": {
"mockery/mockery": "^1.4",
"orchestra/testbench": "^5.0|^6.0|^7.0",
"pestphp/pest-plugin-laravel": "^1.3",
"phpunit/phpunit": "^9.5"
},
"autoload": {
Expand All @@ -39,10 +40,13 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit"
"test": "vendor/bin/pest"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"extra": {
"laravel": {
Expand All @@ -53,4 +57,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
60 changes: 24 additions & 36 deletions tests/Commands/CleanupTest.php
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.');
});
172 changes: 77 additions & 95 deletions tests/Commands/CreateTest.php
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"/');
});
56 changes: 20 additions & 36 deletions tests/Commands/DeleteTest.php
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');
});
Loading