1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shamaseen\Repository\Tests\Feature; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Config; |
6
|
|
|
use Shamaseen\Repository\Commands\Generator; |
7
|
|
|
use Shamaseen\Repository\PathResolver; |
8
|
|
|
use Shamaseen\Repository\Tests\TestCase; |
9
|
|
|
|
10
|
|
|
class GenerateFilesTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var mixed |
14
|
|
|
*/ |
15
|
|
|
private PathResolver $pathResolver; |
16
|
|
|
|
17
|
|
|
private array $filesToGenerate = ['Controller', 'Repository', 'Model', 'Request', 'Resource', 'Collection', 'Policy', 'Test']; |
18
|
|
|
|
19
|
|
|
protected string $modelName = 'Test'; |
20
|
|
|
protected string $userPath = 'Tests'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $dataName |
24
|
|
|
*/ |
25
|
|
|
public function __construct(?string $name = null, array $data = [], $dataName = '') |
26
|
|
|
{ |
27
|
|
|
parent::__construct($name, $data, $dataName); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function setUp(): void |
31
|
|
|
{ |
32
|
|
|
parent::setUp(); |
33
|
|
|
$this->pathResolver = new PathResolver($this->modelName, $this->userPath, config('repository.base_path')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testGenerate() |
37
|
|
|
{ |
38
|
|
|
$this->artisan("generate:repository $this->userPath/$this->modelName -f"); |
39
|
|
|
|
40
|
|
|
foreach ($this->filesToGenerate as $type) { |
41
|
|
|
$absolutePath = $this->pathResolver->absolutePath($type); |
42
|
|
|
|
43
|
|
|
$this->assertFileExists($absolutePath); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testGenerateMCROnly() |
48
|
|
|
{ |
49
|
|
|
$this->artisan("generate:repository $this->userPath/$this->modelName -f -mrc"); |
50
|
|
|
|
51
|
|
|
$filesToGenerate = ['Controller', 'Repository', 'Model']; |
52
|
|
|
foreach ($filesToGenerate as $type) { |
53
|
|
|
$absolutePath = $this->pathResolver->absolutePath($type); |
54
|
|
|
|
55
|
|
|
$this->assertFileExists($absolutePath); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function defaultStubsConfigProvider(): array |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
// run 1 |
63
|
|
|
[ |
64
|
|
|
[ |
65
|
|
|
Generator::RESOURCE_OPTION |
66
|
|
|
], |
67
|
|
|
[ |
68
|
|
|
'Resource', |
69
|
|
|
] |
70
|
|
|
], |
71
|
|
|
// run 2 |
72
|
|
|
[ |
73
|
|
|
[ |
74
|
|
|
Generator::MODEL_OPTION, |
75
|
|
|
Generator::CONTROLLER_OPTION, |
76
|
|
|
], |
77
|
|
|
[ |
78
|
|
|
'Model', |
79
|
|
|
'Controller', |
80
|
|
|
], |
81
|
|
|
], |
82
|
|
|
// running Request option should only generate Request |
83
|
|
|
[ |
84
|
|
|
[ |
85
|
|
|
Generator::REQUEST_OPTION, |
86
|
|
|
], |
87
|
|
|
[ |
88
|
|
|
'Request', |
89
|
|
|
] |
90
|
|
|
], |
91
|
|
|
// running Collection option should only generate Collection |
92
|
|
|
[ |
93
|
|
|
[ |
94
|
|
|
Generator::COLLECTION_OPTION, |
95
|
|
|
], |
96
|
|
|
[ |
97
|
|
|
'Collection', |
98
|
|
|
] |
99
|
|
|
], |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** @dataProvider defaultStubsConfigProvider */ |
104
|
|
|
public function testDefaultStubsConfig(array $config, array $generatedNames) |
105
|
|
|
{ |
106
|
|
|
Config::set('repository.default_stubs', $config); |
107
|
|
|
$this->artisan("generate:repository $this->userPath/$this->modelName -f"); |
108
|
|
|
|
109
|
|
|
foreach ($generatedNames as $generatedName) { |
110
|
|
|
$this->assertFileExists($this->pathResolver->absolutePath($generatedName)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$allGeneratedStubs = array_keys(PathResolver::$configTypePathMap); |
114
|
|
|
$filesNotGenerated = array_diff($allGeneratedStubs, $generatedNames); |
115
|
|
|
|
116
|
|
|
foreach ($filesNotGenerated as $fileNotGenerated) { |
117
|
|
|
$this->assertFileDoesNotExist($this->pathResolver->absolutePath($fileNotGenerated)); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|