Skip to content

Commit

Permalink
Merge pull request #104 from tailflow/next
Browse files Browse the repository at this point in the history
v2.1 Release
  • Loading branch information
alexzarbn authored Jun 27, 2021
2 parents c1d36ce + acab3c2 commit 34b3b70
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 35 deletions.
29 changes: 23 additions & 6 deletions src/Concerns/HandlesRelationStandardOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,7 @@ protected function performStore(
array $attributes,
array $pivot
): void {
$entity->fill(
Arr::except($attributes, array_keys($entity->getDirty()))
);
$this->performFill($request, $parentEntity, $entity, $attributes, $pivot);

if (!$parentEntity->{$this->getRelation()}() instanceof BelongsTo) {
$parentEntity->{$this->getRelation()}()->save($entity, $this->preparePivotFields($pivot));
Expand Down Expand Up @@ -696,9 +694,7 @@ protected function performUpdate(
array $attributes,
array $pivot
): void {
$entity->fill(
Arr::except($attributes, array_keys($entity->getDirty()))
);
$this->performFill($request, $parentEntity, $entity, $attributes, $pivot);
$entity->save();

$relation = $parentEntity->{$this->getRelation()}();
Expand Down Expand Up @@ -1028,4 +1024,25 @@ protected function afterRestore(Request $request, Model $parentEntity, Model $en
{
return null;
}

/**
* Fills attributes on the given relation entity.
*
* @param Request $request
* @param Model $parentEntity
* @param Model $entity
* @param array $attributes
* @param array $pivot
*/
protected function performFill(
Request $request,
Model $parentEntity,
Model $entity,
array $attributes,
array $pivot
): void {
$entity->fill(
Arr::except($attributes, array_keys($entity->getDirty()))
);
}
}
22 changes: 16 additions & 6 deletions src/Concerns/HandlesStandardOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,7 @@ protected function beforeSave(Request $request, Model $entity)
*/
protected function performStore(Request $request, Model $entity, array $attributes): void
{
$entity->fill(
Arr::except($attributes, array_keys($entity->getDirty()))
);
$this->performFill($request, $entity, $attributes);
$entity->save();
}

Expand Down Expand Up @@ -426,9 +424,7 @@ protected function beforeUpdate(Request $request, Model $entity)
*/
protected function performUpdate(Request $request, Model $entity, array $attributes): void
{
$entity->fill(
Arr::except($attributes, array_keys($entity->getDirty()))
);
$this->performFill($request, $entity, $attributes);
$entity->save();
}

Expand Down Expand Up @@ -664,4 +660,18 @@ protected function afterRestore(Request $request, Model $entity)
{
return null;
}

/**
* Fills attributes on the given entity.
*
* @param Request $request
* @param Model $entity
* @param array $attributes
*/
protected function performFill(Request $request, Model $entity, array $attributes): void
{
$entity->fill(
Arr::except($attributes, array_keys($entity->getDirty()))
);
}
}
4 changes: 2 additions & 2 deletions src/Contracts/RelationsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function relationForeignKeyFromRelationInstance(Relation $relationInstanc

public function relationLocalKeyFromRelationInstance(Relation $relationInstance): string;

public function guardRelationsForCollection(Collection $entities, array $requestedRelations, bool $normalized = false): Collection;
public function guardRelationsForCollection(Collection $entities, array $requestedRelations, ?string $parentRelation = null, bool $normalized = false): Collection;

public function guardRelations(Model $entity, array $requestedRelations, bool $normalized = false);
public function guardRelations(Model $entity, array $requestedRelations, ?string $parentRelation = null, bool $normalized = false);
}
48 changes: 34 additions & 14 deletions src/Drivers/Standard/RelationsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ public function relationForeignKeyFromRelationInstance(Relation $relationInstanc
{
$laravelVersion = (float)app()->version();

return $laravelVersion > 5.7 || get_class($relationInstance) === HasOne::class ? $relationInstance->getQualifiedForeignKeyName() : $relationInstance->getQualifiedForeignKey();
return $laravelVersion > 5.7 || get_class(
$relationInstance
) === HasOne::class ? $relationInstance->getQualifiedForeignKeyName(
) : $relationInstance->getQualifiedForeignKey();
}

/**
Expand All @@ -129,15 +132,12 @@ public function relationLocalKeyFromRelationInstance(Relation $relationInstance)
switch (get_class($relationInstance)) {
case HasOne::class:
case MorphOne::class:
return $relationInstance->getParent()->getTable().'.'.$relationInstance->getLocalKeyName();
break;
return $relationInstance->getParent()->getTable() . '.' . $relationInstance->getLocalKeyName();
case BelongsTo::class:
case MorphTo::class:
return $relationInstance->getQualifiedOwnerKeyName();
break;
default:
return $relationInstance->getQualifiedLocalKeyName();
break;
}
}

Expand All @@ -146,14 +146,19 @@ public function relationLocalKeyFromRelationInstance(Relation $relationInstance)
*
* @param Collection $entities
* @param array $requestedRelations
* @param string|null $parentRelation
* @param bool $normalized
* @return Collection
*/
public function guardRelationsForCollection(Collection $entities, array $requestedRelations, bool $normalized = false): Collection
{
public function guardRelationsForCollection(
Collection $entities,
array $requestedRelations,
?string $parentRelation = null,
bool $normalized = false
): Collection {
return $entities->transform(
function ($entity) use ($requestedRelations, $normalized) {
return $this->guardRelations($entity, $requestedRelations, $normalized);
function ($entity) use ($requestedRelations, $parentRelation, $normalized) {
return $this->guardRelations($entity, $requestedRelations, $parentRelation, $normalized);
}
);
}
Expand All @@ -163,11 +168,16 @@ function ($entity) use ($requestedRelations, $normalized) {
*
* @param Model $entity
* @param array $requestedRelations
* @param string|null $parentRelation
* @param bool $normalized
* @return Model
*/
public function guardRelations(Model $entity, array $requestedRelations, bool $normalized = false): Model
{
public function guardRelations(
Model $entity,
array $requestedRelations,
?string $parentRelation = null,
bool $normalized = false
): Model {
if (!$normalized) {
$requestedRelations = $this->normalizeRequestedRelations($requestedRelations);
}
Expand All @@ -176,17 +186,27 @@ public function guardRelations(Model $entity, array $requestedRelations, bool $n
ksort($relations);

foreach ($relations as $relationName => $relation) {
if ($relationName === 'pivot') {
if ($relationName === 'pivot' || $relationName === $parentRelation) {
continue;
}

if (!array_key_exists($relationName, $requestedRelations)) {
unset($relations[$relationName]);
} elseif ($relation !== null) {
if ($relation instanceof Model) {
$relation = $this->guardRelations($relation, $requestedRelations[$relationName], true);
$relation = $this->guardRelations(
$relation,
$requestedRelations[$relationName],
$relationName,
true
);
} else {
$relation = $this->guardRelationsForCollection($relation, $requestedRelations[$relationName], true);
$relation = $this->guardRelationsForCollection(
$relation,
$requestedRelations[$relationName],
$relationName,
true
);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Http/Middleware/EnforceExpectsJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Orion\Http\Middleware;

use Illuminate\Http\Request;
use Illuminate\Support\Str;

class EnforceExpectsJson
{
Expand All @@ -13,7 +14,10 @@ class EnforceExpectsJson
*/
public function handle(Request $request, $next)
{
$request->headers->add(['Accept' => 'application/json']);
if (!Str::contains($request->header('Accept'), 'application/json')) {
$request->headers->set('Accept', 'application/json, ' . $request->header('Accept'));
}

return $next($request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function build(Model $resourceModel): ModelResourceComponent
$component->type = 'object';
$component->properties = [
'allOf' => [
['$ref' => "#/components/schemas/{$resourceComponentBaseName}Resource"],
['$ref' => "#/components/schemas/{$resourceComponentBaseName}"],
[
'type' => 'object',
'properties' => $this->getPropertiesFromSchema($resourceModel)
Expand Down
2 changes: 1 addition & 1 deletion src/ValueObjects/RegisteredResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getKeyType(): string
*/
protected function qualifyControllerClass(string $controller): string
{
if (Str::startsWith($controller, config('orion.namespaces.controllers'))) {
if (class_exists($controller) || Str::startsWith($controller, config('orion.namespaces.controllers'))) {
return $controller;
}

Expand Down
37 changes: 35 additions & 2 deletions tests/Unit/Drivers/Standard/RelationsResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Orion\Drivers\Standard\RelationsResolver;
use Orion\Http\Requests\Request;
use Orion\Tests\Fixtures\App\Models\Category;
use Orion\Tests\Fixtures\App\Models\Company;
use Orion\Tests\Fixtures\App\Models\Post;
use Orion\Tests\Fixtures\App\Models\Team;
Expand Down Expand Up @@ -85,6 +86,35 @@ public function guarding_entity_nested_relations()
{
$post = new Post(['title' => 'test post']);

$parentCategory = new Category(['name' => 'parent category']); // categories
$childCategory = new Category(['name' => 'child category']); // categories.categories
$nestedChildCategory = new Category(['name' => 'nested child category']); // categories.categories.categories

$childCategory->setRelation('categories', collect([$nestedChildCategory]));
$parentCategory->setRelation('categories', collect([$childCategory]));

$post->setRelations(
[
'categories' => collect([$parentCategory]),
]
);

$relationsResolver = new RelationsResolver(['categories'], []);
$guardedPost = $relationsResolver->guardRelations($post, ['categories']);

self::assertArrayHasKey('categories', $guardedPost->getRelations());
self::assertArrayHasKey('categories', $guardedPost->getRelation('categories')->first()->getRelations());
self::assertArrayHasKey(
'categories',
$guardedPost->getRelation('categories')->first()->getRelation('categories')->first()->getRelations()
);
}

/** @test */
public function guarding_entity_recursive_nested_relations()
{
$post = new Post(['title' => 'test post']);

$manager = new User(['name' => 'manager user']);

$team = new Team(['name' => 'test team']);
Expand All @@ -102,7 +132,10 @@ public function guarding_entity_nested_relations()
);

$relationsResolver = new RelationsResolver(['user', 'editors.team.users', 'editors.team.company'], []);
$guardedPost = $relationsResolver->guardRelations($post, ['user', 'editors.team.users', 'editors.team.company']);
$guardedPost = $relationsResolver->guardRelations(
$post,
['user', 'editors.team.users', 'editors.team.company']
);

self::assertArrayHasKey('user', $guardedPost->getRelations());
self::assertArrayHasKey('editors', $guardedPost->getRelations());
Expand Down Expand Up @@ -159,4 +192,4 @@ public function resolving_deep_relation_from_param_constraint(): void

self::assertSame('user.posts', $relation);
}
}
}
18 changes: 16 additions & 2 deletions tests/Unit/Http/Middleware/EnforceExpectsJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,28 @@
class EnforceExpectsJsonTest extends TestCase
{
/** @test */
public function adding_accept_header()
public function adding_application_json_to_accept_header(): void
{
$request = Request::create('/api/posts');

(new EnforceExpectsJson())->handle(
$request,
function ($processedRequest) {
$this->assertEquals('application/json', $processedRequest->header('Accept'));
$this->assertTrue($processedRequest->expectsJson());
}
);
}

/** @test */
public function preserving_existing_accept_header_content_types(): void
{
$request = Request::create('/api/posts');
$request->headers->set('Accept', 'application/xml');

(new EnforceExpectsJson())->handle(
$request,
function ($processedRequest) {
$this->assertSame('application/json, application/xml', $processedRequest->header('Accept'));
}
);
}
Expand Down

0 comments on commit 34b3b70

Please sign in to comment.