Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BlueprintServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function register()

$this->app->bind('command.blueprint.build',
function ($app) {
return new BuildCommand($app['files']);
return new BuildCommand($app['files'],app(Builder::class));
}
);
$this->app->bind('command.blueprint.erase',
Expand Down
2 changes: 1 addition & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Builder
{
public static function execute(Blueprint $blueprint, Filesystem $files, string $draft, string $only = '', string $skip = '')
public function execute(Blueprint $blueprint, Filesystem $files, string $draft, string $only = '', string $skip = '')
{
$cache = [];
if ($files->exists('.blueprint')) {
Expand Down
27 changes: 11 additions & 16 deletions src/Commands/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Blueprint\Blueprint;
use Blueprint\Builder;
use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -32,35 +33,35 @@ class BuildCommand extends Command
/** @var Filesystem */
protected $files;

/** @var Builder */
private $builder;

/**
* @param Filesystem $files
* @param Builder $builder
*/
public function __construct(Filesystem $files)
public function __construct(Filesystem $files, Builder $builder)
{
parent::__construct();

$this->files = $files;
$this->builder = $builder;
}

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$file = $this->argument('draft') ?? $this->defaultDraftFile();

if (! file_exists($file)) {
if (!$this->files->exists($file)) {
$this->error('Draft file could not be found: '.($file ?: 'draft.yaml'));
exit(1);
return 1;
}

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

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

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

private function defaultDraftFile()
{
if (file_exists('draft.yaml')) {
return 'draft.yaml';
}

if (file_exists('draft.yml')) {
return 'draft.yml';
}
return file_exists('draft.yml') ? 'draft.yml' : 'draft.yaml';
}
}
99 changes: 99 additions & 0 deletions tests/Feature/Commands/BuildCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Tests\Feature\Commands;

use Blueprint\Blueprint;
use Blueprint\Builder;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Tests\TestCase;

class BuildCommandTest extends TestCase
{
use MockeryPHPUnitIntegration;

/** @test */
public function it_uses_the_default_draft_file()
{
$filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial();
$this->swap('files', $filesystem);

$filesystem->shouldReceive('exists')
->with('draft.yaml')
->andReturnTrue();

$builder = $this->mock(Builder::class);

$builder->shouldReceive('execute')
->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '')
->andReturn(collect([]));

$this->artisan('blueprint:build')
->assertExitCode(0);
}

/** @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')
->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')
->andReturn(collect([]));

$this->artisan('blueprint:build test.yml --only=a,b,c --skip=x,y,z')
->assertExitCode(0);
}

/** @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')
->with('test.yml')
->andReturnFalse();

$builder = $this->mock(Builder::class);

$builder->shouldNotReceive('execute');

$this->artisan('blueprint:build test.yml --only=a,b,c --skip=x,y,z')
->assertExitCode(1);
}

/** @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')
->with('draft.yaml')
->andReturnTrue();

$builder = $this->mock(Builder::class);

$builder->shouldReceive('execute')
->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '')
->andReturn(collect([
"created" => [
"file1",
"file2",
]
]));

$this->artisan('blueprint:build')
->assertExitCode(0)
->expectsOutput('Created:')
->expectsOutput('- file1')
->expectsOutput('- file2');
}
}
4 changes: 2 additions & 2 deletions tests/Unit/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function execute_builds_draft_content()
$file->expects('put')
->with('.blueprint', 'cacheable blueprint content');

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

$this->assertSame($generated, $actual);
}
Expand Down Expand Up @@ -104,7 +104,7 @@ public function execute_uses_cache_and_remembers_models()
$file->expects('put')
->with('.blueprint', 'cacheable blueprint content');

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

$this->assertSame($generated, $actual);
}
Expand Down