From 2d48889fcfd5884f51a826d1d0fe765f21ea4cdf Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 11 Sep 2020 22:15:24 -0500 Subject: [PATCH 1/2] add `castAsJson` method for database assertions database fields of type 'json' cannot be evaluted using strings, they must be cast to a database specific JSON type. --- .../Testing/Concerns/InteractsWithDatabase.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php index 3b4c640d45c5..fc62be1a2aab 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\DB; use Illuminate\Testing\Constraints\CountInDatabase; use Illuminate\Testing\Constraints\HasInDatabase; use Illuminate\Testing\Constraints\SoftDeletedInDatabase; @@ -118,6 +119,17 @@ protected function isSoftDeletableModel($model) && in_array(SoftDeletes::class, class_uses_recursive($model)); } + /** + * Cast a JSON string to a database compatible type. + * + * @param string $value + * @return \Illuminate\Database\Query\Expression + */ + public function castAsJson($value) + { + return DB::raw("CAST('$value' AS JSON)"); + } + /** * Get the database connection. * From 6f0c7265247258b98f3309d13db1874a43203762 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 14 Sep 2020 09:06:34 -0500 Subject: [PATCH 2/2] Update InteractsWithDatabase.php --- .../Foundation/Testing/Concerns/InteractsWithDatabase.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php index fc62be1a2aab..012952155777 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php @@ -122,11 +122,13 @@ protected function isSoftDeletableModel($model) /** * Cast a JSON string to a database compatible type. * - * @param string $value + * @param array|string $value * @return \Illuminate\Database\Query\Expression */ public function castAsJson($value) { + $value = is_array($value) ? json_encode($value) : $value; + return DB::raw("CAST('$value' AS JSON)"); }