Skip to content

Commit

Permalink
fix(laravel): hiding/showing relationships (#6679)
Browse files Browse the repository at this point in the history
* fix(laravel): fix hiding/showing relationships

Check if is in visible or hidden (if exists) just like with regular proeprties

Closes: #6678
Signed-off-by: Tobias Oitzinger <[email protected]>

* test: use a WorkbenchTestCase

---------

Signed-off-by: Tobias Oitzinger <[email protected]>
Co-authored-by: soyuka <[email protected]>
  • Loading branch information
toitzi and soyuka authored Oct 4, 2024
1 parent f839c93 commit 6d4e248
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Laravel/Eloquent/Metadata/ModelMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public function getRelations(Model $model): Collection
|| $method->isAbstract()
|| Model::class === $method->getDeclaringClass()->getName()
|| $method->getNumberOfParameters() > 0
|| $this->attributeIsHidden($method->getName(), $model)
)
->filter(function (\ReflectionMethod $method) {
if ($method->getReturnType() instanceof \ReflectionNamedType
Expand Down
83 changes: 83 additions & 0 deletions src/Laravel/Tests/Eloquent/Metadata/ModelMetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\Tests\Eloquent\Metadata;

use ApiPlatform\Laravel\Eloquent\Metadata\ModelMetadata;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use Workbench\App\Models\Book;

/**
* @author Tobias Oitzinger <[email protected]>
*/
final class ModelMetadataTest extends TestCase
{
use RefreshDatabase;
use WithWorkbench;

public function testHiddenAttributesAreCorrectlyIdentified(): void
{
$model = new class extends Model {
protected $hidden = ['secret'];

/**
* @return HasMany<Book>
*/
public function secret(): HasMany
{
return $this->hasMany(Book::class);
}
};

$metadata = new ModelMetadata();
$this->assertCount(0, $metadata->getRelations($model));
}

public function testVisibleAttributesAreCorrectlyIdentified(): void
{
$model = new class extends Model {
protected $visible = ['secret'];

/**
* @return HasMany<Book>
*/
public function secret(): HasMany
{
return $this->hasMany(Book::class);
}
};

$metadata = new ModelMetadata();
$this->assertCount(1, $metadata->getRelations($model));
}

public function testAllAttributesVisibleByDefault(): void
{
$model = new class extends Model {
/**
* @return HasMany<Book>
*/
public function secret(): HasMany
{
return $this->hasMany(Book::class);
}
};

$metadata = new ModelMetadata();
$this->assertCount(1, $metadata->getRelations($model));
}
}

0 comments on commit 6d4e248

Please sign in to comment.