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->expectExceptionObject( |
78
|
|
|
CommandArgumentsException::noAutoloadPsr4MatchFound('Unknown/Module', ['App']), |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$parser = new CommandArgumentsParser($this->exampleOneLevelComposerJson()); |
82
|
|
|
$parser->parse('Unknown/Module'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function exampleOneLevelComposerJson(): array |
86
|
|
|
{ |
87
|
|
|
$composerJson = <<<'JSON' |
88
|
|
|
{ |
89
|
|
|
"autoload": { |
90
|
|
|
"psr-4": { |
91
|
|
|
"App\\": "src/" |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
JSON; |
96
|
|
|
return json_decode($composerJson, true); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array{autoload: array{psr-4: array<string,string>}} |
101
|
|
|
*/ |
102
|
|
|
private function exampleMultiLevelComposerJson(): array |
103
|
|
|
{ |
104
|
|
|
$composerJson = <<<'JSON' |
105
|
|
|
{ |
106
|
|
|
"autoload": { |
107
|
|
|
"psr-4": { |
108
|
|
|
"App\\TestModule\\": "src/" |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
JSON; |
113
|
|
|
return json_decode($composerJson, true); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|