SerializedDataConverterTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Setup;
  7. use Magento\Framework\Serialize\Serializer\Json;
  8. use Magento\Framework\Serialize\Serializer\Serialize;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. use Magento\Sales\Setup\SerializedDataConverter;
  11. class SerializedDataConverterTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var Serialize|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. private $serializeMock;
  17. /**
  18. * @var Json|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $jsonMock;
  21. /**
  22. * @var SerializedDataConverter
  23. */
  24. private $serializedDataConverter;
  25. protected function setUp()
  26. {
  27. $objectManager = new ObjectManager($this);
  28. $this->serializeMock = $this->createMock(Serialize::class);
  29. $this->jsonMock = $this->createMock(Json::class);
  30. $this->serializedDataConverter = $objectManager->getObject(
  31. SerializedDataConverter::class,
  32. [
  33. 'serialize' => $this->serializeMock,
  34. 'json' => $this->jsonMock
  35. ]
  36. );
  37. }
  38. public function testConvert()
  39. {
  40. $serializedData = 'serialized data';
  41. $jsonEncodedData = 'json encoded data';
  42. $data = [
  43. 'info_buyRequest' => [
  44. 'product' => 1,
  45. 'qty' => 2
  46. ]
  47. ];
  48. $this->serializeMock->expects($this->once())
  49. ->method('unserialize')
  50. ->with($serializedData)
  51. ->willReturn($data);
  52. $this->jsonMock->expects($this->once())
  53. ->method('serialize')
  54. ->with($data)
  55. ->willReturn($jsonEncodedData);
  56. $this->assertEquals(
  57. $jsonEncodedData,
  58. $this->serializedDataConverter->convert($serializedData)
  59. );
  60. }
  61. public function testConvertBundleAttributes()
  62. {
  63. $serializedData = 'serialized data';
  64. $serializedBundleAttributes = 'serialized bundle attributes';
  65. $bundleAttributes = ['foo' => 'bar'];
  66. $jsonEncodedBundleAttributes = 'json encoded bundle attributes';
  67. $jsonEncodedData = 'json encoded data';
  68. $data = [
  69. 'info_buyRequest' => [
  70. 'product' => 1,
  71. 'qty' => 2
  72. ],
  73. 'bundle_selection_attributes' => $serializedBundleAttributes
  74. ];
  75. $dataWithJsonEncodedBundleAttributes = [
  76. 'info_buyRequest' => [
  77. 'product' => 1,
  78. 'qty' => 2
  79. ],
  80. 'bundle_selection_attributes' => $jsonEncodedBundleAttributes
  81. ];
  82. $this->serializeMock->expects($this->at(0))
  83. ->method('unserialize')
  84. ->with($serializedData)
  85. ->willReturn($data);
  86. $this->serializeMock->expects($this->at(1))
  87. ->method('unserialize')
  88. ->with($serializedBundleAttributes)
  89. ->willReturn($bundleAttributes);
  90. $this->jsonMock->expects($this->at(0))
  91. ->method('serialize')
  92. ->with($bundleAttributes)
  93. ->willReturn($jsonEncodedBundleAttributes);
  94. $this->jsonMock->expects($this->at(1))
  95. ->method('serialize')
  96. ->with($dataWithJsonEncodedBundleAttributes)
  97. ->willReturn($jsonEncodedData);
  98. $this->assertEquals(
  99. $jsonEncodedData,
  100. $this->serializedDataConverter->convert($serializedData)
  101. );
  102. }
  103. public function testConvertCustomOptionsTypeFile()
  104. {
  105. $serializedData = 'serialized data';
  106. $serializedOptionValue = 'serialized option value';
  107. $optionValue = ['foo' => 'bar'];
  108. $jsonEncodedOptionValue = 'json encoded option value';
  109. $jsonEncodedData = 'json encoded data';
  110. $data = [
  111. 'info_buyRequest' => [
  112. 'product' => 1,
  113. 'qty' => 2
  114. ],
  115. 'options' => [
  116. [
  117. 'option_type' => 'file',
  118. 'option_value' => $serializedOptionValue
  119. ],
  120. [
  121. 'option_type' => 'text',
  122. 'option_value' => 'option 2'
  123. ]
  124. ]
  125. ];
  126. $dataWithJsonEncodedOptionValue = [
  127. 'info_buyRequest' => [
  128. 'product' => 1,
  129. 'qty' => 2
  130. ],
  131. 'options' => [
  132. [
  133. 'option_type' => 'file',
  134. 'option_value' => $jsonEncodedOptionValue
  135. ],
  136. [
  137. 'option_type' => 'text',
  138. 'option_value' => 'option 2'
  139. ]
  140. ]
  141. ];
  142. $this->serializeMock->expects($this->at(0))
  143. ->method('unserialize')
  144. ->with($serializedData)
  145. ->willReturn($data);
  146. $this->serializeMock->expects($this->at(1))
  147. ->method('unserialize')
  148. ->with($serializedOptionValue)
  149. ->willReturn($optionValue);
  150. $this->jsonMock->expects($this->at(0))
  151. ->method('serialize')
  152. ->with($optionValue)
  153. ->willReturn($jsonEncodedOptionValue);
  154. $this->jsonMock->expects($this->at(1))
  155. ->method('serialize')
  156. ->with($dataWithJsonEncodedOptionValue)
  157. ->willReturn($jsonEncodedData);
  158. $this->assertEquals(
  159. $jsonEncodedData,
  160. $this->serializedDataConverter->convert($serializedData)
  161. );
  162. }
  163. /**
  164. * @expectedException \Magento\Framework\DB\DataConverter\DataConversionException
  165. */
  166. public function testConvertCorruptedData()
  167. {
  168. $this->serializeMock->expects($this->once())
  169. ->method('unserialize')
  170. ->willReturnCallback(
  171. function () {
  172. trigger_error('Can not unserialize string message', E_NOTICE);
  173. }
  174. );
  175. $this->serializedDataConverter->convert('serialized data');
  176. }
  177. public function testConvertSkipConversion()
  178. {
  179. $serialized = '[]';
  180. $this->serializeMock->expects($this->never())
  181. ->method('unserialize');
  182. $this->jsonMock->expects($this->never())
  183. ->method('serialize');
  184. $this->serializedDataConverter->convert($serialized);
  185. }
  186. }