Skip to content

Commit 188e2c4

Browse files
committed
MAGETWO-66120: Create replacement for Magento\Eav\Model\Entity\Attribute\Backend\Serialized class
1 parent 6027e61 commit 188e2c4

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Eav\Model\Entity\Attribute\Backend;
7+
use Magento\Framework\Serialize\Serializer\Json;
8+
9+
/**
10+
* Backend model for attribute that stores structures in json format
11+
*/
12+
class JsonEncoded extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
13+
{
14+
/**
15+
* @var Json
16+
*/
17+
private $jsonSerializer;
18+
19+
/**
20+
* ArrayBackend constructor.
21+
*
22+
* @param Json $jsonSerializer
23+
*/
24+
public function __construct(Json $jsonSerializer)
25+
{
26+
$this->jsonSerializer = $jsonSerializer;
27+
}
28+
29+
/**
30+
* Encode before saving
31+
*
32+
* @param \Magento\Framework\DataObject $object
33+
* @return $this
34+
*/
35+
public function beforeSave($object)
36+
{
37+
// parent::beforeSave() is not called intentionally
38+
$attrCode = $this->getAttribute()->getAttributeCode();
39+
if ($object->hasData($attrCode)) {
40+
$object->setData($attrCode, $this->jsonSerializer->serialize($object->getData($attrCode)));
41+
}
42+
return $this;
43+
}
44+
45+
/**
46+
* Decode after loading
47+
*
48+
* @param \Magento\Framework\DataObject $object
49+
* @return $this
50+
*/
51+
public function afterLoad($object)
52+
{
53+
parent::afterLoad($object);
54+
$attrCode = $this->getAttribute()->getAttributeCode();
55+
$object->setData($attrCode, $this->jsonSerializer->unserialize($object->getData($attrCode)));
56+
return $this;
57+
}
58+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Eav\Test\Unit\Model\Entity\Attribute\Backend;
8+
9+
use Magento\Eav\Model\Entity\Attribute\Backend\JsonEncoded;
10+
11+
class JsonEncodedTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var \Magento\Eav\Model\Entity\Attribute\Backend\JsonEncoded
15+
*/
16+
private $model;
17+
18+
/**
19+
* @var \Magento\Eav\Model\Entity\Attribute|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
private $attributeMock;
22+
23+
/**
24+
* @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $serializerMock;
27+
28+
/**
29+
* Set up before test
30+
*/
31+
protected function setUp()
32+
{
33+
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
34+
->disableOriginalConstructor()
35+
->setMethods(['serialize', 'unserialize'])
36+
->getMock();
37+
38+
$this->serializerMock->expects($this->any())
39+
->method('serialize')
40+
->will(
41+
$this->returnCallback(
42+
function ($value) {
43+
return json_encode($value);
44+
}
45+
)
46+
);
47+
48+
$this->serializerMock->expects($this->any())
49+
->method('unserialize')
50+
->will(
51+
$this->returnCallback(
52+
function ($value) {
53+
return json_decode($value, true);
54+
}
55+
)
56+
);
57+
58+
$this->attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
59+
->disableOriginalConstructor()
60+
->setMethods(['getAttributeCode'])
61+
->getMock();
62+
63+
$this->attributeMock->expects($this->any())
64+
->method('getAttributeCode')
65+
->will($this->returnValue('json_encoded'));
66+
67+
$this->model = new JsonEncoded($this->serializerMock);
68+
$this->model->setAttribute($this->attributeMock);
69+
}
70+
71+
/**
72+
* Test before save handler
73+
*/
74+
public function testBeforeSave()
75+
{
76+
$product = new \Magento\Framework\DataObject(
77+
[
78+
'json_encoded' => [1, 2, 3]
79+
]
80+
);
81+
$this->model->beforeSave($product);
82+
$this->assertEquals(json_encode([1, 2, 3]), $product->getData('json_encoded'));
83+
}
84+
85+
86+
/**
87+
* Test after load handler
88+
*/
89+
public function testAfterLoad()
90+
{
91+
$product = new \Magento\Framework\DataObject(
92+
[
93+
'json_encoded' => json_encode([1, 2, 3])
94+
]
95+
);
96+
$this->model->afterLoad($product);
97+
$this->assertEquals([1, 2, 3], $product->getData('json_encoded'));
98+
}
99+
}

0 commit comments

Comments
 (0)