SerializerTest.php 5.39 KB
Newer Older
Ketan's avatar
Ketan committed
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
<?php
/**
 * @copyright  Vertex. All rights reserved.  https://www.vertexinc.com/
 * @author     Mediotype                     https://www.mediotype.com/
 */

namespace Vertex\Tax\Test\Unit\Model\Cache;

use Vertex\Tax\Model\Cache\Serializer;
use Vertex\Tax\Test\Unit\TestCase;

/**
 * Test cache storage serializer functionality.
 */
class SerializerTest extends TestCase
{
    /** @var Serializer */
    private $serializer;

    /**
     * Perform test setup.
     */
    protected function setUp()
    {
        parent::setUp();

        $this->serializer = $this->getObject(Serializer::class);
    }

    /**
     * Test that string data can be handled by the serializer with integrity.
     *
     * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
     * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
     */
    public function testStringType()
    {
        $expectedValue = 'test';
        $output = $this->serializer->serialize($expectedValue);
        $actualResult = $this->serializer->unserialize($output);

        $this->assertEquals($expectedValue, $actualResult);
    }

    /**
     * Test that double type data can be handled by the serializer with integrity.
     *
     * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
     * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
     */
    public function testDoubleType()
    {
        $expectedValue = 65.18;
        $output = $this->serializer->serialize($expectedValue);
        $actualResult = $this->serializer->unserialize($output);

        $this->assertEquals($expectedValue, $actualResult);
    }

    /**
     * Test that integer data can be handled by the serializer with integrity.
     *
     * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
     * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
     */
    public function testIntegerType()
    {
        $expectedValue = 100;
        $output = $this->serializer->serialize($expectedValue);
        $actualResult = $this->serializer->unserialize($output);

        $this->assertEquals($expectedValue, $actualResult);
    }

    /**
     * Test that boolean data can be handled by the serializer with integrity.
     *
     * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
     * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
     */
    public function testBooleanType()
    {
        $expectedValue = false;
        $output = $this->serializer->serialize($expectedValue);
        $actualResult = $this->serializer->unserialize($output);

        $this->assertEquals($expectedValue, $actualResult);
    }

    /**
     * Test that array data can be handled by the serializer with integrity.
     *
     * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
     * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
     */
    public function testArrayType()
    {
        $expectedValue = ['test'];
        $output = $this->serializer->serialize($expectedValue);
        $actualResult = $this->serializer->unserialize($output);

        $this->assertEquals('array', gettype($actualResult));
        $this->assertTrue(count($actualResult) === 1);
        $this->assertEquals($expectedValue[0], $actualResult[0]);
    }

    /**
     * Test that boolean data can be handled by the serializer with integrity.
     *
     * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
     * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
     */
    public function testNullType()
    {
        $expectedValue = null;
        $output = $this->serializer->serialize($expectedValue);
        $actualResult = $this->serializer->unserialize($output);

        $this->assertEquals($expectedValue, $actualResult);
    }

    /**
     * Test that unsupported types will throw an exception.
     *
     * @dataProvider provideUnsupportedInputs
     * @param mixed $input
     * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
     * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
     */
    public function testUnsupportedInputType($input)
    {
        $this->expectException(\InvalidArgumentException::class);

        $this->serializer->serialize($input);
    }

    /**
     * Test that circular references within an array cannot be accepted.
     *
     * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
     * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
     */
    public function testArrayRecursion()
    {
        // For local environment tests in which XDebug is enabled.
        ini_set('xdebug.max_nesting_level', Serializer::MAX_ARRAY_DEPTH + 1);

        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage(
            sprintf('Serializable array depth cannot exceed %d', Serializer::MAX_ARRAY_DEPTH)
        );

        $input = ['test'];
        $input['reference'] = &$input;

        try {
            $this->serializer->serialize($input);
        } catch (\Error $error) {
            $this->markTestSkipped('Could not set high enough nesting level.');
        }
    }

    /**
     * Provide various inputs that should not be supported by serialization
     *
     * @return array
     */
    public function provideUnsupportedInputs()
    {
        return [
            [$this->getMockBuilder('NonStandardObject')],
            [['key' => $this->getMockBuilder('NonStandardObject')],],
            [['key' => ['otherKey' => $this->getMockBuilder('NonStandardObject')]]],
        ];
    }
}