Skip to content

Commit 0f9c9a1

Browse files
committed
test: move locked row test doubles to support files
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent f429dbc commit 0f9c9a1

3 files changed

Lines changed: 165 additions & 131 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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\Models;
15+
16+
use CodeIgniter\Database\BaseBuilder;
17+
use CodeIgniter\Database\BaseResult;
18+
use CodeIgniter\Database\Exceptions\DatabaseException;
19+
use CodeIgniter\Database\Query;
20+
use CodeIgniter\Database\TableName;
21+
use CodeIgniter\Test\Mock\MockConnection;
22+
use stdClass;
23+
24+
/**
25+
* @internal
26+
*/
27+
final class WithLockedRowConnection extends MockConnection
28+
{
29+
/**
30+
* @param list<array<string, mixed>> $rows
31+
*/
32+
public function __construct(private array $rows = [], public bool $throwOnSelect = false)
33+
{
34+
parent::__construct([]);
35+
}
36+
37+
/**
38+
* @param array<int|string, mixed>|string|null $binds
39+
*/
40+
public function query(string $sql, $binds = null, bool $setEscapeFlags = true, string $queryClass = ''): BaseResult|bool
41+
{
42+
if ($this->connID === false || $this->connID === null) {
43+
$this->initialize();
44+
}
45+
46+
$query = new Query($this);
47+
$query->setQuery($sql, $binds, $setEscapeFlags);
48+
49+
$this->lastQuery = $query;
50+
51+
if ($query->isWriteType()) {
52+
return true;
53+
}
54+
55+
if ($this->throwOnSelect) {
56+
throw new DatabaseException('Locked lookup failed.');
57+
}
58+
59+
return new WithLockedRowResult($this->connID, new stdClass(), $this->rows);
60+
}
61+
62+
/**
63+
* @param array<array-key, mixed>|string|TableName $tableName
64+
*/
65+
public function table($tableName): BaseBuilder
66+
{
67+
return new BaseBuilder($tableName, $this);
68+
}
69+
70+
protected function execute(string $sql): object
71+
{
72+
return new stdClass();
73+
}
74+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\Models;
15+
16+
use CodeIgniter\Database\BaseResult;
17+
use stdClass;
18+
19+
/**
20+
* @internal
21+
*
22+
* @extends BaseResult<object|resource, object|resource>
23+
*/
24+
final class WithLockedRowResult extends BaseResult
25+
{
26+
/**
27+
* @param list<array<string, mixed>> $rows
28+
* @param mixed $connID
29+
* @param mixed $resultID
30+
*/
31+
public function __construct($connID, $resultID, private array $rows)
32+
{
33+
parent::__construct($connID, $resultID);
34+
}
35+
36+
public function getFieldCount(): int
37+
{
38+
return 0;
39+
}
40+
41+
/**
42+
* @return list<string>
43+
*/
44+
public function getFieldNames(): array
45+
{
46+
return [];
47+
}
48+
49+
/**
50+
* @return list<object>
51+
*/
52+
public function getFieldData(): array
53+
{
54+
return [];
55+
}
56+
57+
public function freeResult(): void
58+
{
59+
}
60+
61+
public function dataSeek(int $n = 0): bool
62+
{
63+
return true;
64+
}
65+
66+
/**
67+
* @return array<string, mixed>|false
68+
*/
69+
protected function fetchAssoc(): array|false
70+
{
71+
return array_shift($this->rows) ?? false;
72+
}
73+
74+
protected function fetchObject($className = stdClass::class): false|object
75+
{
76+
$row = $this->fetchAssoc();
77+
78+
if ($row === false) {
79+
return false;
80+
}
81+
82+
$object = new $className();
83+
84+
foreach ($row as $key => $value) {
85+
$object->{$key} = $value;
86+
}
87+
88+
return $object;
89+
}
90+
}

‎tests/system/Models/WithLockedRowUnitTest.php‎

Lines changed: 1 addition & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@
1313

1414
namespace CodeIgniter\Models;
1515

16-
use CodeIgniter\Database\BaseBuilder;
17-
use CodeIgniter\Database\BaseResult;
1816
use CodeIgniter\Database\Exceptions\DatabaseException;
19-
use CodeIgniter\Database\Query;
20-
use CodeIgniter\Database\TableName;
2117
use CodeIgniter\Test\CIUnitTestCase;
22-
use CodeIgniter\Test\Mock\MockConnection;
2318
use PHPUnit\Framework\Attributes\Group;
2419
use RuntimeException;
25-
use stdClass;
2620
use Tests\Support\Models\EventModel;
2721
use Tests\Support\Models\UserModel;
22+
use Tests\Support\Models\WithLockedRowConnection;
2823

2924
/**
3025
* @internal
@@ -132,128 +127,3 @@ public function testWithLockedRowCleansUpModelStateWhenLockedLookupThrows(): voi
132127
$this->assertStringNotContainsString('country', $sql);
133128
}
134129
}
135-
136-
/**
137-
* @internal
138-
*/
139-
final class WithLockedRowConnection extends MockConnection
140-
{
141-
/**
142-
* @param list<array<string, mixed>> $rows
143-
*/
144-
public function __construct(private array $rows = [], public bool $throwOnSelect = false)
145-
{
146-
parent::__construct([]);
147-
}
148-
149-
/**
150-
* @param array<int|string, mixed>|string|null $binds
151-
*/
152-
public function query(string $sql, $binds = null, bool $setEscapeFlags = true, string $queryClass = ''): BaseResult|bool
153-
{
154-
if ($this->connID === false || $this->connID === null) {
155-
$this->initialize();
156-
}
157-
158-
$query = new Query($this);
159-
$query->setQuery($sql, $binds, $setEscapeFlags);
160-
161-
$this->lastQuery = $query;
162-
163-
if ($query->isWriteType()) {
164-
return true;
165-
}
166-
167-
if ($this->throwOnSelect) {
168-
throw new DatabaseException('Locked lookup failed.');
169-
}
170-
171-
return new WithLockedRowResult($this->connID, new stdClass(), $this->rows);
172-
}
173-
174-
/**
175-
* @param array<array-key, mixed>|string|TableName $tableName
176-
*/
177-
public function table($tableName): BaseBuilder
178-
{
179-
return new BaseBuilder($tableName, $this);
180-
}
181-
182-
protected function execute(string $sql): object
183-
{
184-
return new stdClass();
185-
}
186-
}
187-
188-
/**
189-
* @internal
190-
*
191-
* @extends BaseResult<object|resource, object|resource>
192-
*/
193-
final class WithLockedRowResult extends BaseResult
194-
{
195-
/**
196-
* @param list<array<string, mixed>> $rows
197-
* @param mixed $connID
198-
* @param mixed $resultID
199-
*/
200-
public function __construct($connID, $resultID, private array $rows)
201-
{
202-
parent::__construct($connID, $resultID);
203-
}
204-
205-
public function getFieldCount(): int
206-
{
207-
return 0;
208-
}
209-
210-
/**
211-
* @return list<string>
212-
*/
213-
public function getFieldNames(): array
214-
{
215-
return [];
216-
}
217-
218-
/**
219-
* @return list<object>
220-
*/
221-
public function getFieldData(): array
222-
{
223-
return [];
224-
}
225-
226-
public function freeResult(): void
227-
{
228-
}
229-
230-
public function dataSeek(int $n = 0): bool
231-
{
232-
return true;
233-
}
234-
235-
/**
236-
* @return array<string, mixed>|false
237-
*/
238-
protected function fetchAssoc(): array|false
239-
{
240-
return array_shift($this->rows) ?? false;
241-
}
242-
243-
protected function fetchObject($className = stdClass::class): false|object
244-
{
245-
$row = $this->fetchAssoc();
246-
247-
if ($row === false) {
248-
return false;
249-
}
250-
251-
$object = new $className();
252-
253-
foreach ($row as $key => $value) {
254-
$object->{$key} = $value;
255-
}
256-
257-
return $object;
258-
}
259-
}

0 commit comments

Comments
 (0)