Passed
Branch master (d51fdb)
by Joao
05:45 queued 02:33
created

BasePdo::testGetBuggyUT8()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace TestsDb\AnyDataset;
4
5
use ByJG\AnyDataset\DbDriverInterface;
6
use PHPUnit\Framework\TestCase;
7
8
abstract class BasePdo extends TestCase
9
{
10
11
    /**
12
     * @var DbDriverInterface
13
     */
14
    protected $dbDriver;
15
16
    public function setUp()
17
    {
18
        $this->createInstance();
19
        $this->createDatabase();
20
        $this->populateData();
21
    }
22
23
    protected function createInstance()
24
    {
25
        throw new \Exception('Implement createInstance method');
26
    }
27
28
    protected function populateData()
29
    {
30
        //insert some data...
31
        $array = $this->allData();
32
        foreach ($array as $param) {
33
            $this->dbDriver->execute(
34
                "INSERT INTO Dogs (Breed, Name, Age) VALUES (:breed, :name, :age);",
35
                $param
36
            );
37
        }
38
    }
39
40
    abstract protected function createDatabase();
41
42
    abstract protected function deleteDatabase();
43
44
    public function tearDown()
45
    {
46
        $this->deleteDatabase();
47
    }
48
49
    protected function allData()
50
    {
51
        return [
52
            [
53
                'breed' => 'Mutt',
54
                'name' => 'Spyke',
55
                'age' => 8,
56
                'id' => 1
57
            ],
58
            [
59
                'breed' => 'Brazilian Terrier',
60
                'name' => 'Sandy',
61
                'age' => 3,
62
                'id' => 2
63
            ],
64
            [
65
                'breed' => 'Pinscher',
66
                'name' => 'Lola',
67
                'age' => 1,
68
                'id' => 3
69
            ]
70
        ];
71
    }
72
73
    public function testGetIterator()
74
    {
75
        $array = $this->allData();
76
77
        // Step 1
78
        $iterator = $this->dbDriver->getIterator('select * from Dogs');
79
        $this->assertEquals($array, $iterator->toArray());
80
81
        // Step 2
82
        $iterator = $this->dbDriver->getIterator('select * from Dogs');
83
        $i = 0;
84
        foreach ($iterator as $singleRow) {
85
            $this->assertEquals($array[$i++], $singleRow->toArray());
86
        }
87
88
        // Step 3
89
        $iterator = $this->dbDriver->getIterator('select * from Dogs');
90
        $i = 0;
91
        while ($iterator->hasNext()) {
92
            $singleRow = $iterator->moveNext();
93
            $this->assertEquals($array[$i++], $singleRow->toArray());
94
        }
95
    }
96
97
    public function testExecuteAndGetId()
98
    {
99
        $idInserted = $this->dbDriver->executeAndGetId(
100
            "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Cat', 'Doris', 7);"
101
        );
102
103
        $this->assertEquals(4, $idInserted);
104
    }
105
106
    public function testGetAllFields()
107
    {
108
        $allFields = $this->dbDriver->getAllFields('Dogs');
109
110
        $this->assertEquals(
111
            [
112
                'id',
113
                'breed',
114
                'name',
115
                'age'
116
            ],
117
            $allFields
118
        );
119
    }
120
121
    public function testMultipleRowset()
122
    {
123
        if (!$this->dbDriver->isSupportMultRowset()) {
124
            $this->markTestSkipped('This DbDriver does not have this method');
125
            return;
126
        }
127
128
        $sql = "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Cat', 'Doris', 7); " .
129
            "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Dog', 'Lolla', 1); ";
130
131
        $idInserted = $this->dbDriver->executeAndGetId($sql);
132
133
        $this->assertEquals(5, $idInserted);
134
135
        $this->assertEquals(
136
            'Doris',
137
            $this->dbDriver->getScalar('select name from Dogs where Id = :id', ['id' => 4])
138
        );
139
140
        $this->assertEquals(
141
            'Lolla',
142
            $this->dbDriver->getScalar('select name from Dogs where Id = :id', ['id' => 5])
143
        );
144
    }
145
146
    public function testParameterInsideQuotes()
147
    {
148
        $sql = "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Cat', 'a:Doris', 7); ";
149
        $id = $this->dbDriver->executeAndGetId($sql);
150
        $this->assertEquals(4, $id);
151
152
        $sql = "select id from Dogs where name = 'a:Doris'";
153
        $id = $this->dbDriver->getScalar($sql);
154
        $this->assertEquals(4, $id);
155
    }
156
157 View Code Duplication
    public function testInsertSpecialChars()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        $this->dbDriver->execute(
160
            "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Dog', '€ Sign Pètit Pannô', 6);"
161
        );
162
163
        $iterator = $this->dbDriver->getIterator('select Id, Breed, Name, Age from Dogs where id = 4');
164
        $row = $iterator->toArray();
165
166
        $this->assertEquals(4, $row[0]["id"]);
167
        $this->assertEquals('Dog', $row[0]["breed"]);
168
        $this->assertEquals('€ Sign Pètit Pannô', $row[0]["name"]);
169
        $this->assertEquals(6, $row[0]["age"]);
170
    }
171
172 View Code Duplication
    public function testGetBuggyUT8()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
    {
174
        $this->dbDriver->execute(
175
            "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Dog', 'Félix', 6);"
176
        );
177
178
        $iterator = $this->dbDriver->getIterator('select Id, Breed, Name, Age from Dogs where id = 4');
179
        $row = $iterator->toArray();
180
181
        $this->assertEquals(4, $row[0]["id"]);
182
        $this->assertEquals('Dog', $row[0]["breed"]);
183
        $this->assertEquals('Félix', $row[0]["name"]);
184
        $this->assertEquals(6, $row[0]["age"]);
185
    }
186
187
}
188
189