1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\BaseTest; |
7
|
|
|
use Craft\ElementsService; |
8
|
|
|
use Craft\ElementIndexesService; |
9
|
|
|
use Craft\CategoryElementType; |
10
|
|
|
use Craft\EntryElementType; |
11
|
|
|
use NerdsAndCompany\Schematic\Models\Result; |
12
|
|
|
use PHPUnit_Framework_MockObject_MockObject as Mock; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ElementIndexSettingsTest. |
16
|
|
|
* |
17
|
|
|
* @author Nerds & Company |
18
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
19
|
|
|
* @license MIT |
20
|
|
|
* |
21
|
|
|
* @link http://www.nerds.company |
22
|
|
|
* |
23
|
|
|
* @coversDefaultClass NerdsAndCompany\Schematic\Services\ElementIndexSettings |
24
|
|
|
* @covers ::__construct |
25
|
|
|
* @covers ::<!public> |
26
|
|
|
*/ |
27
|
|
|
class ElementIndexSettingsTest extends BaseTest |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var ElementIndexSettings |
31
|
|
|
*/ |
32
|
|
|
private $schematicElementIndexSettingsService; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function setUp() |
38
|
|
|
{ |
39
|
|
|
$this->schematicElementIndexSettingsService = new ElementIndexSettings(); |
40
|
|
|
$this->setMockSources(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return ElementsService|Mock |
45
|
|
|
* |
46
|
|
|
* @param array $getAllElementTypesResponse |
47
|
|
|
* |
48
|
|
|
* @return Mock |
49
|
|
|
*/ |
50
|
|
|
protected function getMockElementsService($getAllElementTypesResponse = []) |
51
|
|
|
{ |
52
|
|
|
$mock = $this->getMockBuilder(ElementsService::class)->getMock(); |
53
|
|
|
$mock->expects($this->any())->method('getAllElementTypes')->willReturn($getAllElementTypesResponse); |
54
|
|
|
|
55
|
|
|
return $mock; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return ElementIndexesService|Mock |
60
|
|
|
* |
61
|
|
|
* @param array $getSettingsResponse |
62
|
|
|
* |
63
|
|
|
* @return Mock |
64
|
|
|
*/ |
65
|
|
|
protected function getMockElementIndexesService($getSettingsResponse = []) |
66
|
|
|
{ |
67
|
|
|
$mock = $this->getMockBuilder(ElementIndexesService::class)->getMock(); |
68
|
|
|
$mock->expects($this->any())->method('getSettings')->willReturn($getSettingsResponse['Entry']); |
69
|
|
|
$mock->expects($this->any())->method('saveSettings')->willReturn(false); |
70
|
|
|
|
71
|
|
|
return $mock; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Test default import functionality. |
76
|
|
|
* |
77
|
|
|
* @covers ::import |
78
|
|
|
*/ |
79
|
|
|
public function testImport() |
80
|
|
|
{ |
81
|
|
|
$data = $this->getElementIndexSettingsData(); |
82
|
|
|
$mockElementIndexesService = $this->getMockElementIndexesService($data); |
83
|
|
|
$this->setComponent(Craft::app(), 'elementIndexes', $mockElementIndexesService); |
84
|
|
|
|
85
|
|
|
$import = $this->schematicElementIndexSettingsService->import($data); |
86
|
|
|
|
87
|
|
|
$this->assertTrue($import instanceof Result); |
88
|
|
|
$this->assertTrue($import->hasErrors()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test export functionality. |
93
|
|
|
* |
94
|
|
|
* @covers ::export |
95
|
|
|
*/ |
96
|
|
|
public function testExport() |
97
|
|
|
{ |
98
|
|
|
$data = $this->getElementsData(); |
99
|
|
|
$mockElementsService = $this->getMockElementsService($data); |
100
|
|
|
$this->setComponent(Craft::app(), 'elements', $mockElementsService); |
101
|
|
|
|
102
|
|
|
$data = $this->getElementIndexSettingsData(); |
103
|
|
|
$mockElementIndexesService = $this->getMockElementIndexesService($data); |
104
|
|
|
$this->setComponent(Craft::app(), 'elementIndexes', $mockElementIndexesService); |
105
|
|
|
|
106
|
|
|
$export = $this->schematicElementIndexSettingsService->export(); |
107
|
|
|
$this->assertEquals($data, $export); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns elements data. |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function getElementsData() |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
new CategoryElementType(), |
119
|
|
|
new EntryElementType(), |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns element index settings data. |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function getElementIndexSettingsData() |
129
|
|
|
{ |
130
|
|
|
return [ |
131
|
|
|
'Category' => [ |
132
|
|
|
'sources' => [ |
133
|
|
|
'*' => [ |
134
|
|
|
'tableAttributes' => [ |
135
|
|
|
'1' => 'section', |
136
|
|
|
'2' => 'postDate', |
137
|
|
|
'3' => 'expiryDate', |
138
|
|
|
'4' => 'author', |
139
|
|
|
'5' => 'link', |
140
|
|
|
], |
141
|
|
|
], |
142
|
|
|
], |
143
|
|
|
], |
144
|
|
|
'Entry' => [ |
145
|
|
|
'sources' => [ |
146
|
|
|
'*' => [ |
147
|
|
|
'tableAttributes' => [ |
148
|
|
|
'1' => 'section', |
149
|
|
|
'2' => 'postDate', |
150
|
|
|
'3' => 'expiryDate', |
151
|
|
|
'4' => 'author', |
152
|
|
|
'5' => 'link', |
153
|
|
|
], |
154
|
|
|
], |
155
|
|
|
], |
156
|
|
|
], |
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return Mock|Sources |
162
|
|
|
*/ |
163
|
|
|
private function setMockSources() |
164
|
|
|
{ |
165
|
|
|
$mockSources = $this->getMockBuilder(Sources::class) |
166
|
|
|
->disableOriginalConstructor() |
167
|
|
|
->getMock(); |
168
|
|
|
|
169
|
|
|
$mockSources->expects($this->any()) |
170
|
|
|
->method('getSource') |
171
|
|
|
->will($this->returnCallback(array($this, 'getMockSourceCallback'))); |
172
|
|
|
|
173
|
|
|
$this->setComponent(Craft::app(), 'schematic_sources', $mockSources); |
174
|
|
|
|
175
|
|
|
return $mockSources; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $fieldType |
180
|
|
|
* @param string $source |
181
|
|
|
* @param string $fromIndex |
182
|
|
|
* @param string $toIndex |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function getMockSourceCallback($fieldType, $source, $fromIndex, $toIndex) |
187
|
|
|
{ |
188
|
|
|
return $source; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|