ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilDatabaseAtomBaseTest.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
38{
39
43 protected $backupGlobals = false;
52
53
54 protected function setUp()
55 {
56 require_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
57 ilUnitUtil::performInitialisation();
58 require_once('./Services/Database/classes/Atom/class.ilAtomQueryBase.php');
59 require_once('./Services/Database/classes/Atom/class.ilAtomQueryTransaction.php');
60 require_once('./Services/Database/classes/Atom/class.ilAtomQueryLock.php');
61 require_once('./Services/Database/classes/class.ilDBWrapperFactory.php');
62
63 global $ilClientIniFile;
64 $this->ilDBInterfaceGalera = ilDBWrapperFactory::getWrapper(ilDBConstants::TYPE_GALERA);
65 $this->ilDBInterfaceGalera->initFromIniFile($ilClientIniFile);
66 $this->ilDBInterfaceGalera->connect();
67
68 $this->ilDBInterfaceInnoDB = ilDBWrapperFactory::getWrapper(ilDBConstants::TYPE_PDO_MYSQL_INNODB);
69 $this->ilDBInterfaceInnoDB->initFromIniFile($ilClientIniFile);
70 $this->ilDBInterfaceInnoDB->connect();
71 }
72
73
74 public function testGetInstance()
75 {
76 $ilAtomQueryTransaction = $this->ilDBInterfaceGalera->buildAtomQuery();
77 $this->assertEquals($ilAtomQueryTransaction->getIsolationLevel(), ilAtomQuery::ISOLATION_SERIALIZABLE);
78 $this->assertTrue($ilAtomQueryTransaction instanceof ilAtomQueryTransaction);
79
80 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
81 $this->assertEquals($ilAtomQuery->getIsolationLevel(), ilAtomQuery::ISOLATION_SERIALIZABLE);
82 $this->assertTrue($ilAtomQuery instanceof ilAtomQueryLock);
83 }
84
85
86 public function testReadUncommited()
87 {
88 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ISO_WRONG_LEVEL);
89 $other = new ilAtomQueryTransaction($this->ilDBInterfaceGalera, ilAtomQuery::ISOLATION_READ_UNCOMMITED);
90 $other->run();
91 }
92
93
94 public function testReadCommited()
95 {
96 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ISO_WRONG_LEVEL);
97 $other = new ilAtomQueryTransaction($this->ilDBInterfaceGalera, ilAtomQuery::ISOLATION_READ_COMMITED);
98 $other->run();
99 }
100
101
102 public function testReadRepeatedRead()
103 {
104 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ISO_WRONG_LEVEL);
105 $other = new ilAtomQueryTransaction($this->ilDBInterfaceGalera, ilAtomQuery::ISOLATION_REPEATED_READ);
106 $other->run();
107 }
108
109
110 public function testAnomalies()
111 {
112 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ANO_NOT_AVAILABLE);
114 }
115
116
117 public function testLevel()
118 {
119 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ISO_WRONG_LEVEL);
121 }
122
123
124 public function testRisks()
125 {
126 $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
127 $ilAtomQuery->addTableLock('object_data');
128 $this->assertEquals(array(), $ilAtomQuery->getRisks());
129 }
130
131
132 public function testCallables()
133 {
134 require_once('./Services/Database/classes/PDO/class.ilDBPdoMySQL.php');
135 require_once('./Services/Database/test/Atom/data/class.ilAtomQueryTestHelper.php');
136
137 $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
138 // Working
139 $this->assertTrue($ilAtomQuery->checkCallable(function (ilDBInterface $ilDBInterface) {
140 })); // ilDBInterface as first Parameter
141 $this->assertTrue($ilAtomQuery->checkCallable(new ilAtomQueryTestHelper())); // Class with implemented __invoke
142
143 // Non working
144 $this->assertFalse($ilAtomQuery->checkCallable(function () {
145 })); // No Parameter
146 $this->assertFalse($ilAtomQuery->checkCallable(function (ilDBInterface $ilDBInterface, $someOtherParameter) {
147 })); // More than one parameter
148 $this->assertFalse($ilAtomQuery->checkCallable(function ($someOtherParameter, ilDBInterface $ilDBInterface) {
149 })); // ilDBInterface not first parameter
150 $this->assertFalse($ilAtomQuery->checkCallable(function (ilDBPdoMySQL $ilDBInterface) {
151 })); // not ilDBInterface
152 $this->assertFalse($ilAtomQuery->checkCallable(function ($ilDBInterface) {
153 })); // not ilDBInterface
154 function noClosure()
155 {
156 }
157
158 $this->assertFalse($ilAtomQuery->checkCallable('noClosure')); // Not a Closure
159 }
160
161
162 public function testWrongIsolationLevel()
163 {
164 $this->setExpectedException('ilDatabaseException');
165 $ilAtomQuery = new ilAtomQueryTransaction($this->ilDBInterfaceGalera, 'non_existing');
166 $ilAtomQuery->addTableLock('il_db_tests_atom');
167 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) {
168 $ilDB->getDBType();
169 });
170 $ilAtomQuery->run();
171 }
172
173
175 {
176 $counter = 0;
177 $max = 5;
178 $result = null;
179 $query = function (ilDBInterface $ilDBInterface) use (&$counter, &$max, &$result) {
180 if ($counter < $max) {
181 $counter++;
183 }
184 $result = $ilDBInterface->listTables();
185 };
186
187 $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
188 $ilAtomQuery->addQueryCallable($query);
189 $ilAtomQuery->addTableLock('object_data');
190 $ilAtomQuery->run();
191 $this->assertTrue(is_array($result));
192 }
193
194
196 {
197 $this->setExpectedException('ilDatabaseException', ilDatabaseException::DB_GENERAL);
198 $counter = 0;
199 $max = 10;
200 $result = null;
201 $query = function (ilDBInterface $ilDBInterface) use (&$counter, &$max, &$result) {
202 if ($counter < $max) {
203 $counter++;
205 }
206 $result = $ilDBInterface->listTables();
207 };
208
209 $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
210 $ilAtomQuery->addQueryCallable($query);
211 $ilAtomQuery->addTableLock('object_data');
212
213 $ilAtomQuery->run();
214
215 $this->assertTrue(is_null($result));
216 }
217
218
219 public function testWithOutLocks()
220 {
221 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_LOCK_NO_TABLE);
222 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
223 $ilAtomQuery->run();
224 }
225
226
227 public function testWithOutClosures()
228 {
229 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_CLOSURE_NONE);
230 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
231 $ilAtomQuery->addTableLock('object_data');
232 $ilAtomQuery->run();
233 }
234
235
236 public function testMultipleClosures()
237 {
238 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_CLOSURE_ALREADY_SET);
239 $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
240 $ilAtomQuery->addTableLock('object_data');
241 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDBInterface) {
242 });
243 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDBInterface) {
244 });
245 }
246
247
248 public function testLockSameTable()
249 {
250 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_IDENTICAL_TABLES);
251 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
252 $ilAtomQuery->addTableLock('il_db_tests_atom');
253 $ilAtomQuery->addTableLock('il_db_tests_atom');
254 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDBInterface) {
255 });
256 $ilAtomQuery->run();
257 }
258
259
261 {
262 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_IDENTICAL_TABLES);
263 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
264 $ilAtomQuery->addTableLock('il_db_tests_atom')->aliasName('alias_one');
265 $ilAtomQuery->addTableLock('il_db_tests_atom')->aliasName('alias_one');
266 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDBInterface) {
267 });
268 $ilAtomQuery->run();
269 }
270}
$result
An exception for terminatinating execution or to throw for unit testing.
static checkAnomaly($anomalie)
static checkIsolationLevel($isolation_level)
Class ilAtomQueryLock.
Class ilAtomQueryTestHelper.
Class ilAtomQueryTransaction.
Class ilDBPdoMySQL.
TestCase for the ilDatabaseAtomBaseTest.
Class ilDatabaseException.
Interface ilDBInterface.
$query
global $ilDB