Skip to content

Commit 87cd753

Browse files
authored
Merge pull request #1 from rappasoft/develop
Develop
2 parents d12ad6b + e2d3171 commit 87cd753

File tree

10 files changed

+365
-22
lines changed

10 files changed

+365
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
All notable changes to `laravel-patches` will be documented in this file.
44

5-
## 0.0.1 - 2021-03-07
5+
## 1.0.0 - 2021-03-07
66

77
- Initial Release

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 Illuminate\Support\Facades\File;
6+
use InvalidArgumentException;
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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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(
13+
database_path('patches/2021_01_01_000000_my_first_patch.php'),
14+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
15+
);
16+
17+
$this->assertDatabaseCount('patches', 0);
18+
19+
$this->artisan('patch')->run();
20+
21+
$this->assertDatabaseCount('patches', 1);
22+
23+
$this->assertDatabaseHas('patches', [
24+
'patch' => '2021_01_01_000000_my_first_patch',
25+
'batch' => 1,
26+
'log' => json_encode(['Hello First!']),
27+
]);
28+
}
29+
30+
/** @test */
31+
public function it_increments_the_batch_number_normally()
32+
{
33+
file_put_contents(
34+
database_path('patches/2021_01_01_000000_my_first_patch.php'),
35+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
36+
);
37+
38+
$this->artisan('patch')->run();
39+
40+
$this->assertDatabaseHas('patches', [
41+
'patch' => '2021_01_01_000000_my_first_patch',
42+
'batch' => 1,
43+
]);
44+
45+
file_put_contents(
46+
database_path('patches/2021_01_02_000000_my_second_patch.php'),
47+
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php')
48+
);
49+
50+
$this->artisan('patch')->run();
51+
52+
$this->assertDatabaseHas('patches', [
53+
'patch' => '2021_01_02_000000_my_second_patch',
54+
'batch' => 2,
55+
]);
56+
}
57+
58+
/** @test */
59+
public function multiple_patches_have_the_same_batch_if_run_at_the_same_time()
60+
{
61+
file_put_contents(
62+
database_path('patches/2021_01_01_000000_my_first_patch.php'),
63+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
64+
);
65+
66+
file_put_contents(
67+
database_path('patches/2021_01_02_000000_my_second_patch.php'),
68+
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php')
69+
);
70+
71+
$this->artisan('patch')->run();
72+
73+
$this->assertDatabaseHas('patches', [
74+
'id' => 1,
75+
'patch' => '2021_01_01_000000_my_first_patch',
76+
'batch' => 1,
77+
]);
78+
79+
$this->assertDatabaseHas('patches', [
80+
'id' => 2,
81+
'patch' => '2021_01_02_000000_my_second_patch',
82+
'batch' => 1,
83+
]);
84+
}
85+
86+
/** @test */
87+
public function it_increments_the_batch_number_by_one_if_step_is_enabled()
88+
{
89+
file_put_contents(
90+
database_path('patches/2021_01_01_000000_my_first_patch.php'),
91+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
92+
);
93+
94+
file_put_contents(
95+
database_path('patches/2021_01_02_000000_my_second_patch.php'),
96+
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php')
97+
);
98+
99+
$this->artisan('patch', ['--step' => true])->run();
100+
101+
$this->assertDatabaseHas('patches', [
102+
'id' => 1,
103+
'patch' => '2021_01_01_000000_my_first_patch',
104+
'batch' => 1,
105+
]);
106+
107+
$this->assertDatabaseHas('patches', [
108+
'id' => 2,
109+
'patch' => '2021_01_02_000000_my_second_patch',
110+
'batch' => 2,
111+
]);
112+
}
113+
}

tests/Commands/RollbackTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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(
16+
database_path('patches/2021_01_01_000000_my_first_patch.php'),
17+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
18+
);
19+
20+
$this->assertDatabaseCount('patches', 0);
21+
22+
$this->artisan('patch')->run();
23+
24+
$this->assertDatabaseCount('patches', 1);
25+
26+
$this->assertDatabaseHas('patches', [
27+
'patch' => '2021_01_01_000000_my_first_patch',
28+
'batch' => 1,
29+
]);
30+
31+
$this->artisan('patch:rollback')->run();
32+
33+
$this->assertDatabaseCount('patches', 0);
34+
35+
$this->assertDatabaseMissing('patches', [
36+
'patch' => '2021_01_01_000000_my_first_patch',
37+
'batch' => 1,
38+
]);
39+
}
40+
41+
/** @test */
42+
public function it_rollsback_all_patches_of_the_previous_batch()
43+
{
44+
Log::shouldReceive('info')->with('Goodbye First');
45+
Log::shouldReceive('info')->with('Goodbye Second');
46+
47+
file_put_contents(
48+
database_path('patches/2021_01_01_000000_my_first_patch.php'),
49+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
50+
);
51+
52+
file_put_contents(
53+
database_path('patches/2021_01_02_000000_my_second_patch.php'),
54+
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php')
55+
);
56+
57+
$this->artisan('patch')->run();
58+
59+
$this->assertDatabaseCount('patches', 2);
60+
61+
$this->artisan('patch:rollback')->run();
62+
63+
$this->assertDatabaseCount('patches', 0);
64+
}
65+
66+
/** @test */
67+
public function it_rollsback_the_correct_patches_with_step()
68+
{
69+
Log::shouldReceive('info')->once();
70+
71+
file_put_contents(
72+
database_path('patches/2021_01_01_000000_my_first_patch.php'),
73+
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
74+
);
75+
76+
file_put_contents(
77+
database_path('patches/2021_01_02_000000_my_second_patch.php'),
78+
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php')
79+
);
80+
81+
$this->artisan('patch')->run();
82+
83+
$this->assertDatabaseHas('patches', [
84+
'patch' => '2021_01_01_000000_my_first_patch',
85+
'batch' => 1,
86+
]);
87+
88+
$this->assertDatabaseHas('patches', [
89+
'patch' => '2021_01_02_000000_my_second_patch',
90+
'batch' => 1,
91+
]);
92+
93+
$this->artisan('patch:rollback', ['--step' => 1])->run();
94+
95+
$this->assertDatabaseHas('patches', [
96+
'patch' => '2021_01_01_000000_my_first_patch',
97+
'batch' => 1,
98+
]);
99+
100+
$this->assertDatabaseMissing('patches', [
101+
'patch' => '2021_01_02_000000_my_second_patch',
102+
'batch' => 1,
103+
]);
104+
}
105+
}
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)