Skip to content

Commit 9d2c20f

Browse files
authored
BuildCommand tests (#315)
1 parent 42824fb commit 9d2c20f

File tree

5 files changed

+114
-20
lines changed

5 files changed

+114
-20
lines changed

src/BlueprintServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function register()
4343

4444
$this->app->bind('command.blueprint.build',
4545
function ($app) {
46-
return new BuildCommand($app['files']);
46+
return new BuildCommand($app['files'],app(Builder::class));
4747
}
4848
);
4949
$this->app->bind('command.blueprint.erase',

src/Builder.php

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

77
class Builder
88
{
9-
public static function execute(Blueprint $blueprint, Filesystem $files, string $draft, string $only = '', string $skip = '')
9+
public function execute(Blueprint $blueprint, Filesystem $files, string $draft, string $only = '', string $skip = '')
1010
{
1111
$cache = [];
1212
if ($files->exists('.blueprint')) {

src/Commands/BuildCommand.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Blueprint\Blueprint;
66
use Blueprint\Builder;
77
use Illuminate\Console\Command;
8+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
89
use Illuminate\Filesystem\Filesystem;
910
use Illuminate\Support\Str;
1011
use Symfony\Component\Console\Input\InputArgument;
@@ -32,35 +33,35 @@ class BuildCommand extends Command
3233
/** @var Filesystem */
3334
protected $files;
3435

36+
/** @var Builder */
37+
private $builder;
38+
3539
/**
3640
* @param Filesystem $files
41+
* @param Builder $builder
3742
*/
38-
public function __construct(Filesystem $files)
43+
public function __construct(Filesystem $files, Builder $builder)
3944
{
4045
parent::__construct();
4146

4247
$this->files = $files;
48+
$this->builder = $builder;
4349
}
4450

45-
/**
46-
* Execute the console command.
47-
*
48-
* @return void
49-
*/
5051
public function handle()
5152
{
5253
$file = $this->argument('draft') ?? $this->defaultDraftFile();
5354

54-
if (! file_exists($file)) {
55+
if (!$this->files->exists($file)) {
5556
$this->error('Draft file could not be found: '.($file ?: 'draft.yaml'));
56-
exit(1);
57+
return 1;
5758
}
5859

5960
$only = $this->option('only') ?: '';
6061
$skip = $this->option('skip') ?: '';
6162

6263
$blueprint = resolve(Blueprint::class);
63-
$generated = Builder::execute($blueprint, $this->files, $file, $only, $skip);
64+
$generated = $this->builder->execute($blueprint, $this->files, $file, $only, $skip);
6465

6566
collect($generated)->each(function ($files, $action) {
6667
$this->line(Str::studly($action).':', $this->outputStyle($action));
@@ -97,12 +98,6 @@ private function outputStyle($action)
9798

9899
private function defaultDraftFile()
99100
{
100-
if (file_exists('draft.yaml')) {
101-
return 'draft.yaml';
102-
}
103-
104-
if (file_exists('draft.yml')) {
105-
return 'draft.yml';
106-
}
101+
return file_exists('draft.yml') ? 'draft.yml' : 'draft.yaml';
107102
}
108103
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Tests\Feature\Commands;
4+
5+
use Blueprint\Blueprint;
6+
use Blueprint\Builder;
7+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
8+
use Tests\TestCase;
9+
10+
class BuildCommandTest extends TestCase
11+
{
12+
use MockeryPHPUnitIntegration;
13+
14+
/** @test */
15+
public function it_uses_the_default_draft_file()
16+
{
17+
$filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial();
18+
$this->swap('files', $filesystem);
19+
20+
$filesystem->shouldReceive('exists')
21+
->with('draft.yaml')
22+
->andReturnTrue();
23+
24+
$builder = $this->mock(Builder::class);
25+
26+
$builder->shouldReceive('execute')
27+
->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '')
28+
->andReturn(collect([]));
29+
30+
$this->artisan('blueprint:build')
31+
->assertExitCode(0);
32+
}
33+
34+
/** @test */
35+
public function it_passes_the_command_args_to_the_builder_in_right_order()
36+
{
37+
$filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial();
38+
$this->swap('files', $filesystem);
39+
40+
$filesystem->shouldReceive('exists')
41+
->with('test.yml')
42+
->andReturnTrue();
43+
44+
$builder = $this->mock(Builder::class);
45+
46+
$builder->shouldReceive('execute')
47+
->with(resolve(Blueprint::class), $filesystem, 'test.yml', 'a,b,c', 'x,y,z')
48+
->andReturn(collect([]));
49+
50+
$this->artisan('blueprint:build test.yml --only=a,b,c --skip=x,y,z')
51+
->assertExitCode(0);
52+
}
53+
54+
/** @test */
55+
public function it_fails_if_the_draft_file_not_exists()
56+
{
57+
$filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial();
58+
$this->swap('files', $filesystem);
59+
60+
$filesystem->shouldReceive('exists')
61+
->with('test.yml')
62+
->andReturnFalse();
63+
64+
$builder = $this->mock(Builder::class);
65+
66+
$builder->shouldNotReceive('execute');
67+
68+
$this->artisan('blueprint:build test.yml --only=a,b,c --skip=x,y,z')
69+
->assertExitCode(1);
70+
}
71+
72+
/** @test */
73+
public function it_shows_the_generated_files_groupbed_by_actions()
74+
{
75+
$filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial();
76+
$this->swap('files', $filesystem);
77+
78+
$filesystem->shouldReceive('exists')
79+
->with('draft.yaml')
80+
->andReturnTrue();
81+
82+
$builder = $this->mock(Builder::class);
83+
84+
$builder->shouldReceive('execute')
85+
->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '')
86+
->andReturn(collect([
87+
"created" => [
88+
"file1",
89+
"file2",
90+
]
91+
]));
92+
93+
$this->artisan('blueprint:build')
94+
->assertExitCode(0)
95+
->expectsOutput('Created:')
96+
->expectsOutput('- file1')
97+
->expectsOutput('- file2');
98+
}
99+
}

tests/Unit/BuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function execute_builds_draft_content()
4646
$file->expects('put')
4747
->with('.blueprint', 'cacheable blueprint content');
4848

49-
$actual = Builder::execute($blueprint, $file, 'draft.yaml');
49+
$actual = (new Builder)->execute($blueprint, $file, 'draft.yaml');
5050

5151
$this->assertSame($generated, $actual);
5252
}
@@ -104,7 +104,7 @@ public function execute_uses_cache_and_remembers_models()
104104
$file->expects('put')
105105
->with('.blueprint', 'cacheable blueprint content');
106106

107-
$actual = Builder::execute($blueprint, $file, 'draft.yaml');
107+
$actual = (new Builder)->execute($blueprint, $file, 'draft.yaml');
108108

109109
$this->assertSame($generated, $actual);
110110
}

0 commit comments

Comments
 (0)