diff --git a/tests/Feature/Commands/BuildCommandTest.php b/tests/Feature/Commands/BuildCommandTest.php index af670300..24bab539 100644 --- a/tests/Feature/Commands/BuildCommandTest.php +++ b/tests/Feature/Commands/BuildCommandTest.php @@ -6,28 +6,26 @@ use Blueprint\Builder; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use Tests\TestCase; +use Tests\Traits\MocksFilesystem; /** * @covers \Blueprint\Commands\BuildCommand */ class BuildCommandTest extends TestCase { - use MockeryPHPUnitIntegration; + use MockeryPHPUnitIntegration, MocksFilesystem; /** @test */ public function it_uses_the_default_draft_file() { - $filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial(); - $this->swap('files', $filesystem); - - $filesystem->shouldReceive('exists') + $this->files->shouldReceive('exists') ->with('draft.yaml') ->andReturnTrue(); $builder = $this->mock(Builder::class); $builder->shouldReceive('execute') - ->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '', false) + ->with(resolve(Blueprint::class), $this->files, 'draft.yaml', '', '', false) ->andReturn(collect([])); $this->artisan('blueprint:build') @@ -37,17 +35,14 @@ public function it_uses_the_default_draft_file() /** @test */ public function it_passes_the_command_args_to_the_builder_in_right_order() { - $filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial(); - $this->swap('files', $filesystem); - - $filesystem->shouldReceive('exists') + $this->files->shouldReceive('exists') ->with('test.yml') ->andReturnTrue(); $builder = $this->mock(Builder::class); $builder->shouldReceive('execute') - ->with(resolve(Blueprint::class), $filesystem, 'test.yml', 'a,b,c', 'x,y,z', false) + ->with(resolve(Blueprint::class), $this->files, 'test.yml', 'a,b,c', 'x,y,z', false) ->andReturn(collect([])); $this->artisan('blueprint:build test.yml --only=a,b,c --skip=x,y,z') @@ -57,10 +52,7 @@ public function it_passes_the_command_args_to_the_builder_in_right_order() /** @test */ public function it_fails_if_the_draft_file_not_exists() { - $filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial(); - $this->swap('files', $filesystem); - - $filesystem->shouldReceive('exists') + $this->files->shouldReceive('exists') ->with('test.yml') ->andReturnFalse(); @@ -75,24 +67,18 @@ public function it_fails_if_the_draft_file_not_exists() /** @test */ public function it_shows_the_generated_files_groupbed_by_actions() { - $filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial(); - $this->swap('files', $filesystem); - - $filesystem->shouldReceive('exists') + $this->files->shouldReceive('exists') ->with('draft.yaml') ->andReturnTrue(); - $builder = $this->mock(Builder::class); - $builder->shouldReceive('execute') - ->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '', false) + ->with(resolve(Blueprint::class), $this->files, 'draft.yaml', '', '', false) ->andReturn(collect([ "created" => [ "file1", "file2", ] ])); - $this->artisan('blueprint:build') ->assertExitCode(0) ->expectsOutput('Created:') diff --git a/tests/Feature/Commands/EraseCommandTest.php b/tests/Feature/Commands/EraseCommandTest.php index 0943ca9a..968dc9bb 100644 --- a/tests/Feature/Commands/EraseCommandTest.php +++ b/tests/Feature/Commands/EraseCommandTest.php @@ -6,27 +6,29 @@ use Blueprint\Tracer; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use Tests\TestCase; +use Tests\Traits\MocksFilesystem; /** * @covers \Blueprint\Commands\EraseCommand */ class EraseCommandTest extends TestCase { - use MockeryPHPUnitIntegration; + use MockeryPHPUnitIntegration, MocksFilesystem; /** @test */ public function it_parses_and_update_the_trace_file() { - $filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial(); - $this->swap('files', $filesystem); - - $filesystem->expects('get') + $this->files->expects('get') ->with('.blueprint') ->andReturn("created: created_file.php \nupdated: updated_file.php \nother: test.php"); - $filesystem->expects('put') + $this->files->expects('delete')->with("created_file.php"); + + $this->files->expects('put') ->with('.blueprint', "other: test.php\n"); + $this->files->expects('exists')->with('app'); + $this->artisan('blueprint:erase') ->assertExitCode(0); } @@ -34,18 +36,18 @@ public function it_parses_and_update_the_trace_file() /** @test */ public function it_deletes_the_created_files() { - $filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial(); - $this->swap('files', $filesystem); - - $filesystem->expects('get') + $this->files->expects('get') ->with('.blueprint') ->andReturn("created:\n - created_file1.php\n - created_file2.php"); - $filesystem->expects('delete')->with([ + $this->files->expects('delete')->with([ "created_file1.php", "created_file2.php", ]); + $this->files->expects('put')->with('.blueprint', '{ }'); + $this->files->expects('exists')->with('app'); + $this->artisan('blueprint:erase') ->assertExitCode(0) ->expectsOutput("Deleted:") @@ -56,13 +58,13 @@ public function it_deletes_the_created_files() /** @test */ public function it_notify_about_the_updated_files() { - $filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial(); - $this->swap('files', $filesystem); - - $filesystem->expects('get') + $this->files->expects('get') ->with('.blueprint') ->andReturn("updated:\n - updated_file1.php\n - updated_file2.php"); + $this->files->expects('put')->with('.blueprint', '{ }'); + $this->files->expects('exists')->with('app'); + $this->artisan('blueprint:erase') ->assertExitCode(0) ->expectsOutput("The updates to the following files can not be erased automatically.") @@ -73,11 +75,8 @@ public function it_notify_about_the_updated_files() /** @test */ public function it_calls_the_trace_command() { - $filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial(); - $this->swap('files', $filesystem); - - $filesystem->expects('get')->with('.blueprint')->andReturn("other: test.php"); - $filesystem->expects('put')->with('.blueprint', "other: test.php\n"); + $this->files->expects('get')->with('.blueprint')->andReturn("other: test.php"); + $this->files->expects('put')->with('.blueprint', "other: test.php\n"); $tracer = $this->spy(Tracer::class); @@ -85,6 +84,6 @@ public function it_calls_the_trace_command() ->assertExitCode(0); $tracer->shouldHaveReceived('execute') - ->with(resolve(Blueprint::class), $filesystem); + ->with(resolve(Blueprint::class), $this->files); } } diff --git a/tests/Feature/Commands/NewCommandTest.php b/tests/Feature/Commands/NewCommandTest.php index 899ebe4e..44b7d9cd 100644 --- a/tests/Feature/Commands/NewCommandTest.php +++ b/tests/Feature/Commands/NewCommandTest.php @@ -4,29 +4,31 @@ use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use Tests\TestCase; +use Tests\Traits\MocksFilesystem; /** * @covers \Blueprint\Commands\NewCommand */ class NewCommandTest extends TestCase { - use MockeryPHPUnitIntegration; + use MockeryPHPUnitIntegration, MocksFilesystem; - /** @test */ + /** + * @test + */ public function it_creates_a_draft_file_from_stub_if_none_exists() { - $filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial(); - $this->swap('files', $filesystem); - - $filesystem->shouldReceive('exists') + $this->files->shouldReceive('exists') ->with('draft.yaml') ->andReturnFalse(); - $filesystem->shouldReceive('stub') + $this->files->shouldReceive('stub') ->with('draft.stub') ->andReturn('stub'); - $filesystem->shouldReceive('put') + $this->files->shouldReceive('put') ->with('draft.yaml', 'stub'); + $this->files->shouldReceive('exists')->with('app'); + $this->artisan('blueprint:new') ->assertExitCode(0); } @@ -34,13 +36,12 @@ public function it_creates_a_draft_file_from_stub_if_none_exists() /** @test */ public function it_does_not_create_a_draft_file_if_one_exists_already() { - $filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial(); - $this->swap('files', $filesystem); - - $filesystem->shouldReceive('exists') + $this->files->shouldReceive('exists') ->with('draft.yaml') ->andReturnTrue(); - $filesystem->shouldNotReceive('put'); + $this->files->shouldNotReceive('put'); + $this->files->shouldReceive('exists') + ->with('app'); $this->artisan('blueprint:new') ->assertExitCode(0); diff --git a/tests/Feature/Commands/TraceCommandTest.php b/tests/Feature/Commands/TraceCommandTest.php index 8781e73b..244b204a 100644 --- a/tests/Feature/Commands/TraceCommandTest.php +++ b/tests/Feature/Commands/TraceCommandTest.php @@ -8,24 +8,22 @@ use Blueprint\Tracer; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use Tests\TestCase; +use Tests\Traits\MocksFilesystem; /** * @covers \Blueprint\Commands\TraceCommand */ class TraceCommandTest extends TestCase { - use MockeryPHPUnitIntegration; + use MockeryPHPUnitIntegration, MocksFilesystem; /** @test */ public function it_shows_error_if_no_model_found() { - $filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial(); - $this->swap('files', $filesystem); - $tracer = $this->mock(Tracer::class); $tracer->shouldReceive('execute') - ->with(resolve(Blueprint::class), $filesystem) + ->with(resolve(Blueprint::class), $this->files) ->andReturn([]); $this->artisan('blueprint:trace') @@ -36,13 +34,10 @@ public function it_shows_error_if_no_model_found() /** @test */ public function it_shows_the_number_of_traced_models() { - $filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial(); - $this->swap('files', $filesystem); - $tracer = $this->mock(Tracer::class); $tracer->shouldReceive('execute') - ->with(resolve(Blueprint::class), $filesystem) + ->with(resolve(Blueprint::class), $this->files) ->andReturn([ "Model" => [], "OtherModel" => [], diff --git a/tests/Feature/Generator/MigrationGeneratorTest.php b/tests/Feature/Generator/MigrationGeneratorTest.php index e367f973..12f25b8e 100644 --- a/tests/Feature/Generator/MigrationGeneratorTest.php +++ b/tests/Feature/Generator/MigrationGeneratorTest.php @@ -164,15 +164,10 @@ public function output_uses_past_timestamp_for_multiple_migrations() /** * @test + * @environment-setup useLaravel6 */ public function output_uses_proper_data_type_for_id_columns_in_laravel6() { - $app = \Mockery::mock(); - $app->shouldReceive('version') - ->withNoArgs() - ->andReturn('6.0.0'); - App::swap($app); - $this->files->expects('stub') ->with('migration.stub') ->andReturn($this->stub('migration.stub')); @@ -222,17 +217,12 @@ public function output_creates_constraints_for_unconventional_foreign_reference_ /** * @test + * @environment-setup useLaravel6 */ public function output_creates_constraints_for_unconventional_foreign_reference_migration_laravel6() { $this->app->config->set('blueprint.use_constraints', true); - $app = \Mockery::mock(); - $app->shouldReceive('version') - ->withNoArgs() - ->andReturn('6.0.0'); - App::swap($app); - $this->files->expects('stub') ->with('migration.stub') ->andReturn($this->stub('migration.stub')); @@ -319,15 +309,10 @@ public function output_also_updates_pivot_table_migration() /** * @test + * @environment-setup useLaravel6 */ public function output_also_creates_pivot_table_migration_laravel6() { - $app = \Mockery::mock(); - $app->shouldReceive('version') - ->withNoArgs() - ->andReturn('6.0.0'); - App::swap($app); - $this->files->expects('stub') ->with('migration.stub') ->andReturn($this->stub('migration.stub')); @@ -385,17 +370,12 @@ public function output_also_creates_constraints_for_pivot_table_migration() /** * @test + * @environment-setup useLaravel6 */ public function output_also_creates_constraints_for_pivot_table_migration_laravel6() { $this->app->config->set('blueprint.use_constraints', true); - $app = \Mockery::mock(); - $app->shouldReceive('version') - ->withNoArgs() - ->andReturn('6.0.0'); - App::swap($app); - $this->files->expects('stub') ->with('migration.stub') ->andReturn($this->stub('migration.stub')); @@ -452,15 +432,10 @@ public function output_does_not_duplicate_pivot_table_migration() /** * @test + * @environment-setup useLaravel6 */ public function output_does_not_duplicate_pivot_table_migration_laravel6() { - $app = \Mockery::mock(); - $app->shouldReceive('version') - ->withNoArgs() - ->andReturn('6.0.0'); - App::swap($app); - $this->files->expects('stub') ->with('migration.stub') ->andReturn($this->stub('migration.stub')); @@ -517,15 +492,10 @@ public function output_also_creates_pivot_table_migration_with_custom_name() /** * @test + * @environment-setup useLaravel6 */ public function output_also_creates_pivot_table_migration_with_custom_name_laravel6() { - $app = \Mockery::mock(); - $app->shouldReceive('version') - ->withNoArgs() - ->andReturn('6.0.0'); - App::swap($app); - $this->files->expects('stub') ->with('migration.stub') ->andReturn($this->stub('migration.stub')); @@ -579,17 +549,12 @@ public function output_creates_foreign_keys_with_nullable_chained_correctly() /** * @test + * @environment-setup useLaravel6 */ public function output_creates_foreign_keys_with_nullable_chained_correctly_laravel6() { $this->app->config->set('blueprint.on_delete', 'null'); - $app = \Mockery::mock(); - $app->shouldReceive('version') - ->withNoArgs() - ->andReturn('6.0.0'); - App::swap($app); - $this->files->expects('stub') ->with('migration.stub') ->andReturn($this->stub('migration.stub')); @@ -639,15 +604,10 @@ public function output_creates_foreign_keys_with_on_delete() /** * @test + * @environment-setup useLaravel6 */ public function output_creates_foreign_keys_with_on_delete_laravel6() { - $app = \Mockery::mock(); - $app->shouldReceive('version') - ->withNoArgs() - ->andReturn('6.0.0'); - App::swap($app); - $this->files->expects('stub') ->with('migration.stub') ->andReturn($this->stub('migration.stub')); @@ -702,15 +662,10 @@ public function output_works_with_polymorphic_relationships() /** * @test + * @environment-setup useLaravel6 */ public function output_works_with_polymorphic_relationships_laravel6() { - $app = \Mockery::mock(); - $app->shouldReceive('version') - ->withNoArgs() - ->andReturn('6.0.0'); - App::swap($app); - $this->files->expects('stub') ->with('migration.stub') ->andReturn($this->stub('migration.stub')); diff --git a/tests/TestCase.php b/tests/TestCase.php index 8a748496..3905aeb4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -33,4 +33,14 @@ protected function getPackageProviders($app) BlueprintServiceProvider::class, ]; } + + protected function useLaravel6($app) + { + $appMock = \Mockery::mock($app); + $appMock->shouldReceive('version') + ->withNoArgs() + ->andReturn('6.0.0'); + + \App::swap($appMock); + } } diff --git a/tests/Traits/MocksFilesystem.php b/tests/Traits/MocksFilesystem.php new file mode 100644 index 00000000..7193bfdf --- /dev/null +++ b/tests/Traits/MocksFilesystem.php @@ -0,0 +1,14 @@ +files = $this->mock(\Illuminate\Filesystem\Filesystem::class); + $this->swap('files', $this->files); + } +}