ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
24use PHPUnit\Framework\TestCase;
25
34class ilDatabaseAtomRunTest extends TestCase
35{
36
40 protected $backupGlobals = false;
57
58
59 protected function setUp() : void
60 {
61 ilUnitUtil::performInitialisation();
62
63 global $ilClientIniFile;
65 $this->ilDBInterfaceGalera->initFromIniFile($ilClientIniFile);
66 $this->ilDBInterfaceGalera->connect();
67
68 $this->ilDBInterfaceGaleraSecond = ilDBWrapperFactory::getWrapper(ilDBConstants::TYPE_GALERA);
69 $this->ilDBInterfaceGaleraSecond->initFromIniFile($ilClientIniFile);
70 $this->ilDBInterfaceGaleraSecond->connect();
71
73 $this->ilDBInterfaceInnoDB->initFromIniFile($ilClientIniFile);
74 $this->ilDBInterfaceInnoDB->connect();
75
77 $this->ilDBInterfaceInnoDBSecond->initFromIniFile($ilClientIniFile);
78 $this->ilDBInterfaceInnoDBSecond->connect();
79
80 $this->setupTable();
81 }
82
83
84 public function tearDown() : void
85 {
86 $this->ilDBInterfaceGalera->dropSequence('il_db_tests_atom');
87 $this->ilDBInterfaceGalera->dropTable('il_db_tests_atom');
88 }
89
90
91 public function testConnection()
92 {
93 $this->assertTrue($this->ilDBInterfaceGalera->connect(true));
94 $this->assertTrue($this->ilDBInterfaceGaleraSecond->connect(true));
95 $this->assertTrue($this->ilDBInterfaceInnoDB->connect(true));
96 }
97
98
99 public function setupTable()
100 {
101 if ($this->ilDBInterfaceGalera->sequenceExists('il_db_tests_atom')) {
102 $this->ilDBInterfaceGalera->dropSequence('il_db_tests_atom');
103 }
104 $this->ilDBInterfaceGalera->createTable('il_db_tests_atom', $fields = array(
105 'id' => array(
106 'type' => 'integer',
107 'length' => 4,
108 'notnull' => true,
109 ),
110 'is_online' => array(
111 'type' => 'integer',
112 'length' => 1,
113 'notnull' => false,
114 ),
115 ), true);
116 $this->ilDBInterfaceGalera->addPrimaryKey('il_db_tests_atom', array( 'id' ));
117 $this->ilDBInterfaceGalera->createSequence('il_db_tests_atom');
118 }
119
120
121 public function testTableExists()
122 {
123 $this->assertTrue($this->ilDBInterfaceGalera->tableExists('il_db_tests_atom'));
124 }
125
126
128 {
129 $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
130 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true);
131 $ilAtomQuery->addQueryCallable($this->getInsertQueryCallable());
132
133 $ilAtomQuery->run();
134
135 $this->assertEquals($this->getExpectedResult(), $this->getResultFromDB());
136 }
137
138
139 public function testWriteWithLocks()
140 {
141 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
142 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true);
143 $ilAtomQuery->addQueryCallable($this->getInsertQueryCallable());
144
145 $ilAtomQuery->run();
146
147 $this->assertEquals($this->getExpectedResult(), $this->getResultFromDB());
148 }
149
150
152 {
153 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
154 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true)->aliasName('my_alias');
155 $ilAtomQuery->addQueryCallable($this->getInsertQueryCallable());
156
157 $ilAtomQuery->run();
158
159 $this->assertEquals($this->getExpectedResult(), $this->getResultFromDB());
160 }
161
162
164 {
165 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
166 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true)->aliasName('my_alias');
167 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true)->aliasName('my_second_alias');
168 $ilAtomQuery->addQueryCallable($this->getInsertQueryCallable());
169
170 $ilAtomQuery->run();
171
172 $this->assertEquals($this->getExpectedResult(), $this->getResultFromDB());
173 }
174
175
177 {
178 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
179 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true);
180 $ilAtomQuery->addTableLock('il_db_tests_atom')->lockSequence(true)->aliasName('my_alias');
181 $ilAtomQuery->addQueryCallable($this->getInsertQueryCallable());
182
183 $ilAtomQuery->run();
184
185 $this->assertEquals($this->getExpectedResult(), $this->getResultFromDB());
186 }
187
188
189 public function testNoTables()
190 {
191 $this->setExpectedException('ilDatabaseException');
192 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
193 $ilAtomQuery->addQueryCallable($this->getInsertQueryCallable());
194
195 $ilAtomQuery->run();
196 }
197
198
199 public function testNoQueries()
200 {
201 $this->setExpectedException('ilDatabaseException');
202 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
203 $ilAtomQuery->addTableLock('il_db_tests_atom');
204
205 $ilAtomQuery->run();
206 }
207
208
210 {
211 $this->ilDBInterfaceGalera->insert('il_db_tests_atom', array(
212 'id' => array( 'integer', $this->ilDBInterfaceGalera->nextId('il_db_tests_atom') ),
213 'is_online' => array( 'integer', 1 ),
214 ));
215
216 // Start a Transaction with one instance and update the same entry as another instance
217 $this->ilDBInterfaceGalera->beginTransaction();
218 $this->ilDBInterfaceGalera->update('il_db_tests_atom', array(
219 'is_online' => array( 'integer', 5 ),
220 ), array( 'id' => array( 'integer', 1 ) ));
221
222 // Update the same entry with another instance (which currently fails due to missing multi-thread in PHP)
223 // $this->ilDBInterfaceGaleraSecond->update('il_db_tests_atom', array(
224 // 'is_online' => array( 'integer', 6 ),
225 // ), array( 'id' => array( 'integer', 1 ) ), true);
226
227 // Commit the other
228 $this->ilDBInterfaceGalera->commit();
229
230 // Check
231 $query = 'SELECT is_online FROM il_db_tests_atom WHERE id = ' . $this->ilDBInterfaceGalera->quote(1, 'integer');
232 $res = $this->ilDBInterfaceGalera->query($query);
233 $d = $this->ilDBInterfaceGalera->fetchAssoc($res);
234
235 $this->assertEquals(5, $d['is_online']);
236 }
237
238
239 public function testUpdateDuringLock()
240 {
241 $this->ilDBInterfaceInnoDB->insert('il_db_tests_atom', array(
242 'id' => array( 'integer', $this->ilDBInterfaceInnoDB->nextId('il_db_tests_atom') ),
243 'is_online' => array( 'integer', 1 ),
244 ));
245 // Start a Transaction with one instance and update the same entry as another instance
246 $this->ilDBInterfaceInnoDB->lockTables(array( array( 'name' => 'il_db_tests_atom', 'type' => ilAtomQuery::LOCK_WRITE ) ));
247 $this->ilDBInterfaceInnoDB->update('il_db_tests_atom', array(
248 'is_online' => array( 'integer', 5 ),
249 ), array( 'id' => array( 'integer', 1 ) ));
250
251 // Update the same entry with another instance (which currently fails due to missing multi-thread in PHP)
252 // $this->ilDBInterfaceInnoDBSecond->update('il_db_tests_atom', array(
253 // 'is_online' => array( 'integer', 6 ),
254 // ), array( 'id' => array( 'integer', 1 ) ), true);
255
256 // Unlock Tables
257 $this->ilDBInterfaceInnoDB->unlockTables();
258
259 // Check
260 $query = 'SELECT is_online FROM il_db_tests_atom WHERE id = ' . $this->ilDBInterfaceInnoDB->quote(1, 'integer');
261 $res = $this->ilDBInterfaceInnoDB->query($query);
262 $d = $this->ilDBInterfaceInnoDB->fetchAssoc($res);
263
264 $this->assertEquals(5, $d['is_online']);
265 }
266
267
268
269 //
270 // HELPERS
271 //
272
276 protected function getInsertQueryCallable()
277 {
278 $query = function (ilDBInterface $ilDB) {
279 $ilDB->insert('il_db_tests_atom', array(
280 'id' => array( 'integer', $ilDB->nextId('il_db_tests_atom') ),
281 'is_online' => array( 'integer', 1 ),
282 ));
283 $ilDB->insert('il_db_tests_atom', array(
284 'id' => array( 'integer', $ilDB->nextId('il_db_tests_atom') ),
285 'is_online' => array( 'integer', 0 ),
286 ));
287 };
288
289 return $query;
290 }
291
292
296 protected function getTableLocksForDbInterface()
297 {
298 $tables = array(
299 array(
300 'name' => 'il_db_tests_atom',
301 'type' => ilAtomQuery::LOCK_WRITE,
302 'sequence' => true,
303 ),
304 );
305
306 return $tables;
307 }
308
309
313 protected function getResultFromDB()
314 {
315 $res = $this->ilDBInterfaceGalera->query('SELECT * FROM il_db_tests_atom');
316 $results = array();
317 while ($d = $this->ilDBInterfaceGalera->fetchAssoc($res)) {
318 $results[] = $d;
319 }
320
321 return $results;
322 }
323
324
328 protected function getExpectedResult()
329 {
330 return array(
331 0 => array(
332 'id' => '1',
333 'is_online' => '1',
334 ),
335 1 => array(
336 'id' => '2',
337 'is_online' => '0',
338 ),
339 );
340 }
341}
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
$results
foreach($_POST as $key=> $value) $res
global $ilDB