|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
|
4
|
|
|
|
|
5
|
|
|
use HexMakina\Crudites\Connection; |
|
6
|
|
|
use HexMakina\Crudites\Result; |
|
7
|
|
|
|
|
8
|
|
|
class ResultTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
// setup |
|
11
|
|
|
private $connection; |
|
12
|
|
|
private $result; |
|
13
|
|
|
private $result_fetch_num; |
|
14
|
|
|
private $result_fetch_ass; |
|
15
|
|
|
|
|
16
|
|
|
public function setUp(): void |
|
17
|
|
|
{ |
|
18
|
|
|
// code to execute before each test |
|
19
|
|
|
$this->connection = new Connection('mysql:host=localhost;dbname=crudites;charset=utf8', 'crudites', '2ce!fNe8(weVz3k4TN#'); |
|
20
|
|
|
$this->result = new Result($this->connection->pdo(), 'SELECT id, username, email, created_at FROM users'); |
|
21
|
|
|
|
|
22
|
|
|
$this->result_fetch_num = [ |
|
23
|
|
|
[ |
|
24
|
|
|
0 => "1", |
|
25
|
|
|
1 => "john_doe", |
|
26
|
|
|
2 => "[email protected]", |
|
27
|
|
|
3 => "2024-11-25 21:23:46" |
|
28
|
|
|
], |
|
29
|
|
|
[ |
|
30
|
|
|
0 => "2", |
|
31
|
|
|
1 => "alice_smith", |
|
32
|
|
|
2 => "[email protected]", |
|
33
|
|
|
3 => "2024-11-25 21:23:46" |
|
34
|
|
|
], |
|
35
|
|
|
[ |
|
36
|
|
|
0 => "3", |
|
37
|
|
|
1 => "bob_jones", |
|
38
|
|
|
2 => "[email protected]", |
|
39
|
|
|
3 => "2024-11-25 21:23:46" |
|
40
|
|
|
] |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
$this->result_fetch_ass = [ |
|
44
|
|
|
[ |
|
45
|
|
|
"id" => "1", |
|
46
|
|
|
"username" => "john_doe", |
|
47
|
|
|
"email" => "[email protected]", |
|
48
|
|
|
"created_at" => "2024-11-25 21:23:46" |
|
49
|
|
|
], |
|
50
|
|
|
[ |
|
51
|
|
|
"id" => "2", |
|
52
|
|
|
"username" => "alice_smith", |
|
53
|
|
|
"email" => "[email protected]", |
|
54
|
|
|
"created_at" => "2024-11-25 21:23:46" |
|
55
|
|
|
], |
|
56
|
|
|
[ |
|
57
|
|
|
"id" => "3", |
|
58
|
|
|
"username" => "bob_jones", |
|
59
|
|
|
"email" => "[email protected]", |
|
60
|
|
|
"created_at" => "2024-11-25 21:23:46" |
|
61
|
|
|
] |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function tearDown(): void |
|
66
|
|
|
{ |
|
67
|
|
|
// code to execute after each test |
|
68
|
|
|
$this->connection = null; |
|
69
|
|
|
$this->result = null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testConstructor() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->assertInstanceOf(Result::class, $this->result); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testRan() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->assertTrue($this->result->ran()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testRetWithoutParamIsAssoc() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->assertEquals($this->result->ret(), $this->result_fetch_ass); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testRetWithParamFetchNum() |
|
88
|
|
|
{ |
|
89
|
|
|
$this->assertEquals($this->result->ret(\PDO::FETCH_NUM), $this->result_fetch_num); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testCount() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->assertEquals(3, $this->result->count()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testCallLastInsertId() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->assertEquals(0, $this->result->lastInsertId()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testErrorInfo() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->assertIsArray($this->result->errorInfo()); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|