AbstractDatabaseTest.php 3.43 KB
Newer Older
1
2
<?php
/**
3
 * @author Carsten Rose <carsten.rose@math.uzh.ch>
4
5
 */

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
use PHPUnit\Framework\TestCase;

18
19
20
/**
 * Class AbstractDatabaseTest
 */
21
22
//abstract class AbstractDatabaseTest extends PHPUnit_Framework_TestCase {
abstract class AbstractDatabaseTest extends TestCase {
23

24
25
26
27
    /**
     * @var null
     */
    static protected $mysqli = null;
28

29
30
31
    /**
     * @var qfq\Database
     */
32
    protected $dbArray = array();
33

34
    /**
35
     * @var qfq\Store
36
37
38
     */
    protected $store = null;

39
    /**
40
41
42
43
     * @var qfq\Sip
     */
    protected $sip = null;

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

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

        // SWITCH to TestDB
77
78
79
80
81
82
83
84
//        $dbNamePhpUnit = $this->store->getVar('dbNamePhpUnit', STORE_SYSTEM . STORE_EMPTY);
//        if ($dbNamePhpUnit == '') {
//            $dbNamePhpUnit = $this->store->getVar('DB_1_NAME', STORE_SYSTEM) . '_phpunit';
//            $this->store->setVar('dbNamePhpUnit', $dbNamePhpUnit, STORE_SYSTEM);
//        }
//        $this->store->setVar('DB_1_NAME', $dbNamePhpUnit, STORE_SYSTEM);
        $dbName = $this->store->getVar('DB_NAME_TEST', STORE_SYSTEM);
        if ($dbName == '') {
85
            throw new \qfq\CodeException('Missing DB_NAME_TEST in ' . CONFIG_QFQ_INI, ERROR_MISSING_REQUIRED_PARAMETER);
86
87
        } else {
            $this->store->setVar('DB_1_NAME', $dbName, STORE_SYSTEM);
88
        }
89

90
91
92
93
94
95
96
        $dbIndexData = $this->store->getVar(SYSTEM_DB_INDEX_DATA, STORE_SYSTEM);
        $dbIndexQfq = $this->store->getVar(SYSTEM_DB_INDEX_QFQ, STORE_SYSTEM);
        if ($dbIndexData != $dbIndexQfq) {
            throw new \qfq\CodeException('phpUnit Tests are not prepared for MultiDB tests');
        }


97
        if (empty($this->dbArray)) {
98
            $this->dbArray[DB_INDEX_DEFAULT] = new qfq\Database();
99
100
        }

101
        /// Establish additional mysqli access
102
103
104
105
        $dbserver = $this->store->getVar('DB_1_SERVER', STORE_SYSTEM);
        $dbuser = $this->store->getVar('DB_1_USER', STORE_SYSTEM);
        $db = $this->store->getVar('DB_1_NAME', STORE_SYSTEM);
        $dbpw = $this->store->getVar('DB_1_PASSWORD', STORE_SYSTEM);
106
107
108
109
110
111
112
113
114
115
116
117

        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");
            }
        }

    }
}