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 PHPUnit\Framework\Assert; |
17
|
|
|
use Tarantool\Client\Packer\PeclPacker; |
18
|
|
|
use Tarantool\Client\Packer\PurePacker; |
19
|
|
|
use Tarantool\Client\Schema\Criteria; |
20
|
|
|
use Tarantool\Client\Tests\Integration\ClientBuilder; |
21
|
|
|
use Tarantool\Client\Tests\Integration\TestCase; |
22
|
|
|
|
23
|
|
|
final class MessagePackTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @dataProvider providePackUnpackData |
27
|
|
|
*/ |
28
|
|
|
public function testPackUnpack($arg) : void |
29
|
|
|
{ |
30
|
|
|
self::assertSame([$arg], $this->client->evaluate('return ...', $arg)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function providePackUnpackData() : iterable |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
[[]], |
37
|
|
|
[42], |
38
|
|
|
[-42], |
39
|
|
|
[4.2], |
40
|
|
|
[-4.2], |
41
|
|
|
[null], |
42
|
|
|
[false], |
43
|
|
|
['string'], |
44
|
|
|
["\x04\x00\xa0\x00\x00"], |
45
|
|
|
[[1, 2]], |
46
|
|
|
[[[[1, 2]]]], |
47
|
|
|
[['foo' => 'bar']], |
48
|
|
|
// User defined types (MessagePack extensions) are not yet supported: |
49
|
|
|
// https://github.com/tarantool/tarantool/issues/465 |
50
|
|
|
// [[(object) ['foo' => 'bar']]], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testPackUnpackMultiDimensionalArray() : void |
55
|
|
|
{ |
56
|
|
|
$array = [ |
57
|
|
|
[ |
58
|
|
|
'foo' => [42, 'a' => [null]], |
59
|
|
|
'bar' => [], |
60
|
|
|
10000 => -1, |
61
|
|
|
], |
62
|
|
|
true, |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
[$result] = $this->client->evaluate('return ...', $array); |
|
|
|
|
66
|
|
|
|
67
|
|
|
method_exists(Assert::class, 'assertEqualsCanonicalizing') |
68
|
|
|
? self::assertEqualsCanonicalizing($array, $result) |
69
|
|
|
: self::assertEquals($array, $result, '', 0.0, 10, true); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @lua space = create_space('custom_type') |
74
|
|
|
* @lua space:create_index('primary', {type = 'hash', parts = {1, 'unsigned'}}) |
75
|
|
|
*/ |
76
|
|
|
public function testCustomType() : void |
77
|
|
|
{ |
78
|
|
|
$client = ClientBuilder::createFromEnv() |
79
|
|
|
->setPackerPureFactory(static function () { |
80
|
|
|
return PurePacker::fromExtensions(new DateTimeExtension(42)); |
81
|
|
|
}) |
82
|
|
|
->setPackerPeclFactory(static function () { |
83
|
|
|
return new PeclPacker(true); |
84
|
|
|
}) |
85
|
|
|
->build(); |
86
|
|
|
|
87
|
|
|
// @see https://github.com/msgpack/msgpack-php/issues/137 |
88
|
|
|
if (PHP_VERSION_ID >= 70400 && $client->getHandler()->getPacker() instanceof PeclPacker) { |
89
|
|
|
self::markTestSkipped('The msgpack extension does not pack objects correctly on PHP 7.4'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$date = new \DateTimeImmutable(); |
93
|
|
|
$space = $client->getSpace('custom_type'); |
94
|
|
|
$result = $space->insert([100, $date]); |
95
|
|
|
|
96
|
|
|
self::assertEquals($date, $result[0][1]); |
97
|
|
|
self::assertEquals($date, $space->select(Criteria::key([100]))[0][1]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @requires clientPacker pure |
102
|
|
|
*/ |
103
|
|
|
public function testUnpackingBigIntegerAsString() : void |
104
|
|
|
{ |
105
|
|
|
[$number] = $this->client->evaluate('return 18446744073709551615ULL'); |
|
|
|
|
106
|
|
|
|
107
|
|
|
self::assertSame('18446744073709551615', $number); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.