-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
GromNaN
merged 5 commits into
mongodb:4.1
from
Treggats:feature/1119-add-addition-tests
Nov 30, 2023
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5afe5d6
add bare models for checking of the hidden property
Treggats dbe13c4
add test for the hidden property
Treggats 7f018dc
Apply suggestions from code review
Treggats 8ecaf24
remove usage of Animal class
Treggats a7dd344
remove unused Animal class
Treggats File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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 | ||
{ | ||
protected $fillable = [ | ||
'name', | ||
'country', | ||
'can_be_eaten', | ||
]; | ||
} |
This file contains hidden or 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,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']; | ||
} |
This file contains hidden or 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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Laravel\Tests\Eloquent; | ||
|
||
use MongoDB\Laravel\Tests\Models\Animal; | ||
Treggats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use MongoDB\Laravel\Tests\Models\HiddenAnimal; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
use PHPUnit\Framework\Attributes\Test; | ||
Treggats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
use function assert; | ||
|
||
final class PropertyTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Animal::truncate(); | ||
Treggats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
HiddenAnimal::truncate(); | ||
} | ||
|
||
#[Test] | ||
public function canHideCertainProperties(): void | ||
Treggats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
Animal::create([ | ||
'name' => 'Lion', | ||
'country' => 'Central Africa', | ||
'can_be_eaten' => false, | ||
]); | ||
Treggats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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()); | ||
Treggats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$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()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 withHiddenAnimal
.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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