Completed
Pull Request — master (#93)
by Bart
04:43
created

getElementIndexSettingsSavedData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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