Skip to content

Commit 7d90697

Browse files
committed
feature: add InteractsWithJsonFields trait with castField(s)ToJson helper methods
1 parent f034566 commit 7d90697

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Orion\Testing;
6+
7+
use Illuminate\Contracts\Support\Jsonable;
8+
use Illuminate\Database\Query\Expression;
9+
use Illuminate\Support\Facades\DB;
10+
11+
trait InteractsWithJsonFields
12+
{
13+
protected function castFieldsToJson(array $fields): array
14+
{
15+
return collect($fields)->map(function ($value) {
16+
if (is_array($value) || $value instanceof Jsonable) {
17+
return $this->castFieldToJson($value);
18+
}
19+
20+
return $value;
21+
})->toArray();
22+
}
23+
24+
protected function castFieldToJson($value): Expression
25+
{
26+
if ($value instanceof Jsonable) {
27+
$value = $value->toJson();
28+
} else {
29+
$value = json_encode($value);
30+
}
31+
32+
if (config('database.default') === 'mysql') {
33+
$value = DB::raw("CAST('{$value}' AS JSON)");
34+
}
35+
if (config('database.default') === 'pgsql') {
36+
$value = DB::raw("'{$value}'::jsonb");
37+
}
38+
39+
return $value;
40+
}
41+
}

src/Testing/InteractsWithResources.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ protected function assertResourcesToggled($response, string $relation, Model $pa
326326

327327
protected function assertResourceAttached(string $relation, Model $parentModel, Model $relationModel, array $pivotFields = []): void
328328
{
329-
$pivotFields = $this->castPivotFields($pivotFields);
329+
$pivotFields = $this->castFieldsToJson($pivotFields);
330330

331331
$this->assertDatabaseHas($parentModel->{$relation}()->getTable(), array_merge([
332332
$parentModel->{$relation}()->getForeignPivotKeyName() => $parentModel->getKey(),
@@ -346,7 +346,7 @@ protected function assertResourcePivotUpdated($response, string $relation, Model
346346
{
347347
$this->assertResponseContent(['updated' => [$relationModel->getKey()]], $response, $exact);
348348

349-
$pivotFields = $this->castPivotFields($pivotFields);
349+
$pivotFields = $this->castFieldsToJson($pivotFields);
350350

351351
$this->assertDatabaseHas($parentModel->{$relation}()->getTable(), array_merge([
352352
$parentModel->{$relation}()->getForeignPivotKeyName() => $parentModel->getKey(),
@@ -397,23 +397,6 @@ protected function assertJsonSame(array $expected, $response): void
397397
self::assertSame($expected, $actual);
398398
}
399399

400-
protected function castPivotFields(array $pivotFields): array
401-
{
402-
return collect($pivotFields)->map(function ($pivotFieldValue) {
403-
if (is_array($pivotFieldValue)) {
404-
$pivotFieldValue = json_encode($pivotFieldValue);
405-
if (config('database.default') === 'mysql') {
406-
$pivotFieldValue = DB::raw("CAST('{$pivotFieldValue}' AS JSON)");
407-
}
408-
if (config('database.default') === 'pgsql') {
409-
$pivotFieldValue = DB::raw("'{$pivotFieldValue}'::jsonb");
410-
}
411-
}
412-
413-
return $pivotFieldValue;
414-
})->toArray();
415-
}
416-
417400
protected function buildSyncMap(array $attached = [], array $detached = [], array $updated = [], array $remained = []): array
418401
{
419402
return [

tests/Feature/TestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
namespace Orion\Tests\Feature;
44

55
use Orion\Testing\InteractsWithAuthorization;
6+
use Orion\Testing\InteractsWithJsonFields;
67
use Orion\Testing\InteractsWithResources;
78
use Orion\Tests\Fixtures\App\Models\User;
89
use Orion\Tests\TestCase as BaseTestCase;
910

1011
abstract class TestCase extends BaseTestCase
1112
{
12-
use InteractsWithResources, InteractsWithAuthorization;
13+
use InteractsWithResources, InteractsWithJsonFields, InteractsWithAuthorization;
1314

1415
protected function setUp(): void
1516
{

0 commit comments

Comments
 (0)