trypost/tests/Feature/Observers/PostObserverTest.php
Paulo Castellano d1d38865ef Dispatch PostCreated from PostObserver on every create.
Centralize provenance/broadcast/PostHog triggers so CreatePost and DuplicatePost no longer fire the event by hand.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-24 12:01:01 -03:00

40 lines
1 KiB
PHP

<?php
declare(strict_types=1);
use App\Events\PostCreated;
use App\Models\Post;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\Event;
test('creating a post dispatches PostCreated via the observer', function () {
Event::fake([PostCreated::class]);
$user = User::factory()->create();
$workspace = Workspace::factory()->create(['user_id' => $user->id]);
$post = Post::factory()->create([
'workspace_id' => $workspace->id,
'user_id' => $user->id,
]);
Event::assertDispatched(
PostCreated::class,
fn (PostCreated $event) => $event->post->id === $post->id,
);
});
test('creating a post quietly does not dispatch PostCreated', function () {
Event::fake([PostCreated::class]);
$user = User::factory()->create();
$workspace = Workspace::factory()->create(['user_id' => $user->id]);
Post::factory()->createQuietly([
'workspace_id' => $workspace->id,
'user_id' => $user->id,
]);
Event::assertNotDispatched(PostCreated::class);
});