|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Laravel\Tests\Eloquent\Metadata; |
| 15 | + |
| 16 | +use ApiPlatform\Laravel\Eloquent\Metadata\ModelMetadata; |
| 17 | +use Illuminate\Database\Eloquent\Model; |
| 18 | +use Illuminate\Database\Eloquent\Relations\HasMany; |
| 19 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 20 | +use Orchestra\Testbench\Concerns\WithWorkbench; |
| 21 | +use Orchestra\Testbench\TestCase; |
| 22 | +use Workbench\App\Models\Book; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Tobias Oitzinger <tobiasoitzinger@gmail.com> |
| 26 | + */ |
| 27 | +final class ModelMetadataTest extends TestCase |
| 28 | +{ |
| 29 | + use RefreshDatabase; |
| 30 | + use WithWorkbench; |
| 31 | + |
| 32 | + public function testHiddenAttributesAreCorrectlyIdentified(): void |
| 33 | + { |
| 34 | + $model = new class extends Model { |
| 35 | + protected $hidden = ['secret']; |
| 36 | + |
| 37 | + /** |
| 38 | + * @return HasMany<Book> |
| 39 | + */ |
| 40 | + public function secret(): HasMany |
| 41 | + { |
| 42 | + return $this->hasMany(Book::class); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + $metadata = new ModelMetadata(); |
| 47 | + $this->assertCount(0, $metadata->getRelations($model)); |
| 48 | + } |
| 49 | + |
| 50 | + public function testVisibleAttributesAreCorrectlyIdentified(): void |
| 51 | + { |
| 52 | + $model = new class extends Model { |
| 53 | + protected $visible = ['secret']; |
| 54 | + |
| 55 | + /** |
| 56 | + * @return HasMany<Book> |
| 57 | + */ |
| 58 | + public function secret(): HasMany |
| 59 | + { |
| 60 | + return $this->hasMany(Book::class); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + $metadata = new ModelMetadata(); |
| 65 | + $this->assertCount(1, $metadata->getRelations($model)); |
| 66 | + } |
| 67 | + |
| 68 | + public function testAllAttributesVisibleByDefault(): void |
| 69 | + { |
| 70 | + $model = new class extends Model { |
| 71 | + /** |
| 72 | + * @return HasMany<Book> |
| 73 | + */ |
| 74 | + public function secret(): HasMany |
| 75 | + { |
| 76 | + return $this->hasMany(Book::class); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + $metadata = new ModelMetadata(); |
| 81 | + $this->assertCount(1, $metadata->getRelations($model)); |
| 82 | + } |
| 83 | +} |
0 commit comments