1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\DBAL\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 testSetFetchModeThrowsInvalidArgumentException() : void |
63
|
|
|
{ |
64
|
|
|
$statement = $this->createTestArrayStatement(); |
65
|
|
|
|
66
|
|
|
self::expectException(InvalidArgumentException::class); |
|
|
|
|
67
|
|
|
self::expectExceptionMessage('Caching layer does not support 2nd/3rd argument to setFetchMode().'); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$statement->setFetchMode(FetchMode::ASSOCIATIVE, 'arg1', 'arg2'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGetIterator() : void |
73
|
|
|
{ |
74
|
|
|
$statement = $this->createTestArrayStatement(); |
75
|
|
|
$statement->setFetchMode(FetchMode::ASSOCIATIVE); |
76
|
|
|
|
77
|
|
|
self::assertSame($this->users, iterator_to_array($statement->getIterator())); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testFetchModeAssociative() : void |
81
|
|
|
{ |
82
|
|
|
$statement = $this->createTestArrayStatement(); |
83
|
|
|
|
84
|
|
|
self::assertSame($this->users[0], $statement->fetch(FetchMode::ASSOCIATIVE)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testFetchModeNumeric() : void |
88
|
|
|
{ |
89
|
|
|
$statement = $this->createTestArrayStatement(); |
90
|
|
|
|
91
|
|
|
self::assertSame(array_values($this->users[0]), $statement->fetch(FetchMode::NUMERIC)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testFetchModeMixed() : void |
95
|
|
|
{ |
96
|
|
|
$statement = $this->createTestArrayStatement(); |
97
|
|
|
|
98
|
|
|
self::assertSame([ |
99
|
|
|
'username' => 'jwage', |
100
|
|
|
'active' => true, |
101
|
|
|
0 => 'jwage', |
102
|
|
|
1 => true, |
103
|
|
|
], $statement->fetch(FetchMode::MIXED)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testFetchModeColumn() : void |
107
|
|
|
{ |
108
|
|
|
$statement = $this->createTestArrayStatement(); |
109
|
|
|
|
110
|
|
|
self::assertSame('jwage', $statement->fetch(FetchMode::COLUMN)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testFetchThrowsInvalidArgumentException() : void |
114
|
|
|
{ |
115
|
|
|
$statement = $this->createTestArrayStatement(); |
116
|
|
|
|
117
|
|
|
self::expectException(InvalidArgumentException::class); |
|
|
|
|
118
|
|
|
self::expectExceptionMessage('Invalid fetch mode given for fetching result, 9999 given.'); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$statement->fetch(9999); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testFetchAll() : void |
124
|
|
|
{ |
125
|
|
|
$statement = $this->createTestArrayStatement(); |
126
|
|
|
|
127
|
|
|
self::assertSame($this->users, $statement->fetchAll(FetchMode::ASSOCIATIVE)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testFetchColumn() : void |
131
|
|
|
{ |
132
|
|
|
$statement = $this->createTestArrayStatement(); |
133
|
|
|
|
134
|
|
|
self::assertSame('jwage', $statement->fetchColumn(0)); |
135
|
|
|
self::assertSame('romanb', $statement->fetchColumn(0)); |
136
|
|
|
|
137
|
|
|
$statement = $this->createTestArrayStatement(); |
138
|
|
|
|
139
|
|
|
self::assertSame(true, $statement->fetchColumn(1)); |
140
|
|
|
self::assertSame(false, $statement->fetchColumn(1)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function createTestArrayStatement() : ArrayStatement |
144
|
|
|
{ |
145
|
|
|
return new ArrayStatement($this->users); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|