File tree Expand file tree Collapse file tree 4 files changed +73
-1
lines changed
contexts/ArticlePublishing Expand file tree Collapse file tree 4 files changed +73
-1
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Contexts \ArticlePublishing \Domain \Events ;
6+
7+ use Illuminate \Foundation \Events \Dispatchable ;
8+ use Contexts \ArticlePublishing \Domain \Models \ArticleId ;
9+
10+ class ArticlePublishedEvent
11+ {
12+ use Dispatchable;
13+
14+ public function __construct (
15+ private readonly ArticleId $ articleId ,
16+ ) {
17+ }
18+
19+ public function getArticleId (): ArticleId
20+ {
21+ return $ this ->articleId ;
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Contexts \ArticlePublishing \Infrastructure \EventListeners ;
6+
7+ use Contexts \ArticlePublishing \Domain \Events \ArticlePublishedEvent ;
8+
9+ class ConsoleOutputListener
10+ {
11+ public function handle (ArticlePublishedEvent $ event ): void
12+ {
13+ echo "Article published: {$ event ->getArticleId ()->value }\n" ;
14+ }
15+ }
Original file line number Diff line number Diff line change 44
55namespace Contexts \ArticlePublishing \Infrastructure ;
66
7+ use Contexts \ArticlePublishing \Domain \Events \ArticlePublishedEvent ;
78use Illuminate \Foundation \Support \Providers \RouteServiceProvider ;
89use Illuminate \Support \Facades \Route ;
910use Illuminate \Support \ServiceProvider as BaseServiceProvider ;
11+ use Contexts \ArticlePublishing \Infrastructure \EventListeners \ConsoleOutputListener ;
12+ use Illuminate \Support \Facades \Event ;
1013
1114class ServiceProvider extends BaseServiceProvider
1215{
1316 public function boot (): void
1417 {
1518 $ this ->loadMigrationsFrom (__DIR__ .'/Migrations ' );
1619 $ this ->loadJsonTranslationsFrom (__DIR__ .'/Lang ' );
20+ Event::listen (
21+ ArticlePublishedEvent::class,
22+ ConsoleOutputListener::class
23+ );
1724 }
1825
1926 public function register (): void
@@ -31,6 +38,8 @@ public function map(): void
3138 });
3239 }
3340
41+
42+
3443 public function provides (): array
3544 {
3645 return [];
Original file line number Diff line number Diff line change 22
33declare (strict_types=1 );
44
5- it ('can publish aritcle via api ' , function () {
5+ use Contexts \ArticlePublishing \Domain \Events \ArticlePublishedEvent ;
6+ use Contexts \ArticlePublishing \Infrastructure \EventListeners \ConsoleOutputListener ;
7+
8+ it ('can publish aritcle drafts via api ' , function () {
69 $ response = $ this ->postJson ('articles ' , [
710 'title ' => 'My Article ' ,
811 'body ' => 'This is my article body ' ,
1114
1215 $ response ->assertStatus (201 );
1316});
17+
18+ it ('can publish published articles via api ' , function () {
19+ $ response = $ this ->postJson ('articles ' , [
20+ 'title ' => 'My Article ' ,
21+ 'body ' => 'This is my article body ' ,
22+ 'status ' => 'published ' ,
23+ ]);
24+
25+ $ response ->assertStatus (201 );
26+ });
27+
28+ it ('dispatches an event when an article is published ' , function () {
29+ Event::fake ();
30+
31+ $ this ->postJson ('articles ' , [
32+ 'title ' => 'My Article ' ,
33+ 'body ' => 'This is my article body ' ,
34+ 'status ' => 'published ' ,
35+ ]);
36+
37+ Event::assertDispatched (ArticlePublishedEvent::class);
38+ });
You can’t perform that action at this time.
0 commit comments