Skip to content

Commit

Permalink
Merge pull request #1 from rappasoft/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
rappasoft authored Mar 8, 2021
2 parents d12ad6b + e2d3171 commit 87cd753
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

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

## 0.0.1 - 2021-03-07
## 1.0.0 - 2021-03-07

- Initial Release
7 changes: 6 additions & 1 deletion src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ public function getLast(): array
*/
public function log(string $file, int $batch, array $log = []): void
{
Patch::create(['patch' => $file, 'batch' => $batch, 'log' => $log]);
Patch::create([
'patch' => $file,
'batch' => $batch,
'log' => $log,
'ran_on' => now(),
]);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/Commands/PatchMakeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Rappasoft\LaravelPatches\Tests\Commands;

use Illuminate\Support\Facades\File;
use InvalidArgumentException;
use Rappasoft\LaravelPatches\Tests\TestCase;

class PatchMakeTest extends TestCase
{
/** @test */
public function it_makes_a_patch_file()
{
$this->artisan('make:patch', ['name' => 'new_patch'])->run();

$this->assertDirectoryExists(database_path('patches'));
$this->assertEquals(1, collect(File::files(database_path('patches')))->count());
}

/** @test */
public function it_prepopulates_the_patch_with_the_stub_file()
{
$this->artisan('make:patch', ['name' => 'new_patch'])->run();

foreach (glob(database_path('patches').'/*') as $file) {
$this->assertTrue(filesize($file) > 0);
}
}

/** @test */
public function it_doesnt_make_two_patches_with_the_same_name()
{
$this->expectException(InvalidArgumentException::class);

$this->artisan('make:patch', ['name' => 'new_patch'])->run();
$this->artisan('make:patch', ['name' => 'new_patch'])->run();
}
}
113 changes: 113 additions & 0 deletions tests/Commands/PatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Rappasoft\LaravelPatches\Tests\Commands;

use Rappasoft\LaravelPatches\Tests\TestCase;

class PatchTest extends TestCase
{
/** @test */
public function it_runs_pending_patches()
{
file_put_contents(
database_path('patches/2021_01_01_000000_my_first_patch.php'),
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
);

$this->assertDatabaseCount('patches', 0);

$this->artisan('patch')->run();

$this->assertDatabaseCount('patches', 1);

$this->assertDatabaseHas('patches', [
'patch' => '2021_01_01_000000_my_first_patch',
'batch' => 1,
'log' => json_encode(['Hello First!']),
]);
}

/** @test */
public function it_increments_the_batch_number_normally()
{
file_put_contents(
database_path('patches/2021_01_01_000000_my_first_patch.php'),
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
);

$this->artisan('patch')->run();

$this->assertDatabaseHas('patches', [
'patch' => '2021_01_01_000000_my_first_patch',
'batch' => 1,
]);

file_put_contents(
database_path('patches/2021_01_02_000000_my_second_patch.php'),
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php')
);

$this->artisan('patch')->run();

$this->assertDatabaseHas('patches', [
'patch' => '2021_01_02_000000_my_second_patch',
'batch' => 2,
]);
}

/** @test */
public function multiple_patches_have_the_same_batch_if_run_at_the_same_time()
{
file_put_contents(
database_path('patches/2021_01_01_000000_my_first_patch.php'),
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
);

file_put_contents(
database_path('patches/2021_01_02_000000_my_second_patch.php'),
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php')
);

$this->artisan('patch')->run();

$this->assertDatabaseHas('patches', [
'id' => 1,
'patch' => '2021_01_01_000000_my_first_patch',
'batch' => 1,
]);

$this->assertDatabaseHas('patches', [
'id' => 2,
'patch' => '2021_01_02_000000_my_second_patch',
'batch' => 1,
]);
}

/** @test */
public function it_increments_the_batch_number_by_one_if_step_is_enabled()
{
file_put_contents(
database_path('patches/2021_01_01_000000_my_first_patch.php'),
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
);

file_put_contents(
database_path('patches/2021_01_02_000000_my_second_patch.php'),
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php')
);

$this->artisan('patch', ['--step' => true])->run();

$this->assertDatabaseHas('patches', [
'id' => 1,
'patch' => '2021_01_01_000000_my_first_patch',
'batch' => 1,
]);

$this->assertDatabaseHas('patches', [
'id' => 2,
'patch' => '2021_01_02_000000_my_second_patch',
'batch' => 2,
]);
}
}
105 changes: 105 additions & 0 deletions tests/Commands/RollbackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Rappasoft\LaravelPatches\Tests\Commands;

use Illuminate\Support\Facades\Log;
use Rappasoft\LaravelPatches\Tests\TestCase;

class RollbackTest extends TestCase
{
/** @test */
public function it_rollsback_a_patch()
{
Log::shouldReceive('info')->with('Goodbye First');

file_put_contents(
database_path('patches/2021_01_01_000000_my_first_patch.php'),
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
);

$this->assertDatabaseCount('patches', 0);

$this->artisan('patch')->run();

$this->assertDatabaseCount('patches', 1);

$this->assertDatabaseHas('patches', [
'patch' => '2021_01_01_000000_my_first_patch',
'batch' => 1,
]);

$this->artisan('patch:rollback')->run();

$this->assertDatabaseCount('patches', 0);

$this->assertDatabaseMissing('patches', [
'patch' => '2021_01_01_000000_my_first_patch',
'batch' => 1,
]);
}

/** @test */
public function it_rollsback_all_patches_of_the_previous_batch()
{
Log::shouldReceive('info')->with('Goodbye First');
Log::shouldReceive('info')->with('Goodbye Second');

file_put_contents(
database_path('patches/2021_01_01_000000_my_first_patch.php'),
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
);

file_put_contents(
database_path('patches/2021_01_02_000000_my_second_patch.php'),
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php')
);

$this->artisan('patch')->run();

$this->assertDatabaseCount('patches', 2);

$this->artisan('patch:rollback')->run();

$this->assertDatabaseCount('patches', 0);
}

/** @test */
public function it_rollsback_the_correct_patches_with_step()
{
Log::shouldReceive('info')->once();

file_put_contents(
database_path('patches/2021_01_01_000000_my_first_patch.php'),
file_get_contents(__DIR__.'/patches/2021_01_01_000000_my_first_patch.php')
);

file_put_contents(
database_path('patches/2021_01_02_000000_my_second_patch.php'),
file_get_contents(__DIR__.'/patches/2021_01_02_000000_my_second_patch.php')
);

$this->artisan('patch')->run();

$this->assertDatabaseHas('patches', [
'patch' => '2021_01_01_000000_my_first_patch',
'batch' => 1,
]);

$this->assertDatabaseHas('patches', [
'patch' => '2021_01_02_000000_my_second_patch',
'batch' => 1,
]);

$this->artisan('patch:rollback', ['--step' => 1])->run();

$this->assertDatabaseHas('patches', [
'patch' => '2021_01_01_000000_my_first_patch',
'batch' => 1,
]);

$this->assertDatabaseMissing('patches', [
'patch' => '2021_01_02_000000_my_second_patch',
'batch' => 1,
]);
}
}
26 changes: 26 additions & 0 deletions tests/Commands/patches/2021_01_01_000000_my_first_patch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Rappasoft\LaravelPatches\Patch;

class MyFirstPatch extends Patch
{
/**
* Run the patch.
*
* @return void
*/
public function up()
{
$this->log('Hello First!');
}

/**
* Reverse the patch.
*
* @return void
*/
public function down()
{
\Log::info('Goodbye First');
}
}
26 changes: 26 additions & 0 deletions tests/Commands/patches/2021_01_02_000000_my_second_patch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Rappasoft\LaravelPatches\Patch;

class MySecondPatch extends Patch
{
/**
* Run the patch.
*
* @return void
*/
public function up()
{
$this->log('Hello Second!');
}

/**
* Reverse the patch.
*
* @return void
*/
public function down()
{
\Log::info('Goodbye Second');
}
}
26 changes: 26 additions & 0 deletions tests/Commands/patches/2021_01_03_000000_my_third_patch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Rappasoft\LaravelPatches\Patch;

class MyThirdPatch extends Patch
{
/**
* Run the patch.
*
* @return void
*/
public function up()
{
$this->log('Hello Third!');
}

/**
* Reverse the patch.
*
* @return void
*/
public function down()
{
\Log::info('Goodbye Third');
}
}
12 changes: 0 additions & 12 deletions tests/ExampleTest.php

This file was deleted.

Loading

0 comments on commit 87cd753

Please sign in to comment.