summaryrefslogtreecommitdiff
path: root/vendor/symfony/cache/Tests/Marshaller/DefaultMarshallerTest.php
blob: fc625d12fc2ffd86e543ba2f2e1472b1f360e1f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Cache\Tests\Marshaller;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;

class DefaultMarshallerTest extends TestCase
{
    public function testSerialize()
    {
        $marshaller = new DefaultMarshaller();
        $values = array(
            'a' => 123,
            'b' => function () {},
        );

        $expected = array('a' => \extension_loaded('igbinary') ? igbinary_serialize(123) : serialize(123));
        $this->assertSame($expected, $marshaller->marshall($values, $failed));
        $this->assertSame(array('b'), $failed);
    }

    public function testNativeUnserialize()
    {
        $marshaller = new DefaultMarshaller();
        $this->assertNull($marshaller->unmarshall(serialize(null)));
        $this->assertFalse($marshaller->unmarshall(serialize(false)));
        $this->assertSame('', $marshaller->unmarshall(serialize('')));
        $this->assertSame(0, $marshaller->unmarshall(serialize(0)));
    }

    /**
     * @requires extension igbinary
     */
    public function testIgbinaryUnserialize()
    {
        $marshaller = new DefaultMarshaller();
        $this->assertNull($marshaller->unmarshall(igbinary_serialize(null)));
        $this->assertFalse($marshaller->unmarshall(igbinary_serialize(false)));
        $this->assertSame('', $marshaller->unmarshall(igbinary_serialize('')));
        $this->assertSame(0, $marshaller->unmarshall(igbinary_serialize(0)));
    }

    /**
     * @expectedException \DomainException
     * @expectedExceptionMessage Class not found: NotExistingClass
     */
    public function testNativeUnserializeNotFoundClass()
    {
        $marshaller = new DefaultMarshaller();
        $marshaller->unmarshall('O:16:"NotExistingClass":0:{}');
    }

    /**
     * @requires extension igbinary
     * @expectedException \DomainException
     * @expectedExceptionMessage Class not found: NotExistingClass
     */
    public function testIgbinaryUnserializeNotFoundClass()
    {
        $marshaller = new DefaultMarshaller();
        $marshaller->unmarshall(rawurldecode('%00%00%00%02%17%10NotExistingClass%14%00'));
    }

    /**
     * @expectedException \DomainException
     * @expectedExceptionMessage unserialize(): Error at offset 0 of 3 bytes
     */
    public function testNativeUnserializeInvalid()
    {
        $marshaller = new DefaultMarshaller();
        set_error_handler(function () { return false; });
        try {
            @$marshaller->unmarshall(':::');
        } finally {
            restore_error_handler();
        }
    }

    /**
     * @requires extension igbinary
     * @expectedException \DomainException
     * @expectedExceptionMessage igbinary_unserialize_zval: unknown type '61', position 5
     */
    public function testIgbinaryUnserializeInvalid()
    {
        $marshaller = new DefaultMarshaller();
        set_error_handler(function () { return false; });
        try {
            @$marshaller->unmarshall(rawurldecode('%00%00%00%02abc'));
        } finally {
            restore_error_handler();
        }
    }
}