Skip to content

Commit 8f7fdd5

Browse files
committed
Tests
1 parent d12ad6b commit 8f7fdd5

File tree

9 files changed

+349
-17
lines changed

9 files changed

+349
-17
lines changed

src/Repository.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ public function getLast(): array
6767
*/
6868
public function log(string $file, int $batch, array $log = []): void
6969
{
70-
Patch::create(['patch' => $file, 'batch' => $batch, 'log' => $log]);
70+
Patch::create([
71+
'patch' => $file,
72+
'batch' => $batch,
73+
'log' => $log,
74+
'ran_on' => now(),
75+
]);
7176
}
7277

7378
/**

tests/Commands/PatchMakeTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelPatches\Tests\Commands;
4+
5+
use InvalidArgumentException;
6+
use Illuminate\Support\Facades\File;
7+
use Rappasoft\LaravelPatches\Tests\TestCase;
8+
9+
class PatchMakeTest extends TestCase
10+
{
11+
/** @test */
12+
public function it_makes_a_patch_file()
13+
{
14+
$this->artisan('make:patch', ['name' => 'new_patch'])->run();
15+
16+
$this->assertDirectoryExists(database_path('patches'));
17+
$this->assertEquals(1, collect(File::files(database_path('patches')))->count());
18+
}
19+
20+
/** @test */
21+
public function it_prepopulates_the_patch_with_the_stub_file()
22+
{
23+
$this->artisan('make:patch', ['name' => 'new_patch'])->run();
24+
25+
foreach (glob(database_path('patches').'/*') as $file) {
26+
$this->assertTrue(filesize($file) > 0);
27+
}
28+
}
29+
30+
/** @test */
31+
public function it_doesnt_make_two_patches_with_the_same_name()
32+
{
33+
$this->expectException(InvalidArgumentException::class);
34+
35+
$this->artisan('make:patch', ['name' => 'new_patch'])->run();
36+
$this->artisan('make:patch', ['name' => 'new_patch'])->run();
37+
}
38+
}

tests/Commands/PatchTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelPatches\Tests\Commands;
4+
5+
use Rappasoft\LaravelPatches\Tests\TestCase;
6+
7+
class PatchTest extends TestCase
8+
{
9+
/** @test */
10+
public function it_runs_pending_patches()
11+
{
12+
file_put_contents(database_path('patches/2021_01_01_000000_my_first_patch.php'),
13+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php'));
14+
15+
$this->assertDatabaseCount('patches', 0);
16+
17+
$this->artisan('patch')->run();
18+
19+
$this->assertDatabaseCount('patches', 1);
20+
21+
$this->assertDatabaseHas('patches', [
22+
'patch' => '2021_01_01_000000_my_first_patch',
23+
'batch' => 1,
24+
'log' => json_encode(['Hello First!']),
25+
]);
26+
}
27+
28+
/** @test */
29+
public function it_increments_the_batch_number_normally()
30+
{
31+
file_put_contents(database_path('patches/2021_01_01_000000_my_first_patch.php'),
32+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php'));
33+
34+
$this->artisan('patch')->run();
35+
36+
$this->assertDatabaseHas('patches', [
37+
'patch' => '2021_01_01_000000_my_first_patch',
38+
'batch' => 1,
39+
]);
40+
41+
file_put_contents(database_path('patches/2021_01_02_000000_my_second_patch.php'),
42+
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php'));
43+
44+
$this->artisan('patch')->run();
45+
46+
$this->assertDatabaseHas('patches', [
47+
'patch' => '2021_01_02_000000_my_second_patch',
48+
'batch' => 2,
49+
]);
50+
}
51+
52+
/** @test */
53+
public function multiple_patches_have_the_same_batch_if_run_at_the_same_time()
54+
{
55+
file_put_contents(database_path('patches/2021_01_01_000000_my_first_patch.php'),
56+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php'));
57+
58+
file_put_contents(database_path('patches/2021_01_02_000000_my_second_patch.php'),
59+
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php'));
60+
61+
$this->artisan('patch')->run();
62+
63+
$this->assertDatabaseHas('patches', [
64+
'id' => 1,
65+
'patch' => '2021_01_01_000000_my_first_patch',
66+
'batch' => 1,
67+
]);
68+
69+
$this->assertDatabaseHas('patches', [
70+
'id' => 2,
71+
'patch' => '2021_01_02_000000_my_second_patch',
72+
'batch' => 1,
73+
]);
74+
}
75+
76+
/** @test */
77+
public function it_increments_the_batch_number_by_one_if_step_is_enabled()
78+
{
79+
file_put_contents(database_path('patches/2021_01_01_000000_my_first_patch.php'),
80+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php'));
81+
82+
file_put_contents(database_path('patches/2021_01_02_000000_my_second_patch.php'),
83+
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php'));
84+
85+
$this->artisan('patch', ['--step' => true])->run();
86+
87+
$this->assertDatabaseHas('patches', [
88+
'id' => 1,
89+
'patch' => '2021_01_01_000000_my_first_patch',
90+
'batch' => 1,
91+
]);
92+
93+
$this->assertDatabaseHas('patches', [
94+
'id' => 2,
95+
'patch' => '2021_01_02_000000_my_second_patch',
96+
'batch' => 2,
97+
]);
98+
}
99+
}

tests/Commands/RollbackTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelPatches\Tests\Commands;
4+
5+
use Illuminate\Support\Facades\Log;
6+
use Rappasoft\LaravelPatches\Tests\TestCase;
7+
8+
class RollbackTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_rollsback_a_patch()
12+
{
13+
Log::shouldReceive('info')->with('Goodbye First');
14+
15+
file_put_contents(database_path('patches/2021_01_01_000000_my_first_patch.php'),
16+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php'));
17+
18+
$this->assertDatabaseCount('patches', 0);
19+
20+
$this->artisan('patch')->run();
21+
22+
$this->assertDatabaseCount('patches', 1);
23+
24+
$this->assertDatabaseHas('patches', [
25+
'patch' => '2021_01_01_000000_my_first_patch',
26+
'batch' => 1,
27+
]);
28+
29+
$this->artisan('patch:rollback')->run();
30+
31+
$this->assertDatabaseCount('patches', 0);
32+
33+
$this->assertDatabaseMissing('patches', [
34+
'patch' => '2021_01_01_000000_my_first_patch',
35+
'batch' => 1,
36+
]);
37+
}
38+
39+
/** @test */
40+
public function it_rollsback_all_patches_of_the_previous_batch()
41+
{
42+
Log::shouldReceive('info')->with('Goodbye First');
43+
Log::shouldReceive('info')->with('Goodbye Second');
44+
45+
file_put_contents(database_path('patches/2021_01_01_000000_my_first_patch.php'),
46+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php'));
47+
48+
file_put_contents(database_path('patches/2021_01_02_000000_my_second_patch.php'),
49+
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php'));
50+
51+
$this->artisan('patch')->run();
52+
53+
$this->assertDatabaseCount('patches', 2);
54+
55+
$this->artisan('patch:rollback')->run();
56+
57+
$this->assertDatabaseCount('patches', 0);
58+
}
59+
60+
/** @test */
61+
public function it_rollsback_the_correct_patches_with_step()
62+
{
63+
Log::shouldReceive('info')->once();
64+
65+
file_put_contents(database_path('patches/2021_01_01_000000_my_first_patch.php'),
66+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php'));
67+
68+
file_put_contents(database_path('patches/2021_01_02_000000_my_second_patch.php'),
69+
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php'));
70+
71+
$this->artisan('patch')->run();
72+
73+
$this->assertDatabaseHas('patches', [
74+
'patch' => '2021_01_01_000000_my_first_patch',
75+
'batch' => 1,
76+
]);
77+
78+
$this->assertDatabaseHas('patches', [
79+
'patch' => '2021_01_02_000000_my_second_patch',
80+
'batch' => 1,
81+
]);
82+
83+
$this->artisan('patch:rollback', ['--step' => 1])->run();
84+
85+
$this->assertDatabaseHas('patches', [
86+
'patch' => '2021_01_01_000000_my_first_patch',
87+
'batch' => 1,
88+
]);
89+
90+
$this->assertDatabaseMissing('patches', [
91+
'patch' => '2021_01_02_000000_my_second_patch',
92+
'batch' => 1,
93+
]);
94+
}
95+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Rappasoft\LaravelPatches\Patch;
4+
5+
class MyFirstPatch extends Patch
6+
{
7+
/**
8+
* Run the patch.
9+
*
10+
* @return void
11+
*/
12+
public function up()
13+
{
14+
$this->log('Hello First!');
15+
}
16+
17+
/**
18+
* Reverse the patch.
19+
*
20+
* @return void
21+
*/
22+
public function down()
23+
{
24+
\Log::info('Goodbye First');
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Rappasoft\LaravelPatches\Patch;
4+
5+
class MySecondPatch extends Patch
6+
{
7+
/**
8+
* Run the patch.
9+
*
10+
* @return void
11+
*/
12+
public function up()
13+
{
14+
$this->log('Hello Second!');
15+
}
16+
17+
/**
18+
* Reverse the patch.
19+
*
20+
* @return void
21+
*/
22+
public function down()
23+
{
24+
\Log::info('Goodbye Second');
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Rappasoft\LaravelPatches\Patch;
4+
5+
class MyThirdPatch extends Patch
6+
{
7+
/**
8+
* Run the patch.
9+
*
10+
* @return void
11+
*/
12+
public function up()
13+
{
14+
$this->log('Hello Third!');
15+
}
16+
17+
/**
18+
* Reverse the patch.
19+
*
20+
* @return void
21+
*/
22+
public function down()
23+
{
24+
\Log::info('Goodbye Third');
25+
}
26+
}

tests/ExampleTest.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)