Skip to content

Commit 65a3b9d

Browse files
author
Oleksii Korshenko
authored
MAGETWO-66492: Fixes #2461 #8959
2 parents ff14bcf + 983d57d commit 65a3b9d

File tree

6 files changed

+160
-78
lines changed

6 files changed

+160
-78
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
8+
use Magento\Framework\Serialize\Serializer\Json;
9+
10+
/**
11+
* Backend model for attribute that stores structures in json format
12+
*/
13+
class JsonEncoded extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
14+
{
15+
/**
16+
* @var Json
17+
*/
18+
private $jsonSerializer;
19+
20+
/**
21+
* ArrayBackend constructor.
22+
*
23+
* @param Json $jsonSerializer
24+
*/
25+
public function __construct(Json $jsonSerializer)
26+
{
27+
$this->jsonSerializer = $jsonSerializer;
28+
}
29+
30+
/**
31+
* Encode before saving
32+
*
33+
* @param \Magento\Framework\DataObject $object
34+
* @return $this
35+
*/
36+
public function beforeSave($object)
37+
{
38+
// parent::beforeSave() is not called intentionally
39+
$attrCode = $this->getAttribute()->getAttributeCode();
40+
if ($object->hasData($attrCode)) {
41+
$object->setData($attrCode, $this->jsonSerializer->serialize($object->getData($attrCode)));
42+
}
43+
return $this;
44+
}
45+
46+
/**
47+
* Decode after loading
48+
*
49+
* @param \Magento\Framework\DataObject $object
50+
* @return $this
51+
*/
52+
public function afterLoad($object)
53+
{
54+
parent::afterLoad($object);
55+
$attrCode = $this->getAttribute()->getAttributeCode();
56+
$object->setData($attrCode, $this->jsonSerializer->unserialize($object->getData($attrCode)));
57+
return $this;
58+
}
59+
}

app/code/Magento/Eav/Model/Entity/Attribute/Backend/Serialized.php

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
* Test after load handler
87+
*/
88+
public function testAfterLoad()
89+
{
90+
$product = new \Magento\Framework\DataObject(
91+
[
92+
'json_encoded' => json_encode([1, 2, 3])
93+
]
94+
);
95+
$this->model->afterLoad($product);
96+
$this->assertEquals([1, 2, 3], $product->getData('json_encoded'));
97+
}
98+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"monolog/monolog": "^1.17",
4444
"oyejorge/less.php": "~1.7.0",
4545
"pelago/emogrifier": "0.1.1",
46-
"tubalmartin/cssmin": "2.4.8-p4",
46+
"tubalmartin/cssmin": "2.4.8-p6",
4747
"magento/magento-composer-installer": ">=0.1.11",
4848
"braintree/braintree_php": "3.7.0",
4949
"symfony/console": "~2.3, !=2.7.0",

dev/tests/integration/testsuite/Magento/Framework/Composer/_files/testFromClone/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"composer/composer": "1.0.0-alpha9",
3535
"monolog/monolog": "1.11.0",
3636
"oyejorge/less.php": "1.7.0.3",
37-
"tubalmartin/cssmin": "2.4.8-p4",
37+
"tubalmartin/cssmin": "2.4.8-p6",
3838
"magento/magento-composer-installer": "*",
3939
"phpseclib/phpseclib": "~0.3",
4040
"symfony/console": "~2.3",

dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4230,4 +4230,5 @@
42304230
['Magento\Framework\Acl\Cache'],
42314231
['Magento\Framework\Acl\CacheInterface'],
42324232
['Magento\Framework\Acl\Test\Unit\CacheTest'],
4233+
['Magento\Eav\Model\Entity\Attribute\Backend\Serialized'],
42334234
];

0 commit comments

Comments
 (0)