Skip to content

Commit 872b9e7

Browse files
authored
Merge pull request #129 from alexmanase/refactor-tests-to-pest
Refactor tests to Pest
2 parents 237543c + ea6be0a commit 872b9e7

18 files changed

+519
-674
lines changed

.github/workflows/run-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ jobs:
5555
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
5656
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
5757
- name: Execute tests
58-
run: vendor/bin/phpunit --no-coverage
58+
run: vendor/bin/pest --no-coverage

composer.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"require-dev": {
2727
"mockery/mockery": "^1.4",
2828
"orchestra/testbench": "^5.0|^6.0|^7.0",
29+
"pestphp/pest-plugin-laravel": "^1.3",
2930
"phpunit/phpunit": "^9.5"
3031
},
3132
"autoload": {
@@ -39,10 +40,13 @@
3940
}
4041
},
4142
"scripts": {
42-
"test": "vendor/bin/phpunit"
43+
"test": "vendor/bin/pest"
4344
},
4445
"config": {
45-
"sort-packages": true
46+
"sort-packages": true,
47+
"allow-plugins": {
48+
"pestphp/pest-plugin": true
49+
}
4650
},
4751
"extra": {
4852
"laravel": {
@@ -53,4 +57,4 @@
5357
},
5458
"minimum-stability": "dev",
5559
"prefer-stable": true
56-
}
60+
}

tests/Commands/CleanupTest.php

+24-36
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,41 @@
11
<?php
22

3-
namespace Spatie\DbSnapshots\Commands\Test;
4-
53
use Illuminate\Support\Facades\Artisan;
6-
use Spatie\DbSnapshots\Test\TestCase;
74

8-
class CleanupTest extends TestCase
9-
{
10-
/** @test */
11-
public function it_can_delete_old_snapshots_keeping_the_desired_number_of_snapshots()
12-
{
13-
// Add sleep to make sure files do not have the same modified time.
14-
// They may not sort properly if all have the same timestamp.
15-
$this->clearDisk();
5+
it('can delete old snapshots keeping the desired number of snapshots', function () {
6+
// Add sleep to make sure files do not have the same modified time.
7+
// They may not sort properly if all have the same timestamp.
8+
clearDisk();
169

17-
$this->disk->put('snapshot1.sql', 'new content');
10+
$this->disk->put('snapshot1.sql', 'new content');
1811

19-
sleep(1);
12+
sleep(1);
2013

21-
$this->disk->put('snapshot2.sql', 'new content');
14+
$this->disk->put('snapshot2.sql', 'new content');
2215

23-
Artisan::call('snapshot:cleanup', ['--keep' => 1]);
16+
Artisan::call('snapshot:cleanup', ['--keep' => 1]);
2417

25-
$this->disk->assertMissing('snapshot1.sql');
26-
$this->disk->assertExists('snapshot2.sql');
27-
}
18+
$this->disk->assertMissing('snapshot1.sql');
19+
$this->disk->assertExists('snapshot2.sql');
20+
});
2821

29-
/** @test */
30-
public function it_can_delete_all_snapshots_if_keep_is_zero()
31-
{
32-
$this->clearDisk();
22+
it('can delete all snapshots if keep is zero', function () {
23+
clearDisk();
3324

34-
$this->disk->put('snapshot.sql', 'new content');
25+
$this->disk->put('snapshot.sql', 'new content');
3526

36-
Artisan::call('snapshot:cleanup --keep=0');
27+
Artisan::call('snapshot:cleanup --keep=0');
3728

38-
$this->disk->assertMissing('snapshot.sql');
39-
}
29+
$this->disk->assertMissing('snapshot.sql');
30+
});
4031

41-
/** @test */
42-
public function it_warns_if_keep_is_not_specified()
43-
{
44-
$this->clearDisk();
32+
it('warns if keep is not specified', function () {
33+
clearDisk();
4534

46-
$this->disk->put('snapshot.sql', 'new content');
35+
$this->disk->put('snapshot.sql', 'new content');
4736

48-
Artisan::call('snapshot:cleanup');
37+
Artisan::call('snapshot:cleanup');
4938

50-
$this->disk->assertExists('snapshot.sql');
51-
$this->seeInConsoleOutput('No value for option --keep.');
52-
}
53-
}
39+
$this->disk->assertExists('snapshot.sql');
40+
seeInConsoleOutput('No value for option --keep.');
41+
});

tests/Commands/CreateTest.php

+77-95
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,114 @@
11
<?php
22

3-
namespace Spatie\DbSnapshots\Commands\Test;
4-
53
use Carbon\Carbon;
64
use Illuminate\Support\Facades\Artisan;
7-
use Spatie\DbSnapshots\Test\TestCase;
8-
9-
class CreateTest extends TestCase
10-
{
11-
/** @test */
12-
public function it_can_create_a_snapshot_without_a_specific_name()
13-
{
14-
Artisan::call('snapshot:create');
155

16-
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
6+
it('can create a snapshot without a specific', function () {
7+
Artisan::call('snapshot:create');
178

18-
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
19-
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/');
20-
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/');
21-
}
9+
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
2210

23-
/** @test */
24-
public function it_can_create_a_snapshot_with_specific_name()
25-
{
26-
Artisan::call('snapshot:create', ['name' => 'test']);
11+
expect($fileName)
12+
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/')
13+
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/')
14+
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/');
15+
});
2716

28-
$this->assertFileOnDiskPassesRegex('test.sql', '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
29-
}
17+
it('can create a snapshot with specific name')
18+
->tap(fn () => Artisan::call('snapshot:create', ['name' => 'test']))
19+
->expect('test.sql')
20+
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
3021

31-
/** @test */
32-
public function it_can_create_a_compressed_snapshot_from_cli_param()
33-
{
34-
Artisan::call('snapshot:create', ['--compress' => true]);
22+
it('can create a compressed snapshot from CLI param', function () {
23+
Artisan::call('snapshot:create', ['--compress' => true]);
3524

36-
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql.gz';
25+
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql.gz';
3726

38-
$this->disk->assertExists($fileName);
27+
$this->disk->assertExists($fileName);
3928

40-
$this->assertNotEmpty(gzdecode($this->disk->get($fileName)));
41-
}
29+
expect(
30+
gzdecode($this->disk->get($fileName))
31+
)->not->toBeEmpty();
32+
});
4233

43-
/** @test */
44-
public function it_can_create_a_compressed_snapshot_from_config()
45-
{
46-
$this->app['config']->set('db-snapshots.compress', true);
34+
it('can create a compressed snapshot from config', function () {
35+
$this->app['config']->set('db-snapshots.compress', true);
4736

48-
Artisan::call('snapshot:create');
37+
Artisan::call('snapshot:create');
4938

50-
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql.gz';
39+
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql.gz';
5140

52-
$this->disk->assertExists($fileName);
41+
$this->disk->assertExists($fileName);
5342

54-
$this->assertNotEmpty(gzdecode($this->disk->get($fileName)));
55-
}
43+
expect(gzdecode($this->disk->get($fileName)))->not->toBeEmpty();
44+
});
5645

57-
/** @test */
58-
public function it_can_create_a_snapshot_with_specific_tables_specified_in_the_command_options()
59-
{
60-
Artisan::call('snapshot:create', ['--table' => ['users', 'posts']]);
46+
it('can create a snapshot with specific tables specified in the command options', function () {
47+
Artisan::call('snapshot:create', ['--table' => ['users', 'posts']]);
6148

62-
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
49+
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
6350

64-
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/');
65-
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/');
66-
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
67-
}
51+
expect($fileName)
52+
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/')
53+
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/')
54+
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
55+
});
6856

69-
/** @test */
70-
public function it_can_create_a_snapshot_with_specific_tables_specified_in_the_command_options_as_a_string()
71-
{
72-
Artisan::call('snapshot:create', ['--table' => 'users,posts']);
57+
it('can create a snapshot with specific tables specified in the command options as a string', function () {
58+
Artisan::call('snapshot:create', ['--table' => 'users,posts']);
7359

74-
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
60+
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
7561

76-
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/');
77-
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/');
78-
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
79-
}
62+
expect($fileName)
63+
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/')
64+
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/')
65+
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
66+
});
8067

81-
/** @test */
82-
public function it_can_create_a_snapshot_with_specific_tables_specified_in_the_config()
83-
{
84-
$this->app['config']->set('db-snapshots.tables', ['users', 'posts']);
68+
it('can create a snapshot with specific tables specified in the config', function () {
69+
$this->app['config']->set('db-snapshots.tables', ['users', 'posts']);
8570

86-
Artisan::call('snapshot:create');
71+
Artisan::call('snapshot:create');
8772

88-
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
73+
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
8974

90-
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/');
91-
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/');
92-
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
93-
}
75+
expect($fileName)
76+
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/')
77+
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/')
78+
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
79+
});
9480

95-
/** @test */
96-
public function it_can_create_a_snapshot_without_excluded_tables_specified_in_the_command_options()
97-
{
98-
Artisan::call('snapshot:create', ['--exclude' => ['users', 'posts']]);
81+
it('can create a snapshot without excluded tables specified in the command options', function () {
82+
Artisan::call('snapshot:create', ['--exclude' => ['users', 'posts']]);
9983

100-
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
84+
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
10185

102-
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/');
103-
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/');
104-
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
105-
}
86+
expect($fileName)
87+
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/')
88+
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/')
89+
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
90+
});
10691

107-
/** @test */
108-
public function it_can_create_a_snapshot_without_excluded_tables_specified_in_the_command_options_as_a_string()
109-
{
110-
Artisan::call('snapshot:create', ['--exclude' => 'users,posts']);
92+
it('can create a snapshot without excluded tables specified in the command options as a string', function () {
93+
Artisan::call('snapshot:create', ['--exclude' => 'users,posts']);
11194

112-
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
95+
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
11396

114-
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/');
115-
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/');
116-
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
117-
}
97+
expect($fileName)
98+
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/')
99+
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/')
100+
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
101+
});
118102

119-
/** @test */
120-
public function it_can_create_a_snapshot_without_excluded_tables_specified_in_the_config()
121-
{
122-
$this->app['config']->set('db-snapshots.exclude', ['users', 'posts']);
103+
it('can create a snapshot without excluded tables specified in the config', function () {
104+
$this->app['config']->set('db-snapshots.exclude', ['users', 'posts']);
123105

124-
Artisan::call('snapshot:create');
106+
Artisan::call('snapshot:create');
125107

126-
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
108+
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
127109

128-
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/');
129-
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/');
130-
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
131-
}
132-
}
110+
expect($fileName)
111+
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/')
112+
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/')
113+
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
114+
});

tests/Commands/DeleteTest.php

+20-36
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,33 @@
11
<?php
22

3-
namespace Spatie\DbSnapshots\Commands\Test;
4-
53
use Illuminate\Support\Facades\Artisan;
64
use Mockery as m;
7-
use Spatie\DbSnapshots\Test\TestCase;
8-
9-
class DeleteTest extends TestCase
10-
{
11-
/** @var \Spatie\DbSnapshots\Commands\Delete|m\Mock */
12-
protected $command;
13-
14-
public function setUp(): void
15-
{
16-
parent::setUp();
175

18-
$this->command = m::mock('Spatie\DbSnapshots\Commands\Delete[choice]');
6+
beforeEach(function () {
7+
$this->command = m::mock('Spatie\DbSnapshots\Commands\Delete[choice]');
198

20-
$this->app->bind('command.snapshot:delete', function () {
21-
return $this->command;
22-
});
23-
}
9+
$this->app->bind('command.snapshot:delete', function () {
10+
return $this->command;
11+
});
12+
});
2413

25-
/** @test */
26-
public function it_can_delete_a_snapshot()
27-
{
28-
$this->disk->assertExists('snapshot2.sql');
14+
it('can delete a snapshot', function () {
15+
$this->disk->assertExists('snapshot2.sql');
2916

30-
$this->command
31-
->shouldReceive('choice')
32-
->once()
33-
->andReturn('snapshot2');
17+
$this->command
18+
->shouldReceive('choice')
19+
->once()
20+
->andReturn('snapshot2');
3421

35-
Artisan::call('snapshot:delete');
22+
Artisan::call('snapshot:delete');
3623

37-
$this->disk->assertMissing('snapshot2.sql');
38-
}
24+
$this->disk->assertMissing('snapshot2.sql');
25+
});
3926

40-
/** @test */
41-
public function it_can_delete_a_snapshot_with_a_specific_name()
42-
{
43-
$this->disk->assertExists('snapshot2.sql');
27+
it('can delete a snapshot with a specific name', function () {
28+
$this->disk->assertExists('snapshot2.sql');
4429

45-
Artisan::call('snapshot:delete', ['name' => 'snapshot2']);
30+
Artisan::call('snapshot:delete', ['name' => 'snapshot2']);
4631

47-
$this->disk->assertMissing('snapshot2.sql');
48-
}
49-
}
32+
$this->disk->assertMissing('snapshot2.sql');
33+
});

0 commit comments

Comments
 (0)