These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | * phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
||
30 | */ |
||
31 | protected function _before() |
||
32 | { |
||
33 | $this->converter = new Matrix(); |
||
34 | } |
||
35 | |||
36 | //============================================================================================================== |
||
37 | //================================================= TESTS ==================================================== |
||
38 | //============================================================================================================== |
||
39 | |||
40 | /** |
||
41 | * @dataProvider provideMatrices |
||
42 | * |
||
43 | * @param MatrixField $matrix |
||
44 | * @param array $definition |
||
45 | */ |
||
46 | public function testGetRecordDefinition(MatrixField $matrix, array $definition) |
||
47 | { |
||
48 | Craft::$app->controller->module->modelMapper->expects($this->exactly(1)) |
||
49 | ->method('export') |
||
50 | ->with($matrix->getBlockTypes()) |
||
51 | ->willReturn($definition['blockTypes']); |
||
52 | |||
53 | $result = $this->converter->getRecordDefinition($matrix); |
||
54 | |||
55 | $this->assertSame($definition, $result); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @dataProvider provideMatrices |
||
60 | * |
||
61 | * @param MatrixField $matrix |
||
62 | * @param array $definition |
||
63 | * @param bool $valid |
||
64 | */ |
||
65 | public function testSaveRecord(MatrixField $matrix, array $definition, bool $valid) |
||
66 | { |
||
67 | Craft::$app->fields->expects($this->exactly(1)) |
||
68 | ->method('saveField') |
||
69 | ->with($matrix) |
||
70 | ->willReturn($valid); |
||
71 | |||
72 | Craft::$app->controller->module->modelMapper->expects($this->exactly($valid ? 1 : 0)) |
||
73 | ->method('import') |
||
74 | ->with($definition['blockTypes'], $matrix->getBlockTypes(), [ |
||
75 | 'fieldId' => $matrix->id, |
||
76 | ]) |
||
77 | ->willReturn($matrix->getBlockTypes()); |
||
78 | |||
79 | $result = $this->converter->saveRecord($matrix, $definition); |
||
80 | |||
81 | $this->assertSame($valid, $result); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @dataProvider provideMatrices |
||
86 | * |
||
87 | * @param MatrixField $matrix |
||
88 | */ |
||
89 | public function testDeleteRecord(MatrixField $matrix) |
||
90 | { |
||
91 | Craft::$app->fields->expects($this->exactly(1)) |
||
92 | ->method('deleteField') |
||
93 | ->with($matrix); |
||
94 | |||
95 | $this->converter->deleteRecord($matrix); |
||
96 | } |
||
97 | |||
98 | //============================================================================================================== |
||
99 | //============================================== PROVIDERS =================================================== |
||
100 | //============================================================================================================== |
||
101 | |||
102 | /** |
||
103 | * @return array |
||
104 | */ |
||
105 | public function provideMatrices() |
||
106 | { |
||
107 | $mockMatrix1 = $this->getMockMatrix(1, 1); |
||
108 | $mockMatrix2 = $this->getMockMatrix(2, 1); |
||
109 | |||
110 | return [ |
||
111 | 'valid matrix' => [ |
||
112 | 'matrix' => $mockMatrix1, |
||
113 | 'definition' => $this->getMockMatrixDefinition($mockMatrix1), |
||
0 ignored issues
–
show
|
|||
114 | 'validSave' => true, |
||
115 | ], |
||
116 | 'invalid matrix' => [ |
||
117 | 'matrix' => $mockMatrix2, |
||
118 | 'definition' => $this->getMockMatrixDefinition($mockMatrix2), |
||
0 ignored issues
–
show
$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...
|
|||
119 | 'validSave' => false, |
||
120 | ], |
||
121 | ]; |
||
122 | } |
||
123 | |||
124 | //============================================================================================================== |
||
125 | //================================================ HELPERS =================================================== |
||
126 | //============================================================================================================== |
||
127 | |||
128 | /** |
||
129 | * @param MatrixField $mockMatrix |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | private function getMockMatrixDefinition(MatrixField $mockMatrix) |
||
134 | { |
||
135 | return [ |
||
136 | 'class' => get_class($mockMatrix), |
||
137 | 'attributes' => [ |
||
138 | 'minBlocks' => null, |
||
139 | 'maxBlocks' => null, |
||
140 | 'localizeBlocks' => false, |
||
141 | 'name' => 'matrixName'.$mockMatrix->id, |
||
142 | 'handle' => 'matrixHandle'.$mockMatrix->id, |
||
143 | 'instructions' => null, |
||
144 | 'translationMethod' => 'none', |
||
145 | 'translationKeyFormat' => null, |
||
146 | 'oldHandle' => null, |
||
147 | 'columnPrefix' => null, |
||
148 | 'required' => false, |
||
149 | 'sortOrder' => null, |
||
150 | ], |
||
151 | 'group' => $mockMatrix->group->name, |
||
152 | 'blockTypes' => [ |
||
153 | 'blockTypeDefinition1', |
||
154 | 'blockTypeDefinition2', |
||
155 | ], |
||
156 | ]; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param int $matrixId |
||
161 | * @param int $groupId |
||
162 | * |
||
163 | * @return Mock|MatrixField |
||
164 | */ |
||
165 | private function getMockMatrix(int $matrixId, int $groupId) |
||
166 | { |
||
167 | $mockMatrix = $this->getMockBuilder(MatrixField::class) |
||
168 | ->setMethods(['getGroup', 'getBlockTypes']) |
||
169 | ->disableOriginalConstructor() |
||
170 | ->getMock(); |
||
171 | |||
172 | $mockMatrix->id = $matrixId; |
||
0 ignored issues
–
show
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
Loading history...
|
|||
173 | $mockMatrix->groupId = $matrixId; |
||
0 ignored issues
–
show
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
Loading history...
|
|||
174 | $mockMatrix->handle = 'matrixHandle'.$matrixId; |
||
0 ignored issues
–
show
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
Loading history...
|
|||
175 | $mockMatrix->name = 'matrixName'.$matrixId; |
||
0 ignored issues
–
show
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
Loading history...
|
|||
176 | |||
177 | $mockMatrix->expects($this->any()) |
||
178 | ->method('getGroup') |
||
179 | ->willReturn($this->getMockFieldGroup($groupId)); |
||
180 | |||
181 | $mockMatrix->expects($this->any()) |
||
182 | ->method('getBlockTypes') |
||
183 | ->willReturn([ |
||
184 | $this->getMockMatrixBlockType(1), |
||
185 | $this->getMockMatrixBlockType(2), |
||
186 | ]); |
||
187 | |||
188 | return $mockMatrix; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Get a mock field group. |
||
193 | * |
||
194 | * @param int $groupId |
||
195 | * |
||
196 | * @return Mock|FieldGroup |
||
197 | */ |
||
198 | private function getMockFieldGroup(int $groupId) |
||
199 | { |
||
200 | $mockGroup = $this->getMockBuilder(FieldGroup::class) |
||
201 | ->disableOriginalConstructor() |
||
202 | ->getmock(); |
||
203 | |||
204 | $mockGroup->id = $groupId; |
||
0 ignored issues
–
show
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
Loading history...
|
|||
205 | $mockGroup->name = 'fieldGroup'.$groupId; |
||
0 ignored issues
–
show
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
Loading history...
|
|||
206 | |||
207 | return $mockGroup; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Get a mock matrix block type. |
||
212 | * |
||
213 | * @param int $blockId |
||
214 | * |
||
215 | * @return Mock|MatrixBlockType |
||
216 | */ |
||
217 | private function getMockMatrixBlockType($blockId) |
||
218 | { |
||
219 | $mockBlockType = $this->getMockBuilder(MatrixBlockType::class) |
||
220 | ->disableOriginalConstructor() |
||
221 | ->getmock(); |
||
222 | |||
223 | $mockBlockType->id = $blockId; |
||
0 ignored issues
–
show
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
Loading history...
|
|||
224 | $mockBlockType->handle = 'blockHandle'.$blockId; |
||
0 ignored issues
–
show
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
Loading history...
|
|||
225 | |||
226 | return $mockBlockType; |
||
227 | } |
||
228 | } |
||
229 |
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: