forked from mongodb/mongo-php-driver-legacy
/
MongoInt32Test.php
352 lines (309 loc) · 9.27 KB
/
MongoInt32Test.php
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
require_once 'PHPUnit/Framework.php';
class MongoInt32Test extends PHPUnit_Framework_TestCase
{
public function __call($method, $args) {
if ($method == 'assertInternalType') {
$this->assertType($args[0], $args[1]);
}
}
function setup()
{
if (PHP_INT_SIZE != 4) {
$this->markTestSkipped("Only for 32 bit platforms");
}
ini_set('mongo.native_long', 0);
ini_set('mongo.long_as_object', 0);
$m = new Mongo();
$this->object = $m->selectCollection("phpunit", "ints");
$this->object->drop();
}
function testNormalInsert32()
{
$c = $this->object;
$c->insert(array('int32' => 1234567890));
$x = $c->findOne();
$this->assertSame(1234567890, $x['int32']);
}
function testNormalInsert64()
{
$c = $this->object;
$c->insert(array('int64' => 12345678901234567890));
$x = $c->findOne();
$this->assertSame(12345678901234567890, $x['int64']);
}
function testNormalInsertNativeLong()
{
ini_set('mongo.native_long', 1);
$c = $this->object;
$c->insert(array('int64' => 12345678901234567890));
$x = $c->findOne();
$this->assertSame(12345678901234567890, $x['int64']);
}
function testNormalLongAsObject()
{
ini_set('mongo.long_as_object', 1);
$c = $this->object;
$c->insert(array('int64' => 123456789012345));
$x = $c->findOne();
$this->assertInternalType('float', $x['int64']);
$this->assertSame(123456789012345, $x['int64']);
}
function testNativeLongAsObject()
{
ini_set('mongo.native_long', 1);
ini_set('mongo.long_as_object', 1);
$c = $this->object;
$c->insert(array('int64' => 123456789012345));
$x = $c->findOne();
$this->assertInternalType('float', $x['int64']);
$this->assertSame(123456789012345, $x['int64']);
}
/** objects with int ctor */
function testInsertIntMongo32()
{
$c = $this->object;
$c->insert(array('int32' => new MongoInt32(1234567890)));
$x = $c->findOne();
$this->assertSame(1234567890, $x['int32']);
}
function testInsertIntMongo32B()
{
$c = $this->object;
$c->insert(array('int32' => new MongoInt32(1234567890123)));
$x = $c->findOne();
$this->assertSame(PHP_INT_MAX, $x['int32']);
}
function testInsertIntMongo32NativeLong()
{
ini_set('mongo.native_long', 1);
$c = $this->object;
$c->insert(array('int32' => new MongoInt32(1234567890)));
$x = $c->findOne();
$this->assertSame(1234567890, $x['int32']);
}
function testInsertIntMongo32NativeLongB()
{
ini_set('mongo.native_long', 1);
$c = $this->object;
$c->insert(array('int32' => new MongoInt32(1234567890123)));
$x = $c->findOne();
$this->assertSame(PHP_INT_MAX, $x['int32']);
}
function testInsertIntMongo32LongAsObject()
{
ini_set('mongo.long_as_object', 1);
$c = $this->object;
$c->insert(array('int32' => new MongoInt32(1234567890)));
$x = $c->findOne();
$this->assertSame(1234567890, $x['int32']);
}
function testInsertIntMongo32LongAsObjectB()
{
ini_set('mongo.long_as_object', 1);
$c = $this->object;
$c->insert(array('int32' => new MongoInt32(1234567890123)));
$x = $c->findOne();
$this->assertSame(PHP_INT_MAX, $x['int32']);
}
function testInsertIntMongo64()
{
$c = $this->object;
$c->insert(array('int64' => new MongoInt64(1234567890)));
$x = $c->findOne();
$this->assertInternalType('float', $x['int64']);
$this->assertSame(1234567890.0, $x['int64']);
}
function testInsertIntMongo64B()
{
$c = $this->object;
$c->insert(array('int64' => new MongoInt64(123456789012345)));
$x = $c->findOne();
$this->assertInternalType('float', $x['int64']);
$this->assertSame(1.0, $x['int64']);
}
/**
* @expectedException MongoCursorException
*/
function testInsertIntMongo64NativeLong()
{
ini_set('mongo.native_long', 1);
$c = $this->object;
$c->insert(array('int64' => new MongoInt64(1234567890)));
$x = $c->findOne();
}
/**
* @expectedException MongoCursorException
*/
function testInsertIntMongo64NativeLongB()
{
ini_set('mongo.native_long', 1);
$c = $this->object;
$c->insert(array('int64' => new MongoInt64(123456789012345)));
$x = $c->findOne();
}
function testInsertIntMongo64LongAsObject()
{
ini_set('mongo.long_as_object', 1);
$c = $this->object;
$c->insert(array('int64' => new MongoInt64(1234567890)));
$x = $c->findOne();
$this->assertEquals(new MongoInt64('1234567890'), $x['int64']);
}
function testInsertIntMongo64LongAsObjectB()
{
ini_set('mongo.long_as_object', 1);
$c = $this->object;
$c->insert(array('int64' => new MongoInt64(1234567890123)));
$x = $c->findOne();
$this->assertEquals(new MongoInt64('1234567890123'), $x['int64']);
}
function testInsertIntMongo64LongAsObjectC()
{
ini_set('mongo.long_as_object', 1);
$c = $this->object;
$c->insert(array('int64' => new MongoInt64(123456789012345)));
$x = $c->findOne();
$this->assertEquals(new MongoInt64('1'), $x['int64']);
}
/** objects with string ctor */
function testInsertStringMongo32()
{
$c = $this->object;
$c->insert(array('int32' => new MongoInt32('1234567890')));
$x = $c->findOne();
$this->assertSame(1234567890, $x['int32']);
}
function testInsertStringMongo32B()
{
$c = $this->object;
$c->insert(array('int32' => new MongoInt32('123456789012345')));
$x = $c->findOne();
$this->assertSame(PHP_INT_MAX, $x['int32']);
}
function testInsertStringMongo32NativeLong()
{
ini_set('mongo.native_long', 1);
$c = $this->object;
$c->insert(array('int32' => new MongoInt32('1234567890')));
$x = $c->findOne();
$this->assertSame(1234567890, $x['int32']);
}
function testInsertStringMongo32NativeLongB()
{
ini_set('mongo.native_long', 1);
$c = $this->object;
$c->insert(array('int32' => new MongoInt32('123456789012345')));
$x = $c->findOne();
$this->assertSame(PHP_INT_MAX, $x['int32']);
}
function testInsertStringMongo32LongAsObject()
{
ini_set('mongo.long_as_object', 1);
$c = $this->object;
$c->insert(array('int32' => new MongoInt32('1234567890')));
$x = $c->findOne();
$this->assertSame(1234567890, $x['int32']);
}
function testInsertStringMongo32LongAsObjectB()
{
ini_set('mongo.long_as_object', 1);
$c = $this->object;
$c->insert(array('int32' => new MongoInt32('123456789012345')));
$x = $c->findOne();
$this->assertSame(PHP_INT_MAX, $x['int32']);
}
function testInsertStringMongo64()
{
$c = $this->object;
$c->insert(array('int64' => new MongoInt64('1234567890')));
$x = $c->findOne();
$this->assertInternalType('float', $x['int64']);
$this->assertSame(1234567890.0, $x['int64']);
}
function testInsertStringMongo64B()
{
$c = $this->object;
$c->insert(array('int64' => new MongoInt64('123456789012345')));
$x = $c->findOne();
$this->assertInternalType('float', $x['int64']);
$this->assertSame(123456789012345.0, $x['int64']);
}
/**
* @expectedException MongoCursorException
*/
function testInsertStringMongo64NativeLong()
{
ini_set('mongo.native_long', 1);
$c = $this->object;
$c->insert(array('int64' => new MongoInt64('1234567890')));
$x = $c->findOne();
$this->assertSame(1234567890, $x['int64']);
}
/**
* @expectedException MongoCursorException
*/
function testInsertStringMongo64NativeLongB()
{
ini_set('mongo.native_long', 1);
$c = $this->object;
$c->insert(array('int64' => new MongoInt64('123456789012345')));
$x = $c->findOne();
$this->assertSame(123456789012345, $x['int64']);
}
function testInsertStringMongo64LongAsObject()
{
ini_set('mongo.long_as_object', 1);
$c = $this->object;
$c->insert(array('int64' => new MongoInt64('1234567890')));
$x = $c->findOne();
$this->assertEquals(new MongoInt64('1234567890'), $x['int64']);
$this->assertSame('1234567890', $x['int64']->__toString());
}
function testInsertStringMongo64LongAsObjectB()
{
ini_set('mongo.long_as_object', 1);
$c = $this->object;
$c->insert(array('int64' => new MongoInt64('123456789012345')));
$x = $c->findOne();
$this->assertEquals(new MongoInt64('123456789012345'), $x['int64']);
$this->assertSame('123456789012345', $x['int64']->__toString());
}
/** Tests for object generation */
function testObjectCreationInt32()
{
$a = new MongoInt32('1234567890');
$this->assertSame('1234567890', $a->__toString());
$a = new MongoInt32('1234567890123456');
$this->assertSame('1234567890123456', $a->__toString());
$a = new MongoInt32('123456789012345678901234567890');
$this->assertSame('123456789012345678901234567890', $a->__toString());
}
function testObjectCreationInt64()
{
$a = new MongoInt64('1234567890');
$this->assertSame('1234567890', $a->__toString());
$a = new MongoInt64('1234567890123456');
$this->assertSame('1234567890123456', $a->__toString());
$a = new MongoInt64('123456789012345678901234567890');
$this->assertSame('123456789012345678901234567890', $a->__toString());
}
/** Tests for things outside of the int64 range */
function testInsertIntMongo64Big()
{
$c = $this->object;
$c->insert(array('int64' => new MongoInt64(123456789012345678901234567890)));
$x = $c->findOne();
$this->assertInternalType('float', $x['int64']);
$this->assertSame((double) 1, $x['int64']);
}
function testInsertStringMongo64Big()
{
$c = $this->object;
$c->insert(array('int64' => new MongoInt64('123456789012345678901234567890')));
$x = $c->findOne();
$this->assertInternalType('float', $x['int64']);
$this->assertSame(9223372036854775807, $x['int64']);
}
}
?>