|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Craft\Craft; |
|
6
|
|
|
use Craft\BaseTest; |
|
7
|
|
|
use Craft\Exception; |
|
8
|
|
|
use Craft\PluginsService; |
|
9
|
|
|
use Craft\UpdatesService; |
|
10
|
|
|
use Craft\BasePlugin; |
|
11
|
|
|
use NerdsAndCompany\Schematic\Models\Result; |
|
12
|
|
|
use PHPUnit_Framework_MockObject_MockObject as Mock; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class PluginsTest. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Nerds & Company |
|
18
|
|
|
* @copyright Copyright (c) 2015-2016, Nerds & Company |
|
19
|
|
|
* @license MIT |
|
20
|
|
|
* |
|
21
|
|
|
* @link http://www.nerds.company |
|
22
|
|
|
* |
|
23
|
|
|
* @coversDefaultClass NerdsAndCompany\Schematic\Services\Plugins |
|
24
|
|
|
* @covers ::__construct |
|
25
|
|
|
* @covers ::<!public> |
|
26
|
|
|
*/ |
|
27
|
|
|
class PluginsTest extends BaseTest |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var Plugins |
|
31
|
|
|
*/ |
|
32
|
|
|
private $schematicPluginsService; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $pluginHandle; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setUp() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->schematicPluginsService = new Plugins(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Prevent code duplication by mocking multiple services. |
|
49
|
|
|
* |
|
50
|
|
|
* @param bool $returnPlugin |
|
51
|
|
|
* @param bool $installPluginResponse |
|
52
|
|
|
*/ |
|
53
|
|
|
public function mockMultipleServices( |
|
54
|
|
|
$returnPlugin = true, |
|
55
|
|
|
$installPluginResponse = true |
|
56
|
|
|
) { |
|
57
|
|
|
$mockPluginsService = $this->getMockPluginsService($returnPlugin, $installPluginResponse); |
|
58
|
|
|
$this->setComponent(Craft::app(), 'plugins', $mockPluginsService); |
|
59
|
|
|
$mockUpdatesService = $this->getMockUpdatesService(); |
|
60
|
|
|
$this->setComponent(Craft::app(), 'updates', $mockUpdatesService); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param bool $returnPlugin |
|
65
|
|
|
* @param bool $installPluginResponse |
|
66
|
|
|
* @param bool $enablePluginResponse |
|
67
|
|
|
* @param bool $disablePluginResponse |
|
68
|
|
|
* @param bool $uninstallPluginResponse |
|
69
|
|
|
* |
|
70
|
|
|
* @return PluginsService|Mock |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getMockPluginsService( |
|
73
|
|
|
$returnPlugin = true, |
|
74
|
|
|
$installPluginResponse = true, |
|
75
|
|
|
$enablePluginResponse = true, |
|
76
|
|
|
$disablePluginResponse = true, |
|
77
|
|
|
$uninstallPluginResponse = true |
|
78
|
|
|
) { |
|
79
|
|
|
$mock = $this->getMockBuilder(PluginsService::class)->getMock(); |
|
80
|
|
|
|
|
81
|
|
|
$mock->expects($this->any())->method('getPlugin')->willReturn(($returnPlugin) ? $this->getMockBasePlugin() : null); |
|
82
|
|
|
|
|
83
|
|
|
if ($installPluginResponse) { |
|
84
|
|
|
$mock->expects($this->any())->method('installPlugin')->willReturn($installPluginResponse); |
|
85
|
|
|
} else { |
|
86
|
|
|
$mock->expects($this->any())->method('installPlugin')->willThrowException(new Exception()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$mock->expects($this->any())->method('enablePlugin')->willReturn($enablePluginResponse); |
|
90
|
|
|
$mock->expects($this->any())->method('disablePlugin')->willReturn($disablePluginResponse); |
|
91
|
|
|
$mock->expects($this->any())->method('uninstallPlugin')->willReturn($uninstallPluginResponse); |
|
92
|
|
|
|
|
93
|
|
|
return $mock; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return UpdatesService|Mock |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getMockUpdatesService() |
|
100
|
|
|
{ |
|
101
|
|
|
$mock = $this->getMockBuilder(UpdatesService::class)->getMock(); |
|
102
|
|
|
$mock->expects($this->any())->method('updateDatabase')->willReturn(array('success' => true)); |
|
103
|
|
|
|
|
104
|
|
|
return $mock; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return Mock|BasePlugin |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getMockBasePlugin() |
|
111
|
|
|
{ |
|
112
|
|
|
$mock = $this->getMockBuilder(BasePlugin::class)->getMock(); |
|
113
|
|
|
|
|
114
|
|
|
$this->pluginHandle = get_class($mock); |
|
115
|
|
|
|
|
116
|
|
|
return $mock; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Test default import functionality. |
|
121
|
|
|
* |
|
122
|
|
|
* @covers ::import |
|
123
|
|
|
*/ |
|
124
|
|
|
public function testImportWithInstalledPlugins() |
|
125
|
|
|
{ |
|
126
|
|
|
$data = $this->getPluginsData(); |
|
127
|
|
|
|
|
128
|
|
|
$this->mockMultipleServices(); |
|
129
|
|
|
|
|
130
|
|
|
$import = $this->schematicPluginsService->import($data); |
|
131
|
|
|
|
|
132
|
|
|
$this->assertTrue($import instanceof Result); |
|
133
|
|
|
$this->assertFalse($import->hasErrors()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Test default import functionality. |
|
138
|
|
|
* |
|
139
|
|
|
* @covers ::import |
|
140
|
|
|
*/ |
|
141
|
|
|
public function testImportWithInstalledDisabledPlugins() |
|
142
|
|
|
{ |
|
143
|
|
|
$this->getMockBasePlugin(); |
|
144
|
|
|
|
|
145
|
|
|
$data = $this->getPluginsData(); |
|
146
|
|
|
$data[$this->pluginHandle]['isEnabled'] = false; |
|
147
|
|
|
|
|
148
|
|
|
$this->mockMultipleServices(); |
|
149
|
|
|
|
|
150
|
|
|
$import = $this->schematicPluginsService->import($data); |
|
151
|
|
|
|
|
152
|
|
|
$this->assertTrue($import instanceof Result); |
|
153
|
|
|
$this->assertFalse($import->hasErrors()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Test default import functionality. |
|
158
|
|
|
* |
|
159
|
|
|
* @covers ::import |
|
160
|
|
|
*/ |
|
161
|
|
|
public function testImportWithMissingPlugin() |
|
162
|
|
|
{ |
|
163
|
|
|
$data = $this->getPluginsData(); |
|
164
|
|
|
|
|
165
|
|
|
$this->mockMultipleServices(false); |
|
166
|
|
|
|
|
167
|
|
|
$import = $this->schematicPluginsService->import($data); |
|
168
|
|
|
|
|
169
|
|
|
$this->assertTrue($import instanceof Result); |
|
170
|
|
|
$this->assertTrue($import->hasErrors()); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Test default import functionality. |
|
175
|
|
|
* |
|
176
|
|
|
* @covers ::import |
|
177
|
|
|
*/ |
|
178
|
|
|
public function testImportWithInstallException() |
|
179
|
|
|
{ |
|
180
|
|
|
$data = $this->getPluginsData(); |
|
181
|
|
|
|
|
182
|
|
|
$this->mockMultipleServices(true, false); |
|
183
|
|
|
|
|
184
|
|
|
$import = $this->schematicPluginsService->import($data); |
|
185
|
|
|
|
|
186
|
|
|
$this->assertTrue($import instanceof Result); |
|
187
|
|
|
$this->assertTrue($import->hasErrors()); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Test default import functionality. |
|
192
|
|
|
* |
|
193
|
|
|
* @covers ::import |
|
194
|
|
|
*/ |
|
195
|
|
|
public function testImportWithNotInstalledPlugin() |
|
196
|
|
|
{ |
|
197
|
|
|
$this->mockMultipleServices(); |
|
198
|
|
|
|
|
199
|
|
|
$this->getMockBasePlugin(); |
|
200
|
|
|
|
|
201
|
|
|
$data = $this->getPluginsData(); |
|
202
|
|
|
$data[$this->pluginHandle]['isInstalled'] = false; |
|
203
|
|
|
|
|
204
|
|
|
$import = $this->schematicPluginsService->import($data); |
|
205
|
|
|
|
|
206
|
|
|
$this->assertTrue($import instanceof Result); |
|
207
|
|
|
$this->assertFalse($import->hasErrors()); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Test export functionality. |
|
212
|
|
|
* |
|
213
|
|
|
* @covers ::export |
|
214
|
|
|
*/ |
|
215
|
|
|
public function testExport() |
|
216
|
|
|
{ |
|
217
|
|
|
$mockBasePlugin = $this->getMockBasePlugin(); |
|
218
|
|
|
$mockBasePlugin->isInstalled = true; |
|
219
|
|
|
$mockBasePlugin->isEnabled = true; |
|
220
|
|
|
|
|
221
|
|
|
$data = $this->getPluginsData(); |
|
222
|
|
|
|
|
223
|
|
|
$mockBasePlugin |
|
224
|
|
|
->expects($this->any()) |
|
225
|
|
|
->method('getSettings') |
|
226
|
|
|
->willReturn((Object) ['attributes' => $data[$this->pluginHandle]['settings']]); |
|
227
|
|
|
|
|
228
|
|
|
$mockPluginsService = $this->getMockPluginsService(); |
|
229
|
|
|
$mockPluginsService->expects($this->any()) |
|
230
|
|
|
->method('getPlugins') |
|
231
|
|
|
->willReturn([$this->pluginHandle => $mockBasePlugin]); |
|
232
|
|
|
|
|
233
|
|
|
$this->setComponent(Craft::app(), 'plugins', $mockPluginsService); |
|
234
|
|
|
|
|
235
|
|
|
$export = $this->schematicPluginsService->export(); |
|
236
|
|
|
$this->assertEquals($data, $export); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Returns plugins data. |
|
241
|
|
|
* |
|
242
|
|
|
* @return array |
|
243
|
|
|
*/ |
|
244
|
|
|
public function getPluginsData() |
|
245
|
|
|
{ |
|
246
|
|
|
return [ |
|
247
|
|
|
$this->pluginHandle => [ |
|
248
|
|
|
'isInstalled' => true, |
|
249
|
|
|
'isEnabled' => true, |
|
250
|
|
|
'settings' => [ |
|
251
|
|
|
'pluginName' => 'Menu', |
|
252
|
|
|
'canDoActions' => '', |
|
253
|
|
|
'quietErrors' => '', |
|
254
|
|
|
], |
|
255
|
|
|
], |
|
256
|
|
|
]; |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|