3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Framework \Model \Test \Unit \ResourceModel ;
7
9
8
10
use Magento \Framework \DataObject ;
9
11
use Magento \Framework \DB \Adapter \AdapterInterface ;
10
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
11
12
use Magento \Framework \Serialize \Serializer \Json ;
13
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14
+ use PHPUnit \Framework \MockObject \MockObject as MockObject ;
15
+ use PHPUnit \Framework \TestCase ;
16
+ use Psr \Log \LoggerInterface ;
12
17
13
- class AbstractResourceTest extends \PHPUnit \Framework \TestCase
18
+ /**
19
+ * Test for \Magento\Framework\Model\ResourceModel\AbstractResource.
20
+ */
21
+ class AbstractResourceTest extends TestCase
14
22
{
15
23
/**
16
24
* @var AbstractResourceStub
17
25
*/
18
- private $ abstractResource ;
26
+ private $ model ;
19
27
20
28
/**
21
- * @var Json|\PHPUnit_Framework_MockObject_MockObject
29
+ * @var Json|MockObject
22
30
*/
23
31
private $ serializerMock ;
24
32
25
33
/**
26
- * @var \Psr\Log\ LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
34
+ * @var LoggerInterface|MockObject
27
35
*/
28
36
private $ loggerMock ;
29
37
38
+ /**
39
+ * @inheritdoc
40
+ */
30
41
protected function setUp ()
31
42
{
32
43
$ objectManager = new ObjectManager ($ this );
44
+ $ this ->model = $ objectManager ->getObject (AbstractResourceStub::class);
33
45
$ this ->serializerMock = $ this ->createMock (Json::class);
34
- $ this ->loggerMock = $ this ->createMock (\Psr \Log \LoggerInterface::class);
35
- $ this ->abstractResource = $ objectManager ->getObject (AbstractResourceStub::class);
36
- $ objectManager ->setBackwardCompatibleProperty ($ this ->abstractResource , 'serializer ' , $ this ->serializerMock );
37
- $ objectManager ->setBackwardCompatibleProperty ($ this ->abstractResource , '_logger ' , $ this ->loggerMock );
46
+ $ this ->loggerMock = $ this ->createMock (LoggerInterface::class);
47
+ $ objectManager ->setBackwardCompatibleProperty ($ this ->model , 'serializer ' , $ this ->serializerMock );
48
+ $ objectManager ->setBackwardCompatibleProperty ($ this ->model , '_logger ' , $ this ->loggerMock );
38
49
}
39
50
40
51
/**
52
+ * Test fields serialize
53
+ *
41
54
* @param array $arguments
42
- * @param string $expected
55
+ * @param string|null $expected
43
56
* @param array|string|int $serializeCalledWith
44
57
* @param int $numSerializeCalled
58
+ * @return void
45
59
* @dataProvider serializeFieldsDataProvider
46
60
*/
47
61
public function testSerializeFields (
48
62
array $ arguments ,
49
- $ expected ,
63
+ ? string $ expected ,
50
64
$ serializeCalledWith ,
51
- $ numSerializeCalled = 1
52
- ) {
65
+ int $ numSerializeCalled = 1
66
+ ): void {
53
67
/** @var DataObject $dataObject */
54
- list ( $ dataObject , $ field , $ defaultValue , $ unsetEmpty) = $ arguments ;
68
+ [ $ dataObject , $ field , $ defaultValue , $ unsetEmpty] = $ arguments ;
55
69
$ this ->serializerMock ->expects ($ this ->exactly ($ numSerializeCalled ))
56
70
->method ('serialize ' )
57
71
->with ($ serializeCalledWith )
58
72
->willReturn ($ expected );
59
- $ this ->abstractResource ->_serializeField ($ dataObject , $ field , $ defaultValue , $ unsetEmpty );
73
+ $ this ->model ->_serializeField ($ dataObject , $ field , $ defaultValue , $ unsetEmpty );
60
74
$ this ->assertEquals ($ expected , $ dataObject ->getData ($ field ));
61
75
}
62
76
63
77
/**
78
+ * DataProvider for testSerializeFields()
79
+ *
64
80
* @return array
65
81
*/
66
- public function serializeFieldsDataProvider ()
82
+ public function serializeFieldsDataProvider (): array
67
83
{
68
84
$ array = ['a ' , 'b ' , 'c ' ];
69
85
$ string = 'i am string ' ;
@@ -75,60 +91,66 @@ public function serializeFieldsDataProvider()
75
91
'string ' => $ string ,
76
92
'integer ' => $ integer ,
77
93
'empty ' => $ empty ,
78
- 'empty_with_default ' => ''
94
+ 'empty_with_default ' => '' ,
79
95
]
80
96
);
97
+
81
98
return [
82
99
[
83
100
[$ dataObject , 'array ' , null , false ],
84
101
'["a","b","c"] ' ,
85
- $ array
102
+ $ array,
86
103
],
87
104
[
88
105
[$ dataObject , 'string ' , null , false ],
89
106
'"i am string" ' ,
90
- $ string
107
+ $ string,
91
108
],
92
109
[
93
110
[$ dataObject , 'integer ' , null , false ],
94
111
'969 ' ,
95
- $ integer
112
+ $ integer,
96
113
],
97
114
[
98
115
[$ dataObject , 'empty ' , null , true ],
99
116
null ,
100
117
$ empty ,
101
- 0
118
+ 0 ,
102
119
],
103
120
[
104
121
[$ dataObject , 'empty_with_default ' , 'default ' , false ],
105
122
'"default" ' ,
106
- 'default '
107
- ]
123
+ 'default ' ,
124
+ ],
108
125
];
109
126
}
110
127
111
128
/**
129
+ * Test fields unserialize
130
+ *
112
131
* @param array $arguments
113
132
* @param array|string|int|boolean $expected
133
+ * @return void
114
134
* @dataProvider unserializeFieldsDataProvider
115
135
*/
116
- public function testUnserializeFields (array $ arguments , $ expected )
136
+ public function testUnserializeFields (array $ arguments , $ expected ): void
117
137
{
118
138
/** @var DataObject $dataObject */
119
- list ( $ dataObject , $ field , $ defaultValue) = $ arguments ;
139
+ [ $ dataObject , $ field , $ defaultValue] = $ arguments ;
120
140
$ this ->serializerMock ->expects ($ this ->once ())
121
141
->method ('unserialize ' )
122
142
->with ($ dataObject ->getData ($ field ))
123
143
->willReturn ($ expected );
124
- $ this ->abstractResource ->_unserializeField ($ dataObject , $ field , $ defaultValue );
144
+ $ this ->model ->_unserializeField ($ dataObject , $ field , $ defaultValue );
125
145
$ this ->assertEquals ($ expected , $ dataObject ->getData ($ field ));
126
146
}
127
147
128
148
/**
149
+ * DataProvider for testUnserializeFields()
150
+ *
129
151
* @return array
130
152
*/
131
- public function unserializeFieldsDataProvider ()
153
+ public function unserializeFieldsDataProvider (): array
132
154
{
133
155
$ dataObject = new DataObject (
134
156
[
@@ -137,54 +159,60 @@ public function unserializeFieldsDataProvider()
137
159
'integer ' => '969 ' ,
138
160
'empty_with_default ' => '"" ' ,
139
161
'not_serialized_string ' => 'i am string ' ,
140
- 'serialized_boolean_false ' => 'false '
162
+ 'serialized_boolean_false ' => 'false ' ,
141
163
]
142
164
);
165
+
143
166
return [
144
167
[
145
168
[$ dataObject , 'array ' , null ],
146
- ['a ' , 'b ' , 'c ' ]
169
+ ['a ' , 'b ' , 'c ' ],
147
170
],
148
171
[
149
172
[$ dataObject , 'string ' , null ],
150
- 'i am string '
173
+ 'i am string ' ,
151
174
],
152
175
[
153
176
[$ dataObject , 'integer ' , null ],
154
- 969
177
+ 969 ,
155
178
],
156
179
[
157
180
[$ dataObject , 'empty_with_default ' , 'default ' , false ],
158
- 'default '
181
+ 'default ' ,
159
182
],
160
183
[
161
184
[$ dataObject , 'not_serialized_string ' , null ],
162
- 'i am string '
185
+ 'i am string ' ,
163
186
],
164
187
[
165
188
[$ dataObject , 'serialized_boolean_false ' , null ],
166
189
false ,
167
- ]
190
+ ],
168
191
];
169
192
}
170
-
171
- public function testCommitZeroLevel ()
193
+
194
+ /**
195
+ * Commit zero level
196
+ *
197
+ * @return void
198
+ */
199
+ public function testCommitZeroLevel (): void
172
200
{
173
- /** @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject $connection */
201
+ /** @var AdapterInterface|MockObject $connection */
174
202
$ connection = $ this ->createMock (AdapterInterface::class);
175
- /** @var DataObject|\PHPUnit_Framework_MockObject_MockObject $closureExpectation */
203
+ /** @var DataObject|MockObject $closureExpectation */
176
204
$ closureExpectation = $ this ->getMockBuilder (DataObject::class)
177
205
->disableOriginalConstructor ()
178
206
->getMock ();
179
207
180
- $ this ->abstractResource ->setConnection ($ connection );
181
- $ this ->abstractResource ->addCommitCallback (
208
+ $ this ->model ->setConnection ($ connection );
209
+ $ this ->model ->addCommitCallback (
182
210
function () use ($ closureExpectation ) {
183
211
$ closureExpectation ->setData (1 );
184
212
}
185
213
);
186
214
187
- $ this ->abstractResource ->addCommitCallback (
215
+ $ this ->model ->addCommitCallback (
188
216
function () use ($ closureExpectation ) {
189
217
$ closureExpectation ->getData ();
190
218
}
@@ -201,16 +229,21 @@ function () use ($closureExpectation) {
201
229
$ closureExpectation ->expects ($ this ->once ())
202
230
->method ('getData ' );
203
231
204
- $ this ->abstractResource ->commit ();
232
+ $ this ->model ->commit ();
205
233
}
206
234
207
- public function testCommitZeroLevelCallbackException ()
235
+ /**
236
+ * Commit zero level callback with exception
237
+ *
238
+ * @return void
239
+ */
240
+ public function testCommitZeroLevelCallbackException (): void
208
241
{
209
242
/** @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject $connection */
210
243
$ connection = $ this ->createMock (AdapterInterface::class);
211
244
212
- $ this ->abstractResource ->setConnection ($ connection );
213
- $ this ->abstractResource ->addCommitCallback (
245
+ $ this ->model ->setConnection ($ connection );
246
+ $ this ->model ->addCommitCallback (
214
247
function () {
215
248
throw new \Exception ();
216
249
}
@@ -224,20 +257,25 @@ function () {
224
257
$ this ->loggerMock ->expects ($ this ->once ())
225
258
->method ('critical ' );
226
259
227
- $ this ->abstractResource ->commit ();
260
+ $ this ->model ->commit ();
228
261
}
229
262
230
- public function testCommitNotCompletedTransaction ()
263
+ /**
264
+ * Commit of transactions that have not been completed
265
+ *
266
+ * @return void
267
+ */
268
+ public function testCommitNotCompletedTransaction (): void
231
269
{
232
- /** @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject $connection */
270
+ /** @var AdapterInterface|MockObject $connection */
233
271
$ connection = $ this ->createMock (AdapterInterface::class);
234
- /** @var DataObject|\PHPUnit_Framework_MockObject_MockObject $closureExpectation */
272
+ /** @var DataObject|MockObject $closureExpectation */
235
273
$ closureExpectation = $ this ->getMockBuilder (DataObject::class)
236
274
->disableOriginalConstructor ()
237
275
->getMock ();
238
276
239
- $ this ->abstractResource ->setConnection ($ connection );
240
- $ this ->abstractResource ->addCommitCallback (
277
+ $ this ->model ->setConnection ($ connection );
278
+ $ this ->model ->addCommitCallback (
241
279
function () use ($ closureExpectation ) {
242
280
$ closureExpectation ->setData (1 );
243
281
}
@@ -253,6 +291,47 @@ function () use ($closureExpectation) {
253
291
->method ('setData ' )
254
292
->with (1 );
255
293
256
- $ this ->abstractResource ->commit ();
294
+ $ this ->model ->commit ();
295
+ }
296
+
297
+ /**
298
+ * Test commit case when first callback throws an exception but other callbacks will be called
299
+ *
300
+ * @return void
301
+ */
302
+ public function testCommitFewCallbacksWithException (): void
303
+ {
304
+ /** @var AdapterInterface|MockObject $connection */
305
+ $ connection = $ this ->createMock (AdapterInterface::class);
306
+
307
+ /** @var DataObject|MockObject $closureExpectation */
308
+ $ closureExpectation = $ this ->getMockBuilder (DataObject::class)
309
+ ->disableOriginalConstructor ()
310
+ ->getMock ();
311
+
312
+ $ this ->model ->setConnection ($ connection );
313
+ $ this ->model ->addCommitCallback (
314
+ function () {
315
+ throw new \Exception ();
316
+ }
317
+ );
318
+
319
+ $ this ->model ->addCommitCallback (
320
+ function () use ($ closureExpectation ) {
321
+ $ closureExpectation ->getData ();
322
+ }
323
+ );
324
+
325
+ $ connection ->expects ($ this ->once ())
326
+ ->method ('commit ' );
327
+ $ connection ->expects ($ this ->once ())
328
+ ->method ('getTransactionLevel ' )
329
+ ->willReturn (0 );
330
+ $ this ->loggerMock ->expects ($ this ->once ())
331
+ ->method ('critical ' );
332
+ $ closureExpectation ->expects ($ this ->once ())
333
+ ->method ('getData ' );
334
+
335
+ $ this ->model ->commit ();
257
336
}
258
337
}
0 commit comments