ILIAS  release_7 Revision v7.30-3-g800a261c036
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
24use PHPUnit\Framework\TestCase;
25
39class ilDatabaseAtomBaseTest extends TestCase
40{
41
45 protected $backupGlobals = false;
54
55
56 protected function setUp() : void
57 {
58 ilUnitUtil::performInitialisation();
59
60 global $ilClientIniFile;
62 $this->ilDBInterfaceGalera->initFromIniFile($ilClientIniFile);
63 $this->ilDBInterfaceGalera->connect();
64
66 $this->ilDBInterfaceInnoDB->initFromIniFile($ilClientIniFile);
67 $this->ilDBInterfaceInnoDB->connect();
68 }
69
70
71 public function testGetInstance()
72 {
73 $ilAtomQueryTransaction = $this->ilDBInterfaceGalera->buildAtomQuery();
74 $this->assertEquals($ilAtomQueryTransaction->getIsolationLevel(), ilAtomQuery::ISOLATION_SERIALIZABLE);
75 $this->assertTrue($ilAtomQueryTransaction instanceof ilAtomQueryTransaction);
76
77 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
78 $this->assertEquals($ilAtomQuery->getIsolationLevel(), ilAtomQuery::ISOLATION_SERIALIZABLE);
79 $this->assertTrue($ilAtomQuery instanceof ilAtomQueryLock);
80 }
81
82
83 public function testReadUncommited()
84 {
85 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ISO_WRONG_LEVEL);
86 $other = new ilAtomQueryTransaction($this->ilDBInterfaceGalera, ilAtomQuery::ISOLATION_READ_UNCOMMITED);
87 $other->run();
88 }
89
90
91 public function testReadCommited()
92 {
93 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ISO_WRONG_LEVEL);
94 $other = new ilAtomQueryTransaction($this->ilDBInterfaceGalera, ilAtomQuery::ISOLATION_READ_COMMITED);
95 $other->run();
96 }
97
98
99 public function testReadRepeatedRead()
100 {
101 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ISO_WRONG_LEVEL);
102 $other = new ilAtomQueryTransaction($this->ilDBInterfaceGalera, ilAtomQuery::ISOLATION_REPEATED_READ);
103 $other->run();
104 }
105
106
107 public function testAnomalies()
108 {
109 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ANO_NOT_AVAILABLE);
111 }
112
113
114 public function testLevel()
115 {
116 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ISO_WRONG_LEVEL);
118 }
119
120
121 public function testRisks()
122 {
123 $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
124 $ilAtomQuery->addTableLock('object_data');
125 $this->assertEquals(array(), $ilAtomQuery->getRisks());
126 }
127
128
129 public function testCallables()
130 {
131 $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
132 // Working
133 $this->assertTrue($ilAtomQuery->checkCallable(function (ilDBInterface $ilDBInterface) {
134 })); // ilDBInterface as first Parameter
135 $this->assertTrue($ilAtomQuery->checkCallable(new ilAtomQueryTestHelper())); // Class with implemented __invoke
136
137 // Non working
138 $this->assertFalse($ilAtomQuery->checkCallable(function () {
139 })); // No Parameter
140 $this->assertFalse($ilAtomQuery->checkCallable(function (ilDBInterface $ilDBInterface, $someOtherParameter) {
141 })); // More than one parameter
142 $this->assertFalse($ilAtomQuery->checkCallable(function ($someOtherParameter, ilDBInterface $ilDBInterface) {
143 })); // ilDBInterface not first parameter
144 $this->assertFalse($ilAtomQuery->checkCallable(function (ilDBPdoMySQL $ilDBInterface) {
145 })); // not ilDBInterface
146 $this->assertFalse($ilAtomQuery->checkCallable(function ($ilDBInterface) {
147 })); // not ilDBInterface
148 function noClosure()
149 {
150 }
151
152 $this->assertFalse($ilAtomQuery->checkCallable('noClosure')); // Not a Closure
153 }
154
155
156 public function testWrongIsolationLevel()
157 {
158 $this->setExpectedException('ilDatabaseException');
159 $ilAtomQuery = new ilAtomQueryTransaction($this->ilDBInterfaceGalera, 'non_existing');
160 $ilAtomQuery->addTableLock('il_db_tests_atom');
161 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) {
162 $ilDB->getDBType();
163 });
164 $ilAtomQuery->run();
165 }
166
167
169 {
170 $counter = 0;
171 $max = 5;
172 $result = null;
173 $query = function (ilDBInterface $ilDBInterface) use (&$counter, &$max, &$result) {
174 if ($counter < $max) {
175 $counter++;
177 }
178 $result = $ilDBInterface->listTables();
179 };
180
181 $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
182 $ilAtomQuery->addQueryCallable($query);
183 $ilAtomQuery->addTableLock('object_data');
184 $ilAtomQuery->run();
185 $this->assertTrue(is_array($result));
186 }
187
188
190 {
191 $this->setExpectedException('ilDatabaseException', ilDatabaseException::DB_GENERAL);
192 $counter = 0;
193 $max = 10;
194 $result = null;
195 $query = function (ilDBInterface $ilDBInterface) use (&$counter, &$max, &$result) {
196 if ($counter < $max) {
197 $counter++;
199 }
200 $result = $ilDBInterface->listTables();
201 };
202
203 $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
204 $ilAtomQuery->addQueryCallable($query);
205 $ilAtomQuery->addTableLock('object_data');
206
207 $ilAtomQuery->run();
208
209 $this->assertTrue(is_null($result));
210 }
211
212
213 public function testWithOutLocks()
214 {
215 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_LOCK_NO_TABLE);
216 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
217 $ilAtomQuery->run();
218 }
219
220
221 public function testWithOutClosures()
222 {
223 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_CLOSURE_NONE);
224 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
225 $ilAtomQuery->addTableLock('object_data');
226 $ilAtomQuery->run();
227 }
228
229
230 public function testMultipleClosures()
231 {
232 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_CLOSURE_ALREADY_SET);
233 $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
234 $ilAtomQuery->addTableLock('object_data');
235 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDBInterface) {
236 });
237 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDBInterface) {
238 });
239 }
240
241
242 public function testLockSameTable()
243 {
244 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_IDENTICAL_TABLES);
245 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
246 $ilAtomQuery->addTableLock('il_db_tests_atom');
247 $ilAtomQuery->addTableLock('il_db_tests_atom');
248 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDBInterface) {
249 });
250 $ilAtomQuery->run();
251 }
252
253
255 {
256 $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_IDENTICAL_TABLES);
257 $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
258 $ilAtomQuery->addTableLock('il_db_tests_atom')->aliasName('alias_one');
259 $ilAtomQuery->addTableLock('il_db_tests_atom')->aliasName('alias_one');
260 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDBInterface) {
261 });
262 $ilAtomQuery->run();
263 }
264}
$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.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$query
global $ilDB