Skip to content

Commit 2b61abd

Browse files
committed
refactor(db-adapter): Add tests about fetch methods
Signed-off-by: Carl Schwan <[email protected]>
1 parent 4d47fda commit 2b61abd

File tree

3 files changed

+141
-6
lines changed

3 files changed

+141
-6
lines changed

apps/dav/tests/unit/BackgroundJob/CleanupOrphanedChildrenJobTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testRunWithoutOrphans(): void {
8787
->method('executeQuery')
8888
->willReturn($result);
8989
$result->expects(self::once())
90-
->method('fetchAll')
90+
->method('fetchAllAssociative')
9191
->willReturn([]);
9292
$result->expects(self::once())
9393
->method('closeCursor');
@@ -115,7 +115,7 @@ public function testRunWithPartialBatch(): void {
115115
->method('executeQuery')
116116
->willReturn($result);
117117
$result->expects(self::once())
118-
->method('fetchAll')
118+
->method('fetchAllAssociative')
119119
->willReturn([
120120
['id' => 42],
121121
['id' => 43],
@@ -152,7 +152,7 @@ public function testRunWithFullBatch(): void {
152152
->method('executeQuery')
153153
->willReturn($result);
154154
$result->expects(self::once())
155-
->method('fetchAll')
155+
->method('fetchAllAssociative')
156156
->willReturn(array_map(static fn ($i) => ['id' => 42 + $i], range(0, 999)));
157157
$result->expects(self::once())
158158
->method('closeCursor');

apps/dav/tests/unit/Controller/InvitationResponseControllerTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,7 @@ private function buildQueryExpects(string $token, ?array $return, int $time): vo
421421
]);
422422

423423
$stmt->expects($this->once())
424-
->method('fetch')
425-
->with(\PDO::FETCH_ASSOC)
424+
->method('fetchAssociative')
426425
->willReturn($return);
427426
$stmt->expects($this->once())
428427
->method('closeCursor');

tests/lib/DB/AdapterTest.php

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,142 @@ private function getRows(string $configKey): array {
6464
->where($qb->expr()->eq('appid', $qb->createNamedParameter($this->appId)))
6565
->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($configKey)))
6666
->executeQuery()
67-
->fetchAll();
67+
->fetchAllAssociative();
68+
}
69+
70+
public function fetchAssociative(): void {
71+
$insert = $this->connection->getQueryBuilder();
72+
$insert->insert('appconfig')
73+
->values([
74+
'appid' => $this->appId,
75+
'configkey' => 'test',
76+
'configvalue' => '1',
77+
])
78+
->executeStatement();
79+
80+
// fetch all associative
81+
$qb = $this->connection->getQueryBuilder();
82+
$result = $qb->select(['configkey', 'configvalue', 'appid'])
83+
->from('appconfig')
84+
->executeQuery();
85+
86+
$rows = $result->fetchAllAssociative();
87+
$this->assertEquals([
88+
[
89+
'appid' => $this->appId,
90+
'configkey' => 'test',
91+
'configvalue' => '1',
92+
]
93+
], $rows);
94+
95+
// fetch associative
96+
$qb = $this->connection->getQueryBuilder();
97+
$result = $qb->select(['configkey', 'configvalue', 'appid'])
98+
->from('appconfig')
99+
->executeQuery();
100+
$row = $result->fetchAssociative();
101+
$this->assertEquals([
102+
'appid' => $this->appId,
103+
'configkey' => 'test',
104+
'configvalue' => '1',
105+
], $row);
106+
107+
// iterate associative
108+
$qb = $this->connection->getQueryBuilder();
109+
$result = $qb->select(['configkey', 'configvalue', 'appid'])
110+
->from('appconfig')
111+
->executeQuery();
112+
$row = iterator_to_array($result->iterateAssociative());
113+
$this->assertEquals([
114+
'appid' => $this->appId,
115+
'configkey' => 'test',
116+
'configvalue' => '1',
117+
], $row);
118+
}
119+
120+
public function fetchNumeric(): void {
121+
$insert = $this->connection->getQueryBuilder();
122+
$insert->insert('appconfig')
123+
->values([
124+
'appid' => $this->appId,
125+
'configkey' => 'test',
126+
'configvalue' => '1',
127+
])
128+
->executeStatement();
129+
130+
// fetch all associative
131+
$qb = $this->connection->getQueryBuilder();
132+
$result = $qb->select(['configkey', 'configvalue', 'appid'])
133+
->from('appconfig')
134+
->executeQuery();
135+
136+
$rows = $result->fetchAllNumeric();
137+
$this->assertEquals([
138+
[
139+
0 => $this->appId,
140+
1 => 'test',
141+
2 => '1',
142+
]
143+
], $rows);
144+
145+
// fetch associative
146+
$qb = $this->connection->getQueryBuilder();
147+
$result = $qb->select(['configkey', 'configvalue', 'appid'])
148+
->from('appconfig')
149+
->executeQuery();
150+
$row = $result->fetchNumeric();
151+
$this->assertEquals([
152+
0 => $this->appId,
153+
1 => 'test',
154+
2 => '1',
155+
], $row);
156+
157+
// iterate associative
158+
$qb = $this->connection->getQueryBuilder();
159+
$result = $qb->select(['configkey', 'configvalue', 'appid'])
160+
->from('appconfig')
161+
->executeQuery();
162+
$row = iterator_to_array($result->iterateNumeric());
163+
$this->assertEquals([
164+
0 => $this->appId,
165+
1 => 'test',
166+
2 => '1',
167+
], $row);
168+
}
169+
170+
public function fetchOne(): void {
171+
$insert = $this->connection->getQueryBuilder();
172+
$insert->insert('appconfig')
173+
->values([
174+
'appid' => $this->appId,
175+
'configkey' => 'test',
176+
'configvalue' => '1',
177+
])
178+
->executeStatement();
179+
180+
// fetch all associative
181+
$qb = $this->connection->getQueryBuilder();
182+
$result = $qb->select(['configkey', 'configvalue', 'appid'])
183+
->from('appconfig')
184+
->executeQuery();
185+
186+
$rows = $result->fetchFirstColumn();
187+
$this->assertEquals($this->appId, $rows);
188+
189+
// fetch associative
190+
$qb = $this->connection->getQueryBuilder();
191+
$result = $qb->select(['configkey', 'configvalue', 'appid'])
192+
->from('appconfig')
193+
->executeQuery();
194+
$row = $result->fetchFirstColumn();
195+
$this->assertEquals($this->appId, $row);
196+
197+
// iterate associative
198+
$qb = $this->connection->getQueryBuilder();
199+
$result = $qb->select(['configkey', 'configvalue', 'appid'])
200+
->from('appconfig')
201+
->executeQuery();
202+
$rows = iterator_to_array($result->iterateNumeric());
203+
$this->assertEquals([$this->appId], $rows);
68204
}
69205
}

0 commit comments

Comments
 (0)