SerializeTest.php 2.9 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\Serialize\Serializer\Serialize;
  8. use Magento\Framework\Serialize\Signer;
  9. use Magento\Framework\Serialize\InvalidSignatureException;
  10. class SerializeTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var Serialize
  14. */
  15. private $serialize;
  16. protected function setUp()
  17. {
  18. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  19. $this->serialize = $objectManager->getObject(Serialize::class);
  20. }
  21. /**
  22. * @param string|int|float|bool|array|null $value
  23. * @param string $serializedValue
  24. * @dataProvider serializeDataProvider
  25. */
  26. public function testSerialize($value, $serializedValue)
  27. {
  28. $this->assertEquals($serializedValue, $this->serialize->serialize($value));
  29. }
  30. /**
  31. * @return array
  32. */
  33. public function serializeDataProvider()
  34. {
  35. return [
  36. ['string', 's:6:"string";'],
  37. ['', 's:0:"";'],
  38. [10, 'i:10;'],
  39. [10.5, 'd:10.5;'],
  40. [null, 'N;'],
  41. [false, 'b:0;'],
  42. [['foo' => 'bar'], 'a:1:{s:3:"foo";s:3:"bar";}'],
  43. ];
  44. }
  45. /**
  46. * @param string $serializedValue
  47. * @param string|int|float|bool|array|null $value
  48. * @dataProvider unserializeDataProvider
  49. */
  50. public function testUnserialize($serializedValue, $value)
  51. {
  52. $this->assertEquals($value, $this->serialize->unserialize($serializedValue));
  53. }
  54. /**
  55. * @return array
  56. */
  57. public function unserializeDataProvider()
  58. {
  59. return [
  60. ['s:6:"string";', 'string'],
  61. ['s:0:"";', ''],
  62. ['i:10;', 10],
  63. ['d:10.5;', 10.5],
  64. ['N;', null],
  65. ['b:0;', false],
  66. ['a:1:{s:3:"foo";s:3:"bar";}', ['foo' => 'bar']],
  67. ];
  68. }
  69. /**
  70. * @expectedException \InvalidArgumentException
  71. * @expectedExceptionMessage Unable to serialize value.
  72. */
  73. public function testSerializeException()
  74. {
  75. $this->serialize->serialize(STDOUT);
  76. }
  77. /**
  78. * @expectedException \InvalidArgumentException
  79. * @expectedExceptionMessage Unable to unserialize value.
  80. * @dataProvider unserializeExceptionDataProvider
  81. */
  82. public function testUnserializeException($value)
  83. {
  84. $this->serialize->unserialize($value);
  85. }
  86. /**
  87. * @return array
  88. */
  89. public function unserializeExceptionDataProvider()
  90. {
  91. return [
  92. [''],
  93. [false],
  94. [null]
  95. ];
  96. }
  97. /**
  98. * @expectedException \InvalidArgumentException
  99. * @expectedExceptionMessage Unable to unserialize value, string is corrupted.
  100. */
  101. public function testUnserializeExceptionCorruptedString()
  102. {
  103. $this->serialize->unserialize('a:');
  104. }
  105. }