Skip to content

Commit 5282450

Browse files
Add blueprint:new command (#192)
1 parent 4d930ba commit 5282450

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

src/BlueprintServiceProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Blueprint\Commands\BlueprintCommand;
66
use Blueprint\Commands\EraseCommand;
7+
use Blueprint\Commands\NewCommand;
78
use Blueprint\Commands\TraceCommand;
89
use Illuminate\Contracts\Support\DeferrableProvider;
910
use Illuminate\Support\Facades\File;
@@ -55,6 +56,11 @@ function ($app) {
5556
return new TraceCommand($app['files']);
5657
}
5758
);
59+
$this->app->bind('command.blueprint.new',
60+
function ($app) {
61+
return new NewCommand($app['files']);
62+
}
63+
);
5864

5965
$this->app->singleton(Blueprint::class, function ($app) {
6066
$blueprint = new Blueprint();
@@ -83,6 +89,7 @@ function ($app) {
8389
'command.blueprint.build',
8490
'command.blueprint.erase',
8591
'command.blueprint.trace',
92+
'command.blueprint.new',
8693
]);
8794
}
8895

@@ -97,6 +104,7 @@ public function provides()
97104
'command.blueprint.build',
98105
'command.blueprint.erase',
99106
'command.blueprint.trace',
107+
'command.blueprint.new',
100108
Blueprint::class,
101109
];
102110
}

src/Commands/NewCommand.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Blueprint\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
8+
class NewCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'blueprint:new';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a draft.yaml file and load existing models';
23+
24+
/** @var Filesystem $files */
25+
protected $files;
26+
27+
/**
28+
* @param Filesystem $files
29+
*/
30+
public function __construct(Filesystem $files)
31+
{
32+
parent::__construct();
33+
34+
$this->files = $files;
35+
}
36+
37+
/**
38+
* Execute the console command.
39+
*
40+
* @return mixed
41+
*/
42+
public function handle()
43+
{
44+
if (!$this->files->exists('draft.yaml')) {
45+
$this->files->put('draft.yaml', $this->files->stub('draft.stub'));
46+
47+
$this->info('Created example draft.yaml');
48+
}
49+
50+
$this->call('blueprint:trace');
51+
}
52+
}

stubs/draft.stub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
models:
2+
# ...
3+
4+
controllers:
5+
# ...
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Tests\Feature\Commands;
4+
5+
use Illuminate\Support\Facades\Artisan;
6+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
7+
use Tests\TestCase;
8+
9+
/**
10+
* @covers \Blueprint\Commands\NewCommand;
11+
*/
12+
class StartCommandTest extends TestCase
13+
{
14+
use MockeryPHPUnitIntegration;
15+
16+
/**
17+
* @test
18+
*/
19+
public function it_creates_a_draft_file_from_stub_if_none_exists()
20+
{
21+
$filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial();
22+
$this->swap('files', $filesystem);
23+
24+
$filesystem->shouldReceive('exists')
25+
->with('draft.yaml')
26+
->andReturnFalse();
27+
$filesystem->shouldReceive('stub')
28+
->with('draft.stub')
29+
->andReturn('stub');
30+
$filesystem->shouldReceive('put')
31+
->with('draft.yaml', 'stub');
32+
33+
$this->artisan('blueprint:new')
34+
->assertExitCode(0);
35+
}
36+
37+
/**
38+
* @test
39+
*/
40+
public function it_does_not_create_a_draft_file_if_one_exists_already()
41+
{
42+
$filesystem = \Mockery::mock(\Illuminate\Filesystem\Filesystem::class)->makePartial();
43+
$this->swap('files', $filesystem);
44+
45+
$filesystem->shouldReceive('exists')
46+
->with('draft.yaml')
47+
->andReturnTrue();
48+
$filesystem->shouldNotReceive('put');
49+
50+
$this->artisan('blueprint:new')
51+
->assertExitCode(0);
52+
}
53+
}

tests/TestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Tests;
44

5+
use Blueprint\BlueprintServiceProvider;
6+
57
class TestCase extends \Orchestra\Testbench\TestCase
68
{
79
protected function getEnvironmentSetUp($app)
@@ -19,4 +21,11 @@ public function fixture(string $path)
1921
{
2022
return file_get_contents(__DIR__ . '/' . 'fixtures' . '/' . ltrim($path, '/'));
2123
}
24+
25+
protected function getPackageProviders($app)
26+
{
27+
return [
28+
BlueprintServiceProvider::class,
29+
];
30+
}
2231
}

0 commit comments

Comments
 (0)