generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from felabrecque/fix/model-changes-and-observer
Revert PR #74 and fix getChanges()
- Loading branch information
Showing
5 changed files
with
152 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,77 @@ | ||
<?php | ||
|
||
use Spatie\LaravelCipherSweet\Tests\TestClasses\User; | ||
|
||
it('can have correct attributes|dirty|changes properties',function () { | ||
expect(User::count(), 0); | ||
|
||
$user = User::create([ | ||
'name' => 'John Doe', | ||
'password' => bcrypt('password'), | ||
'email' => '[email protected]', | ||
]); | ||
|
||
expect($user) | ||
->name->toBe('John Doe') | ||
->getAttribute('name')->toBe('John Doe') | ||
->created_at->not()->toBeNull() | ||
->updated_at->not()->toBeNull() | ||
->isDirty()->toBeFalse() | ||
->isClean()->toBeTrue() | ||
->getDirty()->toBeEmpty() | ||
->getOriginal('name')->toBe('John Doe') | ||
->wasChanged()->toBeFalse() | ||
->getChanges()->toBeEmpty(); | ||
|
||
expect(User::count(), 1); | ||
|
||
$user = User::first(); | ||
|
||
expect($user) | ||
->name->toBe('John Doe') | ||
->getAttribute('name')->toBe('John Doe') | ||
->created_at->not()->toBeNull() | ||
->updated_at->not()->toBeNull() | ||
->isDirty()->toBeFalse() | ||
->isClean()->toBeTrue() | ||
->getDirty()->toBeEmpty() | ||
->getOriginal('name')->toBe('John Doe') | ||
->wasChanged()->toBeFalse() | ||
->getChanges()->toBeEmpty(); | ||
|
||
$user->name = 'New name'; | ||
|
||
expect($user) | ||
->name->toBe('New name') | ||
->getAttribute('name')->toBe('New name') | ||
->isDirty()->toBeTrue() | ||
->isClean()->toBeFalse() | ||
->getDirty()->toBe(['name' => 'New name']) | ||
->getOriginal('name')->toBe('John Doe') | ||
->wasChanged()->toBeFalse() | ||
->getChanges()->toBeEmpty(); | ||
|
||
$createdAt = $user->created_at; | ||
$updatedAt = $user->updated_at; | ||
|
||
$this->travel(1)->seconds(); | ||
$user->save(); | ||
$this->travelBack(); | ||
|
||
expect($user) | ||
->name->toBe('New name') | ||
->getAttribute('name')->toBe('New name') | ||
->created_at->toEqual($createdAt) | ||
->updated_at->toBeGreaterThan($updatedAt) | ||
->isDirty()->toBeFalse() | ||
->isClean()->toBeTrue() | ||
->getDirty()->toBeEmpty() | ||
->getOriginal('name')->toBe('New name') | ||
->wasChanged('name')->toBeTrue() | ||
->wasChanged('password')->toBeFalse() | ||
->wasChanged('email')->toBeFalse() | ||
->getChanges()->toBe([ | ||
'name' => 'New name', | ||
'updated_at' => (string) $user->updated_at, | ||
]); | ||
}); |
This file contains 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 |
---|---|---|
|
@@ -14,13 +14,40 @@ | |
Log::spy(); | ||
|
||
User::observe(UserObserver::class); | ||
|
||
$this->travel(1)->seconds(); // to force updated_at to be updated | ||
$user->update(['email' => '[email protected]']); | ||
$this->travelBack(); | ||
|
||
Log::shouldHaveReceived('info') | ||
->with("saving: dirty=2") // name attribute + encrypted email attribute | ||
->once(); | ||
|
||
Log::shouldHaveReceived('info') | ||
->with("saved: changed=3") // the name, updated_at & email attribute | ||
->once(); | ||
}); | ||
|
||
it('can exclude unmodified attributes from changes', function () { | ||
$user = User::create([ | ||
'name' => 'John Doe', | ||
'password' => bcrypt('password'), | ||
'email' => '[email protected]', | ||
]); | ||
|
||
Log::spy(); | ||
|
||
User::observe(UserObserver::class); | ||
|
||
$this->travel(1)->seconds(); // to force updated_at to be updated | ||
$user->update(['name' => 'John Doe2']); | ||
$this->travelBack(); | ||
|
||
Log::shouldHaveReceived('info') | ||
->with("saving: dirty=2") | ||
->with("saving: dirty=2") // name attribute + encrypted email attribute (encrypted are always present) | ||
->once(); | ||
|
||
Log::shouldHaveReceived('info') | ||
->with("saved: dirty=2") | ||
->with("saved: changed=2") // the name & updated_at attribute | ||
->once(); | ||
}); |
This file contains 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