-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9133475
commit ae98c5e
Showing
7 changed files
with
158 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\Virtue\Models\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] | ||
final class DispatchesOn | ||
{ | ||
/** | ||
* @param class-string $class | ||
*/ | ||
public function __construct( | ||
public string $event, | ||
public string $class, | ||
) { | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Datasets; | ||
|
||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
final class UserCreated | ||
{ | ||
use Dispatchable; | ||
use SerializesModels; | ||
|
||
public function __construct( | ||
public UserEvents $user, | ||
) { | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Datasets; | ||
|
||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
final class UserDeleted | ||
{ | ||
use Dispatchable; | ||
use SerializesModels; | ||
|
||
public function __construct( | ||
public UserEvents $user, | ||
) { | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Datasets; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use WendellAdriel\Virtue\Models\Attributes\Database; | ||
use WendellAdriel\Virtue\Models\Attributes\DispatchesOn; | ||
use WendellAdriel\Virtue\Models\Attributes\Fillable; | ||
use WendellAdriel\Virtue\Models\Attributes\PrimaryKey; | ||
use WendellAdriel\Virtue\Models\Concerns\Virtue; | ||
|
||
#[Fillable([ | ||
'id', | ||
'name', | ||
'email', | ||
'password', | ||
])] | ||
#[Database(table: 'users')] | ||
#[PrimaryKey(incrementing: false)] | ||
#[DispatchesOn(event: 'created', class: UserCreated::class)] | ||
#[DispatchesOn(event: 'updated', class: UserUpdated::class)] | ||
#[DispatchesOn(event: 'deleted', class: UserDeleted::class)] | ||
final class UserEvents extends Model | ||
{ | ||
use Virtue; | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Datasets; | ||
|
||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
final class UserUpdated | ||
{ | ||
use Dispatchable; | ||
use SerializesModels; | ||
|
||
public function __construct( | ||
public UserEvents $user, | ||
) { | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Support\Facades\Event; | ||
use Tests\Datasets\UserCreated; | ||
use Tests\Datasets\UserDeleted; | ||
use Tests\Datasets\UserEvents; | ||
use Tests\Datasets\UserUpdated; | ||
|
||
it('dispatches events', function () { | ||
Event::fake(); | ||
|
||
$user = UserEvents::create([ | ||
'id' => 10, | ||
'name' => fake()->name, | ||
'email' => fake()->unique()->safeEmail, | ||
'password' => 's3Cr3t@!!!', | ||
]); | ||
Event::assertDispatched(UserCreated::class); | ||
|
||
$user->name = fake()->name; | ||
$user->save(); | ||
Event::assertDispatched(UserUpdated::class); | ||
|
||
$user->delete(); | ||
Event::assertDispatched(UserDeleted::class); | ||
}); |