Skip to content

Commit 5aacf79

Browse files
committed
refactor tests
1 parent a34d653 commit 5aacf79

File tree

4 files changed

+62
-31
lines changed

4 files changed

+62
-31
lines changed

system/Database/BasePreparedQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function prepare(string $sql, array $options = [], string $queryClass = Q
8383
// We only support positional placeholders (?), so convert
8484
// named placeholders (:name or :name:) while leaving dialect
8585
// syntax like PostgreSQL casts (::type) untouched.
86-
$sql = preg_replace('/(?<!:):([a-zA-Z_][a-zA-Z0-9_]*):?(?!:)/', '?', $sql);
86+
$sql = preg_replace('/(?<!:):([a-zA-Z_]\w*):?(?!:)/', '?', $sql);
8787

8888
/** @var Query $query */
8989
$query = new $queryClass($this->db);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Mock;
15+
16+
use CodeIgniter\Database\BasePreparedQuery;
17+
18+
/**
19+
* @internal
20+
*/
21+
final class MockPreparedQuery extends BasePreparedQuery
22+
{
23+
public string $preparedSql = '';
24+
25+
/**
26+
* @param array<string, mixed> $options
27+
*/
28+
public function _prepare(string $sql, array $options = []): self
29+
{
30+
$this->preparedSql = $sql;
31+
32+
return $this;
33+
}
34+
35+
/**
36+
* @param array<int, mixed> $data
37+
*/
38+
public function _execute(array $data): bool
39+
{
40+
return true;
41+
}
42+
43+
public function _getResult()
44+
{
45+
return null;
46+
}
47+
48+
protected function _close(): bool
49+
{
50+
return true;
51+
}
52+
}

tests/system/Database/BasePreparedQueryTest.php

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\Test\CIUnitTestCase;
1717
use CodeIgniter\Test\Mock\MockConnection;
1818
use PHPUnit\Framework\Attributes\Group;
19+
use Tests\Support\Mock\MockPreparedQuery;
1920

2021
/**
2122
* @internal
@@ -53,32 +54,8 @@ public function testPrepareDoesNotConvertTimeLikeLiterals(): void
5354
$this->assertSame("SELECT '12:34' AS time_value, ? AS id", $query->preparedSql);
5455
}
5556

56-
private function createPreparedQuery(): BasePreparedQuery
57+
private function createPreparedQuery(): MockPreparedQuery
5758
{
58-
return new class (new MockConnection([])) extends BasePreparedQuery {
59-
public string $preparedSql = '';
60-
61-
public function _prepare(string $sql, array $options = [])
62-
{
63-
$this->preparedSql = $sql;
64-
65-
return $this;
66-
}
67-
68-
public function _execute(array $data): bool
69-
{
70-
return true;
71-
}
72-
73-
public function _getResult()
74-
{
75-
return null;
76-
}
77-
78-
protected function _close(): bool
79-
{
80-
return true;
81-
}
82-
};
59+
return new MockPreparedQuery(new MockConnection([]));
8360
}
8461
}

tests/system/Database/Live/PreparedQueryTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function tearDown(): void
4545
{
4646
parent::tearDown();
4747

48-
if ($this->query === null) {
48+
if (! $this->query instanceof BasePreparedQuery) {
4949
return;
5050
}
5151

@@ -115,11 +115,13 @@ public function testPrepareReturnsManualPreparedQuery(): void
115115

116116
public function testPrepareAndExecuteManualQueryWithNamedPlaceholdersKeepsTimeLiteral(): void
117117
{
118-
$this->query = $this->db->prepare(static function ($db): Query {
118+
// Quote alias to keep a consistent property name across drivers (OCI8 uppercases unquoted aliases)
119+
$timeValue = $this->db->protectIdentifiers('time_value');
120+
$this->query = $this->db->prepare(static function ($db) use ($timeValue): Query {
119121
$sql = 'SELECT '
120122
. $db->protectIdentifiers('name') . ', '
121123
. $db->protectIdentifiers('email')
122-
. ", '12:34' AS time_value "
124+
. ", '12:34' AS " . $timeValue . ' '
123125
. 'FROM ' . $db->protectIdentifiers($db->DBPrefix . 'user')
124126
. ' WHERE '
125127
. $db->protectIdentifiers('name') . ' = :name:'
@@ -130,7 +132,7 @@ public function testPrepareAndExecuteManualQueryWithNamedPlaceholdersKeepsTimeLi
130132

131133
$preparedSql = $this->query->getQueryString();
132134

133-
$this->assertStringContainsString("'12:34' AS time_value", $preparedSql);
135+
$this->assertStringContainsString("'12:34' AS " . $timeValue, $preparedSql);
134136

135137
if ($this->db->DBDriver === 'Postgre') {
136138
$this->assertStringContainsString(' = $1', $preparedSql);

0 commit comments

Comments
 (0)