Skip to content

Commit 2ef402a

Browse files
fix static, test coverage
1 parent 3658a1b commit 2ef402a

File tree

2 files changed

+138
-57
lines changed

2 files changed

+138
-57
lines changed

lib/internal/Magento/Framework/Model/ResourceModel/AbstractResource.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
/**
1414
* Abstract resource model
1515
*
16+
* phpcs:disable Magento2.Classes.AbstractApi
1617
* @api
1718
*/
1819
abstract class AbstractResource
@@ -93,13 +94,14 @@ public function commit()
9394
if ($this->getConnection()->getTransactionLevel() === 0) {
9495
$callbacks = CallbackPool::get(spl_object_hash($this->getConnection()));
9596
foreach ($callbacks as $callback) {
96-
try {
97-
call_user_func($callback);
98-
} catch (\Exception $e) {
99-
$this->getLogger()->critical($e);
100-
}
97+
try {
98+
call_user_func($callback);
99+
} catch (\Exception $e) {
100+
$this->getLogger()->critical($e);
101+
}
101102
}
102103
}
104+
103105
return $this;
104106
}
105107

lib/internal/Magento/Framework/Model/Test/Unit/ResourceModel/AbstractResourceTest.php

+131-52
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,83 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Framework\Model\Test\Unit\ResourceModel;
79

810
use Magento\Framework\DataObject;
911
use Magento\Framework\DB\Adapter\AdapterInterface;
10-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1112
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;
1217

13-
class AbstractResourceTest extends \PHPUnit\Framework\TestCase
18+
/**
19+
* Test for \Magento\Framework\Model\ResourceModel\AbstractResource.
20+
*/
21+
class AbstractResourceTest extends TestCase
1422
{
1523
/**
1624
* @var AbstractResourceStub
1725
*/
18-
private $abstractResource;
26+
private $model;
1927

2028
/**
21-
* @var Json|\PHPUnit_Framework_MockObject_MockObject
29+
* @var Json|MockObject
2230
*/
2331
private $serializerMock;
2432

2533
/**
26-
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
34+
* @var LoggerInterface|MockObject
2735
*/
2836
private $loggerMock;
2937

38+
/**
39+
* @inheritdoc
40+
*/
3041
protected function setUp()
3142
{
3243
$objectManager = new ObjectManager($this);
44+
$this->model = $objectManager->getObject(AbstractResourceStub::class);
3345
$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);
3849
}
3950

4051
/**
52+
* Test fields serialize
53+
*
4154
* @param array $arguments
42-
* @param string $expected
55+
* @param string|null $expected
4356
* @param array|string|int $serializeCalledWith
4457
* @param int $numSerializeCalled
58+
* @return void
4559
* @dataProvider serializeFieldsDataProvider
4660
*/
4761
public function testSerializeFields(
4862
array $arguments,
49-
$expected,
63+
?string $expected,
5064
$serializeCalledWith,
51-
$numSerializeCalled = 1
52-
) {
65+
int $numSerializeCalled = 1
66+
): void {
5367
/** @var DataObject $dataObject */
54-
list($dataObject, $field, $defaultValue, $unsetEmpty) = $arguments;
68+
[$dataObject, $field, $defaultValue, $unsetEmpty] = $arguments;
5569
$this->serializerMock->expects($this->exactly($numSerializeCalled))
5670
->method('serialize')
5771
->with($serializeCalledWith)
5872
->willReturn($expected);
59-
$this->abstractResource->_serializeField($dataObject, $field, $defaultValue, $unsetEmpty);
73+
$this->model->_serializeField($dataObject, $field, $defaultValue, $unsetEmpty);
6074
$this->assertEquals($expected, $dataObject->getData($field));
6175
}
6276

6377
/**
78+
* DataProvider for testSerializeFields()
79+
*
6480
* @return array
6581
*/
66-
public function serializeFieldsDataProvider()
82+
public function serializeFieldsDataProvider(): array
6783
{
6884
$array = ['a', 'b', 'c'];
6985
$string = 'i am string';
@@ -75,60 +91,66 @@ public function serializeFieldsDataProvider()
7591
'string' => $string,
7692
'integer' => $integer,
7793
'empty' => $empty,
78-
'empty_with_default' => ''
94+
'empty_with_default' => '',
7995
]
8096
);
97+
8198
return [
8299
[
83100
[$dataObject, 'array', null, false],
84101
'["a","b","c"]',
85-
$array
102+
$array,
86103
],
87104
[
88105
[$dataObject, 'string', null, false],
89106
'"i am string"',
90-
$string
107+
$string,
91108
],
92109
[
93110
[$dataObject, 'integer', null, false],
94111
'969',
95-
$integer
112+
$integer,
96113
],
97114
[
98115
[$dataObject, 'empty', null, true],
99116
null,
100117
$empty,
101-
0
118+
0,
102119
],
103120
[
104121
[$dataObject, 'empty_with_default', 'default', false],
105122
'"default"',
106-
'default'
107-
]
123+
'default',
124+
],
108125
];
109126
}
110127

111128
/**
129+
* Test fields unserialize
130+
*
112131
* @param array $arguments
113132
* @param array|string|int|boolean $expected
133+
* @return void
114134
* @dataProvider unserializeFieldsDataProvider
115135
*/
116-
public function testUnserializeFields(array $arguments, $expected)
136+
public function testUnserializeFields(array $arguments, $expected): void
117137
{
118138
/** @var DataObject $dataObject */
119-
list($dataObject, $field, $defaultValue) = $arguments;
139+
[$dataObject, $field, $defaultValue] = $arguments;
120140
$this->serializerMock->expects($this->once())
121141
->method('unserialize')
122142
->with($dataObject->getData($field))
123143
->willReturn($expected);
124-
$this->abstractResource->_unserializeField($dataObject, $field, $defaultValue);
144+
$this->model->_unserializeField($dataObject, $field, $defaultValue);
125145
$this->assertEquals($expected, $dataObject->getData($field));
126146
}
127147

128148
/**
149+
* DataProvider for testUnserializeFields()
150+
*
129151
* @return array
130152
*/
131-
public function unserializeFieldsDataProvider()
153+
public function unserializeFieldsDataProvider(): array
132154
{
133155
$dataObject = new DataObject(
134156
[
@@ -137,54 +159,60 @@ public function unserializeFieldsDataProvider()
137159
'integer' => '969',
138160
'empty_with_default' => '""',
139161
'not_serialized_string' => 'i am string',
140-
'serialized_boolean_false' => 'false'
162+
'serialized_boolean_false' => 'false',
141163
]
142164
);
165+
143166
return [
144167
[
145168
[$dataObject, 'array', null],
146-
['a', 'b', 'c']
169+
['a', 'b', 'c'],
147170
],
148171
[
149172
[$dataObject, 'string', null],
150-
'i am string'
173+
'i am string',
151174
],
152175
[
153176
[$dataObject, 'integer', null],
154-
969
177+
969,
155178
],
156179
[
157180
[$dataObject, 'empty_with_default', 'default', false],
158-
'default'
181+
'default',
159182
],
160183
[
161184
[$dataObject, 'not_serialized_string', null],
162-
'i am string'
185+
'i am string',
163186
],
164187
[
165188
[$dataObject, 'serialized_boolean_false', null],
166189
false,
167-
]
190+
],
168191
];
169192
}
170-
171-
public function testCommitZeroLevel()
193+
194+
/**
195+
* Commit zero level
196+
*
197+
* @return void
198+
*/
199+
public function testCommitZeroLevel(): void
172200
{
173-
/** @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject $connection */
201+
/** @var AdapterInterface|MockObject $connection */
174202
$connection = $this->createMock(AdapterInterface::class);
175-
/** @var DataObject|\PHPUnit_Framework_MockObject_MockObject $closureExpectation */
203+
/** @var DataObject|MockObject $closureExpectation */
176204
$closureExpectation = $this->getMockBuilder(DataObject::class)
177205
->disableOriginalConstructor()
178206
->getMock();
179207

180-
$this->abstractResource->setConnection($connection);
181-
$this->abstractResource->addCommitCallback(
208+
$this->model->setConnection($connection);
209+
$this->model->addCommitCallback(
182210
function () use ($closureExpectation) {
183211
$closureExpectation->setData(1);
184212
}
185213
);
186214

187-
$this->abstractResource->addCommitCallback(
215+
$this->model->addCommitCallback(
188216
function () use ($closureExpectation) {
189217
$closureExpectation->getData();
190218
}
@@ -201,16 +229,21 @@ function () use ($closureExpectation) {
201229
$closureExpectation->expects($this->once())
202230
->method('getData');
203231

204-
$this->abstractResource->commit();
232+
$this->model->commit();
205233
}
206234

207-
public function testCommitZeroLevelCallbackException()
235+
/**
236+
* Commit zero level callback with exception
237+
*
238+
* @return void
239+
*/
240+
public function testCommitZeroLevelCallbackException(): void
208241
{
209242
/** @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject $connection */
210243
$connection = $this->createMock(AdapterInterface::class);
211244

212-
$this->abstractResource->setConnection($connection);
213-
$this->abstractResource->addCommitCallback(
245+
$this->model->setConnection($connection);
246+
$this->model->addCommitCallback(
214247
function () {
215248
throw new \Exception();
216249
}
@@ -224,20 +257,25 @@ function () {
224257
$this->loggerMock->expects($this->once())
225258
->method('critical');
226259

227-
$this->abstractResource->commit();
260+
$this->model->commit();
228261
}
229262

230-
public function testCommitNotCompletedTransaction()
263+
/**
264+
* Commit of transactions that have not been completed
265+
*
266+
* @return void
267+
*/
268+
public function testCommitNotCompletedTransaction(): void
231269
{
232-
/** @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject $connection */
270+
/** @var AdapterInterface|MockObject $connection */
233271
$connection = $this->createMock(AdapterInterface::class);
234-
/** @var DataObject|\PHPUnit_Framework_MockObject_MockObject $closureExpectation */
272+
/** @var DataObject|MockObject $closureExpectation */
235273
$closureExpectation = $this->getMockBuilder(DataObject::class)
236274
->disableOriginalConstructor()
237275
->getMock();
238276

239-
$this->abstractResource->setConnection($connection);
240-
$this->abstractResource->addCommitCallback(
277+
$this->model->setConnection($connection);
278+
$this->model->addCommitCallback(
241279
function () use ($closureExpectation) {
242280
$closureExpectation->setData(1);
243281
}
@@ -253,6 +291,47 @@ function () use ($closureExpectation) {
253291
->method('setData')
254292
->with(1);
255293

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();
257336
}
258337
}

0 commit comments

Comments
 (0)