Added tests (84,19% of coverance) to release first stable ASAP
[activemongo.git] / tests / ReferencesTest.php
blob940791497817ba0b6b33a71739ce44498ffe8966
1 <?php
3 class ReferencesTest extends PHPUnit_Framework_TestCase
5 public function testReferences()
8 $c = new Model1;
9 $c->a = "foobar";
10 $c->save();
12 $ref = array(
13 '$ref' => 'model1',
14 '$id' => $c->getID(),
15 '$db' => DB,
16 'class' => 'Model1'
18 $this->assertEquals($c->getReference(), $ref);
20 $d = new Model1;
21 $d->a = "barfoo";
22 /* Reference into a document */
23 $d->next = $c;
24 /* References into sub documents */
25 $d->nested = array($c, $c);
26 /* Get Dynamic query; AKA save the query */
27 /* in this case it would be a get all */
28 $query = new Model1;
29 $query->where('a', 'foobar');
30 $query->doQuery();
31 $d->query = $query->getReference(TRUE);
33 $d->save();
37 /**
38 * @depends testReferences
40 public function testReferenceSave()
42 $d = new Model1;
43 $d->where('a', 'barfoo');
45 foreach ($d as $doc) {
46 $this->assertTrue(isset($doc->next));
47 $this->assertTrue(MongoDBRef::isRef($doc->next));
48 $this->assertTrue(MongoDBRef::isRef($doc->nested[0]));
49 $this->assertTrue(MongoDBRef::isRef($doc->nested[1]));
50 $this->assertTrue(MongoDBRef::isRef($doc->query));
52 /* Check dynamic references properties */
53 $this->assertTrue(is_array($doc->query['dynamic']));
54 $this->assertTrue(count($doc->query['dynamic']) > 0);
56 /* Deference */
57 $doc->doDeferencing();
59 /* Test */
60 $this->assertTrue($doc->next InstanceOf Model1);
61 $this->assertTrue($doc->nested[0] InstanceOf Model1);
62 $this->assertTrue($doc->nested[1] InstanceOf Model1);
63 $this->assertTrue(is_array($doc->query));
64 $this->assertTrue($doc->query[0] InstanceOf Model1);
66 /* Testing Iteration in defered documents */
67 /* They should fail because they are cloned */
68 /* instances of a real document */
69 try {
70 $doc->next->next();
71 $this->assertTrue(FALSE);
72 } catch (ActiveMongo_Exception $e) {
73 $this->assertTrue(TRUE);