Skip to content

Commit db5376b

Browse files
committed
feat: implement ArticlePublishedEvent and ConsoleOutputListener for article publishing events
1 parent d56fad0 commit db5376b

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

contexts/ArticlePublishing/Infrastructure/ServiceProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@
44

55
namespace Contexts\ArticlePublishing\Infrastructure;
66

7+
use Contexts\ArticlePublishing\Domain\Events\ArticlePublishedEvent;
78
use Illuminate\Foundation\Support\Providers\RouteServiceProvider;
89
use Illuminate\Support\Facades\Route;
910
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
11+
use Contexts\ArticlePublishing\Infrastructure\EventListeners\ConsoleOutputListener;
12+
use Illuminate\Support\Facades\Event;
1013

1114
class 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 [];

contexts/ArticlePublishing/Tests/Feature/ArticlePublishingTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
declare(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',
@@ -11,3 +14,25 @@
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+
});

0 commit comments

Comments
 (0)