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 testInstall()
50 ActiveMongo
::install();
51 $index = Model1
::getIndexes();
52 $this->assertTrue(isset($index[1]['key']['b']));
53 $this->assertTrue(isset($index[2]['key']['a']));
54 $this->assertEquals($index[1]['key']['b'], 1);
55 $this->assertEquals($index[2]['key']['a'], -1);
56 $this->asserTEquals(count($index), 3);
57 $index = Model2
::getIndexes();
58 $this->assertTrue(isset($index[1]['key']['M1']));
59 $this->assertEquals($index[1]['key']['M1'], 1);
60 $this->asserTEquals(count($index), 2);
64 * @depends testBulkInserts
71 $this->assertTrue(isset($c->int));
72 $this->assertTrue(isset($c['int']));
74 $this->assertFalse(isset($c->int));
75 $this->assertFalse(isset($c['int']));
79 * @depends testBulkInserts
81 function testModQuery()
85 $c->properties('int');
86 $c->where('int %', array(2, 0));
87 $this->assertLessThan($c->count(), 0);
89 $this->assertEquals($r->int %
2, 0);
105 /* prepare the query */
106 $c->properties('a,b')->where('a >', $val1)->where('b <', $val2)->where('c !=', $val3);
107 $c->where('h regexp', '/[a-f0-9]+/');
108 $c->where('x in', array(1, 2));
109 $c->where('x nin', array(4));
110 $c->where('y ==', array(4));
111 $c->where('f exists');
112 $c->where('f !=', array(5,6));
113 $c->where(array("bar exists", "a exists"));
114 $c->where('xxx ==', 5);
115 $c->where('bar >=', 5);
116 $c->sort('c DESC, a ASC')->limit($val4, $val5);
121 /* Get cursor info */
122 $sQuery = $c->getReference(TRUE);
124 /* expected cursor info */
126 'ns' => DB
.'.model1',
131 'a' => array('$gt' => $val1, '$exists' => 1),
132 'b' => array('$lt' => $val2),
133 'c' => array('$ne' => $val3),
134 'h' => new MongoRegex('/[a-f0-9]+/'),
135 'x' => array('$in' => array(1,2), '$nin' => array(4)),
136 'y' => array('$all' => array(4)),
137 'f' => array('$exists' => 1, '$nin' => array(5,6)),
139 'bar' => array('$exists' => 1, '$gte' => 5),
149 '_id' => 1, /* from now on _id is included by default */
153 $this->assertEquals($sQuery['dynamic'], $eQuery);
157 function testQueryArray()
169 /* prepare the query */
174 'h regexp' => '/[a-f0-9]+/',
175 'x in ' => array(1,2),
176 'x nin ' => array(4),
179 $c->properties('a,b')->sort('c DESC, a ASC')->limit($val4, $val5);;
185 /* Get cursor info */
186 $sQuery = $c->getReference(TRUE);
188 /* expected cursor info */
190 'ns' => DB
.'.model1',
195 'a' => array('$gt' => $val1),
196 'b' => array('$lt' => $val2),
197 'c' => array('$ne' => $val3),
198 'h' => new MongoRegex('/[a-f0-9]+/'),
199 'x' => array('$in' => array(1,2), '$nin' => array(4)),
200 'y' => array('$all' => array(4)),
214 $this->assertEquals($sQuery['dynamic'], $eQuery);
217 function testQueryRequireArray()
221 $c->where('c near', 'string');
222 $this->assertTrue(FALSE);
223 } catch (ActiveMongo_Exception
$e) {
224 $this->assertTrue(TRUE);
227 $c->where('c in', 55);
228 $this->assertTrue(FALSE);
229 } catch (ActiveMongo_Exception
$e) {
230 $this->assertTrue(TRUE);
233 $c->where('c >', array(1));
234 $this->assertTrue(FALSE);
235 } catch (ActiveMongo_Exception
$e) {
236 $this->assertTrue(TRUE);
239 $c->where('c >>>', array(1));
240 $this->assertTrue(FALSE);
241 } catch (ActiveMongo_Exception
$e) {
242 $this->assertTrue(TRUE);
245 $c->where('c nin', 559);
246 $this->assertTrue(FALSE);
247 } catch (ActiveMongo_Exception
$e) {
248 $this->assertTrue(TRUE);
252 function testMultipleOperationsPerProperty()
254 list($min, $max) = array(50, 100);
257 foreach ($c->where('int >', $min)->where('int <', $max) as $item) {
258 $this->assertGreaterThan($min, $item['int']);
259 $this->assertLessThan($max, $item['int']);
262 /* this could be done with a single regexp but
263 * this test should cover the multiple ALL amoung
266 * str regexp '//' AND str regexp '//' AND str regexp '//'
269 $c->where('str regex', '/^4/')->where('str regexp', '/a$/');
270 foreach ($c->where('str regex', '/[a-z0-9]+/') as $item) {
271 $this->assertEquals($item['str'][0], 4);
272 $this->assertEquals($item['str'][strlen($item['str'])-1], 'a');
277 $c->where('int >', $min)->where('int <', $max);
278 foreach ($c->where('int nin', array($min+
1, $min+
2, $min+
3)) as $item) {
279 $this->assertNotEquals($min+
1, $item['int']);
280 $this->assertNotEquals($min+
2, $item['int']);
281 $this->assertNotEquals($min+
3, $item['int']);
285 function testMultipleUpdate()
287 $str = sha1(uniqid());
295 $c->update(array('newproperty' => $str));
297 $c->where(array('int >' => 5, 'int <' => 30));
298 foreach ($c as $item) {
299 if ($item['int'] == 6) {
300 /* 6 is not included */
301 $this->assertFalse(isset($item['newproperty']));
303 $this->assertEquals($str, $item['newproperty']);
308 function testNullUpdate()
315 $c->arr
= array(5, array(1));
319 /* Testing Save also :-) */
320 $this->assertEquals(TRUE, $c->save());
321 /* Now nothing should be done */
322 $this->assertEquals(NULL, $c->save());
334 $this->assertEquals(TRUE, $c->save());
335 $this->assertEquals(NULL, $c->save());
337 unset($c->arr
[1][1]);
341 $this->assertEquals(TRUE, $c->save());
342 $this->assertEquals(NULL, $c->save());
345 /* now empty $c and query for `int` value */
347 $c->where('_id', $id);
349 $this->assertEquals($c->int, 0);
350 $this->assertEquals($c->arr
, array(5,array(1,NULL, 2,3), 0));
351 $this->assertEquals($c->bool, FALSE);
352 $this->assertEquals($c->null, NULL);
355 function testOnQueryModifyError()
362 $this->assertTrue(FALSE);
363 } catch (ActiveMongo_Exception
$e) {
364 $this->assertTrue(TRUE);
369 * @depends testBulkInserts
379 /* object with no results can't be cloned */
382 $this->AssertTrue(FALSE);
383 } catch (ActiveMongo_Exception
$e) {
384 $this->AssertTrue(TRUE);
387 foreach ($c as $item) {
388 $item_cloned = clone $item;
390 $item_cloned->save();
392 /* iterations are forbidden in cloned objects */
393 foreach ($item_cloned as $nitem) {
394 $this->assertTrue(FALSE);
396 } catch (ActiveMongo_Exception
$e) {
397 $this->assertTrue(TRUE);
401 /* cloned object can't be reused */
403 $item_cloned->reset();
404 $this->AssertTrue(FALSE);
405 } catch (ActiveMongo_Exception
$e) {
406 $this->AssertTrue(TRUE);
409 $item_cloned->where('a IN', array(1));
410 $this->AssertTrue(FALSE);
411 } catch (ActiveMongo_Exception
$e) {
412 $this->AssertTrue(TRUE);
417 function testToSTring()
421 $this->assertEquals((string)$c, (string)$c->getID());
422 $this->assertEquals((string)$c, $c->key());
425 function testDelete()
427 /* Delete using a criteria */
429 $c->where('int < ', 100);
432 $this->assertEquals($c->count(), 4900);
434 /* delete on iteration (element by element) */
436 $c->where('int', array(200, 300));
441 $this->assertFalse(isset($c->int));
447 $this->assertEquals(2, $i);
448 $this->assertEquals($c->count(), 4898);
457 $this->assertFalse(ActiveMongo
::drop());
458 $this->assertTrue(Dummy
::drop());
460 $this->assertFalse(Dummy
::drop());
461 } catch (ActiveMongo_Exception
$e) {
462 $this->assertTrue(TRUE);
466 function testInvalidLimits()
469 $this->assertFalse($c->limit(-1, 5));
470 $this->assertFalse($c->limit(5, -1));
471 $this->assertFalse($c->limit(-1, -5));
474 function testInvalidProperties()
477 $this->assertFalse($c->properties(1));
478 $this->assertFalse($c->columns(1));
479 $this->assertFalse($c->columns(NULL));
480 $this->assertFalse($c->columns(TRUE));
484 function testInvalidBatchInsert()
486 /* Invalid document for Model2 */
488 array('foo' => 'bar'),
492 ActiveMongo
::BatchInsert($documents);
493 $this->assertTrue(False);
494 } catch (ActiveMongo_Exception
$e) {
495 $this->assertTrue(TRUE);
499 Model2
::BatchInsert($documents);
500 $this->assertTrue(False);
501 } catch (ActiveMongo_FilterException
$e) {
502 $this->assertTrue(FALSE);
503 } catch (MongoException
$e) {
504 $this->assertTrue(TRUE);
508 Model2
::BatchInsert($documents, TRUE, FALSE);
509 $this->assertTrue(False);
510 } catch (ActiveMongo_FilterException
$e) {
511 $this->assertTrue(TRUE);
516 function testInvalidQueries()
521 $c->where("invalid field property", 3);
522 $this->assertTrue(FALSE);
523 } catch (ActiveMongo_Exception
$e) {
524 $this->assertTrue(TRUE);
531 $this->assertTrue(FALSE);
532 } catch (ActiveMongo_Exception
$e) {
533 $this->assertTrue(TRUE);
539 $this->assertTrue(FALSE);
540 } catch (ActiveMongo_Exception
$e) {
541 $this->assertTrue(TRUE);
545 $c->sort("c DESC, field BAR");
546 $this->assertTrue(FALSE);
547 } catch (ActiveMongo_Exception
$e) {
548 $this->assertTrue(TRUE);
551 /* These are valid, so no exception should be thrown */
552 $c->sort("foo ASC, bar DESC");
553 $c->sort("foo DESC");
557 function testFindWithSingleID()
564 $c->find($d->getID());
565 $this->assertEquals(1, $c->count());
566 $this->assertEquals($c->a
, $d->a
);
569 function testFindAndModify()
573 $c->where('int <= ', 1000);
574 $c->where('processing exists', FALSE);
576 $c->sort('int DESC');
577 $c->findAndModify(array("processing" => TRUE));
582 $this->assertEquals($d->processing
, TRUE);
585 $this->assertLessThan($last, $d->int);
590 $this->assertEquals($i, 50);
594 $c->where('int <= ', 1000);
595 $c->where('processing exists', FALSE);
597 $c->findAndModify(array());
598 $this->assertTrue(FALSE);
599 } catch (ActiveMongo_Exception
$e) {
600 $this->assertTrue(TRUE);