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 2 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
26 changes: 26 additions & 0 deletions tests/Models/Animal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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 Animal extends Eloquent
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see what is tested with this model class. Hidden fields and the toArray feature are tested with HiddenAnimal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea of the two classes is that one has the hidden property and another doesn't. As to prove the working of the hidden property.

Or do you suggest to have one class and use different fields to test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, the test on HiddenAnimal already covers the feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, will remove this class and its usage

{
protected $fillable = [
'name',
'country',
'can_be_eaten',
];
}
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'];
}
56 changes: 56 additions & 0 deletions tests/PropertyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Eloquent;

use MongoDB\Laravel\Tests\Models\Animal;
use MongoDB\Laravel\Tests\Models\HiddenAnimal;
use MongoDB\Laravel\Tests\TestCase;
use PHPUnit\Framework\Attributes\Test;

use function assert;

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

Animal::truncate();
HiddenAnimal::truncate();
}

#[Test]
public function canHideCertainProperties(): void
{
Animal::create([
'name' => 'Lion',
'country' => 'Central Africa',
'can_be_eaten' => false,
]);
HiddenAnimal::create([
'name' => 'Sheep',
'country' => 'Ireland',
'can_be_eaten' => true,
]);

$animal = Animal::sole();
assert($animal instanceof Animal);
self::assertSame('Central Africa', $animal->country);
self::assertFalse($animal->can_be_eaten);

self::assertArrayHasKey('name', $animal->toArray());
self::assertArrayHasKey('country', $animal->toArray());
self::assertArrayHasKey('can_be_eaten', $animal->toArray());

$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());
}
}