ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DbTestHelperTrait.php
Go to the documentation of this file.
1<?php
2
3namespace Sabre\DAV;
4
5use PDO;
6use PDOException;
7
8class DbCache {
9
10 static $cache = [];
11
12}
13
15
19 public $driver = null;
20
26 function getDb() {
27
28 if (!$this->driver) {
29 throw new \Exception('You must set the $driver public property');
30 }
31
32 if (array_key_exists($this->driver, DbCache::$cache)) {
34 if ($pdo === null) {
35 $this->markTestSkipped($this->driver . ' was not enabled, not correctly configured or of the wrong version');
36 }
37 return $pdo;
38 }
39
40 try {
41
42 switch ($this->driver) {
43
44 case 'mysql' :
45 $pdo = new PDO(SABRE_MYSQLDSN, SABRE_MYSQLUSER, SABRE_MYSQLPASS);
46 break;
47 case 'sqlite' :
48 $pdo = new \PDO('sqlite:' . SABRE_TEMPDIR . '/testdb');
49 break;
50 case 'pgsql' :
51 $pdo = new \PDO(SABRE_PGSQLDSN);
52 $version = $pdo->query('SELECT VERSION()')->fetchColumn();
53 preg_match('|([0-9\.]){5,}|', $version, $matches);
54 $version = $matches[0];
55 if (version_compare($version, '9.5.0', '<')) {
57 $this->markTestSkipped('We require at least Postgres 9.5. This server is running ' . $version);
58 }
59 break;
60
61
62
63 }
64 $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
65
66 } catch (PDOException $e) {
67
68 $this->markTestSkipped($this->driver . ' was not enabled or not correctly configured. Error message: ' . $e->getMessage());
69
70 }
71
73 return $pdo;
74
75 }
76
82 function getPDO() {
83
84 return $this->getDb();
85
86 }
87
94 function createSchema($schemaName) {
95
96 $db = $this->getDb();
97
98 $queries = file_get_contents(
99 __DIR__ . '/../../../examples/sql/' . $this->driver . '.' . $schemaName . '.sql'
100 );
101
102 foreach (explode(';', $queries) as $query) {
103
104 if (trim($query) === '') {
105 continue;
106 }
107
108 $db->exec($query);
109
110 }
111
112 }
113
120 function dropTables($tableNames) {
121
122 $tableNames = (array)$tableNames;
123 $db = $this->getDb();
124 foreach ($tableNames as $tableName) {
125 $db->exec('DROP TABLE IF EXISTS ' . $tableName);
126 }
127
128
129 }
130
131 function tearDown() {
132
133 switch ($this->driver) {
134
135 case 'sqlite' :
136 // Recreating sqlite, just in case
137 unset(DbCache::$cache[$this->driver]);
138 unlink(SABRE_TEMPDIR . '/testdb');
139 }
140
141 }
142
143}
$version
Definition: build.php:27
An exception for terminatinating execution or to throw for unit testing.
$driver
Definition: migrateto20.php:66
$pdo
Definition: migrateto20.php:62
trait DbTestHelperTrait
dropTables($tableNames)
Drops tables, if they exist.
getPDO()
Alias for getDb.
getDb()
Returns a fully configured PDO object.
createSchema($schemaName)
Uses .sql files from the examples directory to initialize the database.
$query