Skip to content
This repository was archived by the owner on Jul 15, 2021. It is now read-only.

Commit 7f98851

Browse files
committed
fixes #43 - Returning comment object after comment operation
1 parent b0f0204 commit 7f98851

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/CanComment.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@
44
namespace Actuallymab\LaravelComment;
55

66
use Actuallymab\LaravelComment\Contracts\Commentable;
7+
use Actuallymab\LaravelComment\Models\Comment;
78
use Illuminate\Database\Eloquent\Relations\MorphMany;
89

910
trait CanComment
1011
{
11-
public function comment(Commentable $commentable, string $commentText = '', int $rate = 0): self
12+
public function comment(Commentable $commentable, string $commentText = '', int $rate = 0): Comment
1213
{
1314
$commentModel = config('comment.model');
1415

15-
$commentable->comments()->save(new $commentModel([
16+
$comment = new $commentModel([
1617
'comment' => $commentText,
1718
'rate' => $commentable->canBeRated() ? $rate : null,
1819
'approved' => $commentable->mustBeApproved() && !$this->canCommentWithoutApprove() ? false : true,
1920
'commented_id' => $this->primaryId(),
2021
'commented_type' => get_class(),
21-
]));
22+
]);
2223

23-
return $this;
24+
$commentable->comments()->save($comment);
25+
26+
return $comment;
2427
}
2528

2629
public function canCommentWithoutApprove(): bool

tests/CommentTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Actuallymab\LaravelComment\Tests;
55

6+
use Actuallymab\LaravelComment\Models\Comment;
67
use Actuallymab\LaravelComment\Tests\Models\Product;
78
use Actuallymab\LaravelComment\Tests\Models\User;
89
use Illuminate\Foundation\Testing\WithFaker;
@@ -27,6 +28,15 @@ public function it_belongs_to_a_can_comment_object()
2728
$this->assertTrue($product->comments()->first()->commented->is($user));
2829
}
2930

31+
/** @test */
32+
public function comment_object_is_returned_after_comment_operation()
33+
{
34+
$user = $this->createUser();
35+
$product = $this->createProduct();
36+
37+
$this->assertInstanceOf(Comment::class, $user->comment($product, $this->faker->sentence));
38+
}
39+
3040
/** @test */
3141
public function comment_can_be_checked()
3242
{

0 commit comments

Comments
 (0)