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