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

Marc Egger's avatar
Marc Egger committed
6
namespace IMATHUZH\Qfq\Tests\Unit\Core\Database;
7

Marc Egger's avatar
Marc Egger committed
8
9
10
11
use IMATHUZH\Qfq\Core\Database\Database;
 
use IMATHUZH\Qfq\Core\Store\Sip;
use IMATHUZH\Qfq\Core\Store\Store;
12
13
use PHPUnit\Framework\TestCase;

14
15
16
/**
 * Class AbstractDatabaseTest
 */
17
18
//abstract class AbstractDatabaseTest extends PHPUnit_Framework_TestCase {
abstract class AbstractDatabaseTest extends TestCase {
19

20
    /**
Carsten  Rose's avatar
Carsten Rose committed
21
     * @var \mysqli
22
23
     */
    static protected $mysqli = null;
24

25
    /**
Carsten  Rose's avatar
Carsten Rose committed
26
     * @var Database[]
27
     */
28
    protected $dbArray = array();
29

30
    /**
31
     * @var Store
32
33
34
     */
    protected $store = null;

35
    /**
36
     * @var Sip
37
38
39
     */
    protected $sip = null;

40
41
42
43
    /**
     * @param $filename
     * @param $ignoreError
     *
Carsten  Rose's avatar
Carsten Rose committed
44
     * @throws \Exception
45
46
47
48
49
     */
    protected function executeSQLFile($filename, $ignoreError) {

        $sqlStatements = file_get_contents($filename);
        if (!self::$mysqli->multi_query($sqlStatements) && !$ignoreError) {
Carsten  Rose's avatar
Carsten Rose committed
50
            throw new \Exception("mysqli->multi_query() failed: " . AbstractDatabaseTest::$mysqli->error);
51
52
53
54
55
56
57
58
59
60
61
        }

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

    /**
Marc Egger's avatar
Marc Egger committed
62
63
64
65
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
66
     * @throws \Exception
67
68
69
70
     */
    protected function setUp() {

        // Init the store also reads db credential configuration
71
        $this->store = Store::getInstance('', true);
72

73
        $this->sip = new Sip(true);
74
        $this->sip->sipUniqId('badcaffee1234');
75

76
77
        $dbName = $this->store->getVar('DB_1_NAME', STORE_SYSTEM);

78
        if ($dbName == '') {
Marc Egger's avatar
Marc Egger committed
79
            throw new \CodeException('Missing DB_1_NAME in ' . CONFIG_QFQ_PHP, ERROR_MISSING_REQUIRED_PARAMETER);
80
        } else {
81
82
83
84
            if (strpos($dbName, '_phpunit') === false) {
                $dbName .= '_phpunit';
                $this->store->setVar('DB_1_NAME', $dbName, STORE_SYSTEM);
            }
85
        }
86

87
88
89
        $dbIndexData = $this->store->getVar(SYSTEM_DB_INDEX_DATA, STORE_SYSTEM);
        $dbIndexQfq = $this->store->getVar(SYSTEM_DB_INDEX_QFQ, STORE_SYSTEM);
        if ($dbIndexData != $dbIndexQfq) {
Marc Egger's avatar
Marc Egger committed
90
            throw new \CodeException('phpUnit Tests are not prepared for MultiDB tests');
91
92
        }

93
        /// Establish additional mysqli access
94
95
96
97
        $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);
98
99
100

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

101
            self::$mysqli = new \mysqli($dbserver, $dbuser, $dbpw, '');
102
103

            if (self::$mysqli->connect_errno) {
Carsten  Rose's avatar
Carsten Rose committed
104
                throw new \Exception("Unable to connect to mysql server");
105
106
107
            }
        }

108
109
110
111
112
113
114
115
116
117
        self::$mysqli->query('CREATE DATABASE IF NOT EXISTS ' . $db . ';');
        self::$mysqli->query('USE ' . $db . ';');



        if (empty($this->dbArray)) {
            $this->dbArray[DB_INDEX_DEFAULT] = new Database();
        }


118
119
    }
}