|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Mappers; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\models\CategoryGroup; |
|
7
|
|
|
use craft\models\CategoryGroup_SiteSettings; |
|
8
|
|
|
use craft\models\FieldLayout; |
|
9
|
|
|
use craft\models\Site; |
|
10
|
|
|
use craft\services\Fields; |
|
11
|
|
|
use Codeception\Test\Unit; |
|
12
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class ModelMapperTest. |
|
16
|
|
|
* |
|
17
|
|
|
* @TODO: Isolate from category groups |
|
18
|
|
|
* |
|
19
|
|
|
* @author Nerds & Company |
|
20
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
|
21
|
|
|
* @license MIT |
|
22
|
|
|
* |
|
23
|
|
|
* @see http://www.nerds.company |
|
24
|
|
|
*/ |
|
25
|
|
|
class ModelMapperTest extends Unit |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var CategoryGroups |
|
29
|
|
|
*/ |
|
30
|
|
|
private $service; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Set the service. |
|
34
|
|
|
* |
|
35
|
|
|
* @SuppressWarnings(PHPMD.CamelCaseMethodName) |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function _before() |
|
38
|
|
|
{ |
|
39
|
|
|
Craft::$app->sites->expects($this->any()) |
|
40
|
|
|
->method('getSiteByHandle') |
|
41
|
|
|
->willReturn($this->getMockSite()); |
|
42
|
|
|
|
|
43
|
|
|
$this->service = new ModelMapper(); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
//============================================================================================================== |
|
47
|
|
|
//================================================= TESTS ==================================================== |
|
48
|
|
|
//============================================================================================================== |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @dataProvider provideValidCategoryGroups |
|
52
|
|
|
* |
|
53
|
|
|
* @param CategoryGroupModel[] $groups |
|
54
|
|
|
* @param array $expectedResult |
|
55
|
|
|
*/ |
|
56
|
|
|
public function testSuccessfulExport(array $groups, array $expectedResult = []) |
|
57
|
|
|
{ |
|
58
|
|
|
$actualResult = $this->service->export($groups); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertSame($expectedResult, $actualResult); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @dataProvider provideValidCategoryGroupDefinitions |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $groupDefinitions |
|
67
|
|
|
*/ |
|
68
|
|
|
public function testSuccessfulImport(array $groupDefinitions, array $existingGroups, int $saveCount) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->expectSaves($saveCount); |
|
71
|
|
|
$this->expectDeletes(0); |
|
72
|
|
|
|
|
73
|
|
|
$this->service->import($groupDefinitions, $existingGroups); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @dataProvider provideValidCategoryGroupDefinitions |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $groupDefinitions |
|
80
|
|
|
*/ |
|
81
|
|
|
public function testImportWithForceOption(array $groupDefinitions, array $existingGroups, int $saveCount, int $deleteCount) |
|
82
|
|
|
{ |
|
83
|
|
|
Schematic::$force = true; |
|
84
|
|
|
$this->expectSaves($saveCount); |
|
85
|
|
|
$this->expectDeletes($deleteCount); |
|
86
|
|
|
|
|
87
|
|
|
$this->service->import($groupDefinitions, $existingGroups); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
//============================================================================================================== |
|
91
|
|
|
//============================================== PROVIDERS =================================================== |
|
92
|
|
|
//============================================================================================================== |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
|
|
public function provideValidCategoryGroups() |
|
98
|
|
|
{ |
|
99
|
|
|
$mockCategoryGroup1 = $this->getMockCategoryGroup(1); |
|
100
|
|
|
$mockCategoryGroup2 = $this->getMockCategoryGroup(2); |
|
101
|
|
|
|
|
102
|
|
|
return [ |
|
103
|
|
|
'emptyArray' => [ |
|
104
|
|
|
'categoryGroups' => [], |
|
105
|
|
|
'expectedResult' => [], |
|
106
|
|
|
], |
|
107
|
|
|
'single group' => [ |
|
108
|
|
|
'categoryGroups' => [ |
|
109
|
|
|
'group1' => $mockCategoryGroup1, |
|
110
|
|
|
], |
|
111
|
|
|
'expectedResult' => [ |
|
112
|
|
|
'groupHandle1' => $this->getMockCategoryGroupDefinition($mockCategoryGroup1), |
|
|
|
|
|
|
113
|
|
|
], |
|
114
|
|
|
], |
|
115
|
|
|
'multiple groups' => [ |
|
116
|
|
|
'categoryGroups' => [ |
|
117
|
|
|
'group1' => $mockCategoryGroup1, |
|
118
|
|
|
'group2' => $mockCategoryGroup2, |
|
119
|
|
|
], |
|
120
|
|
|
'expectedResult' => [ |
|
121
|
|
|
'groupHandle1' => $this->getMockCategoryGroupDefinition($mockCategoryGroup1), |
|
|
|
|
|
|
122
|
|
|
'groupHandle2' => $this->getMockCategoryGroupDefinition($mockCategoryGroup2), |
|
|
|
|
|
|
123
|
|
|
], |
|
124
|
|
|
], |
|
125
|
|
|
]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
|
public function provideValidCategoryGroupDefinitions() |
|
132
|
|
|
{ |
|
133
|
|
|
$mockCategoryGroup1 = $this->getMockCategoryGroup(1); |
|
134
|
|
|
$mockCategoryGroup2 = $this->getMockCategoryGroup(2); |
|
135
|
|
|
|
|
136
|
|
|
return [ |
|
137
|
|
|
'emptyArray' => [ |
|
138
|
|
|
'groupDefinitions' => [], |
|
139
|
|
|
'existingGroups' => [ |
|
140
|
|
|
$mockCategoryGroup1, |
|
141
|
|
|
], |
|
142
|
|
|
'saveCount' => 0, |
|
143
|
|
|
'deleteCount' => 1, |
|
144
|
|
|
], |
|
145
|
|
|
'single new group' => [ |
|
146
|
|
|
'groupDefinitions' => [ |
|
147
|
|
|
'groupHandle1' => $this->getMockCategoryGroupDefinition($mockCategoryGroup1), |
|
|
|
|
|
|
148
|
|
|
'groupHandle2' => $this->getMockCategoryGroupDefinition($mockCategoryGroup2), |
|
|
|
|
|
|
149
|
|
|
], |
|
150
|
|
|
'existingGroups' => [ |
|
151
|
|
|
$mockCategoryGroup1, |
|
152
|
|
|
], |
|
153
|
|
|
'saveCount' => 1, |
|
154
|
|
|
'deleteCount' => 0, |
|
155
|
|
|
], |
|
156
|
|
|
]; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
//============================================================================================================== |
|
160
|
|
|
//================================================ HELPERS =================================================== |
|
161
|
|
|
//============================================================================================================== |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param CategoryGroup $mockCategoryGroup |
|
165
|
|
|
* |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
|
|
private function getMockCategoryGroupDefinition(CategoryGroup $mockCategoryGroup) |
|
169
|
|
|
{ |
|
170
|
|
|
return [ |
|
171
|
|
|
'class' => get_class($mockCategoryGroup), |
|
172
|
|
|
'attributes' => [ |
|
173
|
|
|
'name' => $mockCategoryGroup->name, |
|
174
|
|
|
'handle' => $mockCategoryGroup->handle, |
|
175
|
|
|
'maxLevels' => 3, |
|
176
|
|
|
], |
|
177
|
|
|
'fieldLayout' => [ |
|
178
|
|
|
'fields' => [], |
|
179
|
|
|
], |
|
180
|
|
|
'siteSettings' => [ |
|
181
|
|
|
'' => [ |
|
182
|
|
|
'class' => get_class($mockCategoryGroup->getSiteSettings()[0]), |
|
183
|
|
|
'attributes' => [ |
|
184
|
|
|
'hasUrls' => null, |
|
185
|
|
|
'uriFormat' => null, |
|
186
|
|
|
'template' => null, |
|
187
|
|
|
], |
|
188
|
|
|
], |
|
189
|
|
|
], |
|
190
|
|
|
]; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param int $groupId |
|
195
|
|
|
* |
|
196
|
|
|
* @return Mock|CategoryGroup |
|
197
|
|
|
*/ |
|
198
|
|
|
private function getMockCategoryGroup(int $groupId) |
|
199
|
|
|
{ |
|
200
|
|
|
$mockGroup = $this->getMockBuilder(CategoryGroup::class) |
|
201
|
|
|
->setMethods(['getFieldLayout', 'getSiteSettings']) |
|
202
|
|
|
->getMock(); |
|
203
|
|
|
$mockGroup->setAttributes([ |
|
204
|
|
|
'id' => $groupId, |
|
205
|
|
|
'fieldLayoutId' => $groupId, |
|
206
|
|
|
'handle' => 'groupHandle'.$groupId, |
|
207
|
|
|
'name' => 'groupName'.$groupId, |
|
208
|
|
|
'maxLevels' => 3, |
|
209
|
|
|
]); |
|
210
|
|
|
|
|
211
|
|
|
$mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock(); |
|
212
|
|
|
|
|
213
|
|
|
$mockGroup->expects($this->any()) |
|
214
|
|
|
->method('getFieldLayout') |
|
215
|
|
|
->willReturn($mockFieldLayout); |
|
216
|
|
|
|
|
217
|
|
|
$mockSiteSettings = $this->getMockSiteSettings(); |
|
218
|
|
|
|
|
219
|
|
|
$mockGroup->expects($this->any()) |
|
220
|
|
|
->method('getSiteSettings') |
|
221
|
|
|
->willReturn([$mockSiteSettings]); |
|
222
|
|
|
|
|
223
|
|
|
return $mockGroup; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Get mock siteSettings. |
|
228
|
|
|
* |
|
229
|
|
|
* @param string $class |
|
|
|
|
|
|
230
|
|
|
* |
|
231
|
|
|
* @return Mock|CategoryGroup_SiteSettings |
|
232
|
|
|
*/ |
|
233
|
|
|
private function getMockSiteSettings() |
|
234
|
|
|
{ |
|
235
|
|
|
$mockSiteSettings = $this->getMockBuilder(CategoryGroup_SiteSettings::class) |
|
236
|
|
|
->setMethods(['getSite']) |
|
237
|
|
|
->getMock(); |
|
238
|
|
|
|
|
239
|
|
|
$mockSiteSettings->expects($this->any()) |
|
240
|
|
|
->method('getSite') |
|
241
|
|
|
->willReturn($this->getMockSite()); |
|
242
|
|
|
|
|
243
|
|
|
return $mockSiteSettings; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Get a mock site. |
|
248
|
|
|
* |
|
249
|
|
|
* @return Mock|Site |
|
250
|
|
|
*/ |
|
251
|
|
|
private function getMockSite() |
|
252
|
|
|
{ |
|
253
|
|
|
return $this->getMockBuilder(Site::class)->getMock(); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Expect a number of group saves. |
|
258
|
|
|
* |
|
259
|
|
|
* @param int $saveCount |
|
260
|
|
|
*/ |
|
261
|
|
|
private function expectSaves(int $saveCount) |
|
262
|
|
|
{ |
|
263
|
|
|
Craft::$app->categories |
|
264
|
|
|
->expects($this->exactly($saveCount)) |
|
265
|
|
|
->method('saveGroup') |
|
266
|
|
|
->willReturn(true); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Expect a number of group deletes. |
|
271
|
|
|
* |
|
272
|
|
|
* @param int $deleteCount |
|
273
|
|
|
*/ |
|
274
|
|
|
private function expectDeletes(int $deleteCount) |
|
275
|
|
|
{ |
|
276
|
|
|
Craft::$app->categories |
|
277
|
|
|
->expects($this->exactly($deleteCount)) |
|
278
|
|
|
->method('deleteGroupById'); |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..