-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordingConnection.php
More file actions
116 lines (101 loc) · 3.66 KB
/
Copy pathRecordingConnection.php
File metadata and controls
116 lines (101 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
// SPDX-FileCopyrightText: 2026 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Tests\Icinga\Module\Notifications\Lib\EntityManager;
use ipl\Sql\Connection;
use ipl\Sql\Sql;
use PDOStatement;
/**
* Test double for {@see Connection} that records every write call and forwards to the real parent.
*
* Lets tests assert that the EntityManager issues the *exact* set of writes expected — and skips
* the ones it shouldn't — without giving up the real sqlite-backed end-to-end execution.
*/
class RecordingConnection extends Connection
{
/**
* Each entry is one recorded write keyed by `method` ('insert'|'update'|'delete'), plus `table`,
* `data` (for insert/update), and `condition` (for update/delete).
*
* @var list<array<string, mixed>>
*/
public array $calls = [];
public function insert(string $table, iterable $data): PDOStatement
{
$data = is_array($data) ? $data : iterator_to_array($data);
$this->calls[] = [
'method' => 'insert',
'table' => $this->unquote($table),
'data' => $this->unquoteKeys($data),
];
return parent::insert($table, $data);
}
public function update(
string|array $table,
iterable $data,
string|array|null $condition = null,
string $operator = Sql::ALL
): PDOStatement {
$data = is_array($data) ? $data : iterator_to_array($data);
$this->calls[] = [
'method' => 'update',
'table' => is_string($table) ? $this->unquote($table) : $table,
'data' => $this->unquoteKeys($data),
'condition' => is_array($condition) ? $this->unquoteKeys($condition) : $condition,
];
return parent::update($table, $data, $condition, $operator);
}
public function delete(
string|array $table,
string|array|null $condition = null,
string $operator = Sql::ALL
): PDOStatement {
$this->calls[] = [
'method' => 'delete',
'table' => is_string($table) ? $this->unquote($table) : $table,
'condition' => is_array($condition) ? $this->unquoteKeys($condition) : $condition,
];
return parent::delete($table, $condition, $operator);
}
/**
* Strip the adapter's identifier quoting from a recorded string
*
* The EntityManager quotes every identifier it emits ({@see Connection::quoteIdentifier()}). The recorded
* calls are normalized back to their logical names so assertions can express the expected write set in plain
* table/column names, independent of the adapter's quote character. Only the real SQL forwarded to the parent
* keeps the quoting.
*
* @param string $identifier
*
* @return string
*/
private function unquote(string $identifier): string
{
$quoted = $this->quoteIdentifier('x');
return str_replace([$quoted[0], $quoted[strlen($quoted) - 1]], '', $identifier);
}
/**
* Copy a map with its keys (quoted identifiers or `column = ?` expressions) unquoted via {@see self::unquote()}
*
* @param array<string, mixed> $data
*
* @return array<string, mixed>
*/
private function unquoteKeys(array $data): array
{
$unquoted = [];
foreach ($data as $key => $value) {
$unquoted[$this->unquote((string) $key)] = $value;
}
return $unquoted;
}
/**
* Drop the recorded calls so subsequent assertions only see writes from the next action
*
* @return void
*/
public function resetCalls(): void
{
$this->calls = [];
}
}