All tests pass again
[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 require_once HOPLITE_ROOT . '/data/model.php';
21
22 class TestModel extends data\Model
23 {
24 protected $table = 'test_table';
25 protected $primary_key = 'id';
26 protected $condition = 'id = :id';
27
28 protected $fields = array(
29 'id',
30 'title',
31 'description',
32 'value',
33 'is_hidden',
34 'reference_id'
35 );
36
37 static public function SetUpDatabase()
38 {
39 $db = new \PDO('sqlite::memory:');
40 $db->SetAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
41 $db->Query("
42 CREATE TABLE test_table
43 (
44 id integer PRIMARY KEY AUTOINCREMENT,
45 title varchar(100),
46 description text,
47 value text,
48 is_hidden boolean,
49 reference_id integer
50 );
51 ");
52 return $db;
53 }
54 }
55
56 class CompoundKeyModel extends data\Model
57 {
58 protected $table = 'test_compound';
59 protected $primary_key = array('id_1', 'id_2');
60 protected $condition = 'id_1 = :id_1 AND id_2 = :id_2';
61
62 protected $fields = array(
63 'id_1',
64 'id_2',
65 'value'
66 );
67 }
68
69 class PrefixTest extends data\Model
70 {
71 protected $table_prefix = 'test_';
72 protected $table = 'prefix';
73
74 public function table() { return $this->table; }
75 }
76
77 class ModelTest extends \PHPUnit_Framework_TestCase
78 {
79 public $db;
80
81 public function setUp()
82 {
83 $this->db = TestModel::SetUpDatabase();
84 $this->db->Query("
85 CREATE TABLE test_compound
86 (
87 id_1 integer,
88 id_2 integer,
89 value text,
90 PRIMARY KEY (id_1, id_2)
91 );
92 ");
93 TestModel::set_db($this->db);
94 $this->assertSame($this->db, TestModel::db());
95 CompoundKeyModel::set_db($this->db);
96 $this->assertSame($this->db, CompoundKeyModel::db());
97 }
98
99 public function testBadCreate()
100 {
101 $this->setExpectedException('hoplite\data\ModelException');
102 $model = new TestModel(array('id' => 1));
103 }
104
105 public function testInsert()
106 {
107 $model = new TestModel();
108 $model->title = 'Hello';
109 $model->description = 'A test';
110 $model->Insert();
111 $this->assertEquals(1, $model->id);
112 $model->Insert();
113 $this->assertEquals(2, $model->id);
114 }
115
116 public function testFetch()
117 {
118 $this->testInsert();
119 $model = new TestModel(1);
120 $model->FetchInto();
121 $this->assertEquals('Hello', $model->title);
122 $this->assertEquals('A test', $model->description);
123 }
124
125 public function testFetchCustomCondition()
126 {
127 $model = new TestModel();
128 $model->title = 'test';
129 $model->description = 'foobar';
130 $model->Insert();
131
132 $model = new TestModel();
133 $model->set_condition('title = :title');
134 $model->title = 'test';
135 $model->FetchInto();
136 $this->assertEquals('foobar', $model->description);
137 }
138
139 public function testFetchGroup()
140 {
141 $model = new TestModel();
142 $model->title = 'test';
143 $model->description = 'foo';
144 $model->Insert();
145
146 $model = new TestModel();
147 $model->title = 'test';
148 $model->description = 'bar';
149 $model->Insert();
150
151 $model = new TestModel();
152 $model->title = 'foo';
153 $model->description = 'test';
154 $model->Insert();
155
156 $model = new TestModel();
157 $model->title = 'test';
158 $model->description = 'baz';
159 $model->Insert();
160
161 $results = TestModel::FetchGroup('title = ?', 'test');
162 $this->assertEquals(3, count($results));
163
164 $results = TestModel::FetchGroup('title = :title', array('title' => 'test'));
165 $this->assertEquals(3, count($results));
166
167 $results = TestModel::FetchGroup();
168 $this->assertEquals(4, count($results));
169 }
170
171 public function testUpdate()
172 {
173 $model = new TestModel();
174 $model->title = 'Test Update';
175 $model->description = 'foobar';
176 $model->value = 'alpha';
177 $model->Insert();
178
179 $model = new TestModel(1);
180 $model->value = 'bravo';
181 $model->Update();
182
183 $model = new TestModel(1);
184 $model->FetchInto();
185 $this->assertEquals('Test Update', $model->title);
186 $this->assertEquals('foobar', $model->description);
187 $this->assertEquals('bravo', $model->value);
188 }
189
190 public function testDelete()
191 {
192 $this->testInsert();
193 $model = new TestModel(1);
194 $model->Delete();
195
196 $model = new TestModel(2);
197 $model->FetchInto();
198 $this->assertEquals('Hello', $model->title);
199
200 $this->setExpectedException('hoplite\data\ModelException');
201 $model = new TestModel(1);
202 $model->FetchInto();
203 }
204
205 public function testCompoundBadCreate()
206 {
207 $this->setExpectedException('hoplite\data\ModelException');
208 $model = new CompoundKeyModel(1);
209 }
210
211 public function testCompoundInsert()
212 {
213 $model = new CompoundKeyModel(array('id_1' => 1, 'id_2' => 2));
214 $model->value = 'foo';
215 $model->Insert();
216 }
217
218 public function testCompoundFetch()
219 {
220 $this->testCompoundInsert();
221 $model = new CompoundKeyModel(array('id_1' => 1, 'id_2' => 2));
222 $model->FetchInto();
223 $this->assertEquals('foo', $model->value);
224 }
225
226 public function testPrefix()
227 {
228 $test = new PrefixTest();
229 $this->assertEquals('test_prefix', $test->table());
230 }
231
232 public function testSuccessfulQueryWithTMI()
233 {
234 $model = new TestModel();
235 $model->title = 'Title';
236 $model->description = 'Desc';
237 $model->value = 'Value';
238 $model->Insert();
239
240 $model = new TestModel(1);
241 $model->title = 'Title2';
242 $model->Update();
243 $data = $model->Fetch();
244 $this->assertEquals('Title2', $data->title);
245 }
246 }