|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GacelaTest\Unit\Console\Domain\CommandArguments; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Console\Domain\CommandArguments\CommandArgumentsException; |
|
8
|
|
|
use Gacela\Console\Domain\CommandArguments\CommandArgumentsParser; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
|
|
11
|
|
|
final class CommandArgumentsParserTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
public function test_exception_when_no_autoload_found(): void |
|
14
|
|
|
{ |
|
15
|
|
|
$this->expectExceptionObject(CommandArgumentsException::noAutoloadFound()); |
|
16
|
|
|
$parser = new CommandArgumentsParser([]); |
|
17
|
|
|
$parser->parse(''); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function test_exception_when_no_psr4_found(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$this->expectExceptionObject(CommandArgumentsException::noAutoloadPsr4Found()); |
|
23
|
|
|
$parser = new CommandArgumentsParser(['autoload' => []]); |
|
24
|
|
|
$parser->parse(''); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function test_parse_one_level_from_root_namespace(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$parser = new CommandArgumentsParser($this->exampleOneLevelComposerJson()); |
|
30
|
|
|
$args = $parser->parse('App/TestModule'); |
|
31
|
|
|
|
|
32
|
|
|
self::assertSame('App\TestModule', $args->namespace()); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function test_parse_multi_level_from_root_namespace(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$parser = new CommandArgumentsParser($this->exampleOneLevelComposerJson()); |
|
38
|
|
|
$args = $parser->parse('App/TestModule/TestSubModule'); |
|
39
|
|
|
|
|
40
|
|
|
self::assertSame('App\TestModule\TestSubModule', $args->namespace()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function test_parse_one_level_from_target_directory(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$parser = new CommandArgumentsParser($this->exampleOneLevelComposerJson()); |
|
46
|
|
|
$args = $parser->parse('App/TestModule'); |
|
47
|
|
|
|
|
48
|
|
|
self::assertSame('src/TestModule', $args->directory()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function test_parse_multi_level_from_target_directory(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$parser = new CommandArgumentsParser($this->exampleOneLevelComposerJson()); |
|
54
|
|
|
$args = $parser->parse('App/TestModule/TestSubModule'); |
|
55
|
|
|
|
|
56
|
|
|
self::assertSame('src/TestModule/TestSubModule', $args->directory()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function test_parse_multilevel_root_namespace(): void |
|
60
|
|
|
{ |
|
61
|
|
|
$parser = new CommandArgumentsParser($this->exampleMultiLevelComposerJson()); |
|
62
|
|
|
$args = $parser->parse('App/TestModule/TestSubModule'); |
|
63
|
|
|
|
|
64
|
|
|
self::assertSame('App\TestModule\TestSubModule', $args->namespace()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function test_parse_multilevel_target_directory(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$parser = new CommandArgumentsParser($this->exampleMultiLevelComposerJson()); |
|
70
|
|
|
$args = $parser->parse('App/TestModule/TestSubModule'); |
|
71
|
|
|
|
|
72
|
|
|
self::assertSame('src/TestSubModule', $args->directory()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function test_no_autoload_psr4_match_found(): void |
|
76
|
|
|
{ |
|
77
|
|
|
$this->expectExceptionMessage( |
|
78
|
|
|
'No autoload psr-4 match found for Unknown/Module. Known PSR-4: App, VendorPackage', |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$parser = new CommandArgumentsParser($this->exampleComposerJsonWithVendorNamespace()); |
|
82
|
|
|
$parser->parse('Unknown/Module'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function test_parse_with_multibyte_namespace(): void |
|
86
|
|
|
{ |
|
87
|
|
|
$parser = new CommandArgumentsParser($this->exampleMultibyteComposerJson()); |
|
88
|
|
|
$args = $parser->parse('Tëst/Mödülé'); |
|
89
|
|
|
|
|
90
|
|
|
self::assertSame('Tëst\Mödülé', $args->namespace()); |
|
91
|
|
|
self::assertSame('src/Mödülé', $args->directory()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function test_parse_prefers_longest_psr4_match(): void |
|
95
|
|
|
{ |
|
96
|
|
|
$parser = new CommandArgumentsParser($this->exampleComposerJsonWithMultipleNamespaces()); |
|
97
|
|
|
$args = $parser->parse('App/Test/SubModule'); |
|
98
|
|
|
|
|
99
|
|
|
self::assertSame('App\Test\SubModule', $args->namespace()); |
|
100
|
|
|
self::assertSame('modules/Test/SubModule', $args->directory()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function exampleOneLevelComposerJson(): array |
|
104
|
|
|
{ |
|
105
|
|
|
$composerJson = <<<'JSON' |
|
106
|
|
|
{ |
|
107
|
|
|
"autoload": { |
|
108
|
|
|
"psr-4": { |
|
109
|
|
|
"App\\": "src/" |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
JSON; |
|
114
|
|
|
return json_decode($composerJson, true, 512, JSON_THROW_ON_ERROR); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return array{autoload: array{psr-4: array<string,string>}} |
|
119
|
|
|
*/ |
|
120
|
|
|
private function exampleComposerJsonWithMultipleNamespaces(): array |
|
121
|
|
|
{ |
|
122
|
|
|
$composerJson = <<<'JSON' |
|
123
|
|
|
{ |
|
124
|
|
|
"autoload": { |
|
125
|
|
|
"psr-4": { |
|
126
|
|
|
"App\\": "src/", |
|
127
|
|
|
"App\\Test\\": "modules/Test/" |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
JSON; |
|
132
|
|
|
return json_decode($composerJson, true, 512, JSON_THROW_ON_ERROR); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return array{autoload: array{psr-4: array<string,string>}} |
|
137
|
|
|
*/ |
|
138
|
|
|
private function exampleComposerJsonWithVendorNamespace(): array |
|
139
|
|
|
{ |
|
140
|
|
|
$composerJson = <<<'JSON' |
|
141
|
|
|
{ |
|
142
|
|
|
"autoload": { |
|
143
|
|
|
"psr-4": { |
|
144
|
|
|
"App\\": "src/", |
|
145
|
|
|
"Vendor\\Package\\": "packages/" |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
JSON; |
|
150
|
|
|
return json_decode($composerJson, true, 512, JSON_THROW_ON_ERROR); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return array{autoload: array{psr-4: array<string,string>}} |
|
155
|
|
|
*/ |
|
156
|
|
|
private function exampleMultiLevelComposerJson(): array |
|
157
|
|
|
{ |
|
158
|
|
|
$composerJson = <<<'JSON' |
|
159
|
|
|
{ |
|
160
|
|
|
"autoload": { |
|
161
|
|
|
"psr-4": { |
|
162
|
|
|
"App\\TestModule\\": "src/" |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
JSON; |
|
167
|
|
|
return json_decode($composerJson, true, 512, JSON_THROW_ON_ERROR); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return array{autoload: array{psr-4: array<string,string>}} |
|
172
|
|
|
*/ |
|
173
|
|
|
private function exampleMultibyteComposerJson(): array |
|
174
|
|
|
{ |
|
175
|
|
|
$composerJson = <<<'JSON' |
|
176
|
|
|
{ |
|
177
|
|
|
"autoload": { |
|
178
|
|
|
"psr-4": { |
|
179
|
|
|
"Tëst\\": "src/" |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
JSON; |
|
184
|
|
|
return json_decode($composerJson, true); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|