Skip to content

Commit 1fb3e9e

Browse files
authored
Add test for the $hidden property (#2687)
1 parent 973b6b9 commit 1fb3e9e

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

tests/Models/HiddenAnimal.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Tests\Models;
6+
7+
use MongoDB\Laravel\Eloquent\Model as Eloquent;
8+
use MongoDB\Laravel\Query\Builder;
9+
10+
/**
11+
* @property string $name
12+
* @property string $country
13+
* @property bool $can_be_eaten
14+
* @mixin Eloquent
15+
* @method static Builder create(...$values)
16+
* @method static Builder truncate()
17+
* @method static Eloquent sole(...$parameters)
18+
*/
19+
final class HiddenAnimal extends Eloquent
20+
{
21+
protected $fillable = [
22+
'name',
23+
'country',
24+
'can_be_eaten',
25+
];
26+
27+
protected $hidden = ['country'];
28+
}

tests/PropertyTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Tests\Eloquent;
6+
7+
use MongoDB\Laravel\Tests\Models\HiddenAnimal;
8+
use MongoDB\Laravel\Tests\TestCase;
9+
10+
use function assert;
11+
12+
final class PropertyTest extends TestCase
13+
{
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
HiddenAnimal::truncate();
19+
}
20+
21+
public function testCanHideCertainProperties(): void
22+
{
23+
HiddenAnimal::create([
24+
'name' => 'Sheep',
25+
'country' => 'Ireland',
26+
'can_be_eaten' => true,
27+
]);
28+
29+
$hiddenAnimal = HiddenAnimal::sole();
30+
assert($hiddenAnimal instanceof HiddenAnimal);
31+
self::assertSame('Ireland', $hiddenAnimal->country);
32+
self::assertTrue($hiddenAnimal->can_be_eaten);
33+
34+
self::assertArrayHasKey('name', $hiddenAnimal->toArray());
35+
self::assertArrayNotHasKey('country', $hiddenAnimal->toArray(), 'the country column should be hidden');
36+
self::assertArrayHasKey('can_be_eaten', $hiddenAnimal->toArray());
37+
}
38+
}

0 commit comments

Comments
 (0)