Completed
Pull Request — master (#93)
by Bart
06:30
created

ElementIndexSettingsTest::setMockSources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft\Craft;
6
use Craft\BaseTest;
7
use Craft\CategoryElementType;
8
use Craft\ElementIndexesService;
9
use Craft\ElementsService;
10
use Craft\EntryElementType;
11
use Craft\FieldModel;
12
use Craft\FieldsService;
13
use NerdsAndCompany\Schematic\Models\Result;
14
use PHPUnit_Framework_MockObject_MockObject as Mock;
15
16
/**
17
 * Class ElementIndexSettingsTest.
18
 *
19
 * @author    Nerds & Company
20
 * @copyright Copyright (c) 2015-2017, Nerds & Company
21
 * @license   MIT
22
 *
23
 * @link      http://www.nerds.company
24
 *
25
 * @coversDefaultClass NerdsAndCompany\Schematic\Services\ElementIndexSettings
26
 * @covers ::__construct
27
 * @covers ::<!public>
28
 */
29
class ElementIndexSettingsTest extends BaseTest
30
{
31
    /**
32
     * @var ElementIndexSettings
33
     */
34
    private $schematicElementIndexSettingsService;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function setUp()
40
    {
41
        $this->schematicElementIndexSettingsService = new ElementIndexSettings();
42
        $this->setMockSources();
43
        $this->setMockFieldsService();
44
    }
45
46
    /**
47
     * @return ElementsService|Mock
48
     *
49
     * @param array $getAllElementTypesResponse
50
     *
51
     * @return Mock
52
     */
53
    protected function getMockElementsService($getAllElementTypesResponse = [])
54
    {
55
        $mock = $this->getMockBuilder(ElementsService::class)->getMock();
56
        $mock->expects($this->any())->method('getAllElementTypes')->willReturn($getAllElementTypesResponse);
57
58
        return $mock;
59
    }
60
61
    /**
62
     * @return ElementIndexesService|Mock
63
     *
64
     * @param array $getSettingsResponse
0 ignored issues
show
Bug introduced by
There is no parameter named $getSettingsResponse. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
65
     *
66
     * @return Mock
67
     */
68
    protected function getMockElementIndexesService()
69
    {
70
        $getSettingsResponse = $this->getElementIndexSettingsSavedData();
71
        $mock = $this->getMockBuilder(ElementIndexesService::class)->getMock();
72
        $mock->expects($this->any())->method('getSettings')->will($this->returnValueMap([
73
          ['Entry', $getSettingsResponse['Entry']],
74
          ['Category', $getSettingsResponse['Category']]
75
        ]));
76
        $mock->expects($this->any())->method('saveSettings')->willReturn(false);
77
78
        return $mock;
79
    }
80
81
    /**
82
     * Test default import functionality.
83
     *
84
     * @covers ::import
85
     */
86
    public function testImport()
87
    {
88
        $data = $this->getElementIndexSettingsExportedData();
89
        $mockElementIndexesService = $this->getMockElementIndexesService();
90
        $this->setComponent(Craft::app(), 'elementIndexes', $mockElementIndexesService);
91
92
        $import = $this->schematicElementIndexSettingsService->import($data);
93
94
        $this->assertTrue($import instanceof Result);
95
        $this->assertTrue($import->hasErrors());
96
    }
97
98
    /**
99
     * Test export functionality.
100
     *
101
     * @covers ::export
102
     */
103
    public function testExport()
104
    {
105
        $data = $this->getElementsData();
106
        $mockElementsService = $this->getMockElementsService($data);
107
        $this->setComponent(Craft::app(), 'elements', $mockElementsService);
108
109
        $data = $this->getElementIndexSettingsExportedData();
110
        $mockElementIndexesService = $this->getMockElementIndexesService();
111
        $this->setComponent(Craft::app(), 'elementIndexes', $mockElementIndexesService);
112
113
        $export = $this->schematicElementIndexSettingsService->export();
114
        $this->assertEquals($data, $export);
115
    }
116
117
    /**
118
     * Returns elements data.
119
     *
120
     * @return array
121
     */
122
    public function getElementsData()
123
    {
124
        return [
125
            new CategoryElementType(),
126
            new EntryElementType(),
127
        ];
128
    }
129
130
    /**
131
     * Returns element index settings saved data.
132
     *
133
     * @return array
134
     */
135
    private function getElementIndexSettingsSavedData()
136
    {
137
        return [
138
            'Category' => [
139
                'sources' => [
140
                    '*' => [
141
                        'tableAttributes' => [
142
                            '1' => 'section',
143
                            '2' => 'postDate',
144
                            '3' => 'expiryDate',
145
                            '4' => 'author',
146
                            '5' => 'link',
147
                            '6' => 'field:1',
148
                        ],
149
                    ],
150
                ],
151
            ],
152
            'Entry' => [
153
                'sources' => [
154
                    '*' => [
155
                        'tableAttributes' => [
156
                            '1' => 'section',
157
                            '2' => 'postDate',
158
                            '3' => 'expiryDate',
159
                            '4' => 'author',
160
                            '5' => 'link',
161
                        ],
162
                    ],
163
                ],
164
            ],
165
        ];
166
    }
167
168
    /**
169
     * Returns element index settings exported data.
170
     *
171
     * @return array
172
     */
173
    private function getElementIndexSettingsExportedData()
174
    {
175
        return [
176
            'Category' => [
177
                'sources' => [
178
                    '*' => [
179
                        'tableAttributes' => [
180
                            '1' => 'section',
181
                            '2' => 'postDate',
182
                            '3' => 'expiryDate',
183
                            '4' => 'author',
184
                            '5' => 'link',
185
                            '6' => 'field:handle',
186
                        ],
187
                    ],
188
                ],
189
            ],
190
            'Entry' => [
191
                'sources' => [
192
                    '*' => [
193
                        'tableAttributes' => [
194
                            '1' => 'section',
195
                            '2' => 'postDate',
196
                            '3' => 'expiryDate',
197
                            '4' => 'author',
198
                            '5' => 'link',
199
                        ],
200
                    ],
201
                ],
202
            ],
203
        ];
204
    }
205
206
    /**
207
     * @return Mock|Sources
208
     */
209
    private function setMockSources()
210
    {
211
        $mockSources = $this->getMockBuilder(Sources::class)
212
            ->disableOriginalConstructor()
213
            ->getMock();
214
215
        $mockSources->expects($this->any())
216
            ->method('getSource')
217
            ->will($this->returnCallback(array($this, 'getMockSourceCallback')));
218
219
        $this->setComponent(Craft::app(), 'schematic_sources', $mockSources);
220
221
        return $mockSources;
222
    }
223
224
    /**
225
     * @param  string $fieldType
226
     * @param  string $source
227
     * @param  string $fromIndex
228
     * @param  string $toIndex
229
     *
230
     * @return string
231
     */
232
    public function getMockSourceCallback($fieldType, $source, $fromIndex, $toIndex)
233
    {
234
        switch ($source) {
235
            case 'field:handle':
236
                return 'field:1';
237
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
238
            case 'field:1':
239
                return 'field:handle';
240
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
241
            default:
242
                return $source;
243
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
244
        }
245
    }
246
247
    /**
248
     * @return Mock|CraftFieldsService
249
     */
250
    private function setMockFieldsService()
251
    {
252
        $mockFieldsService = $this->getMockBuilder(FieldsService::class)
253
            ->disableOriginalConstructor()
254
            ->getMock();
255
256
        $mockFieldsService->expects($this->any())
257
            ->method('getFieldById')
258
            ->with('1')
259
            ->willReturn(new FieldModel(['handle' => 'handle']));
260
261
        $this->setComponent(Craft::app(), 'fields', $mockFieldsService);
262
263
        return $mockFieldsService;
264
    }
265
}
266