Skip to content

Commit 8cb6ec1

Browse files
authored
Merge pull request #48 from tailflow/next
Release v1.2.3
2 parents d38bfde + dfa6b75 commit 8cb6ec1

File tree

5 files changed

+58
-24
lines changed

5 files changed

+58
-24
lines changed

src/Concerns/HandlesRelationStandardOperations.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Database\Eloquent\Relations\MorphToMany;
1515
use Illuminate\Database\Eloquent\Relations\Relation;
1616
use Illuminate\Database\Eloquent\SoftDeletes;
17+
use Illuminate\Support\Arr;
1718
use InvalidArgumentException;
1819
use Orion\Http\Requests\Request;
1920
use Orion\Http\Resources\CollectionResource;
@@ -215,7 +216,9 @@ protected function runStoreParentFetchQuery(Request $request, Builder $query, $p
215216
*/
216217
protected function performStore(Request $request, Model $parentEntity, Model $entity, array $attributes, array $pivot): void
217218
{
218-
$entity->fill($attributes);
219+
$entity->fill(
220+
Arr::except($attributes, array_keys($entity->getDirty()))
221+
);
219222

220223
if (!$parentEntity->{$this->getRelation()}() instanceof BelongsTo) {
221224
$parentEntity->{$this->getRelation()}()->save($entity, $this->preparePivotFields($pivot));
@@ -442,7 +445,9 @@ protected function runUpdateFetchQuery(Request $request, Relation $query, Model
442445
*/
443446
protected function performUpdate(Request $request, Model $parentEntity, Model $entity, array $attributes, array $pivot): void
444447
{
445-
$entity->fill($attributes);
448+
$entity->fill(
449+
Arr::except($attributes, array_keys($entity->getDirty()))
450+
);
446451
$entity->save();
447452

448453
$relation = $parentEntity->{$this->getRelation()}();

src/Concerns/HandlesStandardOperations.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Collection;
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Database\Eloquent\SoftDeletes;
11+
use Illuminate\Support\Arr;
1112
use Orion\Http\Requests\Request;
1213
use Orion\Http\Resources\CollectionResource;
1314
use Orion\Http\Resources\Resource;
@@ -133,7 +134,9 @@ public function store(Request $request)
133134
*/
134135
protected function performStore(Request $request, Model $entity, array $attributes): void
135136
{
136-
$entity->fill($attributes);
137+
$entity->fill(
138+
Arr::except($attributes, array_keys($entity->getDirty()))
139+
);
137140
$entity->save();
138141
}
139142

@@ -275,7 +278,9 @@ protected function runUpdateFetchQuery(Request $request, Builder $query, $key):
275278
*/
276279
protected function performUpdate(Request $request, Model $entity, array $attributes): void
277280
{
278-
$entity->fill($attributes);
281+
$entity->fill(
282+
Arr::except($attributes, array_keys($entity->getDirty()))
283+
);
279284
$entity->save();
280285
}
281286

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

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)