DefaultMarshallerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Tests\Marshaller;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
  13. class DefaultMarshallerTest extends TestCase
  14. {
  15. public function testSerialize()
  16. {
  17. $marshaller = new DefaultMarshaller();
  18. $values = [
  19. 'a' => 123,
  20. 'b' => function () {},
  21. ];
  22. $expected = ['a' => \extension_loaded('igbinary') && \PHP_VERSION_ID !== 70400 ? igbinary_serialize(123) : serialize(123)];
  23. $this->assertSame($expected, $marshaller->marshall($values, $failed));
  24. $this->assertSame(['b'], $failed);
  25. }
  26. public function testNativeUnserialize()
  27. {
  28. $marshaller = new DefaultMarshaller();
  29. $this->assertNull($marshaller->unmarshall(serialize(null)));
  30. $this->assertFalse($marshaller->unmarshall(serialize(false)));
  31. $this->assertSame('', $marshaller->unmarshall(serialize('')));
  32. $this->assertSame(0, $marshaller->unmarshall(serialize(0)));
  33. }
  34. /**
  35. * @requires extension igbinary
  36. */
  37. public function testIgbinaryUnserialize()
  38. {
  39. if (\PHP_VERSION_ID === 70400) {
  40. $this->markTestSkipped('igbinary is not compatible with PHP 7.4.0.');
  41. }
  42. $marshaller = new DefaultMarshaller();
  43. $this->assertNull($marshaller->unmarshall(igbinary_serialize(null)));
  44. $this->assertFalse($marshaller->unmarshall(igbinary_serialize(false)));
  45. $this->assertSame('', $marshaller->unmarshall(igbinary_serialize('')));
  46. $this->assertSame(0, $marshaller->unmarshall(igbinary_serialize(0)));
  47. }
  48. public function testNativeUnserializeNotFoundClass()
  49. {
  50. $this->expectException('DomainException');
  51. $this->expectExceptionMessage('Class not found: NotExistingClass');
  52. $marshaller = new DefaultMarshaller();
  53. $marshaller->unmarshall('O:16:"NotExistingClass":0:{}');
  54. }
  55. /**
  56. * @requires extension igbinary
  57. */
  58. public function testIgbinaryUnserializeNotFoundClass()
  59. {
  60. if (\PHP_VERSION_ID === 70400) {
  61. $this->markTestSkipped('igbinary is not compatible with PHP 7.4.0.');
  62. }
  63. $this->expectException('DomainException');
  64. $this->expectExceptionMessage('Class not found: NotExistingClass');
  65. $marshaller = new DefaultMarshaller();
  66. $marshaller->unmarshall(rawurldecode('%00%00%00%02%17%10NotExistingClass%14%00'));
  67. }
  68. public function testNativeUnserializeInvalid()
  69. {
  70. $this->expectException('DomainException');
  71. $this->expectExceptionMessage('unserialize(): Error at offset 0 of 3 bytes');
  72. $marshaller = new DefaultMarshaller();
  73. set_error_handler(function () { return false; });
  74. try {
  75. @$marshaller->unmarshall(':::');
  76. } finally {
  77. restore_error_handler();
  78. }
  79. }
  80. /**
  81. * @requires extension igbinary
  82. */
  83. public function testIgbinaryUnserializeInvalid()
  84. {
  85. if (\PHP_VERSION_ID === 70400) {
  86. $this->markTestSkipped('igbinary is not compatible with PHP 7.4.0');
  87. }
  88. $this->expectException('DomainException');
  89. $this->expectExceptionMessage('igbinary_unserialize_zval: unknown type \'61\', position 5');
  90. $marshaller = new DefaultMarshaller();
  91. set_error_handler(function () { return false; });
  92. try {
  93. @$marshaller->unmarshall(rawurldecode('%00%00%00%02abc'));
  94. } finally {
  95. restore_error_handler();
  96. }
  97. }
  98. }