1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Tests\Cache; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Cache\ArrayStatement; |
8
|
|
|
use Doctrine\DBAL\FetchMode; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use function array_values; |
12
|
|
|
use function iterator_to_array; |
13
|
|
|
|
14
|
|
|
class ArrayStatementTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** @var array<int, array<string, mixed>> */ |
17
|
|
|
private $users = [ |
18
|
|
|
[ |
19
|
|
|
'username' => 'jwage', |
20
|
|
|
'active' => true, |
21
|
|
|
], |
22
|
|
|
[ |
23
|
|
|
'username' => 'romanb', |
24
|
|
|
'active' => false, |
25
|
|
|
], |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
public function testCloseCursor() : void |
29
|
|
|
{ |
30
|
|
|
$statement = $this->createTestArrayStatement(); |
31
|
|
|
|
32
|
|
|
self::assertSame(2, $statement->rowCount()); |
33
|
|
|
|
34
|
|
|
$statement->closeCursor(); |
35
|
|
|
|
36
|
|
|
self::assertSame(0, $statement->rowCount()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testColumnCount() : void |
40
|
|
|
{ |
41
|
|
|
$statement = $this->createTestArrayStatement(); |
42
|
|
|
|
43
|
|
|
self::assertSame(2, $statement->columnCount()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testRowCount() : void |
47
|
|
|
{ |
48
|
|
|
$statement = $this->createTestArrayStatement(); |
49
|
|
|
|
50
|
|
|
self::assertSame(2, $statement->rowCount()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testSetFetchMode() : void |
54
|
|
|
{ |
55
|
|
|
$statement = $this->createTestArrayStatement(); |
56
|
|
|
|
57
|
|
|
$statement->setFetchMode(FetchMode::ASSOCIATIVE); |
58
|
|
|
|
59
|
|
|
self::assertSame($this->users[0], $statement->fetch()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testGetIterator() : void |
63
|
|
|
{ |
64
|
|
|
$statement = $this->createTestArrayStatement(); |
65
|
|
|
$statement->setFetchMode(FetchMode::ASSOCIATIVE); |
66
|
|
|
|
67
|
|
|
self::assertSame($this->users, iterator_to_array($statement->getIterator())); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testFetchModeAssociative() : void |
71
|
|
|
{ |
72
|
|
|
$statement = $this->createTestArrayStatement(); |
73
|
|
|
|
74
|
|
|
self::assertSame($this->users[0], $statement->fetch(FetchMode::ASSOCIATIVE)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testFetchModeNumeric() : void |
78
|
|
|
{ |
79
|
|
|
$statement = $this->createTestArrayStatement(); |
80
|
|
|
|
81
|
|
|
self::assertSame(array_values($this->users[0]), $statement->fetch(FetchMode::NUMERIC)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testFetchModeMixed() : void |
85
|
|
|
{ |
86
|
|
|
$statement = $this->createTestArrayStatement(); |
87
|
|
|
|
88
|
|
|
self::assertSame([ |
89
|
|
|
'username' => 'jwage', |
90
|
|
|
'active' => true, |
91
|
|
|
0 => 'jwage', |
92
|
|
|
1 => true, |
93
|
|
|
], $statement->fetch(FetchMode::MIXED)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testFetchModeColumn() : void |
97
|
|
|
{ |
98
|
|
|
$statement = $this->createTestArrayStatement(); |
99
|
|
|
|
100
|
|
|
self::assertSame('jwage', $statement->fetch(FetchMode::COLUMN)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testFetchThrowsInvalidArgumentException() : void |
104
|
|
|
{ |
105
|
|
|
$statement = $this->createTestArrayStatement(); |
106
|
|
|
|
107
|
|
|
$this->expectException(InvalidArgumentException::class); |
108
|
|
|
$this->expectExceptionMessage('Invalid fetch mode given for fetching result, 9999 given.'); |
109
|
|
|
|
110
|
|
|
$statement->fetch(9999); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testFetchAll() : void |
114
|
|
|
{ |
115
|
|
|
$statement = $this->createTestArrayStatement(); |
116
|
|
|
|
117
|
|
|
self::assertSame($this->users, $statement->fetchAll(FetchMode::ASSOCIATIVE)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testFetchColumn() : void |
121
|
|
|
{ |
122
|
|
|
$statement = $this->createTestArrayStatement(); |
123
|
|
|
|
124
|
|
|
self::assertSame('jwage', $statement->fetchColumn()); |
125
|
|
|
self::assertSame('romanb', $statement->fetchColumn()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function createTestArrayStatement() : ArrayStatement |
129
|
|
|
{ |
130
|
|
|
return new ArrayStatement($this->users); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|