SerializedToJsonTest.php 1.54 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\DB\Test\Unit\DataConverter;

use Magento\Framework\Serialize\Serializer\Serialize;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\DB\DataConverter\SerializedToJson;

class SerializedToJsonTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var SerializedToJson
     */
    private $serializedToJson;

    protected function setUp()
    {
        $this->serializedToJson =  new SerializedToJson(
            new Serialize(),
            new Json()
        );
    }

    /**
     * Tests converting from serialized to JSON format with different precision settings.
     *
     * @param $serializedData
     * @param $expectedJson
     * @dataProvider convertDataProvider
     */
    public function testConvert($serializedData, $expectedJson)
    {
        $this->assertEquals($expectedJson, $this->serializedToJson->convert($serializedData));
    }

    /**
     * @case #1 - Serialized 0.1234567890123456789 with serialize_precision = 17 (default for PHP version < 7.1.0)
     * @case #2 - Serialized 2.203 with serialize_precision = 17 (default for PHP version < 7.1.0 )
     * @return array
     */
    public function convertDataProvider()
    {
        return [
            1 => ['serializedData' => 'a:1:{i:0;d:0.12345678901234568;}', 'expectedJson' => '[0.12345678901234568]'],
            2 => ['serializedData' => 'a:1:{i:0;d:2.2029999999999998;}', 'expectedJson' => '[2.2029999999999998]']
        ];
    }
}