Add data\Model from phalanx
[hoplite.git] / testing / tests / data / model_test.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\test;
18 use \hoplite\data as data;
19
20 class CompoundKeyModel extends data\Model
21 {
22 protected $table = 'test_compound';
23 protected $primary_key = array('id_1', 'id_2');
24 protected $condition = 'id_1 = :id_1 AND id_2 = :id_2';
25
26 protected $fields = array(
27 'id_1',
28 'id_2',
29 'value'
30 );
31 }
32
33 class PrefixTest extends data\Model
34 {
35 protected $table_prefix = 'test_';
36 protected $table = 'prefix';
37
38 public function table() { return $this->table; }
39 }
40
41 class ModelTest extends \PHPUnit_Framework_TestCase
42 {
43 public $db;
44
45 public function setUp()
46 {
47 $this->db = TestModel::SetUpDatabase();
48 $this->db->Query("
49 CREATE TABLE test_compound
50 (
51 id_1 integer,
52 id_2 integer,
53 value text,
54 PRIMARY KEY (id_1, id_2)
55 );
56 ");
57 TestModel::set_db($this->db);
58 $this->assertSame($this->db, TestModel::db());
59 CompoundKeyModel::set_db($this->db);
60 $this->assertSame($this->db, CompoundKeyModel::db());
61 }
62
63 public function testBadCreate()
64 {
65 $this->setExpectedException('hoplite\data\ModelException');
66 $model = new TestModel(array('id' => 1));
67 }
68
69 public function testInsert()
70 {
71 $model = new TestModel();
72 $model->title = 'Hello';
73 $model->description = 'A test';
74 $model->Insert();
75 $this->assertEquals(1, $model->id);
76 $model->Insert();
77 $this->assertEquals(2, $model->id);
78 }
79
80 public function testFetch()
81 {
82 $this->testInsert();
83 $model = new TestModel(1);
84 $model->FetchInto();
85 $this->assertEquals('Hello', $model->title);
86 $this->assertEquals('A test', $model->description);
87 }
88
89 public function testFetchCustomCondition()
90 {
91 $model = new TestModel();
92 $model->title = 'test';
93 $model->description = 'foobar';
94 $model->Insert();
95
96 $model = new TestModel();
97 $model->set_condition('title = :title');
98 $model->title = 'test';
99 $model->FetchInto();
100 $this->assertEquals('foobar', $model->description);
101 }
102
103 public function testFetchGroup()
104 {
105 $model = new TestModel();
106 $model->title = 'test';
107 $model->description = 'foo';
108 $model->Insert();
109
110 $model = new TestModel();
111 $model->title = 'test';
112 $model->description = 'bar';
113 $model->Insert();
114
115 $model = new TestModel();
116 $model->title = 'foo';
117 $model->description = 'test';
118 $model->Insert();
119
120 $model = new TestModel();
121 $model->title = 'test';
122 $model->description = 'baz';
123 $model->Insert();
124
125 $results = TestModel::FetchGroup('title = ?', 'test');
126 $this->assertEquals(3, count($results));
127
128 $results = TestModel::FetchGroup('title = :title', array('title' => 'test'));
129 $this->assertEquals(3, count($results));
130
131 $results = TestModel::FetchGroup();
132 $this->assertEquals(4, count($results));
133 }
134
135 public function testUpdate()
136 {
137 $model = new TestModel();
138 $model->title = 'Test Update';
139 $model->description = 'foobar';
140 $model->value = 'alpha';
141 $model->Insert();
142
143 $model = new TestModel(1);
144 $model->value = 'bravo';
145 $model->Update();
146
147 $model = new TestModel(1);
148 $model->FetchInto();
149 $this->assertEquals('Test Update', $model->title);
150 $this->assertEquals('foobar', $model->description);
151 $this->assertEquals('bravo', $model->value);
152 }
153
154 public function testDelete()
155 {
156 $this->testInsert();
157 $model = new TestModel(1);
158 $model->Delete();
159
160 $model = new TestModel(2);
161 $model->FetchInto();
162 $this->assertEquals('Hello', $model->title);
163
164 $this->setExpectedException('hoplite\data\ModelException');
165 $model = new TestModel(1);
166 $model->FetchInto();
167 }
168
169 public function testCompoundBadCreate()
170 {
171 $this->setExpectedException('hoplite\data\ModelException');
172 $model = new CompoundKeyModel(1);
173 }
174
175 public function testCompoundInsert()
176 {
177 $model = new CompoundKeyModel(array('id_1' => 1, 'id_2' => 2));
178 $model->value = 'foo';
179 $model->Insert();
180 }
181
182 public function testCompoundFetch()
183 {
184 $this->testCompoundInsert();
185 $model = new CompoundKeyModel(array('id_1' => 1, 'id_2' => 2));
186 $model->FetchInto();
187 $this->assertEquals('foo', $model->value);
188 }
189
190 public function testPrefix()
191 {
192 $test = new PrefixTest();
193 $this->assertEquals('test_prefix', $test->table());
194 }
195
196 public function testSuccessfulQueryWithTMI()
197 {
198 $model = new TestModel();
199 $model->title = 'Title';
200 $model->description = 'Desc';
201 $model->value = 'Value';
202 $model->Insert();
203
204 $model = new TestModel(1);
205 $model->title = 'Title2';
206 $model->Update();
207 $data = $model->Fetch();
208 $this->assertEquals('Title2', $data->title);
209 }
210 }