Completed
Pull Request — master (#93)
by Bart
05:31
created

ElementIndexSettingsTest::getMockSourceCallback()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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