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