Skip to content

Commit d93983d

Browse files
authored
Refactor filesystem mocks (#449)
1 parent 759eac3 commit d93983d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1112
-969
lines changed

composer.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,20 @@
88
],
99
"license": "MIT",
1010
"require": {
11+
"doctrine/dbal": "^2.9 || ^3.0",
12+
"illuminate/console": "^6.0 || ^7.0 || ^8.0",
13+
"illuminate/filesystem": "^6.0 || ^7.0 || ^8.0",
14+
"illuminate/support": "^6.0 || ^7.0 || ^8.0",
1115
"laravel-shift/faker-registry": "^0.1",
12-
"symfony/yaml": "^4.3|^5.0",
13-
"illuminate/console": "^6.0|^7.0|^8.0",
14-
"illuminate/filesystem": "^6.0|^7.0|^8.0",
15-
"illuminate/support": "^6.0|^7.0|^8.0",
16-
"doctrine/dbal": "^2.9|^3.0"
16+
"symfony/yaml": "^4.3 || ^5.0"
1717
},
1818
"require-dev": {
19-
"phpunit/phpunit": "^8.0|^9.3",
2019
"mockery/mockery": "^1.3",
21-
"orchestra/testbench": "^4.0|^5.0|^6.0"
20+
"orchestra/testbench": "^4.0 || ^5.0 || ^6.0",
21+
"phpunit/phpunit": "^8.0 || ^9.3"
2222
},
23-
"autoload": {
24-
"psr-4": {
25-
"Blueprint\\": "src/"
26-
}
27-
},
28-
"autoload-dev": {
29-
"psr-4": {
30-
"Tests\\": "tests/"
31-
}
23+
"suggest": {
24+
"jasonmccreary/laravel-test-assertions": "Required to use additional assertions in generated tests (^1.0)."
3225
},
3326
"config": {
3427
"sort-packages": true
@@ -40,7 +33,14 @@
4033
]
4134
}
4235
},
43-
"suggest": {
44-
"jasonmccreary/laravel-test-assertions": "Required to use additional assertions in generated tests (^1.0)."
36+
"autoload": {
37+
"psr-4": {
38+
"Blueprint\\": "src/"
39+
}
40+
},
41+
"autoload-dev": {
42+
"psr-4": {
43+
"Tests\\": "tests/"
44+
}
4545
}
4646
}

src/Blueprint.php

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,45 @@ public function parse($content, $strip_dashes = true)
5353
$content = preg_replace('/^(\s*)-\s*/m', '\1', $content);
5454
}
5555

56-
$content = preg_replace_callback('/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)$/mi', function ($matches) {
57-
return $matches[1] . strtolower($matches[2]) . ': ' . $matches[2];
58-
}, $content);
59-
60-
$content = preg_replace_callback('/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?): true$/mi', function ($matches) {
61-
return $matches[1] . strtolower($matches[2]) . ': ' . $matches[2];
62-
}, $content);
63-
64-
$content = preg_replace_callback('/^(\s+)resource?$/mi', function ($matches) {
65-
return $matches[1] . 'resource: web';
66-
}, $content);
67-
68-
$content = preg_replace_callback('/^(\s+)invokable?$/mi', function ($matches) {
69-
return $matches[1].'invokable: true';
70-
}, $content);
71-
72-
$content = preg_replace_callback('/^(\s+)uuid(: true)?$/mi', function ($matches) {
73-
return $matches[1] . 'id: uuid primary';
74-
}, $content);
56+
$content = preg_replace_callback(
57+
'/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)$/mi',
58+
function ($matches) {
59+
return $matches[1] . strtolower($matches[2]) . ': ' . $matches[2];
60+
},
61+
$content
62+
);
63+
64+
$content = preg_replace_callback(
65+
'/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?): true$/mi',
66+
function ($matches) {
67+
return $matches[1] . strtolower($matches[2]) . ': ' . $matches[2];
68+
},
69+
$content
70+
);
71+
72+
$content = preg_replace_callback(
73+
'/^(\s+)resource?$/mi',
74+
function ($matches) {
75+
return $matches[1] . 'resource: web';
76+
},
77+
$content
78+
);
79+
80+
$content = preg_replace_callback(
81+
'/^(\s+)invokable?$/mi',
82+
function ($matches) {
83+
return $matches[1] . 'invokable: true';
84+
},
85+
$content
86+
);
87+
88+
$content = preg_replace_callback(
89+
'/^(\s+)uuid(: true)?$/mi',
90+
function ($matches) {
91+
return $matches[1] . 'id: uuid primary';
92+
},
93+
$content
94+
);
7595

7696
return Yaml::parse($content);
7797
}

src/Builder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
class Builder
88
{
9-
public function execute(Blueprint $blueprint, Filesystem $files, string $draft, string $only = '', string $skip = '', $overwriteMigrations = false)
9+
public function execute(Blueprint $blueprint, Filesystem $filesystem, string $draft, string $only = '', string $skip = '', $overwriteMigrations = false)
1010
{
1111
$cache = [];
12-
if ($files->exists('.blueprint')) {
13-
$cache = $blueprint->parse($files->get('.blueprint'));
12+
if ($filesystem->exists('.blueprint')) {
13+
$cache = $blueprint->parse($filesystem->get('.blueprint'));
1414
}
1515

16-
$contents = $files->get($draft);
16+
$contents = $filesystem->get($draft);
1717
$using_indexes = preg_match('/^\s+indexes:\R/m', $contents) !== 1;
1818

1919
$tokens = $blueprint->parse($contents, $using_indexes);
@@ -27,7 +27,7 @@ public function execute(Blueprint $blueprint, Filesystem $files, string $draft,
2727

2828
$models = array_merge($tokens['cache'], $tokens['models'] ?? []);
2929

30-
$files->put(
30+
$filesystem->put(
3131
'.blueprint',
3232
$blueprint->dump($generated + ($models ? ['models' => $models] : []))
3333
);

src/Commands/BuildCommand.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,28 @@ class BuildCommand extends Command
3131
protected $description = 'Build components from a Blueprint draft';
3232

3333
/** @var Filesystem */
34-
protected $files;
34+
protected $filesystem;
3535

3636
/** @var Builder */
3737
private $builder;
3838

3939
/**
40-
* @param Filesystem $files
41-
* @param Builder $builder
40+
* @param Filesystem $filesystem
41+
* @param Builder $builder
4242
*/
43-
public function __construct(Filesystem $files, Builder $builder)
43+
public function __construct(Filesystem $filesystem, Builder $builder)
4444
{
4545
parent::__construct();
4646

47-
$this->files = $files;
47+
$this->filesystem = $filesystem;
4848
$this->builder = $builder;
4949
}
5050

5151
public function handle()
5252
{
5353
$file = $this->argument('draft') ?? $this->defaultDraftFile();
5454

55-
if (!$this->files->exists($file)) {
55+
if (!$this->filesystem->exists($file)) {
5656
$this->error('Draft file could not be found: ' . ($file ?: 'draft.yaml'));
5757
return 1;
5858
}
@@ -62,16 +62,20 @@ public function handle()
6262
$overwriteMigrations = $this->option('overwrite-migrations') ?: false;
6363

6464
$blueprint = resolve(Blueprint::class);
65-
$generated = $this->builder->execute($blueprint, $this->files, $file, $only, $skip, $overwriteMigrations);
66-
67-
collect($generated)->each(function ($files, $action) {
68-
$this->line(Str::studly($action) . ':', $this->outputStyle($action));
69-
collect($files)->each(function ($file) {
70-
$this->line('- ' . $file);
71-
});
72-
73-
$this->line('');
74-
});
65+
$generated = $this->builder->execute($blueprint, $this->filesystem, $file, $only, $skip, $overwriteMigrations);
66+
67+
collect($generated)->each(
68+
function ($files, $action) {
69+
$this->line(Str::studly($action) . ':', $this->outputStyle($action));
70+
collect($files)->each(
71+
function ($file) {
72+
$this->line('- ' . $file);
73+
}
74+
);
75+
76+
$this->line('');
77+
}
78+
);
7579
}
7680

7781
/**

src/Commands/EraseCommand.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@ class EraseCommand extends Command
2323
*/
2424
protected $description = 'Erase components created from last Blueprint build';
2525

26-
/** @var Filesystem $files */
27-
protected $files;
26+
/** @var Filesystem */
27+
protected $filesystem;
2828

29-
/**
30-
* @param Filesystem $files
31-
* @param \Illuminate\Contracts\View\Factory $view
32-
*/
33-
public function __construct(Filesystem $files)
29+
public function __construct(Filesystem $filesystem)
3430
{
3531
parent::__construct();
3632

37-
$this->files = $files;
33+
$this->filesystem = $filesystem;
3834
}
3935

4036
/**
@@ -44,33 +40,37 @@ public function __construct(Filesystem $files)
4440
*/
4541
public function handle()
4642
{
47-
$contents = $this->files->get('.blueprint');
43+
$contents = $this->filesystem->get('.blueprint');
4844

4945
$blueprint = resolve(Blueprint::class);
5046

5147
$generated = $blueprint->parse($contents, false);
5248

53-
collect($generated)->each(function ($files, $action) {
54-
if ($action === 'created') {
55-
$this->line('Deleted:', $this->outputStyle($action));
56-
$this->files->delete($files);
57-
} elseif ($action === 'updated') {
58-
$this->comment('The updates to the following files can not be erased automatically.');
59-
} else {
60-
return;
49+
collect($generated)->each(
50+
function ($files, $action) {
51+
if ($action === 'created') {
52+
$this->line('Deleted:', $this->outputStyle($action));
53+
$this->filesystem->delete($files);
54+
} elseif ($action === 'updated') {
55+
$this->comment('The updates to the following files can not be erased automatically.');
56+
} else {
57+
return;
58+
}
59+
60+
collect($files)->each(
61+
function ($file) {
62+
$this->line('- ' . $file);
63+
}
64+
);
65+
66+
$this->line('');
6167
}
62-
63-
collect($files)->each(function ($file) {
64-
$this->line('- ' . $file);
65-
});
66-
67-
$this->line('');
68-
});
68+
);
6969

7070
unset($generated['created']);
7171
unset($generated['updated']);
7272

73-
$this->files->put('.blueprint', $blueprint->dump($generated));
73+
$this->filesystem->put('.blueprint', $blueprint->dump($generated));
7474

7575
$this->call('blueprint:trace');
7676
}

src/Commands/NewCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ class NewCommand extends Command
2222
protected $description = 'Create a draft.yaml file and load existing models';
2323

2424
/** @var Filesystem $files */
25-
protected $files;
25+
protected $filesystem;
2626

2727
/**
28-
* @param Filesystem $files
28+
* @param Filesystem $filesystem
2929
*/
30-
public function __construct(Filesystem $files)
30+
public function __construct(Filesystem $filesystem)
3131
{
3232
parent::__construct();
3333

34-
$this->files = $files;
34+
$this->filesystem = $filesystem;
3535
}
3636

3737
/**
@@ -41,8 +41,8 @@ public function __construct(Filesystem $files)
4141
*/
4242
public function handle()
4343
{
44-
if (!$this->files->exists('draft.yaml')) {
45-
$this->files->put('draft.yaml', $this->files->stub('draft.stub'));
44+
if (!$this->filesystem->exists('draft.yaml')) {
45+
$this->filesystem->put('draft.yaml', $this->filesystem->stub('draft.stub'));
4646

4747
$this->info('Created example draft.yaml');
4848
}

src/Commands/TraceCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ class TraceCommand extends Command
2525
protected $description = 'Create definitions for existing models to reference in new drafts';
2626

2727
/** @var Filesystem $files */
28-
protected $files;
28+
protected $filesystem;
2929

3030
/** @var Tracer */
3131
private $tracer;
3232

3333
/**
34-
* @param Filesystem $files
35-
* @param Tracer $tracer
34+
* @param Filesystem $filesystem
35+
* @param Tracer $tracer
3636
*/
37-
public function __construct(Filesystem $files, Tracer $tracer)
37+
public function __construct(Filesystem $filesystem, Tracer $tracer)
3838
{
3939
parent::__construct();
4040

41-
$this->files = $files;
41+
$this->filesystem = $filesystem;
4242
$this->tracer = $tracer;
4343
}
4444

@@ -50,7 +50,7 @@ public function __construct(Filesystem $files, Tracer $tracer)
5050
public function handle()
5151
{
5252
$blueprint = resolve(Blueprint::class);
53-
$definitions = $this->tracer->execute($blueprint, $this->files);
53+
$definitions = $this->tracer->execute($blueprint, $this->filesystem);
5454

5555
if (empty($definitions)) {
5656
$this->error('No models found');

src/Contracts/Generator.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
namespace Blueprint\Contracts;
44

55
use Blueprint\Tree;
6+
use Illuminate\Filesystem\Filesystem;
67

78
interface Generator
89
{
9-
/**
10-
* @param \Illuminate\Contracts\Filesystem\Filesystem
11-
*/
12-
public function __construct($files);
10+
public function __construct(Filesystem $files);
1311

1412
public function output(Tree $tree): array;
1513

0 commit comments

Comments
 (0)