AbstractDatabaseTest.php 2.48 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 $dbArray = array();
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
75
        $dbName = $this->store->getVar('DB_1_NAME', STORE_SYSTEM);
        $this->store->setVar('DB_1_NAME', $dbName, STORE_SYSTEM);
76

77
78
        if (empty($this->dbArray)) {
            $this->dbArray[DB_INDEX_DATA_DEFAULT] = new qfq\Database();
79
80
        }

81
        /// Establish additional mysqli access
82
83
84
85
        $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);
86
87
88
89
90
91
92
93
94
95
96
97

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

    }
}