1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Mappers; |
4
|
|
|
|
5
|
|
|
use Craft; |
6
|
|
|
use Exception; |
7
|
|
|
use craft\base\Model; |
8
|
|
|
use craft\base\Plugin; |
9
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
10
|
|
|
use Codeception\Test\Unit; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Plugin Mapper Test. |
14
|
|
|
* |
15
|
|
|
* @author Nerds & Company |
16
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
17
|
|
|
* @license MIT |
18
|
|
|
* |
19
|
|
|
* @see http://www.nerds.company |
20
|
|
|
*/ |
21
|
|
|
class PluginMapperTest extends Unit |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var PluginMapper |
25
|
|
|
*/ |
26
|
|
|
private $mapper; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Set the mapper. |
30
|
|
|
* |
31
|
|
|
* @SuppressWarnings(PHPMD.CamelCaseMethodName) |
32
|
|
|
*/ |
33
|
|
|
protected function _before() |
34
|
|
|
{ |
35
|
|
|
$this->mapper = new PluginMapper(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
//============================================================================================================== |
39
|
|
|
//================================================= TESTS ==================================================== |
40
|
|
|
//============================================================================================================== |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Test default import functionality. |
44
|
|
|
*/ |
45
|
|
|
public function testImportWithInstalledPlugins() |
46
|
|
|
{ |
47
|
|
|
$definitions = $this->getPluginsDefinition(); |
48
|
|
|
$data = $this->getPluginsData(); |
49
|
|
|
|
50
|
|
|
$this->mockPluginsService(); |
51
|
|
|
|
52
|
|
|
$result = $this->mapper->import($definitions, $data); |
53
|
|
|
|
54
|
|
|
$this->assertCount(count($definitions), $result); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Test default import functionality. |
59
|
|
|
*/ |
60
|
|
|
public function testImportWithInstalledDisabledPlugins() |
61
|
|
|
{ |
62
|
|
|
$this->getMockplugin(); |
63
|
|
|
|
64
|
|
|
$definitions = $this->getPluginsDefinition(); |
65
|
|
|
$definitions['pluginHandle']['isEnabled'] = false; |
66
|
|
|
$data = $this->getPluginsData(); |
67
|
|
|
|
68
|
|
|
$this->mockPluginsService(); |
69
|
|
|
|
70
|
|
|
$result = $this->mapper->import($definitions, $data); |
71
|
|
|
|
72
|
|
|
$this->assertCount(count($definitions), $result); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Test default import functionality. |
77
|
|
|
*/ |
78
|
|
|
public function testImportWithMissingPlugin() |
79
|
|
|
{ |
80
|
|
|
$definitions = $this->getPluginsDefinition(); |
81
|
|
|
|
82
|
|
|
$this->mockPluginsService(false); |
83
|
|
|
|
84
|
|
|
$result = $this->mapper->import($definitions, []); |
85
|
|
|
|
86
|
|
|
$this->assertCount(0, $result); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Test default import functionality. |
91
|
|
|
*/ |
92
|
|
|
public function testImportWithInstallException() |
93
|
|
|
{ |
94
|
|
|
$definitions = $this->getPluginsDefinition(); |
95
|
|
|
$data = $this->getPluginsData(); |
96
|
|
|
|
97
|
|
|
$this->mockPluginsService(true, false); |
98
|
|
|
|
99
|
|
|
$this->expectException(Exception::class); |
100
|
|
|
|
101
|
|
|
$this->mapper->import($definitions, $data); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Test default import functionality. |
106
|
|
|
*/ |
107
|
|
|
public function testImportWithNotInstalledPlugin() |
108
|
|
|
{ |
109
|
|
|
$this->getMockplugin(); |
110
|
|
|
|
111
|
|
|
$definitions = $this->getPluginsDefinition(); |
112
|
|
|
$definitions['pluginHandle']['isInstalled'] = false; |
113
|
|
|
$data = $this->getPluginsData(); |
114
|
|
|
|
115
|
|
|
$this->mockPluginsService(); |
116
|
|
|
|
117
|
|
|
$result = $this->mapper->import($definitions, $data); |
118
|
|
|
|
119
|
|
|
$this->assertCount(0, $result); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Test default import functionality. |
124
|
|
|
*/ |
125
|
|
|
public function testImportWithForce() |
126
|
|
|
{ |
127
|
|
|
$data = $this->getPluginsData(); |
128
|
|
|
$data['pluginHandle']['isInstalled'] = true; |
129
|
|
|
|
130
|
|
|
$this->mockPluginsService(false); |
131
|
|
|
|
132
|
|
|
Craft::$app->plugins->expects($this->exactly(1)) |
133
|
|
|
->method('uninstallPlugin') |
134
|
|
|
->willReturn(true); |
135
|
|
|
|
136
|
|
|
Schematic::$force = true; |
137
|
|
|
$result = $this->mapper->import([], $data); |
138
|
|
|
|
139
|
|
|
$this->assertCount(0, $result); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Test export functionality. |
144
|
|
|
*/ |
145
|
|
|
public function testExport() |
146
|
|
|
{ |
147
|
|
|
$data = $this->getPluginsData(); |
148
|
|
|
$data['pluginHandle']['isInstalled'] = true; |
149
|
|
|
$data['pluginHandle']['isEnabled'] = true; |
150
|
|
|
|
151
|
|
|
$definitions = $this->getPluginsDefinition(); |
152
|
|
|
$this->mockPluginsService(); |
153
|
|
|
|
154
|
|
|
$result = $this->mapper->export($data); |
155
|
|
|
|
156
|
|
|
$this->assertEquals($definitions, $result); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
//============================================================================================================== |
160
|
|
|
//================================================ HELPERS =================================================== |
161
|
|
|
//============================================================================================================== |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param bool $returnPlugin |
165
|
|
|
* @param bool $installPluginResponse |
166
|
|
|
* @param bool $enablePluginResponse |
167
|
|
|
* @param bool $disablePluginResponse |
168
|
|
|
* @param bool $saveSettingsResponse |
169
|
|
|
*/ |
170
|
|
|
private function mockPluginsService( |
171
|
|
|
$returnPlugin = true, |
172
|
|
|
$installPluginResponse = true, |
173
|
|
|
$enablePluginResponse = true, |
174
|
|
|
$disablePluginResponse = true, |
175
|
|
|
$saveSettingsResponse = true |
176
|
|
|
) { |
177
|
|
|
Craft::$app->plugins->expects($this->any())->method('getPlugin')->willReturn(($returnPlugin) ? $this->getMockplugin() : null); |
178
|
|
|
|
179
|
|
|
if ($installPluginResponse) { |
180
|
|
|
Craft::$app->plugins->expects($this->any())->method('installPlugin')->willReturn($installPluginResponse); |
181
|
|
|
} else { |
182
|
|
|
Craft::$app->plugins->expects($this->any())->method('installPlugin')->willThrowException(new Exception()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
Craft::$app->plugins->expects($this->any())->method('enablePlugin')->willReturn($enablePluginResponse); |
186
|
|
|
Craft::$app->plugins->expects($this->any())->method('disablePlugin')->willReturn($disablePluginResponse); |
187
|
|
|
Craft::$app->plugins->expects($this->any())->method('savePluginSettings')->willReturn($saveSettingsResponse); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return Mock|plugin |
192
|
|
|
*/ |
193
|
|
|
private function getMockplugin() |
194
|
|
|
{ |
195
|
|
|
$mockPlugin = $this->getMockBuilder(Plugin::class) |
196
|
|
|
->disableOriginalConstructor() |
197
|
|
|
->getMock(); |
198
|
|
|
|
199
|
|
|
$mockPlugin->expects($this->any()) |
200
|
|
|
->method('getSettings') |
201
|
|
|
->willReturn($this->getMockSettings()); |
202
|
|
|
|
203
|
|
|
return $mockPlugin; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get mock settings. |
208
|
|
|
* |
209
|
|
|
* @return Mock|Model |
210
|
|
|
*/ |
211
|
|
|
private function getMockSettings() |
212
|
|
|
{ |
213
|
|
|
$mockSettings = $this->getMockBuilder(Model::class)->getMock(); |
214
|
|
|
$mockSettings->expects($this->any()) |
215
|
|
|
->method('__get') |
216
|
|
|
->willReturnMap([ |
217
|
|
|
['attributes', [ |
218
|
|
|
'pluginName' => 'Menu', |
219
|
|
|
'canDoActions' => '', |
220
|
|
|
'quietErrors' => '', |
221
|
|
|
]], |
222
|
|
|
]); |
223
|
|
|
|
224
|
|
|
return $mockSettings; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Returns plugins data. |
229
|
|
|
* |
230
|
|
|
* @return array |
231
|
|
|
*/ |
232
|
|
|
private function getPluginsData() |
233
|
|
|
{ |
234
|
|
|
return [ |
235
|
|
|
'pluginHandle' => [ |
236
|
|
|
'isInstalled' => false, |
237
|
|
|
'isEnabled' => false, |
238
|
|
|
'settings' => [ |
239
|
|
|
'pluginName' => 'Menu', |
240
|
|
|
'canDoActions' => '', |
241
|
|
|
'quietErrors' => '', |
242
|
|
|
], |
243
|
|
|
], |
244
|
|
|
]; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Returns plugins data. |
249
|
|
|
* |
250
|
|
|
* @return array |
251
|
|
|
*/ |
252
|
|
|
private function getPluginsDefinition() |
253
|
|
|
{ |
254
|
|
|
return [ |
255
|
|
|
'pluginHandle' => [ |
256
|
|
|
'isInstalled' => true, |
257
|
|
|
'isEnabled' => true, |
258
|
|
|
'settings' => [ |
259
|
|
|
'pluginName' => 'Menu', |
260
|
|
|
'canDoActions' => '', |
261
|
|
|
'quietErrors' => '', |
262
|
|
|
], |
263
|
|
|
], |
264
|
|
|
]; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: