DbTestCase   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 114
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 4 1
A getConnection() 0 4 1
A setConnection() 0 4 1
A assertTableCount() 0 10 1
A getNumberOfTableRows() 0 4 1
A createTableHelper() 0 4 1
A selectQuery() 0 6 1
A insertQuery() 0 5 1
A filterResultSet() 0 16 4
1
<?php
2
3
namespace Koine\Repository\Test;
4
5
use PHPUnit_Framework_TestCase;
6
use Koine\Repository\Storage\MySql;
7
use PDO;
8
9
class DbTestCase extends PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @var PDO
13
     */
14
    private static $connection;
15
16
    public function setUp()
17
    {
18
        $this->getConnection()->beginTransaction();
19
    }
20
21
    public function tearDown()
22
    {
23
        $this->getConnection()->rollBack();
24
    }
25
26
    /**
27
     * @return PDO
28
     */
29
    public function getConnection()
30
    {
31
        return self::$connection;
32
    }
33
34
    /**
35
     * @param PDO $pdo
36
     */
37
    public static function setConnection(PDO $pdo)
38
    {
39
        self::$connection = $pdo;
40
    }
41
42
    /**
43
     * @param int    $count
44
     * @param string $tableName
45
     */
46
    public function assertTableCount($count, $tableName)
47
    {
48
        $actual = $this->getNumberOfTableRows($tableName);
49
        $count = (int) $count;
50
        $this->assertEquals(
51
            $count,
52
            $actual,
53
            "Failed asserting that table '$tableName' has $count records. $actual found."
54
        );
55
    }
56
57
    /**
58
     * @param string tableName
59
     *
60
     * @return int
61
     */
62
    public function getNumberOfTableRows($tableName)
63
    {
64
        return $this->createTableHelper($tableName)->getNumberOfRows();
65
    }
66
67
    /**
68
     * @param string $tableName
69
     *
70
     * @return MySql
71
     */
72
    protected function createTableHelper($tableName)
73
    {
74
        return new MySql($this->getConnection(), $tableName);
75
    }
76
77
    /**
78
     * @param string $sql
79
     * @param array  $params
80
     *
81
     * @return array
82
     */
83
    public function selectQuery($sql, array $params = array())
84
    {
85
        $stmt = $this->getConnection()->prepare($sql);
86
        $stmt->execute($params);
87
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
88
    }
89
90
    /**
91
     * @param string $sql
92
     * @param array  $params
93
     */
94
    public function insertQuery($sql, array $params = array())
95
    {
96
        $stmt = $this->getConnection()->prepare($sql);
97
        $stmt->execute($params);
98
    }
99
100
    /**
101
     * @param array $resultSet
102
     * @param array $columns
103
     *
104
     * @return array
105
     */
106
    protected function filterResultSet(array $resultSet, array $columns)
107
    {
108
        $filtered = array();
109
110
        foreach ($resultSet as $row) {
111
            $newRow = array();
112
            foreach ($row as $column => $value) {
113
                if (in_array($column, $columns)) {
114
                    $newRow[$column] = $value;
115
                }
116
            }
117
            $filtered[] = $newRow;
118
        }
119
120
        return $filtered;
121
    }
122
}
123