AbstractDatabaseTest.php 2.46 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
require_once(__DIR__ . '/../../qfq/database/Database.php');
13
require_once(__DIR__ . '/../../qfq/store/Store.php');
14
require_once(__DIR__ . '/../../qfq/store/Sip.php');
15
16
17
18
19

/**
 * Class AbstractDatabaseTest
 */
abstract class AbstractDatabaseTest extends PHPUnit_Framework_TestCase {
20

21
22
23
24
    /**
     * @var null
     */
    static protected $mysqli = null;
25

26
27
28
    /**
     * @var qfq\Database
     */
29
    protected $db = null;
30

31
    /**
32
     * @var qfq\Store
33
34
35
     */
    protected $store = null;

36
    /**
37
38
39
40
     * @var qfq\Sip
     */
    protected $sip = null;

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    /**
     * @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
68
69
70
71
        $this->store = qfq\Store::getInstance('', true);

        $this->sip = new qfq\Sip('fakesessionname', true);
        $this->sip->sipUniqId('badcaffee1234');
72
73

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

76
        if ($this->db === null) {
77
            $this->db = new qfq\Database();
78
79
        }

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

Carsten  Rose's avatar
Carsten Rose committed
82
83
84
85
86
87
            /// Establish additional mysqli access
            $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);

88
89
90
91
92
93
94
95
96
            self::$mysqli = new \mysqli($dbserver, $dbuser, $dbpw, $db);

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

    }
}