Files
relaticle-comments/tests/Feature/CommentSubscriptionTest.php
manukminasyan fd5bc5271b refactor: rename for Laravel conventions and better DX
- Rename IsCommenter trait to CanComment, Commenter interface to Commentator
- Move models to Models/ namespace (Comment, Reaction, Attachment, Subscription)
- Rename user_type/user_id polymorphic columns to commenter_type/commenter_id
- Rename Config class to CommentsConfig, update config key tables->table_names
- Rename getCommentName() to getCommentDisplayName() on commentator models
- Add column_names config section for commenter morph customization
- Add table_names config with all 5 tables individually configurable
- Expand translation file with structured i18n groups
- Update all Blade views, Livewire components, events, listeners, and tests
2026-03-27 14:53:12 +04:00

107 lines
3.4 KiB
PHP

<?php
use Relaticle\Comments\CommentsConfig;
use Relaticle\Comments\Models\Subscription;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
it('has commentable morphTo relationship', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
$subscription = Subscription::create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
expect($subscription->commentable)->toBeInstanceOf(Post::class)
->and($subscription->commentable->id)->toBe($post->id);
});
it('has commenter morphTo relationship', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
$subscription = Subscription::create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
expect($subscription->commenter)->toBeInstanceOf(User::class)
->and($subscription->commenter->id)->toBe($user->id);
});
it('returns database as default notification channel', function () {
expect(CommentsConfig::getNotificationChannels())->toBe(['database']);
});
it('returns custom channels when configured', function () {
config()->set('comments.notifications.channels', ['database', 'mail']);
expect(CommentsConfig::getNotificationChannels())->toBe(['database', 'mail']);
});
it('returns true for shouldAutoSubscribe by default', function () {
expect(CommentsConfig::shouldAutoSubscribe())->toBeTrue();
});
it('returns false for shouldAutoSubscribe when configured', function () {
config()->set('comments.subscriptions.auto_subscribe', false);
expect(CommentsConfig::shouldAutoSubscribe())->toBeFalse();
});
it('checks if user is subscribed to a commentable via isSubscribed()', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
expect(Subscription::isSubscribed($post, $user))->toBeFalse();
Subscription::create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
expect(Subscription::isSubscribed($post, $user))->toBeTrue();
});
it('creates subscription via subscribe() static method', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
Subscription::subscribe($post, $user);
expect(Subscription::isSubscribed($post, $user))->toBeTrue();
});
it('removes subscription via unsubscribe() static method', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
Subscription::subscribe($post, $user);
Subscription::unsubscribe($post, $user);
expect(Subscription::isSubscribed($post, $user))->toBeFalse();
});
it('is idempotent when subscribing twice', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
Subscription::subscribe($post, $user);
Subscription::subscribe($post, $user);
expect(Subscription::where([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
])->count())->toBe(1);
});