1: <?php
2:
3: namespace Alchemy\tests;
4: use Alchemy\core\query\Expression;
5: use Alchemy\core\query\Join;
6: use Alchemy\core\query\Query;
7: use Alchemy\core\schema\Table;
8:
9:
10: class ORMQueryTest extends BaseTest {
11:
12: protected $users;
13: protected $addrs;
14: protected $phones;
15:
16: public function setUp() {
17: $this->users = Table::ORM('users', array(
18: 'columns' => array(
19: 'UserID' => 'Integer(11, primary_key = true)',
20: 'UserName' => 'String',
21: 'Email' => 'String',
22: ) ));
23:
24: $this->addrs = Table::ORM('addresses', array(
25: 'columns' => array(
26: 'UserID' => 'Foreign(users.UserID)',
27: 'AddressType' => 'Integer',
28: 'StreetAddress' => 'String',
29: ),
30: 'relationships' => array(
31: 'User' => 'OneToOne(users, backref = "Address")',
32: ) ));
33:
34: $this->phones = Table::ORM('phones', array(
35: 'columns' => array(
36: 'UserID' => 'Foreign(users.UserID)',
37: 'PhoneNum' => 'String',
38: ),
39: 'relationships' => array(
40: 'User' => 'ManyToOne(users, backref = "Phones")',
41: ) ));
42:
43: $this->users->register(true);
44: $this->addrs->getRelationship('User');
45: $this->phones->getRelationship('User');
46: }
47:
48: public function testTableRelated() {
49: $this->assertFalse($this->users->hasRelationship('Nil'));
50: $this->assertTrue($this->users->hasRelationship('Address'));
51: $this->assertTrue($this->users->hasRelationship('Phones'));
52:
53: $this->assertEquals('OneToOne', $this->users->getRelationship('Address')->getType());
54: $this->assertEquals('OneToMany', $this->users->getRelationship('Phones')->getType());
55:
56: $relationships = array(
57: 'Address' => $this->users->getRelationship('Address'),
58: 'Phones' => $this->users->getRelationship('Phones'));
59: $this->assertEquals($relationships, $this->users->listRelationships());
60:
61: $relationships = array(
62: 'User' => $this->addrs->getRelationship('User'));
63: $this->assertEquals($relationships, $this->addrs->listRelationships());
64:
65: $relationships = array(
66: 'User' => $this->phones->getRelationship('User'));
67: $this->assertEquals($relationships, $this->phones->listRelationships());
68: }
69:
70: public function testRefRelated() {
71: $users = $this->users->getRef();
72: $addrs = $this->addrs->getRef();
73: $phones = $this->phones->getRef();
74:
75: $this->assertInstanceOf('Alchemy\orm\ORMTableRef', $users->Address);
76: $this->assertInstanceOf('Alchemy\orm\ORMTableRef', $users->Phones);
77:
78: $this->assertEquals($this->users, $users->schema());
79: $this->assertEquals($this->addrs, $users->Address->schema());
80: $this->assertEquals($this->users, $users->Address->User->schema());
81: $this->assertEquals($this->phones, $users->Phones->schema());
82:
83: $this->assertEquals($users->Address, $users->Address);
84: $this->assertEquals(array($users->Address, $users->Phones), $users->relationships());
85:
86: $this->assertEquals(null, $users->predicate());
87:
88: $expr = $addrs->User->UserID->equal($addrs->UserID);
89: $this->assertEquals($expr, $addrs->User->predicate());
90:
91: $expr = $users->Address->UserID->equal($users->UserID);
92: $this->assertEquals($expr, $users->Address->predicate());
93:
94: $expr = $users->Address->User->UserID->equal($users->Address->UserID);
95: $this->assertEquals($expr, $users->Address->User->predicate());
96: }
97:
98:
99: public function testSimpleSelect() {
100: $users = $this->users->getRef();
101:
102: $query = Query::ORM($users);
103:
104: $this->assertEquals($users, $query->table());
105: $this->assertEquals($users->columns(), $query->columns());
106: $this->assertEquals(array(), $query->joins());
107: $this->assertEquals(null, $query->where());
108:
109: $expr = Expression::UPPER($users->Address->StreetAddress);
110: $query->STREET = $expr;
111:
112:
113: $query->joins(array($users, $users->Address));
114:
115: $join = new Join(Join::LEFT, Join::INNER, $users->Address, $users->Address->predicate());
116:
117: $this->assertEquals($users, $query->table());
118: $this->assertEquals($users->columns() + array('STREET' => $expr), $query->columns());
119: $this->assertEquals(array($join), $query->joins());
120: $this->assertEquals(null, $query->where());
121: }
122:
123:
124: public function testRelatedSelect() {
125: $users = $this->users->getRef();
126:
127: $query = Query::ORM($users->Phones)
128: ->where($users->UserName->like('ex%'));
129:
130: $join = new Join(Join::LEFT, Join::INNER, $users, null);
131: $expr = Expression::AND_(
132: $users->Phones->predicate(),
133: $users->UserName->like('ex%'));
134:
135: $this->assertEquals($users->Phones, $query->table());
136: $this->assertEquals($users->Phones->columns(), $query->columns());
137: $this->assertEquals(array($join), $query->joins());
138: $this->assertEquals($expr, $query->where());
139:
140:
141: $this->assertEquals($query, $users->Phones->where($users->UserName->like('ex%')));
142: }
143: }
144: