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

ElementIndexSettingsTest::testImport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 7
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
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
                        ],
141
                    ],
142
                ],
143
            ],
144
            'Entry' => [
145
                'sources' => [
146
                    '*' => [
147
                        'tableAttributes' => [
148
                            '1' => 'section',
149
                            '2' => 'postDate',
150
                            '3' => 'expiryDate',
151
                            '4' => 'author',
152
                            '5' => 'link',
153
                        ],
154
                    ],
155
                ],
156
            ],
157
        ];
158
    }
159
160
    /**
161
     * @return Mock|Sources
162
     */
163
    private function setMockSources()
164
    {
165
        $mockSources = $this->getMockBuilder(Sources::class)
0 ignored issues
show
Unused Code introduced by
$mockSources is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
166
            ->disableOriginalConstructor()
167
            ->getMock();
168
169
        $mockFieldsService->expects($this->any())
0 ignored issues
show
Bug introduced by
The variable $mockFieldsService does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
170
            ->method('getSource')
171
            ->willReturn($this->returnCallback(array($this, 'getMockSourceCallback')));
172
173
        $this->setComponent(Craft::app(), 'schematic_sources', $mockFieldsService);
174
175
        return $mockFieldsService;
176
    }
177
178
    /**
179
     * @param  string $fieldType
180
     * @param  string $source
181
     * @param  string $fromIndex
182
     * @param  string $toIndex
183
     *
184
     * @return string
185
     */
186
    public function getMockSourceCallback($fieldType, $source, $fromIndex, $toIndex)
187
    {
188
        return $source;
189
    }
190
}
191