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