Skip to content

Commit ebd5b6e

Browse files
authored
[11.x] Allow using castAsJson() on non default db connection during test (#53256)
* [11.x] Allow using `castAsJson()` on non default db connection during test Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> --------- Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent 3807ec7 commit ebd5b6e

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,23 @@ protected function isSoftDeletableModel($model)
231231
* Cast a JSON string to a database compatible type.
232232
*
233233
* @param array|object|string $value
234+
* @param string|null $collection
234235
* @return \Illuminate\Contracts\Database\Query\Expression
235236
*/
236-
public function castAsJson($value)
237+
public function castAsJson($value, $connection = null)
237238
{
238239
if ($value instanceof Jsonable) {
239240
$value = $value->toJson();
240241
} elseif (is_array($value) || is_object($value)) {
241242
$value = json_encode($value);
242243
}
243244

244-
$value = DB::connection()->getPdo()->quote($value);
245+
$db = DB::connection($connection);
245246

246-
return DB::raw(
247-
DB::connection()->getQueryGrammar()->compileJsonValueCast($value)
247+
$value = $db->getPdo()->quote($value);
248+
249+
return $db->raw(
250+
$db->getQueryGrammar()->compileJsonValueCast($value)
248251
);
249252
}
250253

tests/Testing/Concerns/InteractsWithDatabaseTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,15 @@ protected function castAsJson($value, $grammar)
149149

150150
$connection->shouldReceive('getQueryGrammar')->andReturn($grammar);
151151

152+
$connection->shouldReceive('raw')->andReturnUsing(function ($value) {
153+
return new Expression($value);
154+
});
155+
152156
$connection->shouldReceive('getPdo->quote')->andReturnUsing(function ($value) {
153157
return "'".$value."'";
154158
});
155159

156-
DB::shouldReceive('connection')->andReturn($connection);
157-
158-
DB::shouldReceive('raw')->andReturnUsing(function ($value) {
159-
return new Expression($value);
160-
});
160+
DB::shouldReceive('connection')->with(null)->andReturn($connection);
161161

162162
$instance = new class
163163
{

0 commit comments

Comments
 (0)