These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\MigrationsService; |
||
10 | use Craft\UpdatesService; |
||
11 | use Craft\BasePlugin; |
||
12 | use NerdsAndCompany\Schematic\Models\Result; |
||
13 | use PHPUnit_Framework_MockObject_MockObject as Mock; |
||
14 | |||
15 | /** |
||
16 | * Class PluginsTest. |
||
17 | * |
||
18 | * @author Nerds & Company |
||
19 | * @copyright Copyright (c) 2015, Nerds & Company |
||
20 | * @license MIT |
||
21 | * |
||
22 | * @link http://www.nerds.company |
||
23 | * |
||
24 | * @coversDefaultClass NerdsAndCompany\Schematic\Services\Plugins |
||
25 | * @covers ::__construct |
||
26 | * @covers ::<!public> |
||
27 | */ |
||
28 | class PluginsTest extends BaseTest |
||
29 | { |
||
30 | /** |
||
31 | * @var Plugins |
||
32 | */ |
||
33 | private $schematicPluginsService; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $pluginHandle; |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | public function setUp() |
||
44 | { |
||
45 | $this->schematicPluginsService = new Plugins(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Prevent code duplication by mocking multiple services. |
||
50 | * |
||
51 | * @param bool $returnPlugin |
||
52 | * @param bool $installPluginResponse |
||
53 | */ |
||
54 | public function mockMultipleServices( |
||
55 | $returnPlugin = true, |
||
56 | $installPluginResponse = true |
||
57 | ) { |
||
58 | $mockPluginsService = $this->getMockPluginsService($returnPlugin, $installPluginResponse); |
||
59 | $this->setComponent(Craft::app(), 'plugins', $mockPluginsService); |
||
60 | $mockMigrationsService = $this->getMockMigrationsService(); |
||
61 | $this->setComponent(Craft::app(), 'migrations', $mockMigrationsService); |
||
62 | $mockUpdatesService = $this->getMockUpdatesService(); |
||
63 | $this->setComponent(Craft::app(), 'updates', $mockUpdatesService); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param bool $returnPlugin |
||
68 | * @param bool $installPluginResponse |
||
69 | * @param bool $enablePluginResponse |
||
70 | * @param bool $disablePluginResponse |
||
71 | * @param bool $uninstallPluginResponse |
||
72 | * |
||
73 | * @return PluginsService|Mock |
||
74 | */ |
||
75 | public function getMockPluginsService( |
||
76 | $returnPlugin = true, |
||
77 | $installPluginResponse = true, |
||
78 | $enablePluginResponse = true, |
||
79 | $disablePluginResponse = true, |
||
80 | $uninstallPluginResponse = true |
||
81 | ) { |
||
82 | $mock = $this->getMockBuilder(PluginsService::class)->getMock(); |
||
83 | |||
84 | $mock->expects($this->any())->method('getPlugin')->willReturn(($returnPlugin) ? $this->getMockBasePlugin() : null); |
||
85 | |||
86 | if ($installPluginResponse) { |
||
87 | $mock->expects($this->any())->method('installPlugin')->willReturn($installPluginResponse); |
||
88 | } else { |
||
89 | $mock->expects($this->any())->method('installPlugin')->willThrowException(new Exception()); |
||
90 | } |
||
91 | |||
92 | $mock->expects($this->any())->method('enablePlugin')->willReturn($enablePluginResponse); |
||
93 | $mock->expects($this->any())->method('disablePlugin')->willReturn($disablePluginResponse); |
||
94 | $mock->expects($this->any())->method('uninstallPlugin')->willReturn($uninstallPluginResponse); |
||
95 | |||
96 | return $mock; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return MigrationsService|Mock |
||
101 | */ |
||
102 | public function getMockMigrationsService() |
||
103 | { |
||
104 | $mock = $this->getMockBuilder(MigrationsService::class)->getMock(); |
||
105 | $mock->expects($this->any())->method('runToTop')->willReturn(true); |
||
106 | |||
107 | return $mock; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return UpdatesService|Mock |
||
112 | */ |
||
113 | public function getMockUpdatesService() |
||
114 | { |
||
115 | $mock = $this->getMockBuilder(UpdatesService::class)->getMock(); |
||
116 | $mock->expects($this->any())->method('setNewPluginInfo')->willReturn(true); |
||
117 | |||
118 | return $mock; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @return Mock|BasePlugin |
||
123 | */ |
||
124 | public function getMockBasePlugin() |
||
125 | { |
||
126 | $mock = $this->getMockBuilder(BasePlugin::class)->getMock(); |
||
127 | |||
128 | $this->pluginHandle = get_class($mock); |
||
129 | |||
130 | return $mock; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Test default import functionality. |
||
135 | * |
||
136 | * @covers ::import |
||
137 | */ |
||
138 | View Code Duplication | public function testImportWithInstalledPlugins() |
|
139 | { |
||
140 | $data = $this->getPluginsData(); |
||
141 | |||
142 | $this->mockMultipleServices(); |
||
143 | |||
144 | $import = $this->schematicPluginsService->import($data); |
||
145 | |||
146 | $this->assertTrue($import instanceof Result); |
||
0 ignored issues
–
show
|
|||
147 | $this->assertFalse($import->hasErrors()); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Test default import functionality. |
||
152 | * |
||
153 | * @covers ::import |
||
154 | */ |
||
155 | View Code Duplication | public function testImportWithInstalledDisabledPlugins() |
|
156 | { |
||
157 | $this->getMockBasePlugin(); |
||
158 | |||
159 | $data = $this->getPluginsData(); |
||
160 | $data[$this->pluginHandle]['isEnabled'] = false; |
||
161 | |||
162 | $this->mockMultipleServices(); |
||
163 | |||
164 | $import = $this->schematicPluginsService->import($data); |
||
165 | |||
166 | $this->assertTrue($import instanceof Result); |
||
0 ignored issues
–
show
The class
NerdsAndCompany\Schematic\Models\Result does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. ![]() |
|||
167 | $this->assertFalse($import->hasErrors()); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Test default import functionality. |
||
172 | * |
||
173 | * @covers ::import |
||
174 | */ |
||
175 | View Code Duplication | public function testImportWithMissingPlugin() |
|
176 | { |
||
177 | $data = $this->getPluginsData(); |
||
178 | |||
179 | $mockPluginsService = $this->getMockPluginsService(false); |
||
180 | $this->setComponent(Craft::app(), 'plugins', $mockPluginsService); |
||
181 | |||
182 | $import = $this->schematicPluginsService->import($data); |
||
183 | |||
184 | $this->assertTrue($import instanceof Result); |
||
0 ignored issues
–
show
The class
NerdsAndCompany\Schematic\Models\Result does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. ![]() |
|||
185 | $this->assertTrue($import->hasErrors()); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Test default import functionality. |
||
190 | * |
||
191 | * @covers ::import |
||
192 | */ |
||
193 | View Code Duplication | public function testImportWithInstallException() |
|
194 | { |
||
195 | $data = $this->getPluginsData(); |
||
196 | |||
197 | $this->mockMultipleServices(true, false); |
||
198 | |||
199 | $import = $this->schematicPluginsService->import($data); |
||
200 | |||
201 | $this->assertTrue($import instanceof Result); |
||
0 ignored issues
–
show
The class
NerdsAndCompany\Schematic\Models\Result does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. ![]() |
|||
202 | $this->assertTrue($import->hasErrors()); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Test default import functionality. |
||
207 | * |
||
208 | * @covers ::import |
||
209 | */ |
||
210 | public function testImportWithNotInstalledPlugin() |
||
211 | { |
||
212 | $mockPluginsService = $this->getMockPluginsService(); |
||
213 | $this->setComponent(Craft::app(), 'plugins', $mockPluginsService); |
||
214 | |||
215 | $this->getMockBasePlugin(); |
||
216 | |||
217 | $data = $this->getPluginsData(); |
||
218 | $data[$this->pluginHandle]['isInstalled'] = false; |
||
219 | |||
220 | $import = $this->schematicPluginsService->import($data); |
||
221 | |||
222 | $this->assertTrue($import instanceof Result); |
||
0 ignored issues
–
show
The class
NerdsAndCompany\Schematic\Models\Result does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. ![]() |
|||
223 | $this->assertFalse($import->hasErrors()); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Test export functionality. |
||
228 | * |
||
229 | * @covers ::export |
||
230 | */ |
||
231 | public function testExport() |
||
232 | { |
||
233 | $mockBasePlugin = $this->getMockBasePlugin(); |
||
234 | $mockBasePlugin->isInstalled = true; |
||
235 | $mockBasePlugin->isEnabled = true; |
||
236 | |||
237 | $data = $this->getPluginsData(); |
||
238 | |||
239 | $mockBasePlugin |
||
240 | ->expects($this->any()) |
||
241 | ->method('getSettings') |
||
242 | ->willReturn((Object) ['attributes' => $data[$this->pluginHandle]['settings']]); |
||
243 | |||
244 | $mockPluginsService = $this->getMockPluginsService(); |
||
245 | $mockPluginsService->expects($this->any()) |
||
246 | ->method('getPlugins') |
||
247 | ->willReturn([$this->pluginHandle => $mockBasePlugin]); |
||
248 | |||
249 | $this->setComponent(Craft::app(), 'plugins', $mockPluginsService); |
||
250 | |||
251 | $export = $this->schematicPluginsService->export(); |
||
252 | $this->assertEquals($data, $export); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Returns plugins data. |
||
257 | * |
||
258 | * @return array |
||
259 | */ |
||
260 | public function getPluginsData() |
||
261 | { |
||
262 | return [ |
||
263 | $this->pluginHandle => [ |
||
264 | 'isInstalled' => true, |
||
265 | 'isEnabled' => true, |
||
266 | 'settings' => [ |
||
267 | 'pluginName' => 'Menu', |
||
268 | 'canDoActions' => '', |
||
269 | 'quietErrors' => '', |
||
270 | ], |
||
271 | ], |
||
272 | ]; |
||
273 | } |
||
274 | } |
||
275 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.