15
15
use PHPUnit \Framework \TestCase ;
16
16
use Psr \Log \LoggerInterface ;
17
17
18
+ /**
19
+ * Test for \Magento\Framework\Model\ResourceModel\AbstractResource.
20
+ */
18
21
class AbstractResourceTest extends TestCase
19
22
{
20
23
/**
21
24
* @var AbstractResourceStub
22
25
*/
23
- private $ abstractResource ;
26
+ private $ model ;
24
27
25
28
/**
26
29
* @var Json|MockObject
@@ -32,43 +35,51 @@ class AbstractResourceTest extends TestCase
32
35
*/
33
36
private $ loggerMock ;
34
37
38
+ /**
39
+ * @inheritdoc
40
+ */
35
41
protected function setUp (): void
36
42
{
37
43
$ objectManager = new ObjectManager ($ this );
38
44
$ this ->serializerMock = $ this ->createMock (Json::class);
39
45
$ this ->loggerMock = $ this ->getMockForAbstractClass (LoggerInterface::class);
40
- $ this ->abstractResource = $ objectManager ->getObject (AbstractResourceStub::class);
41
- $ objectManager ->setBackwardCompatibleProperty ($ this ->abstractResource , 'serializer ' , $ this ->serializerMock );
42
- $ objectManager ->setBackwardCompatibleProperty ($ this ->abstractResource , '_logger ' , $ this ->loggerMock );
46
+ $ this ->model = $ objectManager ->getObject (AbstractResourceStub::class);
47
+ $ objectManager ->setBackwardCompatibleProperty ($ this ->model , 'serializer ' , $ this ->serializerMock );
48
+ $ objectManager ->setBackwardCompatibleProperty ($ this ->model , '_logger ' , $ this ->loggerMock );
43
49
}
44
50
45
51
/**
52
+ * Test fields serialize
53
+ *
46
54
* @param array $arguments
47
- * @param string $expected
55
+ * @param string|null $expected
48
56
* @param array|string|int $serializeCalledWith
49
57
* @param int $numSerializeCalled
58
+ * @return void
50
59
* @dataProvider serializeFieldsDataProvider
51
60
*/
52
61
public function testSerializeFields (
53
62
array $ arguments ,
54
- $ expected ,
63
+ ? string $ expected ,
55
64
$ serializeCalledWith ,
56
- $ numSerializeCalled = 1
57
- ) {
65
+ int $ numSerializeCalled = 1
66
+ ): void {
58
67
/** @var DataObject $dataObject */
59
- list ( $ dataObject , $ field , $ defaultValue , $ unsetEmpty) = $ arguments ;
68
+ [ $ dataObject , $ field , $ defaultValue , $ unsetEmpty] = $ arguments ;
60
69
$ this ->serializerMock ->expects ($ this ->exactly ($ numSerializeCalled ))
61
70
->method ('serialize ' )
62
71
->with ($ serializeCalledWith )
63
72
->willReturn ($ expected );
64
- $ this ->abstractResource ->_serializeField ($ dataObject , $ field , $ defaultValue , $ unsetEmpty );
73
+ $ this ->model ->_serializeField ($ dataObject , $ field , $ defaultValue , $ unsetEmpty );
65
74
$ this ->assertEquals ($ expected , $ dataObject ->getData ($ field ));
66
75
}
67
76
68
77
/**
78
+ * DataProvider for testSerializeFields()
79
+ *
69
80
* @return array
70
81
*/
71
- public function serializeFieldsDataProvider ()
82
+ public function serializeFieldsDataProvider (): array
72
83
{
73
84
$ array = ['a ' , 'b ' , 'c ' ];
74
85
$ string = 'i am string ' ;
@@ -80,60 +91,66 @@ public function serializeFieldsDataProvider()
80
91
'string ' => $ string ,
81
92
'integer ' => $ integer ,
82
93
'empty ' => $ empty ,
83
- 'empty_with_default ' => ''
94
+ 'empty_with_default ' => '' ,
84
95
]
85
96
);
97
+
86
98
return [
87
99
[
88
100
[$ dataObject , 'array ' , null , false ],
89
101
'["a","b","c"] ' ,
90
- $ array
102
+ $ array,
91
103
],
92
104
[
93
105
[$ dataObject , 'string ' , null , false ],
94
106
'"i am string" ' ,
95
- $ string
107
+ $ string,
96
108
],
97
109
[
98
110
[$ dataObject , 'integer ' , null , false ],
99
111
'969 ' ,
100
- $ integer
112
+ $ integer,
101
113
],
102
114
[
103
115
[$ dataObject , 'empty ' , null , true ],
104
116
null ,
105
117
$ empty ,
106
- 0
118
+ 0 ,
107
119
],
108
120
[
109
121
[$ dataObject , 'empty_with_default ' , 'default ' , false ],
110
122
'"default" ' ,
111
- 'default '
112
- ]
123
+ 'default ' ,
124
+ ],
113
125
];
114
126
}
115
127
116
128
/**
129
+ * Test fields unserialize
130
+ *
117
131
* @param array $arguments
118
132
* @param array|string|int|boolean $expected
133
+ * @return void
119
134
* @dataProvider unserializeFieldsDataProvider
120
135
*/
121
- public function testUnserializeFields (array $ arguments , $ expected )
136
+ public function testUnserializeFields (array $ arguments , $ expected ): void
122
137
{
123
138
/** @var DataObject $dataObject */
124
- list ( $ dataObject , $ field , $ defaultValue) = $ arguments ;
139
+ [ $ dataObject , $ field , $ defaultValue] = $ arguments ;
125
140
$ this ->serializerMock ->expects ($ this ->once ())
126
141
->method ('unserialize ' )
127
142
->with ($ dataObject ->getData ($ field ))
128
143
->willReturn ($ expected );
129
- $ this ->abstractResource ->_unserializeField ($ dataObject , $ field , $ defaultValue );
144
+ $ this ->model ->_unserializeField ($ dataObject , $ field , $ defaultValue );
130
145
$ this ->assertEquals ($ expected , $ dataObject ->getData ($ field ));
131
146
}
132
147
133
148
/**
149
+ * DataProvider for testUnserializeFields()
150
+ *
134
151
* @return array
135
152
*/
136
- public function unserializeFieldsDataProvider ()
153
+ public function unserializeFieldsDataProvider (): array
137
154
{
138
155
$ dataObject = new DataObject (
139
156
[
@@ -142,38 +159,44 @@ public function unserializeFieldsDataProvider()
142
159
'integer ' => '969 ' ,
143
160
'empty_with_default ' => '"" ' ,
144
161
'not_serialized_string ' => 'i am string ' ,
145
- 'serialized_boolean_false ' => 'false '
162
+ 'serialized_boolean_false ' => 'false ' ,
146
163
]
147
164
);
165
+
148
166
return [
149
167
[
150
168
[$ dataObject , 'array ' , null ],
151
- ['a ' , 'b ' , 'c ' ]
169
+ ['a ' , 'b ' , 'c ' ],
152
170
],
153
171
[
154
172
[$ dataObject , 'string ' , null ],
155
- 'i am string '
173
+ 'i am string ' ,
156
174
],
157
175
[
158
176
[$ dataObject , 'integer ' , null ],
159
- 969
177
+ 969 ,
160
178
],
161
179
[
162
180
[$ dataObject , 'empty_with_default ' , 'default ' , false ],
163
- 'default '
181
+ 'default ' ,
164
182
],
165
183
[
166
184
[$ dataObject , 'not_serialized_string ' , null ],
167
- 'i am string '
185
+ 'i am string ' ,
168
186
],
169
187
[
170
188
[$ dataObject , 'serialized_boolean_false ' , null ],
171
189
false ,
172
- ]
190
+ ],
173
191
];
174
192
}
175
193
176
- public function testCommitZeroLevel ()
194
+ /**
195
+ * Commit zero level
196
+ *
197
+ * @return void
198
+ */
199
+ public function testCommitZeroLevel (): void
177
200
{
178
201
/** @var AdapterInterface|MockObject $connection */
179
202
$ connection = $ this ->getMockForAbstractClass (AdapterInterface::class);
@@ -182,14 +205,14 @@ public function testCommitZeroLevel()
182
205
->disableOriginalConstructor ()
183
206
->getMock ();
184
207
185
- $ this ->abstractResource ->setConnection ($ connection );
186
- $ this ->abstractResource ->addCommitCallback (
208
+ $ this ->model ->setConnection ($ connection );
209
+ $ this ->model ->addCommitCallback (
187
210
function () use ($ closureExpectation ) {
188
211
$ closureExpectation ->setData (1 );
189
212
}
190
213
);
191
214
192
- $ this ->abstractResource ->addCommitCallback (
215
+ $ this ->model ->addCommitCallback (
193
216
function () use ($ closureExpectation ) {
194
217
$ closureExpectation ->getData ();
195
218
}
@@ -206,16 +229,21 @@ function () use ($closureExpectation) {
206
229
$ closureExpectation ->expects ($ this ->once ())
207
230
->method ('getData ' );
208
231
209
- $ this ->abstractResource ->commit ();
232
+ $ this ->model ->commit ();
210
233
}
211
234
212
- public function testCommitZeroLevelCallbackException ()
235
+ /**
236
+ * Commit zero level callback with exception
237
+ *
238
+ * @return void
239
+ */
240
+ public function testCommitZeroLevelCallbackException (): void
213
241
{
214
242
/** @var AdapterInterface|MockObject $connection */
215
243
$ connection = $ this ->getMockForAbstractClass (AdapterInterface::class);
216
244
217
- $ this ->abstractResource ->setConnection ($ connection );
218
- $ this ->abstractResource ->addCommitCallback (
245
+ $ this ->model ->setConnection ($ connection );
246
+ $ this ->model ->addCommitCallback (
219
247
function () {
220
248
throw new \Exception ();
221
249
}
@@ -229,10 +257,15 @@ function () {
229
257
$ this ->loggerMock ->expects ($ this ->once ())
230
258
->method ('critical ' );
231
259
232
- $ this ->abstractResource ->commit ();
260
+ $ this ->model ->commit ();
233
261
}
234
262
235
- public function testCommitNotCompletedTransaction ()
263
+ /**
264
+ * Commit of transactions that have not been completed
265
+ *
266
+ * @return void
267
+ */
268
+ public function testCommitNotCompletedTransaction (): void
236
269
{
237
270
/** @var AdapterInterface|MockObject $connection */
238
271
$ connection = $ this ->getMockForAbstractClass (AdapterInterface::class);
@@ -241,8 +274,8 @@ public function testCommitNotCompletedTransaction()
241
274
->disableOriginalConstructor ()
242
275
->getMock ();
243
276
244
- $ this ->abstractResource ->setConnection ($ connection );
245
- $ this ->abstractResource ->addCommitCallback (
277
+ $ this ->model ->setConnection ($ connection );
278
+ $ this ->model ->addCommitCallback (
246
279
function () use ($ closureExpectation ) {
247
280
$ closureExpectation ->setData (1 );
248
281
}
@@ -258,6 +291,47 @@ function () use ($closureExpectation) {
258
291
->method ('setData ' )
259
292
->with (1 );
260
293
261
- $ 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 ();
262
336
}
263
337
}
0 commit comments