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); |
|
|
|
|
48
|
|
|
self::assertTrue($decimal->equals($decimalString)); |
|
|
|
|
49
|
|
|
|
50
|
|
|
[$isEqual] = $client->evaluate( |
|
|
|
|
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'); |
|
|
|
|
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'); |
|
|
|
|
96
|
|
|
|
97
|
|
|
self::assertTrue((new Decimal('18446744073709551615'))->equals($number)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.