AbstractDatabaseTest.php 2.21 KB
Newer Older
1
2
3
4
5
<?php
/**
 * @author Rafael Ostertag <rafael.ostertag@math.uzh.ch>
 */

6
7
//namespace qfq;

8
use qfq\Store;
9

10
11
//use qfq;

12
13
14
15
16
17
18
19
20
21
22
23
24
25
require_once(__DIR__ . '/../../qfq/Database.php');
require_once(__DIR__ . '/../../qfq/store/Store.php');

/**
 * Class AbstractDatabaseTest
 */
abstract class AbstractDatabaseTest extends PHPUnit_Framework_TestCase {
    /**
     * @var null
     */
    static protected $mysqli = null;
    /**
     * @var qfq\Database
     */
26
    protected $db = null;
27
    /*
28
     * @var qfq\Store
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
     */
    protected $store = null;

    /**
     * @param $filename
     * @param $ignoreError
     *
     * @throws Exception
     */
    protected function executeSQLFile($filename, $ignoreError) {

        $sqlStatements = file_get_contents($filename);
        if (!self::$mysqli->multi_query($sqlStatements) && !$ignoreError) {
            throw new Exception("mysqli->multi_query() failed: " . AbstractDatabaseTest::$mysqli->error);
        }

        // discard all results
        do {
            if ($res = AbstractDatabaseTest::$mysqli->store_result()) {
                $res->free();
            }
        } while (AbstractDatabaseTest::$mysqli->more_results() && AbstractDatabaseTest::$mysqli->next_result());
    }

    /**
     * @throws Exception
     */
    protected function setUp() {

        // Init the store also reads db credential configuration
59
        $this->store = \qfq\Store::getInstance('', true);
60
61

        // SWITCH to TestDB
62
        $this->store->setVar(SYSTEM_DB_NAME, $this->store->getVar(SYSTEM_DB_NAME_TEST, STORE_SYSTEM), STORE_SYSTEM);
63

64
65
66
67
        if ($this->db === null) {
            $this->db = new \qfq\Database();
        }

68
        /// Establish additional mysqli access
69
70
71
72
        $dbserver = $this->store->getVar(SYSTEM_DB_SERVER, STORE_SYSTEM);
        $dbuser = $this->store->getVar(SYSTEM_DB_USER, STORE_SYSTEM);
        $db = $this->store->getVar(SYSTEM_DB_NAME, STORE_SYSTEM);
        $dbpw = $this->store->getVar(SYSTEM_DB_PASSWORD, STORE_SYSTEM);
73
74
75
76
77
78
79
80
81
82
83
84

        if (self::$mysqli === null) {

            self::$mysqli = new \mysqli($dbserver, $dbuser, $dbpw, $db);

            if (self::$mysqli->connect_errno) {
                throw new Exception("Unable to connect to mysql server");
            }
        }

    }
}