Skip to content

Commit

Permalink
add observer test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mexion committed Dec 11, 2024
1 parent 5766db4 commit 1997fcc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/ObserverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Illuminate\Support\Facades\Log;
use Spatie\LaravelCipherSweet\Tests\TestClasses\User;
use Spatie\LaravelCipherSweet\Tests\TestClasses\UserObserver;


it('whatever', function () {
$user = User::create([
'name' => 'John Doe',
'password' => bcrypt('password'),
'email' => '[email protected]',
]);

Log::spy();
User::observe(UserObserver::class);
$user->update(['email' => '[email protected]']);

Log::shouldHaveReceived('info')
->with("saving: dirty=2")
->once();

Log::shouldHaveReceived('info')
->with("saved: dirty=2")
->once();
});
19 changes: 19 additions & 0 deletions tests/TestClasses/UserObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace Spatie\LaravelCipherSweet\Tests\TestClasses;

class UserObserver
{
public function saved(User $user)
{
$msg = "saved: dirty=".sizeof($user->getDirty());
\Log::info($msg);
}

public function saving(User $user)
{
$user->name .=".";

$msg = "saving: dirty=".sizeof($user->getDirty());
\Log::info($msg);
}
}

0 comments on commit 1997fcc

Please sign in to comment.