Skip to content

Commit

Permalink
Add tests for the mutator
Browse files Browse the repository at this point in the history
Docker doesn't find typoes?

Styling

Import
  • Loading branch information
RosemaryOrchard committed Aug 25, 2022
1 parent 55e80a2 commit 5a5eb96
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
18 changes: 18 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Collection;
use Jenssegers\Mongodb\Connection;
use Jenssegers\Mongodb\Eloquent\Model;
Expand Down Expand Up @@ -678,6 +679,23 @@ public function testDotNotation(): void
$this->assertEquals('Strasbourg', $user['address.city']);
}

public function testAttributeMutator(): void
{
$username = 'JaneDoe';
$usernameSlug = Str::slug($username);
$user = User::create([
'name' => 'Jane Doe',
'username' => $username,
]);

$this->assertNotEquals($username, $user->getAttribute('username'));
$this->assertNotEquals($username, $user['username']);
$this->assertNotEquals($username, $user->username);
$this->assertEquals($usernameSlug, $user->getAttribute('username'));
$this->assertEquals($usernameSlug, $user['username']);
$this->assertEquals($usernameSlug, $user->username);
}

public function testMultipleLevelDotNotation(): void
{
/** @var Book $book */
Expand Down
16 changes: 15 additions & 1 deletion tests/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Casts\Attribute;

/**
* Class User.
Expand All @@ -21,10 +23,14 @@
* @property \Carbon\Carbon $birthday
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property string $username
*/
class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword, HybridRelations, Notifiable;
use Authenticatable;
use CanResetPassword;
use HybridRelations;
use Notifiable;

protected $connection = 'mongodb';
protected $dates = ['birthday', 'entry.date'];
Expand Down Expand Up @@ -84,4 +90,12 @@ protected function serializeDate(DateTimeInterface $date)
{
return $date->format('l jS \of F Y h:i:s A');
}

protected function username(): Attribute
{
return Attribute::make(
get: fn ($value) => $value,
set: fn ($value) => Str::slug($value)
);
}
}

0 comments on commit 5a5eb96

Please sign in to comment.