ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
ilDatabaseAtomRunTest.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2009 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
33{
34
38 protected $backupGlobals = false;
55
56
57 protected function setUp()
58 {
59 require_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
60 ilUnitUtil::performInitialisation();
61
62 global $ilClientIniFile;
63 $this->ilDBInterfaceGalera = ilDBWrapperFactory::getWrapper(ilDBConstants::TYPE_GALERA);
64 $this->ilDBInterfaceGalera->initFromIniFile($ilClientIniFile);
65 $this->ilDBInterfaceGalera->connect();
66
67 $this->ilDBInterfaceGaleraSecond = ilDBWrapperFactory::getWrapper(ilDBConstants::TYPE_GALERA);
68 $this->ilDBInterfaceGaleraSecond->initFromIniFile($ilClientIniFile);
69 $this->ilDBInterfaceGaleraSecond->connect();
70
71 $this->ilDBInterfaceInnoDB = ilDBWrapperFactory::getWrapper(ilDBConstants::TYPE_PDO_MYSQL_INNODB);
72 $this->ilDBInterfaceInnoDB->initFromIniFile($ilClientIniFile);
73 $this->ilDBInterfaceInnoDB->connect();
74
75 $this->ilDBInterfaceInnoDBSecond = ilDBWrapperFactory::getWrapper(ilDBConstants::TYPE_PDO_MYSQL_INNODB);
76 $this->ilDBInterfaceInnoDBSecond->initFromIniFile($ilClientIniFile);
77 $this->ilDBInterfaceInnoDBSecond->connect();
78
79 $this->setupTable();
80 }
81
82
83 public function tearDown()
84 {
85 $this->ilDBInterfaceGalera->dropSequence('il_db_tests_atom');
86 $this->ilDBInterfaceGalera->dropTable('il_db_tests_atom');
87 }
88
89
90 public function testConnection()
91 {
92 $this->assertTrue($this->ilDBInterfaceGalera->connect(true));
93 $this->assertTrue($this->ilDBInterfaceGaleraSecond->connect(true));
94 $this->assertTrue($this->ilDBInterfaceInnoDB->connect(true));
95 }
96
97
98 public function setupTable()
99 {
100 if ($this->ilDBInterfaceGalera->sequenceExists('il_db_tests_atom')) {
101 $this->ilDBInterfaceGalera->dropSequence('il_db_tests_atom');
102 }
103 $this->ilDBInterfaceGalera->createTable('il_db_tests_atom', $fields = array(
104 'id' => array(
105 'type' => 'integer',
106 'length' => 4,
107 'notnull' => true,
108 ),
109 'is_online' => array(
110 'type' => 'integer',
111 'length' => 1,
112 'notnull' => false,
113 ),
114 ), true);
115 $this->ilDBInterfaceGalera->addPrimaryKey('il_db_tests_atom', array( 'id' ));
116 $this->ilDBInterfaceGalera->createSequence('il_db_tests_atom');
117 }
118
119
120 public function testTableExists()
121 {
122 $this->assertTrue($this->ilDBInterfaceGalera->tableExists('il_db_tests_atom'));
123 }
124
125
127 {
128 $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
129 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true);
130 $ilAtomQuery->addQueryCallable($this->getInsertQueryCallable());
131
132 $ilAtomQuery->run();
133
134 $this->assertEquals($this->getExpectedResult(), $this->getResultFromDB());
135 }
136
137
138 public function testWriteWithLocks()
139 {
140 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
141 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true);
142 $ilAtomQuery->addQueryCallable($this->getInsertQueryCallable());
143
144 $ilAtomQuery->run();
145
146 $this->assertEquals($this->getExpectedResult(), $this->getResultFromDB());
147 }
148
149
151 {
152 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
153 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true)->aliasName('my_alias');
154 $ilAtomQuery->addQueryCallable($this->getInsertQueryCallable());
155
156 $ilAtomQuery->run();
157
158 $this->assertEquals($this->getExpectedResult(), $this->getResultFromDB());
159 }
160
161
163 {
164 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
165 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true)->aliasName('my_alias');
166 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true)->aliasName('my_second_alias');
167 $ilAtomQuery->addQueryCallable($this->getInsertQueryCallable());
168
169 $ilAtomQuery->run();
170
171 $this->assertEquals($this->getExpectedResult(), $this->getResultFromDB());
172 }
173
174
176 {
177 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
178 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true);
179 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true)->aliasName('my_alias');
180 $ilAtomQuery->addQueryCallable($this->getInsertQueryCallable());
181
182 $ilAtomQuery->run();
183
184 $this->assertEquals($this->getExpectedResult(), $this->getResultFromDB());
185 }
186
187
188 public function testNoTables()
189 {
190 $this->setExpectedException('ilDatabaseException');
191 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
192 $ilAtomQuery->addQueryCallable($this->getInsertQueryCallable());
193
194 $ilAtomQuery->run();
195 }
196
197
198 public function testNoQueries()
199 {
200 $this->setExpectedException('ilDatabaseException');
201 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
202 $ilAtomQuery->addTableLock('il_db_tests_atom');
203
204 $ilAtomQuery->run();
205 }
206
207
209 {
210 $this->ilDBInterfaceGalera->insert('il_db_tests_atom', array(
211 'id' => array( 'integer', $this->ilDBInterfaceGalera->nextId('il_db_tests_atom') ),
212 'is_online' => array( 'integer', 1 ),
213 ));
214
215 // Start a Transaction with one instance and update the same entry as another instance
216 $this->ilDBInterfaceGalera->beginTransaction();
217 $this->ilDBInterfaceGalera->update('il_db_tests_atom', array(
218 'is_online' => array( 'integer', 5 ),
219 ), array( 'id' => array( 'integer', 1 ) ));
220
221 // Update the same entry with another instance (which currently fails due to missing multi-thread in PHP)
222 // $this->ilDBInterfaceGaleraSecond->update('il_db_tests_atom', array(
223 // 'is_online' => array( 'integer', 6 ),
224 // ), array( 'id' => array( 'integer', 1 ) ), true);
225
226 // Commit the other
227 $this->ilDBInterfaceGalera->commit();
228
229 // Check
230 $query = 'SELECT is_online FROM il_db_tests_atom WHERE id = ' . $this->ilDBInterfaceGalera->quote(1, 'integer');
231 $res = $this->ilDBInterfaceGalera->query($query);
232 $d = $this->ilDBInterfaceGalera->fetchAssoc($res);
233
234 $this->assertEquals(5, $d['is_online']);
235 }
236
237
238 public function testUpdateDuringLock()
239 {
240 $this->ilDBInterfaceInnoDB->insert('il_db_tests_atom', array(
241 'id' => array( 'integer', $this->ilDBInterfaceInnoDB->nextId('il_db_tests_atom') ),
242 'is_online' => array( 'integer', 1 ),
243 ));
244 // Start a Transaction with one instance and update the same entry as another instance
245 $this->ilDBInterfaceInnoDB->lockTables(array( array( 'name' => 'il_db_tests_atom', 'type' => ilAtomQuery::LOCK_WRITE ) ));
246 $this->ilDBInterfaceInnoDB->update('il_db_tests_atom', array(
247 'is_online' => array( 'integer', 5 ),
248 ), array( 'id' => array( 'integer', 1 ) ));
249
250 // Update the same entry with another instance (which currently fails due to missing multi-thread in PHP)
251 // $this->ilDBInterfaceInnoDBSecond->update('il_db_tests_atom', array(
252 // 'is_online' => array( 'integer', 6 ),
253 // ), array( 'id' => array( 'integer', 1 ) ), true);
254
255 // Unlock Tables
256 $this->ilDBInterfaceInnoDB->unlockTables();
257
258 // Check
259 $query = 'SELECT is_online FROM il_db_tests_atom WHERE id = ' . $this->ilDBInterfaceInnoDB->quote(1, 'integer');
260 $res = $this->ilDBInterfaceInnoDB->query($query);
261 $d = $this->ilDBInterfaceInnoDB->fetchAssoc($res);
262
263 $this->assertEquals(5, $d['is_online']);
264 }
265
266
267
268 //
269 // HELPERS
270 //
271
275 protected function getInsertQueryCallable()
276 {
277 $query = function (ilDBInterface $ilDB) {
278 $ilDB->insert('il_db_tests_atom', array(
279 'id' => array( 'integer', $ilDB->nextId('il_db_tests_atom') ),
280 'is_online' => array( 'integer', 1 ),
281 ));
282 $ilDB->insert('il_db_tests_atom', array(
283 'id' => array( 'integer', $ilDB->nextId('il_db_tests_atom') ),
284 'is_online' => array( 'integer', 0 ),
285 ));
286 };
287
288 return $query;
289 }
290
291
295 protected function getTableLocksForDbInterface()
296 {
297 $tables = array(
298 array(
299 'name' => 'il_db_tests_atom',
300 'type' => ilAtomQuery::LOCK_WRITE,
301 'sequence' => true,
302 ),
303 );
304
305 return $tables;
306 }
307
308
312 protected function getResultFromDB()
313 {
314 $res = $this->ilDBInterfaceGalera->query('SELECT * FROM il_db_tests_atom');
315 $results = array();
316 while ($d = $this->ilDBInterfaceGalera->fetchAssoc($res)) {
317 $results[] = $d;
318 }
319
320 return $results;
321 }
322
323
327 protected function getExpectedResult()
328 {
329 return array(
330 0 => array(
331 'id' => '1',
332 'is_online' => '1',
333 ),
334 1 => array(
335 'id' => '2',
336 'is_online' => '0',
337 ),
338 );
339 }
340}
An exception for terminatinating execution or to throw for unit testing.
TestCase for the ilDatabaseAtomBaseTest.
for( $i=6;$i< 13;$i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
Interface ilDBInterface.
$query
foreach($_POST as $key=> $value) $res
global $ilDB
$results
Definition: svg-scanner.php:47