1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Converters\Models; |
4
|
|
|
|
5
|
|
|
use Craft; |
6
|
|
|
use craft\models\Site as SiteModel; |
7
|
|
|
use craft\models\SiteGroup; |
8
|
|
|
use Codeception\Test\Unit; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class SiteTest. |
12
|
|
|
* |
13
|
|
|
* @author Nerds & Company |
14
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
15
|
|
|
* @license MIT |
16
|
|
|
* |
17
|
|
|
* @see http://www.nerds.company |
18
|
|
|
*/ |
19
|
|
|
class SiteTest extends Unit |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Sites |
23
|
|
|
*/ |
24
|
|
|
private $converter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Set the converter. |
28
|
|
|
* |
29
|
|
|
* @SuppressWarnings(PHPMD.CamelCaseMethodName) |
30
|
|
|
*/ |
31
|
|
|
protected function _before() |
32
|
|
|
{ |
33
|
|
|
$this->converter = new Site(); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
//============================================================================================================== |
37
|
|
|
//================================================= TESTS ==================================================== |
38
|
|
|
//============================================================================================================== |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @dataProvider provideSites |
42
|
|
|
* |
43
|
|
|
* @param SiteModel $site |
44
|
|
|
* @param array $definition |
45
|
|
|
*/ |
46
|
|
|
public function testGetRecordDefinition(SiteModel $site, array $definition) |
47
|
|
|
{ |
48
|
|
|
$result = $this->converter->getRecordDefinition($site); |
49
|
|
|
|
50
|
|
|
$this->assertSame($definition, $result); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @dataProvider provideSites |
55
|
|
|
* |
56
|
|
|
* @param SiteModel $site |
57
|
|
|
* @param array $definition |
58
|
|
|
* @param string $groupStatus existing|new|invalid |
59
|
|
|
*/ |
60
|
|
|
public function testSaveRecord(SiteModel $site, array $definition, string $groupStatus) |
61
|
|
|
{ |
62
|
|
|
Craft::$app->sites->expects($this->exactly(1)) |
63
|
|
|
->method('getAllGroups') |
64
|
|
|
->willReturn([$this->getMockSiteGroup(1)]); |
65
|
|
|
|
66
|
|
|
Craft::$app->sites->expects($this->exactly($groupStatus == 'existing' ? 0 : 1)) |
67
|
|
|
->method('saveGroup') |
68
|
|
|
->willReturn($groupStatus !== 'invalid'); |
69
|
|
|
|
70
|
|
|
Craft::$app->sites->expects($this->exactly(1)) |
71
|
|
|
->method('saveSite') |
72
|
|
|
->with($site) |
73
|
|
|
->willReturn(true); |
74
|
|
|
|
75
|
|
|
$result = $this->converter->saveRecord($site, $definition); |
76
|
|
|
|
77
|
|
|
$this->assertTrue($result); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @dataProvider provideSites |
82
|
|
|
* |
83
|
|
|
* @param SiteModel $site |
84
|
|
|
*/ |
85
|
|
|
public function testDeleteRecord(SiteModel $site) |
86
|
|
|
{ |
87
|
|
|
Craft::$app->sites->expects($this->exactly(1)) |
88
|
|
|
->method('deleteSiteById') |
89
|
|
|
->with($site->id); |
90
|
|
|
|
91
|
|
|
$this->converter->deleteRecord($site); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
//============================================================================================================== |
95
|
|
|
//============================================== PROVIDERS =================================================== |
96
|
|
|
//============================================================================================================== |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function provideSites() |
102
|
|
|
{ |
103
|
|
|
$mockSite1 = $this->getMockSite(1, 1); |
104
|
|
|
$mockSite2 = $this->getMockSite(1, 2); |
105
|
|
|
|
106
|
|
|
return [ |
107
|
|
|
'valid site existing group' => [ |
108
|
|
|
'site' => $mockSite1, |
109
|
|
|
'definition' => $this->getMockSiteDefinition($mockSite1), |
|
|
|
|
110
|
|
|
'groupStatus' => 'existing', |
111
|
|
|
], |
112
|
|
|
'valid site new group' => [ |
113
|
|
|
'site' => $mockSite2, |
114
|
|
|
'definition' => $this->getMockSiteDefinition($mockSite2), |
|
|
|
|
115
|
|
|
'groupStatus' => 'new', |
116
|
|
|
], |
117
|
|
|
'valid site invalid group' => [ |
118
|
|
|
'site' => $mockSite2, |
119
|
|
|
'definition' => $this->getMockSiteDefinition($mockSite2), |
|
|
|
|
120
|
|
|
'groupStatus' => 'invalid', |
121
|
|
|
], |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
//============================================================================================================== |
126
|
|
|
//================================================ HELPERS =================================================== |
127
|
|
|
//============================================================================================================== |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param SiteModel $mockSite |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
private function getMockSiteDefinition(SiteModel $mockSite) |
135
|
|
|
{ |
136
|
|
|
return [ |
137
|
|
|
'class' => get_class($mockSite), |
138
|
|
|
'attributes' => [ |
139
|
|
|
'name' => $mockSite->name, |
140
|
|
|
'handle' => $mockSite->handle, |
141
|
|
|
'language' => 'nl', |
142
|
|
|
'primary' => true, |
143
|
|
|
'hasUrls' => true, |
144
|
|
|
'originalName' => null, |
145
|
|
|
'originalBaseUrl' => null, |
146
|
|
|
'baseUrl' => '@web/', |
147
|
|
|
'sortOrder' => 1, |
148
|
|
|
], |
149
|
|
|
'group' => $mockSite->group->name, |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param int $siteId |
155
|
|
|
* |
156
|
|
|
* @return Mock|SiteModel |
157
|
|
|
*/ |
158
|
|
|
private function getMockSite(int $siteId, int $groupId) |
159
|
|
|
{ |
160
|
|
|
$mockSite = $this->getMockBuilder(SiteModel::class) |
161
|
|
|
->setMethods(['getGroup']) |
162
|
|
|
->disableOriginalConstructor() |
163
|
|
|
->getMock(); |
164
|
|
|
|
165
|
|
|
$mockSite->id = $siteId; |
|
|
|
|
166
|
|
|
$mockSite->groupId = $groupId; |
|
|
|
|
167
|
|
|
$mockSite->handle = 'siteHandle'.$siteId; |
|
|
|
|
168
|
|
|
$mockSite->language = 'nl'; |
|
|
|
|
169
|
|
|
$mockSite->primary = true; |
|
|
|
|
170
|
|
|
$mockSite->hasUrls = true; |
|
|
|
|
171
|
|
|
$mockSite->originalName = null; |
|
|
|
|
172
|
|
|
$mockSite->originalBaseUrl = null; |
|
|
|
|
173
|
|
|
$mockSite->baseUrl = '@web/'; |
|
|
|
|
174
|
|
|
$mockSite->sortOrder = 1; |
|
|
|
|
175
|
|
|
|
176
|
|
|
$mockSite->expects($this->any()) |
177
|
|
|
->method('getGroup') |
178
|
|
|
->willReturn($this->getMockSiteGroup($groupId)); |
179
|
|
|
|
180
|
|
|
return $mockSite; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get a mock site group. |
185
|
|
|
* |
186
|
|
|
* @param int $groupId |
187
|
|
|
* |
188
|
|
|
* @return Mock|SiteGroup |
189
|
|
|
*/ |
190
|
|
|
private function getMockSiteGroup(int $groupId) |
191
|
|
|
{ |
192
|
|
|
$mockGroup = $this->getMockBuilder(SiteGroup::class) |
193
|
|
|
->disableOriginalConstructor() |
194
|
|
|
->getmock(); |
195
|
|
|
|
196
|
|
|
$mockGroup->id = $groupId; |
|
|
|
|
197
|
|
|
$mockGroup->name = 'siteGroup'.$groupId; |
|
|
|
|
198
|
|
|
|
199
|
|
|
return $mockGroup; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
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..