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

MatrixBlockTypeTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 207
rs 10
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\base\Field as FieldModel;
7
use craft\models\MatrixBlockType as MatrixBlockTypeModel;
8
use craft\models\FieldLayout;
9
use Codeception\Test\Unit;
10
11
/**
12
 * Class MatrixBlockTypeTest.
13
 *
14
 * @author    Nerds & Company
15
 * @copyright Copyright (c) 2015-2017, Nerds & Company
16
 * @license   MIT
17
 *
18
 * @see      http://www.nerds.company
19
 */
20
class MatrixBlockTypeTest extends Unit
21
{
22
    /**
23
     * @var MatrixBlockType
24
     */
25
    private $converter;
26
27
    /**
28
     * Set the converter.
29
     *
30
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
31
     * phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
32
     */
33
    protected function _before()
34
    {
35
        $this->converter = new MatrixBlockType();
36
    }
37
38
    //==============================================================================================================
39
    //=================================================  TESTS  ====================================================
40
    //==============================================================================================================
41
42
    /**
43
     * @dataProvider provideMatrixBlockTypes
44
     *
45
     * @param MatrixBlockTypeModel $blockType
46
     * @param array                $definition
47
     */
48
    public function testGetRecordDefinition(MatrixBlockTypeModel $blockType, array $definition)
49
    {
50
        Craft::$app->controller->module->modelMapper->expects($this->exactly(1))
51
                                     ->method('export')
52
                                     ->with($blockType->getFieldLayout()->getFields())
53
                                     ->willReturn($definition['fields']);
54
55
        $result = $this->converter->getRecordDefinition($blockType);
56
57
        $this->assertSame($definition, $result);
58
    }
59
60
    /**
61
     * @dataProvider provideMatrixBlockTypes
62
     *
63
     * @param MatrixBlockTypeModel $blockType
64
     * @param array                $definition
65
     * @param array                $defaultAttributes
66
     */
67
    public function testSetRecordAttributes(
68
        MatrixBlockTypeModel $blockType,
69
        array $definition,
70
        array $defaultAttributes
71
    ) {
72
        $newMatrixBlockType = $this->getMockBuilder(MatrixBlockTypeModel::class)
73
                                   ->setMethods(['setFieldLayout'])
74
                                   ->getMock();
75
76
        $newMatrixBlockType->expects($this->exactly(0))
77
                           ->method('setFieldLayout');
78
79
        $this->converter->setRecordAttributes($newMatrixBlockType, $definition, $defaultAttributes);
80
81
        $this->assertSame($defaultAttributes['fieldId'], $newMatrixBlockType->fieldId);
82
        $this->assertSame($blockType->name, $newMatrixBlockType->name);
83
        $this->assertSame($blockType->handle, $newMatrixBlockType->handle);
84
    }
85
86
    /**
87
     * @dataProvider provideMatrixBlockTypes
88
     *
89
     * @param MatrixBlockTypeModel $blockType
90
     * @param array                $definition
91
     */
92
    public function testSaveRecord(MatrixBlockTypeModel $blockType, array $definition)
93
    {
94
        Craft::$app->matrix->expects($this->exactly(1))
95
                           ->method('saveBlockType')
96
                           ->with($blockType, false)
97
                           ->willReturn(true);
98
99
        $context = 'matrixBlockType:'.$blockType->id;
100
        $existingFields = $blockType->getFieldLayout()->getFields();
101
102
        Craft::$app->fields->expects($this->exactly(1))
103
                           ->method('getAllFields')
104
                           ->with($context)
105
                           ->willReturn($existingFields);
106
107
        Craft::$app->controller->module->modelMapper->expects($this->exactly(1))
108
                                     ->method('import')
109
                                     ->with($definition['fields'], $existingFields, ['context' => $context], false)
110
                                     ->willReturn($existingFields);
111
112
        $result = $this->converter->saveRecord($blockType, $definition);
113
114
        $this->assertTrue($result);
115
    }
116
117
    /**
118
     * @dataProvider provideMatrixBlockTypes
119
     *
120
     * @param MatrixBlockTypeModel $blockType
121
     */
122
    public function testDeleteRecord(MatrixBlockTypeModel $blockType)
123
    {
124
        Craft::$app->matrix->expects($this->exactly(1))
125
                           ->method('deleteBlockType')
126
                           ->with($blockType);
127
128
        $this->converter->deleteRecord($blockType);
129
    }
130
131
    //==============================================================================================================
132
    //==============================================  PROVIDERS  ===================================================
133
    //==============================================================================================================
134
135
    /**
136
     * @return array
137
     */
138
    public function provideMatrixBlockTypes()
139
    {
140
        $mockMatrixBlockType = $this->getMockMatrixBlockType(1);
141
142
        return [
143
            'valid blockType' => [
144
                'blockType' => $mockMatrixBlockType,
145
                'definition' => $this->getMockMatrixBlockTypeDefinition($mockMatrixBlockType),
146
                'defaultAttributes' => ['fieldId' => 1],
147
            ],
148
        ];
149
    }
150
151
    //==============================================================================================================
152
    //================================================  HELPERS  ===================================================
153
    //==============================================================================================================
154
155
    /**
156
     * @param MatrixBlockTypeModel $mockMatrixBlockType
157
     *
158
     * @return array
159
     */
160
    private function getMockMatrixBlockTypeDefinition(MatrixBlockTypeModel $mockMatrixBlockType)
161
    {
162
        return [
163
            'class' => get_class($mockMatrixBlockType),
164
            'attributes' => [
165
                'name' => 'blockTypeName'.$mockMatrixBlockType->id,
166
                'handle' => 'blockTypeHandle'.$mockMatrixBlockType->id,
167
                'sortOrder' => null,
168
            ],
169
            'fields' => [
170
                'fieldDefinition',
171
            ],
172
        ];
173
    }
174
175
    /**
176
     * @param int $blockTypeId
177
     *
178
     * @return Mock|MatrixBlockTypeModel
179
     */
180
    private function getMockMatrixBlockType(int $blockTypeId)
181
    {
182
        $mockMatrixBlockType = $this->getMockBuilder(MatrixBlockTypeModel::class)
183
                              ->setMethods(['getFieldLayout'])
184
                              ->disableOriginalConstructor()
185
                              ->getMock();
186
187
        $mockMatrixBlockType->id = $blockTypeId;
188
        $mockMatrixBlockType->fieldLayoutId = $blockTypeId;
189
        $mockMatrixBlockType->handle = 'blockTypeHandle'.$blockTypeId;
190
        $mockMatrixBlockType->name = 'blockTypeName'.$blockTypeId;
191
192
        $mockField = $this->getMockField($blockTypeId);
193
194
        $mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
195
196
        $mockFieldLayout->expects($this->any())
197
                        ->method('getFields')
198
                        ->willReturn([$mockField]);
199
200
        $mockMatrixBlockType->expects($this->any())
201
                   ->method('getFieldLayout')
202
                   ->willReturn($mockFieldLayout);
203
204
        return $mockMatrixBlockType;
205
    }
206
207
    /**
208
     * Get a mock field.
209
     *
210
     * @param int $fieldId
211
     *
212
     * @return Mock|FieldModel
213
     */
214
    private function getMockField(int $fieldId)
215
    {
216
        $mockField = $this->getMockbuilder(FieldModel::class)
217
                         ->setMethods([])
218
                         ->getMock();
219
220
        $mockField->id = $fieldId;
221
        $mockField->handle = 'field'.$fieldId;
222
        $mockField->required = true;
223
224
        return $mockField;
225
    }
226
}
227