Skip to content

Commit c590905

Browse files
committed
Added warning to escape on $selectClause argument
1 parent b000798 commit c590905

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/Trait/DatabaseTableExtensionTestTrait.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ trait DatabaseTableExtensionTestTrait
1515
* @param string $table Table name
1616
* @param string $whereColumn The column name of the select query
1717
* @param mixed $whereValue The value that will be searched for
18-
* @param string|array $selectClause Fields array or string after SELECT and before FROM like 'id, name'
18+
* @param string|array $selectClause Fields array or string after "SELECT" and before "FROM" like 'id, `name`'
19+
* WARNING: the column names passed as array or string must be escaped with ` if they match a reserved word
20+
* Example: ['`column_name`', '`column_name_2`'] or '`column_name`, `column_name_2`'
1921
*
2022
* @return array[] array or rows
2123
*/
@@ -40,7 +42,9 @@ protected function findTableRowsByColumn(
4042
*
4143
* @param string $table Table name
4244
* @param string $whereString
43-
* @param string|array $selectClause Fields array or string after SELECT and before FROM like 'id, name'
45+
* @param string|array $selectClause Fields array or string after "SELECT" and before "FROM" like 'id, `name`'
46+
* WARNING: the column names passed as array or string must be escaped with ` if they match a reserved word
47+
* Example: ['`column_name`', '`column_name_2`'] or '`column_name`, `column_name_2`'
4448
* @param string $joinString
4549
*
4650
* @return array[] array or rows
@@ -84,7 +88,9 @@ protected function findLastInsertedTableRow(string $table): array
8488
* @param string $table Table to look into
8589
* @param string $whereColumn The column of the search query
8690
* @param mixed $whereValue The value that will be searched for
87-
* @param string|array|null $selectClause Fields array or string after SELECT and before FROM like 'id, name'
91+
* @param string|array|null $selectClause Fields array or string after "SELECT" and before "FROM" like 'id, `name`'
92+
* WARNING: the column names passed as array or string must be escaped with ` if they match a reserved word
93+
* Example: ['`column_name`', '`column_name_2`'] or '`column_name`, `column_name_2`'
8894
* @param string $message Optional message
8995
*
9096
* @return void
@@ -101,7 +107,7 @@ protected function assertTableRowsByColumn(
101107
$table,
102108
$whereColumn,
103109
$whereValue,
104-
$selectClause ?: array_keys($expectedRow)
110+
$selectClause ?: array_map(fn ($key) => "`$key`", array_keys($expectedRow))
105111
);
106112
foreach ($rows as $row) {
107113
$this->assertSame(

src/Trait/DatabaseTableTestTrait.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ trait DatabaseTableTestTrait
1616
* @param array $expectedRow Row expected to find
1717
* @param string $table Table to look into
1818
* @param int $id The primary key
19-
* @param string|array|null $selectClause Fields array or string after SELECT and before FROM like 'id, name'
19+
* @param string|array|null $selectClause Fields array or string after "SELECT" and before "FROM" like 'id, `name`'
20+
* WARNING: the column names passed as array or string must be escaped with ` if they match a reserved word
21+
* Example: ['`column_name`', '`column_name_2`'] or '`column_name`, `column_name_2`'
2022
* @param string $message Optional message
2123
*
2224
* @return void
@@ -30,7 +32,11 @@ protected function assertTableRow(
3032
): void {
3133
$this->assertSame(
3234
$expectedRow,
33-
$this->getTableRowById($table, $id, $selectClause ?: array_keys($expectedRow)),
35+
$this->getTableRowById(
36+
$table,
37+
$id,
38+
$selectClause ?: array_map(fn ($key) => "`$key`", array_keys($expectedRow))
39+
),
3440
$message
3541
);
3642
}
@@ -40,7 +46,9 @@ protected function assertTableRow(
4046
*
4147
* @param string $table Table name
4248
* @param int $id The primary key value
43-
* @param string|array $selectClause Fields string after SELECT and before FROM like 'id, name'
49+
* @param string|array $selectClause Fields array or string after "SELECT" and before "FROM" like 'id, `name`'
50+
* WARNING: the column names passed as array or string must be escaped with ` if they match a reserved word
51+
* Example: ['`column_name`', '`column_name_2`'] or '`column_name`, `column_name_2`'
4452
*
4553
* @throws DomainException
4654
*
@@ -71,7 +79,9 @@ protected function getTableRowById(string $table, int $id, string|array $selectC
7179
* @param array $expectedRow Row expected to find
7280
* @param string $table Table to look into
7381
* @param int $id The primary key
74-
* @param string|array|null $selectClause Fields array or string after SELECT and before FROM like 'id, name'
82+
* @param string|array|null $selectClause Fields array or string after "SELECT" and before "FROM" like 'id, `name`'
83+
* WARNING: the column names passed as array or string must be escaped with ` if they match a reserved word
84+
* Example: ['`column_name`', '`column_name_2`'] or '`column_name`, `column_name_2`'
7585
* @param string $message Optional message
7686
*
7787
* @return void
@@ -85,7 +95,11 @@ protected function assertTableRowEquals(
8595
): void {
8696
$this->assertEquals(
8797
$expectedRow,
88-
$this->getTableRowById($table, $id, $selectClause ?: array_keys($expectedRow)),
98+
$this->getTableRowById(
99+
$table,
100+
$id,
101+
$selectClause ?: array_map(fn ($key) => "`$key`", array_keys($expectedRow))
102+
),
89103
$message
90104
);
91105
}

0 commit comments

Comments
 (0)