ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
42  protected $backupGlobals = false;
51 
52 
53  protected function setUp() {
54  require_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
55  ilUnitUtil::performInitialisation();
56  require_once('./Services/Database/classes/Atom/class.ilAtomQueryBase.php');
57  require_once('./Services/Database/classes/Atom/class.ilAtomQueryTransaction.php');
58  require_once('./Services/Database/classes/Atom/class.ilAtomQueryLock.php');
59  require_once('./Services/Database/classes/class.ilDBWrapperFactory.php');
60 
61  global $ilClientIniFile;
62  $this->ilDBInterfaceGalera = ilDBWrapperFactory::getWrapper(ilDBConstants::TYPE_GALERA);
63  $this->ilDBInterfaceGalera->initFromIniFile($ilClientIniFile);
64  $this->ilDBInterfaceGalera->connect();
65 
66  $this->ilDBInterfaceInnoDB = ilDBWrapperFactory::getWrapper(ilDBConstants::TYPE_PDO_MYSQL_INNODB);
67  $this->ilDBInterfaceInnoDB->initFromIniFile($ilClientIniFile);
68  $this->ilDBInterfaceInnoDB->connect();
69  }
70 
71 
72  public function testGetInstance() {
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  $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ISO_WRONG_LEVEL);
85  $other = new ilAtomQueryTransaction($this->ilDBInterfaceGalera, ilAtomQuery::ISOLATION_READ_UNCOMMITED);
86  $other->run();
87  }
88 
89 
90  public function testReadCommited() {
91  $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ISO_WRONG_LEVEL);
92  $other = new ilAtomQueryTransaction($this->ilDBInterfaceGalera, ilAtomQuery::ISOLATION_READ_COMMITED);
93  $other->run();
94  }
95 
96 
97  public function testReadRepeatedRead() {
98  $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ISO_WRONG_LEVEL);
99  $other = new ilAtomQueryTransaction($this->ilDBInterfaceGalera, ilAtomQuery::ISOLATION_REPEATED_READ);
100  $other->run();
101  }
102 
103 
104  public function testAnomalies() {
105  $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ANO_NOT_AVAILABLE);
107  }
108 
109 
110  public function testLevel() {
111  $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_ISO_WRONG_LEVEL);
113  }
114 
115 
116  public function testRisks() {
117  $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
118  $ilAtomQuery->addTableLock('object_data');
119  $this->assertEquals(array(), $ilAtomQuery->getRisks());
120  }
121 
122 
123  public function testCallables() {
124  require_once('./Services/Database/classes/PDO/class.ilDBPdoMySQL.php');
125  require_once('./Services/Database/test/Atom/data/class.ilAtomQueryTestHelper.php');
126 
127  $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
128  // Working
129  $this->assertTrue($ilAtomQuery->checkCallable(function (ilDBInterface $ilDBInterface) { })); // ilDBInterface as first Parameter
130  $this->assertTrue($ilAtomQuery->checkCallable(new ilAtomQueryTestHelper())); // Class with implemented __invoke
131 
132  // Non working
133  $this->assertFalse($ilAtomQuery->checkCallable(function () { })); // No Parameter
134  $this->assertFalse($ilAtomQuery->checkCallable(function (ilDBInterface $ilDBInterface, $someOtherParameter) { })); // More than one parameter
135  $this->assertFalse($ilAtomQuery->checkCallable(function ($someOtherParameter, ilDBInterface $ilDBInterface) { })); // ilDBInterface not first parameter
136  $this->assertFalse($ilAtomQuery->checkCallable(function (ilDBPdoMySQL $ilDBInterface) { })); // not ilDBInterface
137  $this->assertFalse($ilAtomQuery->checkCallable(function ($ilDBInterface) { })); // not ilDBInterface
138  function noClosure() { }
139 
140  $this->assertFalse($ilAtomQuery->checkCallable('noClosure')); // Not a Closure
141 
142  }
143 
144 
145  public function testWrongIsolationLevel() {
146  $this->setExpectedException('ilDatabaseException');
147  $ilAtomQuery = new ilAtomQueryTransaction($this->ilDBInterfaceGalera, 'non_existing');
148  $ilAtomQuery->addTableLock('il_db_tests_atom');
149  $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) {
150  $ilDB->getDBType();
151  });
152  $ilAtomQuery->run();
153  }
154 
155 
156  public function testQueryWithFiveException() {
157  $counter = 0;
158  $max = 5;
159  $result = null;
160  $query = function (ilDBInterface $ilDBInterface) use (&$counter, &$max, &$result) {
161  if ($counter < $max) {
162  $counter ++;
164  }
165  $result = $ilDBInterface->listTables();
166  };
167 
168  $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
169  $ilAtomQuery->addQueryCallable($query);
170  $ilAtomQuery->addTableLock('object_data');
171  $ilAtomQuery->run();
172  $this->assertTrue(is_array($result));
173  }
174 
175 
176  public function testQueryWithTenException() {
177  $this->setExpectedException('ilDatabaseException', ilDatabaseException::DB_GENERAL);
178  $counter = 0;
179  $max = 10;
180  $result = null;
181  $query = function (ilDBInterface $ilDBInterface) use (&$counter, &$max, &$result) {
182  if ($counter < $max) {
183  $counter ++;
185  }
186  $result = $ilDBInterface->listTables();
187  };
188 
189  $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
190  $ilAtomQuery->addQueryCallable($query);
191  $ilAtomQuery->addTableLock('object_data');
192 
193  $ilAtomQuery->run();
194 
195  $this->assertTrue(is_null($result));
196  }
197 
198 
199  public function testWithOutLocks() {
200  $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_LOCK_NO_TABLE);
201  $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
202  $ilAtomQuery->run();
203  }
204 
205 
206  public function testWithOutClosures() {
207  $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_CLOSURE_NONE);
208  $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
209  $ilAtomQuery->addTableLock('object_data');
210  $ilAtomQuery->run();
211  }
212 
213 
214  public function testMultipleClosures() {
215  $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_CLOSURE_ALREADY_SET);
216  $ilAtomQuery = $this->ilDBInterfaceGalera->buildAtomQuery();
217  $ilAtomQuery->addTableLock('object_data');
218  $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDBInterface) { });
219  $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDBInterface) { });
220  }
221 
222 
223  public function testLockSameTable() {
224  $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_IDENTICAL_TABLES);
225  $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
226  $ilAtomQuery->addTableLock('il_db_tests_atom');
227  $ilAtomQuery->addTableLock('il_db_tests_atom');
228  $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDBInterface) { });
229  $ilAtomQuery->run();
230  }
231 
232 
233  public function testLockSameTableWithAlias() {
234  $this->setExpectedException('ilAtomQueryException', ilAtomQueryException::DB_ATOM_IDENTICAL_TABLES);
235  $ilAtomQuery = $this->ilDBInterfaceInnoDB->buildAtomQuery();
236  $ilAtomQuery->addTableLock('il_db_tests_atom')->aliasName('alias_one');
237  $ilAtomQuery->addTableLock('il_db_tests_atom')->aliasName('alias_one');
238  $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDBInterface) { });
239  $ilAtomQuery->run();
240  }
241 }
Class ilAtomQueryTransaction.
static checkAnomaly($anomalie)
$result
TestCase for the ilDatabaseAtomBaseTest.
Class ilDBPdoMySQL.
Class ilAtomQueryLock.
Class ilDatabaseException.
$counter
Interface ilDBInterface.
Create styles array
The data for the language used.
getDBType()
Get DSN.
global $ilDB
static checkIsolationLevel($isolation_level)
Class ilAtomQueryTestHelper.