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 class CompoundKeyModel
extends data\Model
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';
26 protected $fields = array(
33 class PrefixTest
extends data\Model
35 protected $table_prefix = 'test_';
36 protected $table = 'prefix';
38 public function table() { return $this->table
; }
41 class ModelTest
extends \PHPUnit_Framework_TestCase
45 public function setUp()
47 $this->db
= TestModel
::SetUpDatabase();
49 CREATE TABLE test_compound
54 PRIMARY KEY (id_1, id_2)
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());
63 public function testBadCreate()
65 $this->setExpectedException('hoplite\data\ModelException');
66 $model = new TestModel(array('id' => 1));
69 public function testInsert()
71 $model = new TestModel();
72 $model->title
= 'Hello';
73 $model->description
= 'A test';
75 $this->assertEquals(1, $model->id
);
77 $this->assertEquals(2, $model->id
);
80 public function testFetch()
83 $model = new TestModel(1);
85 $this->assertEquals('Hello', $model->title
);
86 $this->assertEquals('A test', $model->description
);
89 public function testFetchCustomCondition()
91 $model = new TestModel();
92 $model->title
= 'test';
93 $model->description
= 'foobar';
96 $model = new TestModel();
97 $model->set_condition('title = :title');
98 $model->title
= 'test';
100 $this->assertEquals('foobar', $model->description
);
103 public function testFetchGroup()
105 $model = new TestModel();
106 $model->title
= 'test';
107 $model->description
= 'foo';
110 $model = new TestModel();
111 $model->title
= 'test';
112 $model->description
= 'bar';
115 $model = new TestModel();
116 $model->title
= 'foo';
117 $model->description
= 'test';
120 $model = new TestModel();
121 $model->title
= 'test';
122 $model->description
= 'baz';
125 $results = TestModel
::FetchGroup('title = ?', 'test');
126 $this->assertEquals(3, count($results));
128 $results = TestModel
::FetchGroup('title = :title', array('title' => 'test'));
129 $this->assertEquals(3, count($results));
131 $results = TestModel
::FetchGroup();
132 $this->assertEquals(4, count($results));
135 public function testUpdate()
137 $model = new TestModel();
138 $model->title
= 'Test Update';
139 $model->description
= 'foobar';
140 $model->value
= 'alpha';
143 $model = new TestModel(1);
144 $model->value
= 'bravo';
147 $model = new TestModel(1);
149 $this->assertEquals('Test Update', $model->title
);
150 $this->assertEquals('foobar', $model->description
);
151 $this->assertEquals('bravo', $model->value
);
154 public function testDelete()
157 $model = new TestModel(1);
160 $model = new TestModel(2);
162 $this->assertEquals('Hello', $model->title
);
164 $this->setExpectedException('hoplite\data\ModelException');
165 $model = new TestModel(1);
169 public function testCompoundBadCreate()
171 $this->setExpectedException('hoplite\data\ModelException');
172 $model = new CompoundKeyModel(1);
175 public function testCompoundInsert()
177 $model = new CompoundKeyModel(array('id_1' => 1, 'id_2' => 2));
178 $model->value
= 'foo';
182 public function testCompoundFetch()
184 $this->testCompoundInsert();
185 $model = new CompoundKeyModel(array('id_1' => 1, 'id_2' => 2));
187 $this->assertEquals('foo', $model->value
);
190 public function testPrefix()
192 $test = new PrefixTest();
193 $this->assertEquals('test_prefix', $test->table());
196 public function testSuccessfulQueryWithTMI()
198 $model = new TestModel();
199 $model->title
= 'Title';
200 $model->description
= 'Desc';
201 $model->value
= 'Value';
204 $model = new TestModel(1);
205 $model->title
= 'Title2';
207 $data = $model->Fetch();
208 $this->assertEquals('Title2', $data->title
);