trypost/tests/Feature/Actions/Post/CreatePostTest.php
Paulo Castellano 459f4dd5a5 Harden created_via: require it, cover duplicate and all entry points.
Reject CreatePost calls without CreatedVia, set Web on DuplicatePost, and assert wiring for templates, AI, automation, and API spoof attempts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-24 11:30:15 -03:00

54 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
use App\Actions\Post\CreatePost;
use App\Enums\Post\CreatedVia;
use App\Events\PostCreated;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\Event;
test('execute dispatches PostCreated with the persisted post', function () {
Event::fake([PostCreated::class]);
$user = User::factory()->create();
$workspace = Workspace::factory()->create(['user_id' => $user->id]);
$post = CreatePost::execute($workspace, $user, [
'content' => 'Hello world',
'created_via' => CreatedVia::Web,
]);
Event::assertDispatched(
PostCreated::class,
fn (PostCreated $event) => $event->post->id === $post->id
&& $event->post->workspace_id === $workspace->id,
);
});
test('execute persists created_via for each entry point', function (CreatedVia $createdVia) {
$user = User::factory()->create();
$workspace = Workspace::factory()->create(['user_id' => $user->id]);
$post = CreatePost::execute($workspace, $user, [
'content' => 'Hello world',
'created_via' => $createdVia,
]);
expect($post->fresh()->created_via)->toBe($createdVia);
})->with([
'web' => CreatedVia::Web,
'mcp' => CreatedVia::Mcp,
'api' => CreatedVia::Api,
'automation' => CreatedVia::Automation,
]);
test('execute requires created_via', function () {
$user = User::factory()->create();
$workspace = Workspace::factory()->create(['user_id' => $user->id]);
CreatePost::execute($workspace, $user, [
'content' => 'Hello world',
]);
})->throws(InvalidArgumentException::class, 'created_via must be a CreatedVia enum case.');