Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SerializedTest.php
Go to the documentation of this file.
1 <?php
7 
12 
13 class SerializedTest extends \PHPUnit\Framework\TestCase
14 {
16  private $serializedConfig;
17 
19  private $serializerMock;
20 
21  protected function setUp()
22  {
23  $objectManager = new ObjectManager($this);
24  $this->serializerMock = $this->createMock(Json::class);
25  $contextMock = $this->createMock(Context::class);
26  $eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
27  $contextMock->method('getEventDispatcher')
28  ->willReturn($eventManagerMock);
29  $this->serializedConfig = $objectManager->getObject(
30  Serialized::class,
31  [
32  'serializer' => $this->serializerMock,
33  'context' => $contextMock,
34  ]
35  );
36  }
37 
45  public function testAfterLoad($expected, $value, $numCalls, $unserializedValue = null)
46  {
47  $this->serializedConfig->setValue($value);
48  $this->serializerMock->expects($this->exactly($numCalls))
49  ->method('unserialize')
50  ->willReturn($unserializedValue);
51  $this->serializedConfig->afterLoad();
52  $this->assertEquals($expected, $this->serializedConfig->getValue());
53  }
54 
58  public function afterLoadDataProvider()
59  {
60  return [
61  'empty value' => [
62  false,
63  '',
64  0,
65  ],
66  'value' => [
67  ['string array'],
68  'string array',
69  1,
70  ['string array']
71  ]
72  ];
73  }
74 
82  public function testBeforeSave($expected, $value, $numCalls, $serializedValue = null)
83  {
84  $this->serializedConfig->setId('id');
85  $this->serializedConfig->setValue($value);
86  $this->serializerMock->expects($this->exactly($numCalls))
87  ->method('serialize')
88  ->willReturn($serializedValue);
89  $this->serializedConfig->beforeSave();
90  $this->assertEquals($expected, $this->serializedConfig->getValue());
91  }
92 
96  public function beforeSaveDataProvider()
97  {
98  return [
99  'string' => [
100  'string',
101  'string',
102  0,
103  ],
104  'array' => [
105  'string array',
106  ['string array'],
107  1,
108  'string array'
109  ]
110  ];
111  }
112 }
$objectManager
Definition: bootstrap.php:17
$value
Definition: gender.phtml:16
testAfterLoad($expected, $value, $numCalls, $unserializedValue=null)
testBeforeSave($expected, $value, $numCalls, $serializedValue=null)