JsonTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Serialize\Test\Unit\Serializer;
  7. use Magento\Framework\DataObject;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. class JsonTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\Serialize\Serializer\Json
  13. */
  14. private $json;
  15. protected function setUp()
  16. {
  17. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  18. $this->json = $objectManager->getObject(Json::class);
  19. }
  20. /**
  21. * @param string|int|float|bool|array|null $value
  22. * @param string $expected
  23. * @dataProvider serializeDataProvider
  24. */
  25. public function testSerialize($value, $expected)
  26. {
  27. $this->assertEquals(
  28. $expected,
  29. $this->json->serialize($value)
  30. );
  31. }
  32. /**
  33. * @return array
  34. */
  35. public function serializeDataProvider()
  36. {
  37. $dataObject = new DataObject(['something']);
  38. return [
  39. ['', '""'],
  40. ['string', '"string"'],
  41. [null, 'null'],
  42. [false, 'false'],
  43. [['a' => 'b', 'd' => 123], '{"a":"b","d":123}'],
  44. [123, '123'],
  45. [10.56, '10.56'],
  46. [$dataObject, '{}'],
  47. ];
  48. }
  49. /**
  50. * @param string $value
  51. * @param string|int|float|bool|array|null $expected
  52. * @dataProvider unserializeDataProvider
  53. */
  54. public function testUnserialize($value, $expected)
  55. {
  56. $this->assertEquals(
  57. $expected,
  58. $this->json->unserialize($value)
  59. );
  60. }
  61. /**
  62. * @return array
  63. */
  64. public function unserializeDataProvider()
  65. {
  66. return [
  67. ['""', ''],
  68. ['"string"', 'string'],
  69. ['null', null],
  70. ['false', false],
  71. ['{"a":"b","d":123}', ['a' => 'b', 'd' => 123]],
  72. ['123', 123],
  73. ['10.56', 10.56],
  74. ['{}', []],
  75. ];
  76. }
  77. /**
  78. * @expectedException \InvalidArgumentException
  79. * @expectedExceptionMessage Unable to serialize value.
  80. */
  81. public function testSerializeException()
  82. {
  83. $this->json->serialize(STDOUT);
  84. }
  85. /**
  86. * @expectedException \InvalidArgumentException
  87. * @expectedExceptionMessage Unable to unserialize value.
  88. * @dataProvider unserializeExceptionDataProvider
  89. */
  90. public function testUnserializeException($value)
  91. {
  92. $this->json->unserialize($value);
  93. }
  94. /**
  95. * @return array
  96. */
  97. public function unserializeExceptionDataProvider()
  98. {
  99. return [
  100. [''],
  101. [false],
  102. [null],
  103. ['{']
  104. ];
  105. }
  106. }