Completed
Pull Request — master (#114)
by Bart
02:16
created

ElementIndexMapperTest::_before()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 5
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
     */
32
    protected function _before()
33
    {
34
        $this->setMockSectionsService();
35
        $this->setMockFieldsService();
36
        $this->setMockElementIndexesService();
37
38
        $this->mapper = new ElementIndexMapper();
39
    }
40
41
    //==============================================================================================================
42
    //=================================================  TESTS  ====================================================
43
    //==============================================================================================================
44
45
    /**
46
     * Test default import functionality.
47
     */
48
    public function testImport()
49
    {
50
        $data = $this->getElementIndexSettingsExportedData();
51
52
        $result = $this->mapper->import($data, $this->getElementsData());
53
54
        $this->assertSame([], $result);
55
    }
56
57
    /**
58
     * Test export functionality.
59
     */
60
    public function testExport()
61
    {
62
        $data = $this->getElementsData();
63
        $expected = $this->getElementIndexSettingsExportedData();
64
65
        $result = $this->mapper->export($data);
66
        $this->assertEquals($expected, $result);
67
    }
68
69
    //==============================================================================================================
70
    //================================================  HELPERS  ===================================================
71
    //==============================================================================================================
72
73
    /**
74
     * @param array $getAllElementTypesResponse
75
     */
76
    protected function setMockElementsService($getAllElementTypesResponse = [])
77
    {
78
        Craft::$app->elementTypes->expects($this->any())
79
                                 ->method('getAllElementTypes')
80
                                 ->willReturn($getAllElementTypesResponse);
81
    }
82
83
    /**
84
     * St mock element index service.
85
     */
86
    protected function setMockElementIndexesService()
87
    {
88
        $getSettingsResponse = $this->getElementIndexSettingsSavedData();
89
        Craft::$app->elementIndexes
90
                   ->expects($this->any())
91
                   ->method('getSettings')
92
                   ->willReturnMap([
93
                      [Entry::class, $getSettingsResponse['Entry']],
94
                      [Category::class, $getSettingsResponse['Category']],
95
                   ]);
96
97
        Craft::$app->elementIndexes->expects($this->any())
98
                                   ->method('saveSettings')
99
                                   ->willReturn(false);
100
    }
101
102
    /**
103
     * Returns elements data.
104
     *
105
     * @return array
106
     */
107
    public function getElementsData()
108
    {
109
        return [
110
            Category::class,
111
            Entry::class,
112
        ];
113
    }
114
115
    /**
116
     * Returns element index settings saved data.
117
     *
118
     * @return array
119
     */
120
    private function getElementIndexSettingsSavedData()
121
    {
122
        return [
123
            'Category' => [
124
                'sourceOrder' => [
125
                    ['heading', 'Channels'],
126
                    ['key', 'section:1'],
127
                ],
128
                'sources' => [
129
                    '*' => [
130
                        'tableAttributes' => [
131
                            '0' => 'section',
132
                            '1' => 'postDate',
133
                            '2' => 'expiryDate',
134
                            '3' => 'author',
135
                            '4' => 'link',
136
                            '5' => 'field:1',
137
                        ],
138
                    ],
139
                ],
140
            ],
141
            'Entry' => [
142
                'sourceOrder' => [
143
                    ['heading', 'Channels'],
144
                    ['key', 'section:1'],
145
                ],
146
                'sources' => [
147
                    '*' => [
148
                        'tableAttributes' => [
149
                            '0' => 'section',
150
                            '1' => 'postDate',
151
                            '2' => 'expiryDate',
152
                            '3' => 'author',
153
                            '4' => 'link',
154
                        ],
155
                    ],
156
                ],
157
            ],
158
        ];
159
    }
160
161
    /**
162
     * Returns element index settings exported data.
163
     *
164
     * @return array
165
     */
166
    private function getElementIndexSettingsExportedData()
167
    {
168
        return [
169
            'Category' => [
170
                'sourceOrder' => [
171
                    ['heading', 'Channels'],
172
                    ['key', 'section:handle'],
173
                ],
174
                'sources' => [
175
                    '*' => [
176
                        'tableAttributes' => [
177
                            '0' => 'section',
178
                            '1' => 'postDate',
179
                            '2' => 'expiryDate',
180
                            '3' => 'author',
181
                            '4' => 'link',
182
                            '5' => 'field:handle',
183
                        ],
184
                    ],
185
                ],
186
            ],
187
            'Entry' => [
188
                'sourceOrder' => [
189
                    ['heading', 'Channels'],
190
                    ['key', 'section:handle'],
191
                ],
192
                'sources' => [
193
                    '*' => [
194
                        'tableAttributes' => [
195
                            '0' => 'section',
196
                            '1' => 'postDate',
197
                            '2' => 'expiryDate',
198
                            '3' => 'author',
199
                            '4' => 'link',
200
                        ],
201
                    ],
202
                ],
203
            ],
204
        ];
205
    }
206
207
    /**
208
     * @return Mock|Sources
209
     */
210
    private function setMockSectionsService()
211
    {
212
        $mockSection = $this->getMockBuilder(Section::class)->getMock();
213
        $mockSection->handle = 'handle';
0 ignored issues
show
Bug introduced by
Accessing handle on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
214
215
        Craft::$app->sections->expects($this->any())
216
                             ->method('getSectionById')
217
                             ->with('1')
218
                             ->willReturn($mockSection);
219
    }
220
221
    /**
222
     * Set mock fields service.
223
     */
224
    private function setMockFieldsService()
225
    {
226
        $mockField = $this->getMockBuilder(Field::class)->getMock();
227
        $mockField->handle = 'handle';
0 ignored issues
show
Bug introduced by
Accessing handle on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
228
229
        Craft::$app->fields->expects($this->any())
230
                            ->method('getFieldById')
231
                            ->with('1')
232
                            ->willReturn($mockField);
233
    }
234
}
235