Skip to content

Commit cff4a1a

Browse files
committed
[#.x] - fixes from copilot
1 parent 9244835 commit cff4a1a

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

tests/AbstractUnitTestCase.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,20 @@ protected function getFromDatabase(
104104
string $table,
105105
array $criteria
106106
): array {
107-
$sql = 'SELECT * FROM ' . $table . ' WHERE ';
108-
$where = [];
107+
$where = [];
108+
$params = [];
109109
foreach ($criteria as $key => $value) {
110-
$val = $value;
111-
if (true === is_string($value)) {
112-
$val = '"' . $value . '"';
113-
}
114-
115-
$where[] = $key . ' = ' . $val;
110+
$param = ':' . $key;
111+
$where[] = $key . ' = ' . $param;
112+
$params[$param] = $value;
116113
}
117-
118-
$sql .= implode(' AND ', $where);
119-
120-
$result = $this->connection?->query($sql);
121-
$records = $result?->fetchAll(PDO::FETCH_ASSOC);
114+
$sql = 'SELECT * FROM ' . $table;
115+
if (!empty($where)) {
116+
$sql .= ' WHERE ' . implode(' AND ', $where);
117+
}
118+
$stmt = $this->connection?->prepare($sql);
119+
$stmt?->execute($params);
120+
$records = $stmt?->fetchAll(PDO::FETCH_ASSOC);
122121

123122
return $records;
124123
}

tests/Fixtures/Domain/Migrations/UsersMigration.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@ public function insert(
2525
?string $username = null,
2626
?string $password = null,
2727
) {
28-
$id = $id ?: 'null';
29-
$sql = <<<SQL
30-
INSERT INTO {$this->table} (
31-
usr_id, usr_status_flag, usr_username, usr_password
32-
) VALUES (
33-
$id, $status, '$username', '$password'
34-
)
35-
SQL;
36-
37-
$result = $this->connection->exec($sql);
28+
$sql = "INSERT INTO {$this->table} (
29+
usr_id, usr_status_flag, usr_username, usr_password
30+
) VALUES (
31+
:id, :status, :username, :password
32+
)";
33+
$stmt = $this->connection->prepare($sql);
34+
$params = [
35+
':id' => $id,
36+
':status' => $status,
37+
':username' => $username,
38+
':password' => $password,
39+
];
40+
$result = $stmt->execute($params);
3841
if (!$result) {
3942
Assert::fail(
4043
"Failed to insert id [#$id] into table [$this->table]"

0 commit comments

Comments
 (0)