3 class QueryTest
extends PHPUnit_Framework_TestCase
8 ActiveMongo
::connect(DB
, "localhost");
11 } catch (ActiveMongo_Exception
$e) {}
14 } catch (ActiveMongo_Exception
$e) {}
17 } catch (ActiveMongo_Exception
$e) {}
20 } catch (ActiveMongo_Exception
$e) {}
21 $this->assertTrue(TRUE);
24 function testBulkInserts()
28 } catch (ActiveMongo_Exception
$e) {}
32 for ($i=0; $i < 5000; $i++
) {
33 $data[] = array('int' => $i, 'str' => sha1(uniqid()));
36 /* Invalid data, shouldn't be inserted */
37 $data[] = array('xint' => $i, 'str' => sha1(uniqid()));
38 $data[] = array('xint' => $i, 'str' => sha1(uniqid()));
39 $data[] = array('xint' => $i, 'str' => sha1(uniqid()));
42 Model3
::batchInsert($data, TRUE, TRUE);
45 $this->assertEquals($c->count(), 5000);
48 function testNamespace()
50 $this->assertFalse(ActiveMongo
::setNamespace('bad namespace'));
51 $this->assertFalse(ActiveMongo
::setNamespace('bad=namespace'));
52 $this->assertFalse(ActiveMongo
::setNamespace('bad=namespace!'));
53 $this->assertTrue(ActiveMongo
::setNamespace('good_namespace'));
54 ActiveMongo
::setNamespace('testing');
55 Model2
::setNamespace('foobar');
56 list($m1, $m2) = array(new model1
, new model2
);
57 $this->assertEquals($m1->collectionName(), 'testing.model1');
58 $this->assertEquals($m2->collectionName(), 'foobar.model2');
59 $this->assertTrue(ActiveMongo
::setNamespace(NULL)); /* set no-namespace */
60 $this->assertTrue(Model2
::setNamespace(NULL)); /* set no-namespace */
63 function testInstall()
65 ActiveMongo
::install();
66 $index = Model1
::getIndexes();
67 $this->assertTrue(isset($index[1]['key']['b']));
68 $this->assertTrue(isset($index[2]['key']['a']));
69 $this->assertEquals($index[1]['key']['b'], 1);
70 $this->assertEquals($index[2]['key']['a'], -1);
71 $this->asserTEquals(count($index), 3);
72 $index = Model2
::getIndexes();
73 $this->assertTrue(isset($index[1]['key']['M1']));
74 $this->assertEquals($index[1]['key']['M1'], 1);
75 $this->asserTEquals(count($index), 2);
79 * @depends testBulkInserts
86 $this->assertTrue(isset($c->int));
87 $this->assertTrue(isset($c['int']));
89 $this->assertFalse(isset($c->int));
90 $this->assertFalse(isset($c['int']));
94 * @depends testBulkInserts
96 function testModQuery()
100 $c->properties('int');
101 $c->where('int %', array(2, 0));
102 $this->assertLessThan($c->count(), 0);
104 $this->assertEquals($r->int %
2, 0);
120 /* prepare the query */
121 $c->properties('a,b')->where('a >', $val1)->where('b <', $val2)->where('c !=', $val3);
122 $c->where('h regexp', '/[a-f0-9]+/');
123 $c->where('x in', array(1, 2));
124 $c->where('x nin', array(4));
125 $c->where('y ==', array(4));
126 $c->where('f exists');
127 $c->where('f !=', array(5,6));
128 $c->where(array("bar exists", "a exists"));
129 $c->where('xxx ==', 5);
130 $c->where('bar >=', 5);
131 $c->sort('c DESC, a ASC')->limit($val4, $val5);
136 /* Get cursor info */
137 $sQuery = $c->getReference(TRUE);
139 /* expected cursor info */
141 'ns' => DB
.'.model1',
146 'a' => array('$gt' => $val1, '$exists' => 1),
147 'b' => array('$lt' => $val2),
148 'c' => array('$ne' => $val3),
149 'h' => new MongoRegex('/[a-f0-9]+/'),
150 'x' => array('$in' => array(1,2), '$nin' => array(4)),
151 'y' => array('$all' => array(4)),
152 'f' => array('$exists' => 1, '$nin' => array(5,6)),
154 'bar' => array('$exists' => 1, '$gte' => 5),
164 '_id' => 1, /* from now on _id is included by default */
168 $this->assertEquals($sQuery['dynamic'], $eQuery);
172 function testQueryArray()
184 /* prepare the query */
189 'h regexp' => '/[a-f0-9]+/',
190 'x in ' => array(1,2),
191 'x nin ' => array(4),
194 $c->properties('a,b')->sort('c DESC, a ASC')->limit($val4, $val5);;
200 /* Get cursor info */
201 $sQuery = $c->getReference(TRUE);
203 /* expected cursor info */
205 'ns' => DB
.'.model1',
210 'a' => array('$gt' => $val1),
211 'b' => array('$lt' => $val2),
212 'c' => array('$ne' => $val3),
213 'h' => new MongoRegex('/[a-f0-9]+/'),
214 'x' => array('$in' => array(1,2), '$nin' => array(4)),
215 'y' => array('$all' => array(4)),
229 $this->assertEquals($sQuery['dynamic'], $eQuery);
232 function testQueryRequireArray()
236 $c->where('c near', 'string');
237 $this->assertTrue(FALSE);
238 } catch (ActiveMongo_Exception
$e) {
239 $this->assertTrue(TRUE);
242 $c->where('c in', 55);
243 $this->assertTrue(FALSE);
244 } catch (ActiveMongo_Exception
$e) {
245 $this->assertTrue(TRUE);
248 $c->where('c >', array(1));
249 $this->assertTrue(FALSE);
250 } catch (ActiveMongo_Exception
$e) {
251 $this->assertTrue(TRUE);
254 $c->where('c >>>', array(1));
255 $this->assertTrue(FALSE);
256 } catch (ActiveMongo_Exception
$e) {
257 $this->assertTrue(TRUE);
260 $c->where('c nin', 559);
261 $this->assertTrue(FALSE);
262 } catch (ActiveMongo_Exception
$e) {
263 $this->assertTrue(TRUE);
267 function testMultipleOperationsPerProperty()
269 list($min, $max) = array(50, 100);
272 foreach ($c->where('int >', $min)->where('int <', $max) as $item) {
273 $this->assertGreaterThan($min, $item['int']);
274 $this->assertLessThan($max, $item['int']);
277 /* this could be done with a single regexp but
278 * this test should cover the multiple ALL amoung
281 * str regexp '//' AND str regexp '//' AND str regexp '//'
284 $c->where('str regex', '/^4/')->where('str regexp', '/a$/');
285 foreach ($c->where('str regex', '/[a-z0-9]+/') as $item) {
286 $this->assertEquals($item['str'][0], 4);
287 $this->assertEquals($item['str'][strlen($item['str'])-1], 'a');
292 $c->where('int >', $min)->where('int <', $max);
293 foreach ($c->where('int nin', array($min+
1, $min+
2, $min+
3)) as $item) {
294 $this->assertNotEquals($min+
1, $item['int']);
295 $this->assertNotEquals($min+
2, $item['int']);
296 $this->assertNotEquals($min+
3, $item['int']);
300 function testMultipleUpdate()
302 $str = sha1(uniqid());
310 $c->update(array('newproperty' => $str));
312 $c->where(array('int >' => 5, 'int <' => 30));
313 foreach ($c as $item) {
314 if ($item['int'] == 6) {
315 /* 6 is not included */
316 $this->assertFalse(isset($item['newproperty']));
318 $this->assertEquals($str, $item['newproperty']);
323 function testNullUpdate()
330 $c->arr
= array(5, array(1));
334 /* Testing Save also :-) */
335 $this->assertEquals(TRUE, $c->save());
336 /* Now nothing should be done */
337 $this->assertEquals(NULL, $c->save());
349 $this->assertEquals(TRUE, $c->save());
350 $this->assertEquals(NULL, $c->save());
352 unset($c->arr
[1][1]);
356 $this->assertEquals(TRUE, $c->save());
357 $this->assertEquals(NULL, $c->save());
360 /* now empty $c and query for `int` value */
362 $c->where('_id', $id);
364 $this->assertEquals($c->int, 0);
365 $this->assertEquals($c->arr
, array(5,array(1,NULL, 2,3), 0));
366 $this->assertEquals($c->bool, FALSE);
367 $this->assertEquals($c->null, NULL);
370 function testOnQueryModifyError()
377 $this->assertTrue(FALSE);
378 } catch (ActiveMongo_Exception
$e) {
379 $this->assertTrue(TRUE);
384 * @depends testBulkInserts
394 /* object with no results can't be cloned */
397 $this->AssertTrue(FALSE);
398 } catch (ActiveMongo_Exception
$e) {
399 $this->AssertTrue(TRUE);
402 foreach ($c as $item) {
403 $item_cloned = clone $item;
405 $item_cloned->save();
407 /* iterations are forbidden in cloned objects */
408 foreach ($item_cloned as $nitem) {
409 $this->assertTrue(FALSE);
411 } catch (ActiveMongo_Exception
$e) {
412 $this->assertTrue(TRUE);
416 /* cloned object can't be reused */
418 $item_cloned->reset();
419 $this->AssertTrue(FALSE);
420 } catch (ActiveMongo_Exception
$e) {
421 $this->AssertTrue(TRUE);
424 $item_cloned->where('a IN', array(1));
425 $this->AssertTrue(FALSE);
426 } catch (ActiveMongo_Exception
$e) {
427 $this->AssertTrue(TRUE);
432 function testToSTring()
436 $this->assertEquals((string)$c, (string)$c->getID());
437 $this->assertEquals((string)$c, $c->key());
440 function testDelete()
442 /* Delete using a criteria */
444 $c->where('int < ', 100);
447 $this->assertEquals($c->count(), 4900);
449 /* delete on iteration (element by element) */
451 $c->where('int', array(200, 300));
456 $this->assertFalse(isset($c->int));
462 $this->assertEquals(2, $i);
463 $this->assertEquals($c->count(), 4898);
472 $this->assertFalse(ActiveMongo
::drop());
473 $this->assertTrue(Dummy
::drop());
475 $this->assertFalse(Dummy
::drop());
476 } catch (ActiveMongo_Exception
$e) {
477 $this->assertTrue(TRUE);
481 function testInvalidLimits()
484 $this->assertFalse($c->limit(-1, 5));
485 $this->assertFalse($c->limit(5, -1));
486 $this->assertFalse($c->limit(-1, -5));
489 function testInvalidProperties()
492 $this->assertFalse($c->properties(1));
493 $this->assertFalse($c->columns(1));
494 $this->assertFalse($c->columns(NULL));
495 $this->assertFalse($c->columns(TRUE));
499 function testInvalidBatchInsert()
501 /* Invalid document for Model2 */
503 array('foo' => 'bar'),
507 ActiveMongo
::BatchInsert($documents);
508 $this->assertTrue(False);
509 } catch (ActiveMongo_Exception
$e) {
510 $this->assertTrue(TRUE);
514 Model2
::BatchInsert($documents);
515 $this->assertTrue(False);
516 } catch (ActiveMongo_FilterException
$e) {
517 $this->assertTrue(FALSE);
518 } catch (MongoException
$e) {
519 $this->assertTrue(TRUE);
523 Model2
::BatchInsert($documents, TRUE, FALSE);
524 $this->assertTrue(False);
525 } catch (ActiveMongo_FilterException
$e) {
526 $this->assertTrue(TRUE);
531 function testInvalidQueries()
536 $c->where("invalid field property", 3);
537 $this->assertTrue(FALSE);
538 } catch (ActiveMongo_Exception
$e) {
539 $this->assertTrue(TRUE);
546 $this->assertTrue(FALSE);
547 } catch (ActiveMongo_Exception
$e) {
548 $this->assertTrue(TRUE);
554 $this->assertTrue(FALSE);
555 } catch (ActiveMongo_Exception
$e) {
556 $this->assertTrue(TRUE);
560 $c->sort("c DESC, field BAR");
561 $this->assertTrue(FALSE);
562 } catch (ActiveMongo_Exception
$e) {
563 $this->assertTrue(TRUE);
566 /* These are valid, so no exception should be thrown */
567 $c->sort("foo ASC, bar DESC");
568 $c->sort("foo DESC");
572 function testFindWithSingleID()
579 $c->find($d->getID());
580 $this->assertEquals(1, $c->count());
581 $this->assertEquals($c->a
, $d->a
);
584 function testFindAndModify()
588 $c->where('int <= ', 1000);
589 $c->where('processing exists', FALSE);
591 $c->sort('int DESC');
592 $c->findAndModify(array("processing" => TRUE));
597 $this->assertEquals($d->processing
, TRUE);
600 $this->assertLessThan($last, $d->int);
605 $this->assertEquals($i, 50);
609 $c->where('int <= ', 1000);
610 $c->where('processing exists', FALSE);
612 $c->findAndModify(array());
613 $this->assertTrue(FALSE);
614 } catch (ActiveMongo_Exception
$e) {
615 $this->assertTrue(TRUE);
619 function testFindVariations()
621 // add a few entries:
623 array('int' => '1', 'mod3' => '1'),
624 array('int' => '2', 'mod3' => '2'),
625 array('int' => '3', 'mod3' => '0'),
626 array('int' => '4', 'mod3' => '1'),
627 array('int' => '5', 'mod3' => '2'),
628 array('int' => '6', 'mod3' => '0'),
629 array('int' => '7', 'mod3' => '1'),
630 array('int' => '8', 'mod3' => '2'),
631 array('int' => '9', 'mod3' => '0'),
633 Model3
::BatchInsert($documents, TRUE, FALSE);
635 // test findCol (which also tests findPairs and fields)
637 $findCol = $c->findCol('mod3', array('int'=>array('$lt'=>'5')));
638 $this->assertEquals(array_values($findCol), array('1', '2', '0', '1'));
640 // test findOneValue (which will test findOne)
641 $this->assertEquals($c->findOneValue('mod3', array('int'=>'5')), '2');
643 // test findOneObj (which will test findOneAssoc)
644 $obj = $c->findOneObj(array('int'=>'5'));
645 $this->assertEquals($obj, (object)array('int'=>'5', 'mod3'=>'2', '_id'=>$obj->_id
));
648 function testDisconnect()
650 $this->assertTrue(ActiveMongo
::isConnected());
651 ActiveMongo
::Disconnect();
652 $this->assertFalse(ActiveMongo
::isConnected());