Completed
Push — master ( b43ecb...492467 )
by Eugene
07:09
created

testPackerFactorySetsBigIntAsDecUnpackOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 11
rs 9.9
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;
17
use Tarantool\Client\Client;
18
use Tarantool\Client\Handler\DefaultHandler;
19
use Tarantool\Client\Packer\Extension\DecimalExtension;
20
use Tarantool\Client\Packer\PackerFactory;
21
use Tarantool\Client\Packer\PurePacker;
22
use Tarantool\Client\Tests\Integration\ClientBuilder;
23
use Tarantool\Client\Tests\Integration\TestCase;
24
25
/**
26
 * @requires Tarantool ^2.3
27
 * @requires extension decimal
28
 * @requires clientPacker pure
29
 */
30
final class DecimalExtensionTest extends TestCase
31
{
32
    private const TARANTOOL_DECIMAL_PRECISION = 38;
33
34
    /**
35
     * @lua dec = require('decimal')
36
     *
37
     * @dataProvider provideDecimalStrings
38
     */
39
    public function testPackingAndUnpacking(string $decimalString) : void
40
    {
41
        $client = ClientBuilder::createFromEnv()
42
            ->setPackerPureFactory(static function () {
43
                return PurePacker::fromExtensions(new DecimalExtension());
44
            })
45
            ->build();
46
47
        [$decimal] = $client->evaluate('return dec.new(...)', $decimalString);
0 ignored issues
show
Bug introduced by
The variable $decimal does not exist. Did you mean $decimalString?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
48
        self::assertTrue($decimal->equals($decimalString));
0 ignored issues
show
Bug introduced by
The variable $decimal does not exist. Did you mean $decimalString?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
49
50
        [$isEqual] = $client->evaluate(
0 ignored issues
show
Bug introduced by
The variable $isEqual does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
51
            "return dec.new('$decimalString') == ...",
52
            new Decimal($decimalString, self::TARANTOOL_DECIMAL_PRECISION)
53
        );
54
        self::assertTrue($isEqual);
55
    }
56
57
    public function provideDecimalStrings() : iterable
58
    {
59
        return [
60
            ['0'],
61
            ['-0'],
62
            ['42'],
63
            ['-127'],
64
            ['0.0'],
65
            ['00000.0000000'],
66
            ['00009.9000000'],
67
            ['1.000000099'],
68
            ['4.2'],
69
            ['1E-10'],
70
            ['-2E-15'],
71
            ['0.0000234'],
72
            [str_repeat('9', self::TARANTOOL_DECIMAL_PRECISION)],
73
            ['-'.str_repeat('9', self::TARANTOOL_DECIMAL_PRECISION)],
74
            ['0.'.str_repeat('1', self::TARANTOOL_DECIMAL_PRECISION)],
75
            [str_repeat('1', self::TARANTOOL_DECIMAL_PRECISION).'.0'],
76
            ['9.'.str_repeat('9', self::TARANTOOL_DECIMAL_PRECISION - 1)],
77
            [str_repeat('9', self::TARANTOOL_DECIMAL_PRECISION - 1).'.9'],
78
        ];
79
    }
80
81
    public function testBigIntegerUnpacksToDecimal() : void
82
    {
83
        [$number] = $this->client->evaluate('return 18446744073709551615ULL');
0 ignored issues
show
Bug introduced by
The variable $number does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
84
85
        self::assertTrue((new Decimal('18446744073709551615'))->equals($number));
86
    }
87
88
    public function testPackerFactorySetsBigIntAsDecUnpackOption() : void
89
    {
90
        $client = new Client(new DefaultHandler(
91
            ClientBuilder::createFromEnv()->createConnection(),
92
            PackerFactory::create()
93
        ));
94
95
        [$number] = $client->evaluate('return 18446744073709551615ULL');
0 ignored issues
show
Bug introduced by
The variable $number does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
96
97
        self::assertTrue((new Decimal('18446744073709551615'))->equals($number));
98
    }
99
}
100