Skip to content

Commit

Permalink
Merge pull request #939 from magento-troll/MAGETWO-65182
Browse files Browse the repository at this point in the history
MAGETWO-65182: Remove \Magento\Eav\Model\Entity\Attribute\Backend\Serialized class
  • Loading branch information
rganin authored Mar 21, 2017
2 parents 2613378 + bee7998 commit a4b34fe
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model\Entity\Attribute\Backend;

use Magento\Framework\Serialize\Serializer\Json;

/**
* Backend model for attribute that stores structures in json format
*/
class JsonEncoded extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
{
/**
* @var Json
*/
private $jsonSerializer;

/**
* ArrayBackend constructor.
*
* @param Json $jsonSerializer
*/
public function __construct(Json $jsonSerializer)
{
$this->jsonSerializer = $jsonSerializer;
}

/**
* Encode before saving
*
* @param \Magento\Framework\DataObject $object
* @return $this
*/
public function beforeSave($object)
{
// parent::beforeSave() is not called intentionally
$attrCode = $this->getAttribute()->getAttributeCode();
if ($object->hasData($attrCode)) {
$object->setData($attrCode, $this->jsonSerializer->serialize($object->getData($attrCode)));
}
return $this;
}

/**
* Decode after loading
*
* @param \Magento\Framework\DataObject $object
* @return $this
*/
public function afterLoad($object)
{
parent::afterLoad($object);
$attrCode = $this->getAttribute()->getAttributeCode();
$object->setData($attrCode, $this->jsonSerializer->unserialize($object->getData($attrCode)));
return $this;
}
}
76 changes: 0 additions & 76 deletions app/code/Magento/Eav/Model/Entity/Attribute/Backend/Serialized.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Eav\Test\Unit\Model\Entity\Attribute\Backend;

use Magento\Eav\Model\Entity\Attribute\Backend\JsonEncoded;

class JsonEncodedTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Eav\Model\Entity\Attribute\Backend\JsonEncoded
*/
private $model;

/**
* @var \Magento\Eav\Model\Entity\Attribute|\PHPUnit_Framework_MockObject_MockObject
*/
private $attributeMock;

/**
* @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject
*/
private $serializerMock;

/**
* Set up before test
*/
protected function setUp()
{
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
->disableOriginalConstructor()
->setMethods(['serialize', 'unserialize'])
->getMock();

$this->serializerMock->expects($this->any())
->method('serialize')
->will(
$this->returnCallback(
function ($value) {
return json_encode($value);
}
)
);

$this->serializerMock->expects($this->any())
->method('unserialize')
->will(
$this->returnCallback(
function ($value) {
return json_decode($value, true);
}
)
);

$this->attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
->disableOriginalConstructor()
->setMethods(['getAttributeCode'])
->getMock();

$this->attributeMock->expects($this->any())
->method('getAttributeCode')
->will($this->returnValue('json_encoded'));

$this->model = new JsonEncoded($this->serializerMock);
$this->model->setAttribute($this->attributeMock);
}

/**
* Test before save handler
*/
public function testBeforeSave()
{
$product = new \Magento\Framework\DataObject(
[
'json_encoded' => [1, 2, 3]
]
);
$this->model->beforeSave($product);
$this->assertEquals(json_encode([1, 2, 3]), $product->getData('json_encoded'));
}

/**
* Test after load handler
*/
public function testAfterLoad()
{
$product = new \Magento\Framework\DataObject(
[
'json_encoded' => json_encode([1, 2, 3])
]
);
$this->model->afterLoad($product);
$this->assertEquals([1, 2, 3], $product->getData('json_encoded'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4230,4 +4230,5 @@
['Magento\Framework\Acl\Cache'],
['Magento\Framework\Acl\CacheInterface'],
['Magento\Framework\Acl\Test\Unit\CacheTest'],
['Magento\Eav\Model\Entity\Attribute\Backend\Serialized'],
];

0 comments on commit a4b34fe

Please sign in to comment.