Completed
Push — do-not-scrutinize-tests ( fcf005 )
by Bart
04:15 queued 02:38
created

ElementIndexMapperTest::getElementIndexSettingsSavedData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 40
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 27
nc 1
nop 0
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Mappers;
4
5
use Craft;
6
use craft\elements\Category;
7
use craft\elements\Entry;
8
use craft\base\Field;
9
use Codeception\Test\Unit;
10
11
/**
12
 * Class ElementIndexSettingsTest.
13
 *
14
 * @author    Nerds & Company
15
 * @copyright Copyright (c) 2015-2018, Nerds & Company
16
 * @license   MIT
17
 *
18
 * @see      http://www.nerds.company
19
 */
20
class ElementIndexMapperTest extends Unit
21
{
22
    /**
23
     * @var ElementIndexMapper
24
     */
25
    private $mapper;
26
27
    /**
28
     * Set the mapper.
29
     *
30
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
31
     * phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
32
     */
33
    protected function _before()
34
    {
35
        $this->setMockSectionsService();
36
        $this->setMockFieldsService();
37
        $this->setMockElementIndexesService();
38
39
        $this->mapper = new ElementIndexMapper();
40
    }
41
42
    //==============================================================================================================
43
    //=================================================  TESTS  ====================================================
44
    //==============================================================================================================
45
46
    /**
47
     * Test default import functionality.
48
     */
49
    public function testImport()
50
    {
51
        $data = $this->getElementIndexSettingsExportedData();
52
53
        $result = $this->mapper->import($data, $this->getElementsData());
54
55
        $this->assertSame([], $result);
56
    }
57
58
    /**
59
     * Test export functionality.
60
     */
61
    public function testExport()
62
    {
63
        $data = $this->getElementsData();
64
        $expected = $this->getElementIndexSettingsExportedData();
65
66
        $result = $this->mapper->export($data);
67
        $this->assertEquals($expected, $result);
68
    }
69
70
    //==============================================================================================================
71
    //================================================  HELPERS  ===================================================
72
    //==============================================================================================================
73
74
    /**
75
     * @param array $getAllElementTypesResponse
76
     */
77
    protected function setMockElementsService($getAllElementTypesResponse = [])
78
    {
79
        Craft::$app->elementTypes->expects($this->any())
80
                                 ->method('getAllElementTypes')
81
                                 ->willReturn($getAllElementTypesResponse);
82
    }
83
84
    /**
85
     * St mock element index service.
86
     */
87
    protected function setMockElementIndexesService()
88
    {
89
        $getSettingsResponse = $this->getElementIndexSettingsSavedData();
90
        Craft::$app->elementIndexes
91
                   ->expects($this->any())
92
                   ->method('getSettings')
93
                   ->willReturnMap([
94
                      [Entry::class, $getSettingsResponse['Entry']],
95
                      [Category::class, $getSettingsResponse['Category']],
96
                   ]);
97
98
        Craft::$app->elementIndexes->expects($this->any())
99
                                   ->method('saveSettings')
100
                                   ->willReturn(false);
101
    }
102
103
    /**
104
     * Returns elements data.
105
     *
106
     * @return array
107
     */
108
    public function getElementsData()
109
    {
110
        return [
111
            Category::class,
112
            Entry::class,
113
        ];
114
    }
115
116
    /**
117
     * Returns element index settings saved data.
118
     *
119
     * @return array
120
     */
121
    private function getElementIndexSettingsSavedData()
122
    {
123
        return [
124
            'Category' => [
125
                'sourceOrder' => [
126
                    ['heading', 'Channels'],
127
                    ['key', 'section:1'],
128
                ],
129
                'sources' => [
130
                    '*' => [
131
                        'tableAttributes' => [
132
                            '0' => 'section',
133
                            '1' => 'postDate',
134
                            '2' => 'expiryDate',
135
                            '3' => 'author',
136
                            '4' => 'link',
137
                            '5' => 'field:1',
138
                        ],
139
                    ],
140
                ],
141
            ],
142
            'Entry' => [
143
                'sourceOrder' => [
144
                    ['heading', 'Channels'],
145
                    ['key', 'section:1'],
146
                ],
147
                'sources' => [
148
                    '*' => [
149
                        'tableAttributes' => [
150
                            '0' => 'section',
151
                            '1' => 'postDate',
152
                            '2' => 'expiryDate',
153
                            '3' => 'author',
154
                            '4' => 'link',
155
                        ],
156
                    ],
157
                ],
158
            ],
159
        ];
160
    }
161
162
    /**
163
     * Returns element index settings exported data.
164
     *
165
     * @return array
166
     */
167
    private function getElementIndexSettingsExportedData()
168
    {
169
        return [
170
            'Category' => [
171
                'sourceOrder' => [
172
                    ['heading', 'Channels'],
173
                    ['key', 'section:handle'],
174
                ],
175
                'sources' => [
176
                    '*' => [
177
                        'tableAttributes' => [
178
                            '0' => 'section',
179
                            '1' => 'postDate',
180
                            '2' => 'expiryDate',
181
                            '3' => 'author',
182
                            '4' => 'link',
183
                            '5' => 'field:handle',
184
                        ],
185
                    ],
186
                ],
187
            ],
188
            'Entry' => [
189
                'sourceOrder' => [
190
                    ['heading', 'Channels'],
191
                    ['key', 'section:handle'],
192
                ],
193
                'sources' => [
194
                    '*' => [
195
                        'tableAttributes' => [
196
                            '0' => 'section',
197
                            '1' => 'postDate',
198
                            '2' => 'expiryDate',
199
                            '3' => 'author',
200
                            '4' => 'link',
201
                        ],
202
                    ],
203
                ],
204
            ],
205
        ];
206
    }
207
208
    /**
209
     * @return Mock|Sources
210
     */
211
    private function setMockSectionsService()
212
    {
213
        $mockSection = $this->getMockBuilder(Section::class)->getMock();
214
        $mockSection->handle = 'handle';
215
216
        Craft::$app->sections->expects($this->any())
217
                             ->method('getSectionById')
218
                             ->with('1')
219
                             ->willReturn($mockSection);
220
    }
221
222
    /**
223
     * Set mock fields service.
224
     */
225
    private function setMockFieldsService()
226
    {
227
        $mockField = $this->getMockBuilder(Field::class)->getMock();
228
        $mockField->handle = 'handle';
229
230
        Craft::$app->fields->expects($this->any())
231
                            ->method('getFieldById')
232
                            ->with('1')
233
                            ->willReturn($mockField);
234
    }
235
}
236