Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SerializedDataConverterTest.php
Go to the documentation of this file.
1 <?php
7 
12 
13 class SerializedDataConverterTest extends \PHPUnit\Framework\TestCase
14 {
18  private $serializeMock;
19 
23  private $jsonMock;
24 
28  private $serializedDataConverter;
29 
30  protected function setUp()
31  {
32  $objectManager = new ObjectManager($this);
33  $this->serializeMock = $this->createMock(Serialize::class);
34  $this->jsonMock = $this->createMock(Json::class);
35  $this->serializedDataConverter = $objectManager->getObject(
36  SerializedDataConverter::class,
37  [
38  'serialize' => $this->serializeMock,
39  'json' => $this->jsonMock
40  ]
41  );
42  }
43 
44  public function testConvert()
45  {
46  $serializedData = 'serialized data';
47  $jsonEncodedData = 'json encoded data';
48  $data = [
49  'info_buyRequest' => [
50  'product' => 1,
51  'qty' => 2
52  ]
53  ];
54  $this->serializeMock->expects($this->once())
55  ->method('unserialize')
56  ->with($serializedData)
57  ->willReturn($data);
58  $this->jsonMock->expects($this->once())
59  ->method('serialize')
60  ->with($data)
61  ->willReturn($jsonEncodedData);
62  $this->assertEquals(
63  $jsonEncodedData,
64  $this->serializedDataConverter->convert($serializedData)
65  );
66  }
67 
68  public function testConvertBundleAttributes()
69  {
70  $serializedData = 'serialized data';
71  $serializedBundleAttributes = 'serialized bundle attributes';
72  $bundleAttributes = ['foo' => 'bar'];
73  $jsonEncodedBundleAttributes = 'json encoded bundle attributes';
74  $jsonEncodedData = 'json encoded data';
75  $data = [
76  'info_buyRequest' => [
77  'product' => 1,
78  'qty' => 2
79  ],
80  'bundle_selection_attributes' => $serializedBundleAttributes
81  ];
82  $dataWithJsonEncodedBundleAttributes = [
83  'info_buyRequest' => [
84  'product' => 1,
85  'qty' => 2
86  ],
87  'bundle_selection_attributes' => $jsonEncodedBundleAttributes
88  ];
89  $this->serializeMock->expects($this->at(0))
90  ->method('unserialize')
91  ->with($serializedData)
92  ->willReturn($data);
93  $this->serializeMock->expects($this->at(1))
94  ->method('unserialize')
95  ->with($serializedBundleAttributes)
96  ->willReturn($bundleAttributes);
97  $this->jsonMock->expects($this->at(0))
98  ->method('serialize')
99  ->with($bundleAttributes)
100  ->willReturn($jsonEncodedBundleAttributes);
101  $this->jsonMock->expects($this->at(1))
102  ->method('serialize')
103  ->with($dataWithJsonEncodedBundleAttributes)
104  ->willReturn($jsonEncodedData);
105  $this->assertEquals(
106  $jsonEncodedData,
107  $this->serializedDataConverter->convert($serializedData)
108  );
109  }
110 
112  {
113  $serializedData = 'serialized data';
114  $serializedOptionValue = 'serialized option value';
115  $optionValue = ['foo' => 'bar'];
116  $jsonEncodedOptionValue = 'json encoded option value';
117  $jsonEncodedData = 'json encoded data';
118  $data = [
119  'info_buyRequest' => [
120  'product' => 1,
121  'qty' => 2
122  ],
123  'options' => [
124  [
125  'option_type' => 'file',
126  'option_value' => $serializedOptionValue
127  ],
128  [
129  'option_type' => 'text',
130  'option_value' => 'option 2'
131  ]
132  ]
133  ];
134  $dataWithJsonEncodedOptionValue = [
135  'info_buyRequest' => [
136  'product' => 1,
137  'qty' => 2
138  ],
139  'options' => [
140  [
141  'option_type' => 'file',
142  'option_value' => $jsonEncodedOptionValue
143  ],
144  [
145  'option_type' => 'text',
146  'option_value' => 'option 2'
147  ]
148  ]
149  ];
150  $this->serializeMock->expects($this->at(0))
151  ->method('unserialize')
152  ->with($serializedData)
153  ->willReturn($data);
154  $this->serializeMock->expects($this->at(1))
155  ->method('unserialize')
156  ->with($serializedOptionValue)
157  ->willReturn($optionValue);
158  $this->jsonMock->expects($this->at(0))
159  ->method('serialize')
160  ->with($optionValue)
161  ->willReturn($jsonEncodedOptionValue);
162  $this->jsonMock->expects($this->at(1))
163  ->method('serialize')
164  ->with($dataWithJsonEncodedOptionValue)
165  ->willReturn($jsonEncodedData);
166  $this->assertEquals(
167  $jsonEncodedData,
168  $this->serializedDataConverter->convert($serializedData)
169  );
170  }
171 
175  public function testConvertCorruptedData()
176  {
177  $this->serializeMock->expects($this->once())
178  ->method('unserialize')
179  ->willReturnCallback(
180  function () {
181  trigger_error('Can not unserialize string message', E_NOTICE);
182  }
183  );
184  $this->serializedDataConverter->convert('serialized data');
185  }
186 
187  public function testConvertSkipConversion()
188  {
189  $serialized = '[]';
190  $this->serializeMock->expects($this->never())
191  ->method('unserialize');
192  $this->jsonMock->expects($this->never())
193  ->method('serialize');
194  $this->serializedDataConverter->convert($serialized);
195  }
196 }
$objectManager
Definition: bootstrap.php:17