Passed
Pull Request — master (#63)
by Eugene
03:00
created

MessagePackTest::providePackUnpackData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Tarantool Client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
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
declare(strict_types=1);
13
14
namespace Tarantool\Client\Tests\Integration\MessagePack;
15
16
use Decimal\Decimal;
0 ignored issues
show
Bug introduced by
The type Decimal\Decimal was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use PHPUnit\Framework\Assert;
18
use Tarantool\Client\Packer\Extension\DecimalExtension;
19
use Tarantool\Client\Packer\PeclPacker;
20
use Tarantool\Client\Packer\PurePacker;
21
use Tarantool\Client\Schema\Criteria;
22
use Tarantool\Client\Tests\Integration\ClientBuilder;
23
use Tarantool\Client\Tests\Integration\TestCase;
24
25
final class MessagePackTest extends TestCase
26
{
27
    private const TARANTOOL_DECIMAL_PRECISION = 38;
28
29
    /**
30
     * @dataProvider providePackUnpackData
31
     */
32
    public function testPackUnpack($arg) : void
33
    {
34
        self::assertSame([$arg], $this->client->evaluate('return ...', $arg));
35
    }
36
37
    public function providePackUnpackData() : iterable
38
    {
39
        return [
40
            [[]],
41
            [42],
42
            [-42],
43
            [4.2],
44
            [-4.2],
45
            [null],
46
            [false],
47
            ['string'],
48
            ["\x04\x00\xa0\x00\x00"],
49
            [[1, 2]],
50
            [[[[1, 2]]]],
51
            [['foo' => 'bar']],
52
            // User defined types (MessagePack extensions) are not yet supported:
53
            // https://github.com/tarantool/tarantool/issues/465
54
            // [[(object) ['foo' => 'bar']]],
55
        ];
56
    }
57
58
    public function testPackUnpackMultiDimensionalArray() : void
59
    {
60
        $array = [
61
            [
62
                'foo' => [42, 'a' => [null]],
63
                'bar' => [],
64
                10000 => -1,
65
            ],
66
            true,
67
        ];
68
69
        [$result] = $this->client->evaluate('return ...', $array);
70
71
        method_exists(Assert::class, 'assertEqualsCanonicalizing')
72
            ? self::assertEqualsCanonicalizing($array, $result)
73
            : self::assertEquals($array, $result, '', 0.0, 10, true);
74
    }
75
76
    /**
77
     * @eval space = create_space('custom_type')
78
     * @eval space:create_index('primary', {type = 'hash', parts = {1, 'unsigned'}})
79
     */
80
    public function testCustomType() : void
81
    {
82
        $client = ClientBuilder::createFromEnv()
83
            ->setPackerPureFactory(static function () {
84
                return PurePacker::fromExtensions(new DateTimeExtension(42));
85
            })
86
            ->setPackerPeclFactory(static function () {
87
                return new PeclPacker(true);
88
            })
89
            ->build();
90
91
        // @see https://github.com/msgpack/msgpack-php/issues/137
92
        if (PHP_VERSION_ID >= 70400 && $client->getHandler()->getPacker() instanceof PeclPacker) {
93
            self::markTestSkipped('The msgpack extension does not pack objects correctly on PHP 7.4.');
94
        }
95
96
        $date = new \DateTimeImmutable();
97
        $space = $client->getSpace('custom_type');
98
        $result = $space->insert([100, $date]);
99
100
        self::assertEquals($date, $result[0][1]);
101
        self::assertEquals($date, $space->select(Criteria::key([100]))[0][1]);
102
    }
103
104
    /**
105
     * @requires Tarantool 2.3
106
     * @requires extension decimal
107
     * @requires function MessagePack\Packer::pack
108
     *
109
     * @eval dec = require('decimal')
110
     *
111
     * @dataProvider provideDecimalStrings
112
     */
113
    public function testDecimalType(string $decimalString) : void
114
    {
115
        $client = ClientBuilder::createFromEnv()
116
            ->setPackerPureFactory(static function () {
117
                return PurePacker::fromExtensions(new DecimalExtension());
118
            })
119
            ->build();
120
121
        [$decimal] = $client->evaluate('return dec.new(...)', $decimalString);
122
        self::assertTrue($decimal->equals($decimalString));
123
124
        [$isEqual] = $client->evaluate(
125
            'return dec.new(...) == ...',
126
            $decimalString, new Decimal($decimalString, self::TARANTOOL_DECIMAL_PRECISION)
127
        );
128
        self::assertTrue($isEqual);
129
    }
130
131
    public function provideDecimalStrings() : iterable
132
    {
133
        return [
134
            ['0'],
135
            ['-0'],
136
            ['0.0'],
137
            ['00000.0000000'],
138
            ['00009.9000000'],
139
            ['-127'],
140
            ['4.2'],
141
            ['1E-10'],
142
            ['0.0000234'],
143
            ['0.'.str_repeat('1', self::TARANTOOL_DECIMAL_PRECISION)],
144
            [str_repeat('1', self::TARANTOOL_DECIMAL_PRECISION).'.0'],
145
            ['9.'.str_repeat('9', self::TARANTOOL_DECIMAL_PRECISION - 1)],
146
            [str_repeat('9', self::TARANTOOL_DECIMAL_PRECISION - 1).'.9'],
147
        ];
148
    }
149
}
150