-
-
Notifications
You must be signed in to change notification settings - Fork 877
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(laravel): hiding/showing relationships (#6679)
* 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
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |