Completed
Pull Request — master (#93)
by Bart
05:42
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')->willReturn($getSettingsResponse['Entry']);
73
        $mock->expects($this->any())->method('saveSettings')->willReturn(false);
74
75
        return $mock;
76
    }
77
78
    /**
79
     * Test default import functionality.
80
     *
81
     * @covers ::import
82
     */
83
    public function testImport()
84
    {
85
        $data = $this->getElementIndexSettingsExportedData();
86
        $mockElementIndexesService = $this->getMockElementIndexesService();
87
        $this->setComponent(Craft::app(), 'elementIndexes', $mockElementIndexesService);
88
89
        $import = $this->schematicElementIndexSettingsService->import($data);
90
91
        $this->assertTrue($import instanceof Result);
92
        $this->assertTrue($import->hasErrors());
93
    }
94
95
    /**
96
     * Test export functionality.
97
     *
98
     * @covers ::export
99
     */
100
    public function testExport()
101
    {
102
        $data = $this->getElementsData();
103
        $mockElementsService = $this->getMockElementsService($data);
104
        $this->setComponent(Craft::app(), 'elements', $mockElementsService);
105
106
        $data = $this->getElementIndexSettingsExportedData();
107
        $mockElementIndexesService = $this->getMockElementIndexesService();
108
        $this->setComponent(Craft::app(), 'elementIndexes', $mockElementIndexesService);
109
110
        $export = $this->schematicElementIndexSettingsService->export();
111
        $this->assertEquals($data, $export);
112
    }
113
114
    /**
115
     * Returns elements data.
116
     *
117
     * @return array
118
     */
119
    public function getElementsData()
120
    {
121
        return [
122
            new CategoryElementType(),
123
            new EntryElementType(),
124
        ];
125
    }
126
127
    /**
128
     * Returns element index settings saved data.
129
     *
130
     * @return array
131
     */
132
    private function getElementIndexSettingsSavedData()
133
    {
134
        return [
135
            'Category' => [
136
                'sources' => [
137
                    '*' => [
138
                        'tableAttributes' => [
139
                            '1' => 'section',
140
                            '2' => 'postDate',
141
                            '3' => 'expiryDate',
142
                            '4' => 'author',
143
                            '5' => 'link',
144
                            '6' => 'field:1',
145
                        ],
146
                    ],
147
                ],
148
            ],
149
            'Entry' => [
150
                'sources' => [
151
                    '*' => [
152
                        'tableAttributes' => [
153
                            '1' => 'section',
154
                            '2' => 'postDate',
155
                            '3' => 'expiryDate',
156
                            '4' => 'author',
157
                            '5' => 'link',
158
                        ],
159
                    ],
160
                ],
161
            ],
162
        ];
163
    }
164
165
    /**
166
     * Returns element index settings exported data.
167
     *
168
     * @return array
169
     */
170
    private function getElementIndexSettingsExportedData()
171
    {
172
        return [
173
            'Category' => [
174
                'sources' => [
175
                    '*' => [
176
                        'tableAttributes' => [
177
                            '1' => 'section',
178
                            '2' => 'postDate',
179
                            '3' => 'expiryDate',
180
                            '4' => 'author',
181
                            '5' => 'link',
182
                            '6' => 'field:handle',
183
                        ],
184
                    ],
185
                ],
186
            ],
187
            'Entry' => [
188
                'sources' => [
189
                    '*' => [
190
                        'tableAttributes' => [
191
                            '1' => 'section',
192
                            '2' => 'postDate',
193
                            '3' => 'expiryDate',
194
                            '4' => 'author',
195
                            '5' => 'link',
196
                        ],
197
                    ],
198
                ],
199
            ],
200
        ];
201
    }
202
203
    /**
204
     * @return Mock|Sources
205
     */
206
    private function setMockSources()
207
    {
208
        $mockSources = $this->getMockBuilder(Sources::class)
209
            ->disableOriginalConstructor()
210
            ->getMock();
211
212
        $mockSources->expects($this->any())
213
            ->method('getSource')
214
            ->will($this->returnCallback(array($this, 'getMockSourceCallback')));
215
216
        $this->setComponent(Craft::app(), 'schematic_sources', $mockSources);
217
218
        return $mockSources;
219
    }
220
221
    /**
222
     * @param  string $fieldType
223
     * @param  string $source
224
     * @param  string $fromIndex
225
     * @param  string $toIndex
226
     *
227
     * @return string
228
     */
229
    public function getMockSourceCallback($fieldType, $source, $fromIndex, $toIndex)
230
    {
231
        switch ($source) {
232
            case 'field:handle':
233
                return 'field:1';
234
                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...
235
            case 'field:1':
236
                return 'field:handle';
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
            default:
239
                return $source;
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
        }
242
    }
243
244
    /**
245
     * @return Mock|CraftFieldsService
246
     */
247
    private function setMockFieldsService()
248
    {
249
        $mockFieldsService = $this->getMockBuilder(FieldsService::class)
250
            ->disableOriginalConstructor()
251
            ->getMock();
252
253
        $mockFieldsService->expects($this->any())
254
            ->method('getFieldById')
255
            ->with(1)
256
            ->willReturn(new FieldModel(['handle' => 'handle']));
257
258
        $this->setComponent(Craft::app(), 'fields', $mockFieldsService);
259
260
        return $mockFieldsService;
261
    }
262
}
263