-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #939 from magento-troll/MAGETWO-65182
MAGETWO-65182: Remove \Magento\Eav\Model\Entity\Attribute\Backend\Serialized class
- Loading branch information
Showing
4 changed files
with
158 additions
and
76 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
app/code/Magento/Eav/Model/Entity/Attribute/Backend/JsonEncoded.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
76
app/code/Magento/Eav/Model/Entity/Attribute/Backend/Serialized.php
This file was deleted.
Oops, something went wrong.
98 changes: 98 additions & 0 deletions
98
app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Backend/JsonEncodedTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters