summaryrefslogtreecommitdiffstats
path: root/tests/TestBaseHelper.php
blob: 5372d9294b3b2e674ecd8d3f84832ab6561fb5d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

class TestBaseHelper
{
    private $_dbsm, $_host, $_db_name, $_user, $_password, $_table_name;
    private $_PDO = null;

    private $_fields;

    public function __construct($configs = array())
    {
        $this->_dbsm = isset($configs["dbsm"]) ? $configs["dbsm"] : "mysql";
        $this->_host = isset($configs["host"]) ? $configs["host"] : "localhost";
        $this->_db_name = $configs["db_name"];
        $this->_user = $configs["user"];
        $this->_password = $configs["password"];
        $this->_table_name = $configs["table_name"];
        $this->_fields = $configs["fields"];
    }

    private function getConnection()
    {
        $PDO_options = array(
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        );

        $dsn = $this->_dbsm . ":host=" . $this->_host . ";dbname=" . $this->_db_name;
        $this->_PDO = ($this->_PDO) ? $this->_PDO : new PDO($dsn, $this->_user, $this->_password,$PDO_options);
        return $this->_PDO;
    }

    private function closeConnection()
    {
        unset($this->_PDO);
        $this->_PDO = null;
    }

    public function dropTable()
    {
        $conn = $this->getConnection();
        $conn->prepare("DROP TABLE IF EXISTS `$this->_table_name`;")->execute();
        $this->closeConnection();
    }

    public function resetTable()
    {
        $this->dropTable();
        $conn = $this->getConnection();
        $sql = "CREATE TABLE `$this->_table_name`(";
        $f = $this->_fields;
        $fieldsCount = count($f);
        $elNumb = 1;

        foreach($f as $name=>$type) {
            $sql .= "`$name` $type";
            if ($elNumb++ != $fieldsCount)
                $sql .= ",";
        }
        $sql .= ");";

        $conn->prepare($sql)->execute();
        $this->closeConnection();
    }

    public function insertDataFromJSON($data){
        $f = $this->_fields;
        $fieldsCount = count($f);

        $sql = "INSERT INTO `$this->_table_name`(";

        $elNumb = 1;
        foreach($f as $name=>$type) {
            $sql .= "`$name`";
            if ($elNumb++ != $fieldsCount)
                $sql .= ",";
        }

        $sql.= ") VALUES ";

        $evsCount = count($data);
        for($i = 0; $i < $evsCount; $i++) {
            $sql .= "(";
            $event = $data[$i];
            $elNumb = 1;
            foreach($f as $name=>$type) {
                $sql .= isset($event[$name]) && !is_null($event[$name]) ? "'".$event[$name]."'":"NULL";

                if ($elNumb++ != $fieldsCount)
                    $sql .= ",";
            }
            $sql.=")";
            if($i + 1 != $evsCount)
                $sql.=",";
        }
        $sql .= ";";

        $conn = $this->getConnection();
        $conn->prepare($sql)->execute();
        $this->closeConnection();
    }

    public function getDataFromBase($id = NULL){
        $sql = "SELECT * FROM $this->_table_name";
        if(!is_null($id)){
            $sql .= " WHERE id=$id";
        }
        $conn = $this->getConnection()->prepare($sql);
        $conn->execute();
        $data = $conn->fetchAll();
        $this->closeConnection();
        return $data;
    }
}