]>
git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Db.php
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
28 * @package GaletteTests
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2013-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
34 * @link http://galette.tuxfamily.org
38 namespace Galette\Core\test\units
;
43 * Database tests class
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2013-2014 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
54 class Db
extends atoum
61 * @param stgring $testMethod Method tested
65 public function beforeTestMethod($testMethod)
67 $this->db
= new \Galette\Core\
Db();
75 public function tearDown()
77 $this->db
= new \Galette\Core\
Db();
78 $delete = $this->db
->delete(\Galette\Entity\Title
::TABLE
);
79 $delete->where([\Galette\Entity\Title
::PK
=> '150']);
80 $this->db
->execute($delete);
88 public function testConstructor()
90 $db = new \Galette\Core\
Db();
93 ->isIdenticalTo(TYPE_DB
);
100 'PORT_DB' => PORT_DB
,
103 $db = new \Galette\Core\
Db($dsn);
105 $is_pg = $db->isPostgres();
106 $type = $db->type_db
;
110 $this->boolean($is_pg)
113 ->isIdenticalTo(\Galette\Core\Db
::PGSQL
);
115 case \Galette\Core\Db
::MYSQL
:
116 $this->boolean($is_pg)
119 ->isIdenticalTo(\Galette\Core\Db
::MYSQL
);
124 function () use ($dsn) {
125 $dsn['TYPE_DB'] = 'DOES_NOT_EXISTS';
126 $db = new \Galette\Core\
Db($dsn);
132 * Test database connectivity
136 public function testConnectivity()
138 $res = $this->db
->testConnectivity(
146 $this->boolean($res)->isTrue();
150 * Test database grants
154 public function testGrant()
156 $result = $this->db
->dropTestTable();
166 $result = $this->db
->grantCheck();
168 $this->array($result)
170 ->isIdenticalTo($expected);
172 //in update mode, we need alter
173 $result = $this->db
->grantCheck('u');
175 $expected['alter'] = true;
176 $this->array($result)
178 ->isIdenticalTo($expected);
182 * Test database grants that throws an exception
186 public function testGrantWException()
190 //test insert failing
191 $this->db
= new \mock\Galette\Core\
Db();
192 $this->calling($this->db
)->execute
= function ($o) {
193 if ($o instanceof \Zend\Db\Sql\Insert
) {
194 throw new \
LogicException('Error executing query!', 123);
198 $result = $this->db
->grantCheck('u');
200 $this->array($result)
201 ->boolean
['create']->isTrue()
202 ->boolean
['alter']->isTrue()
203 ->object['insert']->isInstanceOf('\LogicException')
204 ->boolean
['update']->isFalse()
205 ->boolean
['select']->isFalse()
206 ->boolean
['delete']->isFalse()
207 ->boolean
['drop']->isTrue();
209 //test select failing
210 $this->calling($this->db
)->execute
= function ($o) use ($atoum) {
211 if ($o instanceof \Zend\Db\Sql\Select
) {
212 throw new \
LogicException('Error executing query!', 123);
214 $rs = new \mock\Zend\Db\
ResultSet();
215 $atoum->calling($rs)->count
= 1;
220 $result = $this->db
->grantCheck('u');
222 $this->array($result)
223 ->boolean
['create']->isTrue()
224 ->boolean
['alter']->isTrue()
225 ->boolean
['insert']->isTrue()
226 ->boolean
['update']->isTrue()
227 ->object['select']->isInstanceOf('\LogicException')
228 ->boolean
['delete']->isTrue()
229 ->boolean
['drop']->isTrue();
231 //test update failing
232 $this->calling($this->db
)->execute
= function ($o) use ($atoum) {
233 if ($o instanceof \Zend\Db\Sql\Update
) {
234 throw new \
LogicException('Error executing query!', 123);
236 $rs = new \mock\Zend\Db\
ResultSet();
237 $atoum->calling($rs)->count
= 1;
242 $result = $this->db
->grantCheck('u');
244 $this->array($result)
245 ->boolean
['create']->isTrue()
246 ->boolean
['alter']->isTrue()
247 ->boolean
['insert']->isTrue()
248 ->object['update']->isInstanceOf('\LogicException')
249 ->boolean
['select']->isTrue()
250 ->boolean
['delete']->isTrue()
251 ->boolean
['drop']->isTrue();
253 //test delete failing
254 $this->calling($this->db
)->execute
= function ($o) use ($atoum) {
255 if ($o instanceof \Zend\Db\Sql\Delete
) {
256 throw new \
LogicException('Error executing query!', 123);
258 $rs = new \mock\Zend\Db\
ResultSet();
259 $atoum->calling($rs)->count
= 1;
264 $result = $this->db
->grantCheck('u');
266 $this->array($result)
267 ->boolean
['create']->isTrue()
268 ->boolean
['alter']->isTrue()
269 ->boolean
['insert']->isTrue()
270 ->boolean
['update']->isTrue()
271 ->boolean
['select']->isTrue()
272 ->object['delete']->isInstanceOf('\LogicException')
273 ->boolean
['drop']->isTrue();
277 * Is database Postgresql powered?
281 public function testIsPostgres()
283 $is_pg = $this->db
->isPostgres();
287 $this->boolean($is_pg)
291 $this->boolean($is_pg)
302 public function testGetters()
306 $type = $this->db
->type_db
;
308 ->isIdenticalTo('pgsql');
311 $type = $this->db
->type_db
;
313 ->isIdenticalTo('mysql');
318 $this->object($db)->isInstanceOf('Zend\Db\Adapter\Adapter');
320 $sql = $this->db
->sql
;
321 $this->object($sql)->isInstanceOf('Zend\Db\Sql\Sql');
323 $connection = $this->db
->connection
;
324 $this->object($connection)
325 ->isInstanceOf('Zend\Db\Adapter\Driver\Pdo\Connection');
327 $driver = $this->db
->driver
;
328 $this->object($driver)
329 ->isInstanceOf('Zend\Db\Adapter\Driver\Pdo\Pdo');
337 public function testSelect()
339 $select = $this->db
->select('preferences', 'p');
340 $select->where(array('p.nom_pref' => 'pref_nom'));
342 $results = $this->db
->execute($select);
344 $query = $this->db
->query_string
;
346 $expected = 'SELECT "p".* FROM "galette_preferences" AS "p" ' .
347 'WHERE "p"."nom_pref" = \'pref_nom\'';
349 if (TYPE_DB
=== 'mysql') {
350 $expected = 'SELECT `p`.* FROM `galette_preferences` AS `p` ' .
351 'WHERE `p`.`nom_pref` = \'pref_nom\'';
354 $this->string($query)->isIdenticalTo($expected);
362 public function testSelectAll()
364 $all = $this->db
->selectAll('preferences');
365 $this->object($all)->isInstanceOf('Zend\Db\ResultSet\ResultSet');
373 public function testInsert()
375 $insert = $this->db
->insert('titles');
378 'short_label' => 'Dr',
379 'long_label' => 'Doctor'
381 $insert->values($data);
382 $res = $this->db
->execute($insert);
384 $select = $this->db
->select('titles', 't');
385 $select->where(['t.id_title' => $data['id_title']]);
387 $results = $this->db
->execute($select);
388 $this->integer($results->count())->isIdenticalTo(1);
390 if (TYPE_DB
=== 'pgsql') {
391 $data['id_title'] = (int)$data['id_title'];
393 $this->array((array)$results->current())->isIdenticalTo($data);
401 public function testUpdate()
403 $update = $this->db
->update('titles');
405 'long_label' => 'DoctorS'
407 $where = ['id_title' => 150];
409 $select = $this->db
->select('titles', 't');
410 $select->columns(['long_label']);
411 $select->where($where);
412 $results = $this->db
->execute($select);
414 $long_label = $results->current()->long_label
;
415 $this->string($long_label)->isIdenticalTo('Doctor');
418 $update->where($where);
419 $res = $this->db
->execute($update);
420 $this->integer($res->count())->isIdenticalTo(1);
422 $results = $this->db
->execute($select);
423 $this->integer($results->count())->isIdenticalTo(1);
425 $long_label = $results->current()->long_label
;
426 $this->string($long_label)->isIdenticalTo('DoctorS');
434 public function testDelete()
436 $delete = $this->db
->delete('titles');
437 $where = ['id_title' => 150];
439 $select = $this->db
->select('titles', 't');
440 $select->where($where);
441 $results = $this->db
->execute($select);
442 $this->integer($results->count())->isIdenticalTo(1);
444 $delete->where($where);
445 $res = $this->db
->execute($delete);
446 $this->integer($res->count())->isIdenticalTo(1);
448 $results = $this->db
->execute($select);
449 $this->integer($results->count())->isIdenticalTo(0);
453 * Test database version
457 public function testDbVersion()
459 $db_version = $this->db
->getDbVersion();
460 $this->variable($db_version)->isIdenticalTo(GALETTE_DB_VERSION
);
462 $res = $this->db
->checkDbVersion();
463 $this->boolean($res)->isTrue();
467 * Test database version that throws an exception
471 public function testDbVersionWException()
473 $this->db
= new \mock\Galette\Core\
Db();
474 $this->calling($this->db
)->execute
= function ($o) {
475 throw new \
LogicException('Error executing query!', 123);
481 function () use ($db) {
484 )->isInstanceOf('\LogicException');
486 $this->boolean($db->checkDbVersion())->isFalse();
490 * Test get columns method
494 public function testGetColumns()
496 $cols = $this->db
->getColumns('preferences');
498 $this->array($cols)->hasSize(3);
501 foreach ($cols as $c) {
502 $columns[] = $c->getName();
505 $this->array($columns)
518 * FIXME: this test will fail if some plugins tables are present
522 public function testTables()
525 'galette_groups_members',
526 'galette_transactions',
528 'galette_types_cotisation',
529 'galette_paymenttypes',
536 'galette_groups_managers',
537 'galette_dynamic_fields',
538 'galette_fields_config',
539 'galette_tmppasswds',
543 'galette_import_model',
544 'galette_cotisations',
545 'galette_field_types',
546 'galette_fields_categories',
547 'galette_mailing_history',
549 'galette_preferences',
554 $tables = $this->db
->getTables();
556 //tables created in grantCheck are sometimes
558 if (in_array('galette_test', $tables)) {
559 unset($tables[array_search('galette_test', $tables)]);
565 $this->array($tables)
566 ->hasSize(count($expected))
567 ->isIdenticalTo($expected);
571 * Test UTF conversion, for MySQL only
575 public function testConvertToUtf()
577 if (TYPE_DB
=== \Galette\Core\Db
::MYSQL
) {
578 $convert = $this->db
->convertToUTF();
580 $this->variable($convert)->isNull();
589 public function testGetPlatform()
591 $quoted = $this->db
->platform
->quoteValue('somethin\' to "quote"');
593 $expected = ($this->db
->isPostgres()) ?
594 "'somethin'' to \"quote\"'" :
595 "'somethin\\' to \\\"quote\\\"'";
597 $this->string($quoted)
598 ->isIdenticalTo($expected);
602 * Test execute Method
606 public function testExecute()
608 $select = $this->db
->select('preferences', 'p');
609 $select->where(['p.nom_pref' => 'azerty']);
610 $results = $this->db
->execute($select);
612 $this->object($results)
613 ->isInstanceOf('\Zend\Db\ResultSet\ResultSet');
616 function () use ($select) {
617 $select->where(['p.notknown' => 'azerty']);
618 $results = $this->db
->execute($select);
620 )->isInstanceOf('\PDOException');
628 public function testSerialization()
631 $serialized = serialize($db);
632 $this->string($serialized)
635 $unserialized = unserialize($serialized);
636 $this->object($unserialized)->isInstanceOf('Galette\Core\Db');