Completed
Push — master ( ae47f3...33ec04 )
by Bob Olde
11s
created

MatrixTest::testSaveRecord()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 1
nop 3
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Fields;
4
5
use Craft;
6
use craft\fields\Matrix as MatrixField;
7
use Codeception\Test\Unit;
8
9
/**
10
 * Class MatrixTest.
11
 *
12
 * @author    Nerds & Company
13
 * @copyright Copyright (c) 2015-2017, Nerds & Company
14
 * @license   MIT
15
 *
16
 * @see      http://www.nerds.company
17
 */
18
class MatrixTest extends Unit
19
{
20
    /**
21
     * @var Matrix
22
     */
23
    private $converter;
24
25
    /**
26
     * Set the converter.
27
     *
28
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
29
     */
30
    protected function _before()
31
    {
32
        $this->converter = new Matrix();
33
    }
34
35
    //==============================================================================================================
36
    //=================================================  TESTS  ====================================================
37
    //==============================================================================================================
38
39
    /**
40
     * @dataProvider provideMatrices
41
     *
42
     * @param MatrixField $matrix
43
     * @param array       $definition
44
     */
45
    public function testGetRecordDefinition(MatrixField $matrix, array $definition)
46
    {
47
        Craft::$app->controller->module->modelMapper->expects($this->exactly(1))
48
                                ->method('export')
49
                                ->with($matrix->getBlockTypes())
50
                                ->willReturn($definition['blockTypes']);
51
52
        $result = $this->converter->getRecordDefinition($matrix);
53
54
        $this->assertSame($definition, $result);
55
    }
56
57
    /**
58
     * @dataProvider provideMatrices
59
     *
60
     * @param MatrixField $matrix
61
     * @param array       $definition
62
     * @param bool        $valid
63
     */
64
    public function testSaveRecord(MatrixField $matrix, array $definition, bool $valid)
65
    {
66
        Craft::$app->fields->expects($this->exactly(1))
67
                           ->method('saveField')
68
                           ->with($matrix)
69
                           ->willReturn($valid);
70
71
        Craft::$app->controller->module->modelMapper->expects($this->exactly($valid ? 1 : 0))
72
                                     ->method('import')
73
                                     ->with($definition['blockTypes'], $matrix->getBlockTypes(), ['fieldId' => $matrix->id])
74
                                     ->willReturn($matrix->getBlockTypes());
75
76
        $result = $this->converter->saveRecord($matrix, $definition);
77
78
        $this->assertSame($valid, $result);
79
    }
80
81
    /**
82
     * @dataProvider provideMatrices
83
     *
84
     * @param MatrixField $matrix
85
     */
86
    public function testDeleteRecord(MatrixField $matrix)
87
    {
88
        Craft::$app->fields->expects($this->exactly(1))
89
                           ->method('deleteField')
90
                           ->with($matrix);
91
92
        $this->converter->deleteRecord($matrix);
93
    }
94
95
    //==============================================================================================================
96
    //==============================================  PROVIDERS  ===================================================
97
    //==============================================================================================================
98
99
    /**
100
     * @return array
101
     */
102
    public function provideMatrices()
103
    {
104
        $mockMatrix1 = $this->getMockMatrix(1, 1);
105
        $mockMatrix2 = $this->getMockMatrix(2, 1);
106
107
        return [
108
            'valid matrix' => [
109
                'matrix' => $mockMatrix1,
110
                'definition' => $this->getMockMatrixDefinition($mockMatrix1),
0 ignored issues
show
Documentation introduced by
$mockMatrix1 is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\fields\Matrix>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
                'validSave' => true,
112
            ],
113
            'invalid matrix' => [
114
                'matrix' => $mockMatrix2,
115
                'definition' => $this->getMockMatrixDefinition($mockMatrix2),
0 ignored issues
show
Documentation introduced by
$mockMatrix2 is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\fields\Matrix>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
116
                'validSave' => false,
117
            ],
118
        ];
119
    }
120
121
    //==============================================================================================================
122
    //================================================  HELPERS  ===================================================
123
    //==============================================================================================================
124
125
    /**
126
     * @param MatrixField $mockMatrix
127
     *
128
     * @return array
129
     */
130
    private function getMockMatrixDefinition(MatrixField $mockMatrix)
131
    {
132
        return [
133
            'class' => get_class($mockMatrix),
134
            'attributes' => [
135
                'minBlocks' => null,
136
                'maxBlocks' => null,
137
                'localizeBlocks' => false,
138
                'name' => 'matrixName'.$mockMatrix->id,
139
                'handle' => 'matrixHandle'.$mockMatrix->id,
140
                'instructions' => null,
141
                'translationMethod' => 'none',
142
                'translationKeyFormat' => null,
143
                'oldHandle' => null,
144
                'columnPrefix' => null,
145
                'required' => false,
146
                'sortOrder' => null,
147
            ],
148
            'group' => $mockMatrix->group->name,
149
            'blockTypes' => [
150
                'blockTypeDefinition1',
151
                'blockTypeDefinition2',
152
            ],
153
        ];
154
    }
155
156
    /**
157
     * @param int $matrixId
158
     * @param int $groupId
159
     *
160
     * @return Mock|MatrixField
161
     */
162
    private function getMockMatrix(int $matrixId, int $groupId)
163
    {
164
        $mockMatrix = $this->getMockBuilder(MatrixField::class)
165
                           ->setMethods(['getGroup', 'getBlockTypes'])
166
                           ->disableOriginalConstructor()
167
                           ->getMock();
168
169
        $mockMatrix->id = $matrixId;
0 ignored issues
show
Bug introduced by
Accessing id 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...
170
        $mockMatrix->groupId = $matrixId;
0 ignored issues
show
Bug introduced by
Accessing groupId 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...
171
        $mockMatrix->handle = 'matrixHandle'.$matrixId;
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...
172
        $mockMatrix->name = 'matrixName'.$matrixId;
0 ignored issues
show
Bug introduced by
Accessing name 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...
173
174
        $mockMatrix->expects($this->any())
175
                 ->method('getGroup')
176
                 ->willReturn($this->getMockFieldGroup($groupId));
177
178
        $mockMatrix->expects($this->any())
179
                   ->method('getBlockTypes')
180
                   ->willReturn([
181
                       $this->getMockMatrixBlockType(1),
182
                       $this->getMockMatrixBlockType(2),
183
                   ]);
184
185
        return $mockMatrix;
186
    }
187
188
    /**
189
     * Get a mock field group.
190
     *
191
     * @param int $groupId
192
     *
193
     * @return Mock|FieldGroup
194
     */
195
    private function getMockFieldGroup(int $groupId)
196
    {
197
        $mockGroup = $this->getMockBuilder(FieldGroup::class)
198
                          ->disableOriginalConstructor()
199
                          ->getmock();
200
201
        $mockGroup->id = $groupId;
0 ignored issues
show
Bug introduced by
Accessing id 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...
202
        $mockGroup->name = 'fieldGroup'.$groupId;
0 ignored issues
show
Bug introduced by
Accessing name 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...
203
204
        return $mockGroup;
205
    }
206
207
    /**
208
     * Get a mock matrix block type.
209
     *
210
     * @param int $blockId
211
     *
212
     * @return Mock|MatrixBlockType
213
     */
214
    private function getMockMatrixBlockType($blockId)
215
    {
216
        $mockBlockType = $this->getMockBuilder(MatrixBlockType::class)
217
                              ->disableOriginalConstructor()
218
                              ->getmock();
219
220
        $mockBlockType->id = $blockId;
0 ignored issues
show
Bug introduced by
Accessing id 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...
221
        $mockBlockType->handle = 'blockHandle'.$blockId;
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...
222
223
        return $mockBlockType;
224
    }
225
}
226