3 // Copyright (c) 2011 Blue Static
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.
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
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/>.
17 namespace hoplite
\test
;
18 use \hoplite\data
as data
;
20 require_once HOPLITE_ROOT
. '/data/model.php';
22 class TestModel
extends data\Model
24 protected $table = 'test_table';
25 protected $primary_key = 'id';
26 protected $condition = 'id = :id';
28 protected $fields = array(
37 static public function SetUpDatabase()
39 $db = new \
PDO('sqlite::memory:');
40 $db->SetAttribute(\PDO
::ATTR_ERRMODE
, \PDO
::ERRMODE_EXCEPTION
);
42 CREATE TABLE test_table
44 id integer PRIMARY KEY AUTOINCREMENT,
56 class CompoundKeyModel
extends data\Model
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';
62 protected $fields = array(
69 class PrefixTest
extends data\Model
71 protected $table_prefix = 'test_';
72 protected $table = 'prefix';
74 public function table() { return $this->table
; }
77 class ModelTest
extends \PHPUnit_Framework_TestCase
81 public function setUp()
83 $this->db
= TestModel
::SetUpDatabase();
85 CREATE TABLE test_compound
90 PRIMARY KEY (id_1, id_2)
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());
99 public function testBadCreate()
101 $this->setExpectedException('hoplite\data\ModelException');
102 $model = new TestModel(array('id' => 1));
105 public function testInsert()
107 $model = new TestModel();
108 $model->title
= 'Hello';
109 $model->description
= 'A test';
111 $this->assertEquals(1, $model->id
);
113 $this->assertEquals(2, $model->id
);
116 public function testFetch()
119 $model = new TestModel(1);
121 $this->assertEquals('Hello', $model->title
);
122 $this->assertEquals('A test', $model->description
);
125 public function testFetchCustomCondition()
127 $model = new TestModel();
128 $model->title
= 'test';
129 $model->description
= 'foobar';
132 $model = new TestModel();
133 $model->set_condition('title = :title');
134 $model->title
= 'test';
136 $this->assertEquals('foobar', $model->description
);
139 public function testFetchGroup()
141 $model = new TestModel();
142 $model->title
= 'test';
143 $model->description
= 'foo';
146 $model = new TestModel();
147 $model->title
= 'test';
148 $model->description
= 'bar';
151 $model = new TestModel();
152 $model->title
= 'foo';
153 $model->description
= 'test';
156 $model = new TestModel();
157 $model->title
= 'test';
158 $model->description
= 'baz';
161 $results = TestModel
::FetchGroup('title = ?', 'test');
162 $this->assertEquals(3, count($results));
164 $results = TestModel
::FetchGroup('title = :title', array('title' => 'test'));
165 $this->assertEquals(3, count($results));
167 $results = TestModel
::FetchGroup();
168 $this->assertEquals(4, count($results));
171 public function testUpdate()
173 $model = new TestModel();
174 $model->title
= 'Test Update';
175 $model->description
= 'foobar';
176 $model->value
= 'alpha';
179 $model = new TestModel(1);
180 $model->value
= 'bravo';
183 $model = new TestModel(1);
185 $this->assertEquals('Test Update', $model->title
);
186 $this->assertEquals('foobar', $model->description
);
187 $this->assertEquals('bravo', $model->value
);
190 public function testDelete()
193 $model = new TestModel(1);
196 $model = new TestModel(2);
198 $this->assertEquals('Hello', $model->title
);
200 $this->setExpectedException('hoplite\data\ModelException');
201 $model = new TestModel(1);
205 public function testCompoundBadCreate()
207 $this->setExpectedException('hoplite\data\ModelException');
208 $model = new CompoundKeyModel(1);
211 public function testCompoundInsert()
213 $model = new CompoundKeyModel(array('id_1' => 1, 'id_2' => 2));
214 $model->value
= 'foo';
218 public function testCompoundFetch()
220 $this->testCompoundInsert();
221 $model = new CompoundKeyModel(array('id_1' => 1, 'id_2' => 2));
223 $this->assertEquals('foo', $model->value
);
226 public function testPrefix()
228 $test = new PrefixTest();
229 $this->assertEquals('test_prefix', $test->table());
232 public function testSuccessfulQueryWithTMI()
234 $model = new TestModel();
235 $model->title
= 'Title';
236 $model->description
= 'Desc';
237 $model->value
= 'Value';
240 $model = new TestModel(1);
241 $model->title
= 'Title2';
243 $data = $model->Fetch();
244 $this->assertEquals('Title2', $data->title
);