1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Porter\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use SilverStripe\Assets\Filesystem; |
7
|
|
|
use SilverStripe\Porter\Commands\CreateModuleCommand; |
8
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
9
|
|
|
use Symfony\Component\Yaml\Exception\RuntimeException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class CreateModuleCommandTest |
13
|
|
|
*/ |
14
|
|
|
class CreateModuleCommandTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
protected static $fixture_file = ''; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var CreateModuleCommand |
20
|
|
|
*/ |
21
|
|
|
private $command; |
22
|
|
|
|
23
|
|
|
protected function setUp() |
24
|
|
|
{ |
25
|
|
|
parent::setUp(); |
26
|
|
|
$this->command = new CreateModuleCommand(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testCommandIsConfiguredCorrectly() |
30
|
|
|
{ |
31
|
|
|
$this->assertEquals('create-module', $this->command->getName()); |
32
|
|
|
$this->assertContains( |
33
|
|
|
'Sets up a new SilverStripe module skeleton at', |
34
|
|
|
$this->command->getDescription() |
35
|
|
|
); |
36
|
|
|
$def = $this->command->getDefinition(); |
37
|
|
|
$this->assertTrue($def->hasArgument(CreateModuleCommand::ARGUMENTS_MODULE_NAME)); |
38
|
|
|
$this->assertTrue($def->hasArgument(CreateModuleCommand::ARGUMENTS_MODULE_NAMESPACE)); |
39
|
|
|
$this->assertTrue($def->hasOption(CreateModuleCommand::OPTIONS_NON_VENDOR)); |
40
|
|
|
$this->assertTrue($def->hasOption(CreateModuleCommand::OPTIONS_SS3)); |
41
|
|
|
$this->assertTrue($def->hasOption(CreateModuleCommand::OPTIONS_TRAVIS_CI)); |
42
|
|
|
$this->assertTrue($def->hasOption(CreateModuleCommand::OPTIONS_CIRCLE_CI)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @expectedException RuntimeException |
47
|
|
|
* @expectedExceptionMessage Invalid module name given. Use the format module/name |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function testInvalidModuleNmeThrowsException() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$tester = new CommandTester($this->command); |
52
|
|
|
$input = [ |
53
|
|
|
'module-name' => 'foo', |
54
|
|
|
'module-namespace' => 'Foo' |
55
|
|
|
]; |
56
|
|
|
$tester->execute($input); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @expectedException RuntimeException |
61
|
|
|
* @expectedExceptionMessage It seems your namespace is formed incorrectly. |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function testInvalidNameSpaceThrowsException() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$tester = new CommandTester($this->command); |
66
|
|
|
$input = [ |
67
|
|
|
'module-name' => 'foo/bar', |
68
|
|
|
'module-namespace' => 'Foo' |
69
|
|
|
]; |
70
|
|
|
$tester->execute($input); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* |
75
|
|
|
*/ |
76
|
|
|
public function testComposerVariablesGetsSet() |
77
|
|
|
{ |
78
|
|
|
$tester = new CommandTester($this->command); |
79
|
|
|
$input = [ |
80
|
|
|
'module-name' => 'foo/bar', |
81
|
|
|
'module-namespace' => 'Foo\\\\Bar' |
82
|
|
|
]; |
83
|
|
|
$tester->execute($input); |
84
|
|
|
$composerContents = $this->command->getComposerFileContents(); |
85
|
|
|
$this->assertNotContains('$moduleName', $composerContents); |
86
|
|
|
$this->assertNotContains('$namespace', $composerContents); |
87
|
|
|
$this->assertNotContains('$moduleType', $composerContents); |
88
|
|
|
$this->deleteDirectory($this->command->getTargetPath()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testCopySkeleton() |
92
|
|
|
{ |
93
|
|
|
$tester = new CommandTester($this->command); |
94
|
|
|
$input = [ |
95
|
|
|
'module-name' => 'foo/bar', |
96
|
|
|
'module-namespace' => 'Foo\\\\bar', |
97
|
|
|
'--withCircleCI' => true |
98
|
|
|
]; |
99
|
|
|
$tester->execute($input); |
100
|
|
|
|
101
|
|
|
$targetPath = $this->command->getTargetPath(); |
102
|
|
|
$sourceComposer = file_get_contents( |
103
|
|
|
$this->command->getSourcePath() . |
104
|
|
|
DIRECTORY_SEPARATOR . 'composer.json' |
105
|
|
|
); |
106
|
|
|
$this->assertTrue(is_dir($targetPath)); |
107
|
|
|
$this->assertTrue( |
108
|
|
|
is_dir( |
109
|
|
|
$targetPath . DIRECTORY_SEPARATOR . '.circleci' |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
$this->assertNotEquals( |
113
|
|
|
$sourceComposer, |
114
|
|
|
$this->command->getComposerFileContents() |
115
|
|
|
); |
116
|
|
|
$this->deleteDirectory($this->command->getTargetPath()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testPreCopyOptions() |
120
|
|
|
{ |
121
|
|
|
$tester = new CommandTester($this->command); |
122
|
|
|
$input = [ |
123
|
|
|
'module-name' => 'foo/bar', |
124
|
|
|
'module-namespace' => 'Foo\\\\Bar', |
125
|
|
|
]; |
126
|
|
|
$tester->execute($input); |
127
|
|
|
$this->assertContains( |
128
|
|
|
'silverstripe-vendormodule', |
129
|
|
|
$this->command->getComposerFileContents() |
130
|
|
|
); |
131
|
|
|
$this->deleteDirectory($this->command->getTargetPath()); |
132
|
|
|
|
133
|
|
|
$input['--nonVendor'] = true; |
134
|
|
|
$tester->execute($input); |
135
|
|
|
$this->assertContains( |
136
|
|
|
'silverstripe-module', |
137
|
|
|
$this->command->getComposerFileContents() |
138
|
|
|
); |
139
|
|
|
$this->deleteDirectory($this->command->getTargetPath()); |
140
|
|
|
$input['--ss3'] = true; |
141
|
|
|
$tester->execute($input); |
142
|
|
|
$this->assertContains( |
143
|
|
|
'3.6', |
144
|
|
|
$this->command->getComposerFileContents() |
145
|
|
|
); |
146
|
|
|
$this->assertFileExists( |
147
|
|
|
$this->command->getTargetPath() . |
148
|
|
|
DIRECTORY_SEPARATOR . '_config.php' |
149
|
|
|
); |
150
|
|
|
$this->deleteDirectory($this->command->getTargetPath()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testPostCopyOptions() |
154
|
|
|
{ |
155
|
|
|
$tester = new CommandTester($this->command); |
156
|
|
|
$input = [ |
157
|
|
|
'module-name' => 'foo/bar', |
158
|
|
|
'module-namespace' => 'Foo\\\\Bar', |
159
|
|
|
]; |
160
|
|
|
$tester->execute($input); |
161
|
|
|
$this->assertFileNotExists( |
162
|
|
|
$this->command->getTargetPath() . |
163
|
|
|
DIRECTORY_SEPARATOR . '.travis.yml' |
164
|
|
|
); |
165
|
|
|
$this->assertDirectoryNotExists( |
166
|
|
|
$this->command->getTargetPath() . |
167
|
|
|
DIRECTORY_SEPARATOR . '.circleci' |
168
|
|
|
); |
169
|
|
|
$this->deleteDirectory($this->command->getTargetPath()); |
170
|
|
|
|
171
|
|
|
$input['--withTravisCI'] = true; |
172
|
|
|
$tester->execute($input); |
173
|
|
|
$this->assertFileExists( |
174
|
|
|
$this->command->getTargetPath() . |
175
|
|
|
DIRECTORY_SEPARATOR . '.travis.yml' |
176
|
|
|
); |
177
|
|
|
$this->deleteDirectory($this->command->getTargetPath()); |
178
|
|
|
$input['--withCircleCI'] = true; |
179
|
|
|
$tester->execute($input); |
180
|
|
|
$this->assertDirectoryExists( |
181
|
|
|
$this->command->getTargetPath() . |
182
|
|
|
DIRECTORY_SEPARATOR . '.circleci' |
183
|
|
|
); |
184
|
|
|
$this->deleteDirectory($this->command->getTargetPath()); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Recursively deletes a directory |
189
|
|
|
* @param $dir |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
|
private function deleteDirectory($dir) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$this->command->getFilesystem()->remove($this->command->getTargetPath()); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.