-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargeArrayBufferTest.php
More file actions
148 lines (134 loc) · 3.75 KB
/
LargeArrayBufferTest.php
File metadata and controls
148 lines (134 loc) · 3.75 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
namespace LargeArrayBuffer\Tests;
use LargeArrayBuffer\LargeArrayBuffer;
use PHPUnit\Framework\TestCase;
/**
* @author Andreas Wahlen
*/
class LargeArrayBufferTest extends TestCase {
private static function getObject(): object {
$o = new \stdClass();
$o->foo = 'hello world!'.PHP_EOL;
$o->bar = new \DateTimeImmutable();
$o->a = ['test', 123];
$o->str = 'hello world!\\n';
return $o;
}
public function testEmpty(): void {
$buf = new LargeArrayBuffer();
$runs = 0;
foreach($buf as $item){
$runs++;
$item;
}
$this->assertEquals(0, $buf->count());
$this->assertEquals(0, $runs);
}
public static function provideConfig(): \Generator {
$serializers = [
'PHP' => LargeArrayBuffer::SERIALIZER_PHP
];
if(extension_loaded('igbinary')){
$serializers['IGBinary'] = LargeArrayBuffer::SERIALIZER_IGBINARY;
}
if(extension_loaded('msgpack')){
$serializers['MsgPack'] = LargeArrayBuffer::SERIALIZER_MSGPACK;
}
$compressors = [
'none' => LargeArrayBuffer::COMPRESSION_NONE,
'GZIP' => LargeArrayBuffer::COMPRESSION_GZIP
];
if(extension_loaded('lz4')){
$compressors['LZ4'] = LargeArrayBuffer::COMPRESSION_LZ4;
}
foreach($serializers as $s => $serializer){
foreach($compressors as $c => $compressor){
yield $s.'-'.$c => [$serializer, $compressor];
}
}
}
/**
* @dataProvider provideConfig
*/
//#[DataProvider('provideConfig')]
public function testReadWrite(int $serializer, int $compression): void {
$o = self::getObject();
$buf = new LargeArrayBuffer(serializer: $serializer, compression: $compression);
$buf->push($o);
$buf->rewind();
$buf->next();
$this->assertEquals($o, $buf->current());
}
/**
* @dataProvider provideConfig
*/
//#[DataProvider('provideConfig')]
public function testLoop(int $serializer, int $compression): void {
$count = 1500;
$buf = new LargeArrayBuffer(serializer: $serializer, compression: $compression);
$objs = [];
for($i = 0; $i < $count; $i++){
$o = new \stdClass();
$o->idx = $i;
$objs[] = $o;
$buf->push($o);
}
$this->assertCount($count, $buf);
$expIdx = 0;
foreach($buf as $idx => $item){
$this->assertEquals($expIdx, $idx);
$this->assertEquals($item->idx, $idx);
$this->assertEquals($objs[$idx], $item);
$expIdx++;
}
$this->assertEquals($count, $expIdx);
}
public function testToJSON(): void {
$o = self::getObject();
$buf = new LargeArrayBuffer();
$buf->push($o);
$buf->push($o);
$stream = fopen('php://memory', 'r+');
$buf->toJSONFile($stream);
fseek($stream, 0); // rewind
$json = stream_get_contents($stream);
fclose($stream);
$this->assertEquals(json_encode([$o, $o], JSON_THROW_ON_ERROR), $json);
}
public function provideItems(): array {
return [
[['hello world!', 'just another string']]
];
}
/**
* @dataProvider provideItems
*/
public function testToArray(array $items): void {
$buf = new LargeArrayBuffer();
foreach($items as $item){
$buf->push($item);
}
$this->assertSame($items, $buf->toArray());
}
/**
* @dataProvider provideItems
*/
public function testToFixedArray(array $items): void {
$buf = new LargeArrayBuffer();
foreach($items as $item){
$buf->push($item);
}
$this->assertSame($items, $buf->toFixedArray()->toArray());
}
/**
* @dataProvider provideItems
*/
public function testToGenerator(array $items): void {
$buf = new LargeArrayBuffer();
foreach($items as $item){
$buf->push($item);
}
$this->assertSame($items, iterator_to_array($buf->toGenerator()));
}
}