Repository navigation
fix: create comments with standard keys and make the comment model configurable #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Happenv\FilamentComments\Support; | ||
|
|
||
| use Happenv\FilamentComments\Models\Comment; | ||
| use LogicException; | ||
|
|
||
| final class CommentModel | ||
| { | ||
| /** | ||
| * Class of comments: the `filament-comments.comment_model` config, or the package's own model. | ||
| * | ||
| * @return class-string<Comment> | ||
| */ | ||
| public static function resolve(): string | ||
| { | ||
| $model = config('filament-comments.comment_model') ?? Comment::class; | ||
|
|
||
| if (! is_string($model) || ! is_a($model, Comment::class, true)) { | ||
| throw new LogicException('[filament-comments.comment_model] must extend ' . Comment::class . '.'); | ||
| } | ||
|
|
||
| return $model; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Happenv\FilamentComments\Filament\Components\Comments; | ||
| use Happenv\FilamentComments\Livewire\CommentsList; | ||
| use Happenv\FilamentComments\Models\Comment; | ||
| use Happenv\FilamentComments\Support\CommentModel; | ||
| use Happenv\FilamentComments\Tests\Fixtures\Models\Post; | ||
| use Happenv\FilamentComments\Tests\Fixtures\Models\User; | ||
| use Happenv\FilamentComments\Tests\Fixtures\Models\UuidComment; | ||
| use Happenv\FilamentComments\Tests\Fixtures\Models\UuidPost; | ||
| use Illuminate\Support\Facades\Schema; | ||
| use Illuminate\Support\Str; | ||
| use Livewire\Livewire; | ||
|
|
||
| it('creates the comments table with standard integer keys', function (): void { | ||
| expect(Schema::getColumnType('comments', 'id'))->toBe('integer') | ||
| ->and(Schema::getColumnType('comments', 'commentable_id'))->toBe('integer'); | ||
| }); | ||
|
|
||
| it('stores comments on a model with integer keys by default', function (): void { | ||
| $post = Post::factory()->create(); | ||
| $this->actingAs(User::factory()->create()); | ||
|
|
||
| expect($post->comments()->getRelated())->toBeInstanceOf(Comment::class); | ||
|
|
||
| commentsList($post) | ||
| ->fillForm(['content' => '<p>Hello</p>']) | ||
| ->call('submitComment') | ||
| ->assertHasNoFormErrors(); | ||
|
|
||
| expect($post->comments()->sole()) | ||
| ->commentable_id->toEqual($post->getKey()) | ||
| ->commentable->is($post)->toBeTrue(); | ||
| }); | ||
|
|
||
| it('uses the comment model set in the config', function (): void { | ||
| config(['filament-comments.comment_model' => UuidComment::class]); | ||
|
|
||
| $post = UuidPost::create(['title' => 'A post with UUID keys']); | ||
| $this->actingAs(User::factory()->create()); | ||
|
|
||
| expect($post->comments()->getRelated())->toBeInstanceOf(UuidComment::class); | ||
|
|
||
| $settings = Comments::make()->toSettings(); | ||
|
|
||
| Livewire::test(CommentsList::class, ['record' => $post, 'settings' => $settings]) | ||
| ->fillForm(['content' => '<p>Hello UUID</p>']) | ||
| ->call('submitComment') | ||
| ->assertHasNoFormErrors() | ||
| ->assertSee('Hello UUID'); | ||
|
|
||
| $comment = $post->comments()->sole(); | ||
|
|
||
| expect($comment)->toBeInstanceOf(UuidComment::class) | ||
| ->and(Str::isUuid($comment->getKey()))->toBeTrue() | ||
| ->and($comment->commentable_id)->toBe($post->getKey()); | ||
|
|
||
| // A deep link finds the comment by its UUID. | ||
| Livewire::withQueryParams([$settings->commentIdParameter() => $comment->getKey()]) | ||
| ->test(CommentsList::class, ['record' => $post, 'settings' => $settings]) | ||
| ->assertDispatched('highlight-comment', commentId: $comment->getKey(), relationship: 'comments'); | ||
| }); | ||
|
|
||
| it('refuses a comment model that does not extend the package model', function (mixed $model): void { | ||
| config(['filament-comments.comment_model' => $model]); | ||
|
|
||
| CommentModel::resolve(); | ||
| })->throws(LogicException::class)->with([ | ||
| 'another model' => User::class, | ||
| 'not a class' => 'nope', | ||
| ]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Happenv\FilamentComments\Tests\Fixtures\Models; | ||
|
|
||
| use Happenv\FilamentComments\Models\Comment; | ||
| use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
|
|
||
| /** | ||
| * An app's own comment model with UUID keys, set as `filament-comments.comment_model`. | ||
| */ | ||
| class UuidComment extends Comment | ||
| { | ||
| use HasUuids; | ||
|
|
||
| protected $table = 'uuid_comments'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Happenv\FilamentComments\Tests\Fixtures\Models; | ||
|
|
||
| use Happenv\FilamentComments\Concerns\HasComments; | ||
| use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
| use Illuminate\Database\Eloquent\Model; | ||
|
|
||
| /** | ||
| * A commentable model with UUID keys, for the `comment_model` override. | ||
| */ | ||
| class UuidPost extends Model | ||
| { | ||
| use HasComments; | ||
| use HasUuids; | ||
|
|
||
| protected $guarded = []; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, and it went further than stale docs: that section documented the container binding, so removing it would have broken apps that use it. Fixed in 841ccd0:
CommentModel::resolve()readscomment_modelfirst and falls back to the class the container binds toComment. ThebindIfis back.bind()as the older way that still works.