forked from PhilJ/symfony-sandbox
/
QueryTest.php
119 lines (92 loc) · 3.21 KB
/
QueryTest.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
<?php
namespace Doctrine\ODM\MongoDB\Tests;
use Doctrine\ODM\MongoDB\QueryBuilder;
class QueryTest extends BaseTest
{
public function testThatOrAcceptsAnotherQuery()
{
$kris = new Person('Kris');
$chris = new Person('Chris');
$this->dm->persist($kris);
$this->dm->persist($chris);
$this->dm->flush();
$class = __NAMESPACE__.'\Person';
$expression1 = array('firstName' => 'Kris');
$expression2 = array('firstName' => 'Chris');
$qb = $this->dm->createQueryBuilder($class);
$qb->addOr($qb->expr()->field('firstName')->equals('Kris'));
$qb->addOr($qb->expr()->field('firstName')->equals('Chris'));
$this->assertEquals(array('$or' => array(
array('firstName' => 'Kris'),
array('firstName' => 'Chris')
)), $qb->getQueryArray());
$query = $qb->getQuery();
$users = $query->execute();
$this->assertInstanceOf('Doctrine\MongoDB\Cursor', $users);
$this->assertEquals(2, count($users));
}
public function testReferences()
{
$kris = new Person('Kris');
$jon = new Person('Jon');
$this->dm->persist($kris);
$this->dm->persist($jon);
$this->dm->flush();
$kris->bestFriend = $jon;
$this->dm->flush();
$qb = $this->dm->createQueryBuilder(__NAMESPACE__.'\Person');
$qb->field('bestFriend')->references($jon);
$queryArray = $qb->getQueryArray();
$this->assertEquals(array(
'bestFriend.$ref' => 'people',
'bestFriend.$id' => new \MongoId($jon->id),
'bestFriend.$db' => 'doctrine_odm_tests',
'bestFriend._doctrine_class_name' => 'Doctrine\ODM\MongoDB\Tests\Person',
), $queryArray);
$query = $qb->getQuery();
$this->assertEquals(1, $query->count());
$this->assertSame($kris, $query->getSingleResult());
}
public function testIncludesReferenceTo()
{
$kris = new Person('Kris');
$jon = new Person('Jon');
$this->dm->persist($kris);
$this->dm->persist($jon);
$this->dm->flush();
$jon->friends[] = $kris;
$this->dm->flush();
$qb = $this->dm->createQueryBuilder(__NAMESPACE__.'\Person');
$qb->field('friends')->includesReferenceTo($kris);
$queryArray = $qb->getQueryArray();
$this->assertEquals(array(
'friends' => array(
'$elemMatch' => array(
'$ref' => 'people',
'$id' => new \MongoId($kris->id),
'$db' => 'doctrine_odm_tests',
'_doctrine_class_name' => 'Doctrine\ODM\MongoDB\Tests\Person',
),
),
), $queryArray);
$query = $qb->getQuery();
$this->assertEquals(1, $query->count());
$this->assertSame($jon, $query->getSingleResult());
}
}
/** @Document(collection="people") */
class Person
{
/** @Id */
public $id;
/** @String */
public $firstName;
/** @ReferenceOne */
public $bestFriend;
/** @ReferenceMany */
public $friends = array();
public function __construct($firstName)
{
$this->firstName = $firstName;
}
}