|
13 | 13 |
|
14 | 14 | namespace CodeIgniter\Models; |
15 | 15 |
|
16 | | -use CodeIgniter\Database\BaseBuilder; |
17 | | -use CodeIgniter\Database\BaseResult; |
18 | 16 | use CodeIgniter\Database\Exceptions\DatabaseException; |
19 | | -use CodeIgniter\Database\Query; |
20 | | -use CodeIgniter\Database\TableName; |
21 | 17 | use CodeIgniter\Test\CIUnitTestCase; |
22 | | -use CodeIgniter\Test\Mock\MockConnection; |
23 | 18 | use PHPUnit\Framework\Attributes\Group; |
24 | 19 | use RuntimeException; |
25 | | -use stdClass; |
26 | 20 | use Tests\Support\Models\EventModel; |
27 | 21 | use Tests\Support\Models\UserModel; |
| 22 | +use Tests\Support\Models\WithLockedRowConnection; |
28 | 23 |
|
29 | 24 | /** |
30 | 25 | * @internal |
@@ -132,128 +127,3 @@ public function testWithLockedRowCleansUpModelStateWhenLockedLookupThrows(): voi |
132 | 127 | $this->assertStringNotContainsString('country', $sql); |
133 | 128 | } |
134 | 129 | } |
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