Skip to content

Add test for the $hidden property #2687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions tests/Models/HiddenAnimal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Models;

use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Query\Builder;

/**
* @property string $name
* @property string $country
* @property bool $can_be_eaten
* @mixin Eloquent
* @method static Builder create(...$values)
* @method static Builder truncate()
* @method static Eloquent sole(...$parameters)
*/
final class HiddenAnimal extends Eloquent
{
protected $fillable = [
'name',
'country',
'can_be_eaten',
];

protected $hidden = ['country'];
}
38 changes: 38 additions & 0 deletions tests/PropertyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Eloquent;

use MongoDB\Laravel\Tests\Models\HiddenAnimal;
use MongoDB\Laravel\Tests\TestCase;

use function assert;

final class PropertyTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

HiddenAnimal::truncate();
}

public function testCanHideCertainProperties(): void
{
HiddenAnimal::create([
'name' => 'Sheep',
'country' => 'Ireland',
'can_be_eaten' => true,
]);

$hiddenAnimal = HiddenAnimal::sole();
assert($hiddenAnimal instanceof HiddenAnimal);
self::assertSame('Ireland', $hiddenAnimal->country);
self::assertTrue($hiddenAnimal->can_be_eaten);

self::assertArrayHasKey('name', $hiddenAnimal->toArray());
self::assertArrayNotHasKey('country', $hiddenAnimal->toArray(), 'the country column should be hidden');
self::assertArrayHasKey('can_be_eaten', $hiddenAnimal->toArray());
}
}