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

Commit

Permalink
fixes #43 - Returning comment object after comment operation
Browse files Browse the repository at this point in the history
  • Loading branch information
ShortlyMAB committed Jan 4, 2019
1 parent b0f0204 commit 7f98851
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/CanComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@
namespace Actuallymab\LaravelComment;

use Actuallymab\LaravelComment\Contracts\Commentable;
use Actuallymab\LaravelComment\Models\Comment;
use Illuminate\Database\Eloquent\Relations\MorphMany;

trait CanComment
{
public function comment(Commentable $commentable, string $commentText = '', int $rate = 0): self
public function comment(Commentable $commentable, string $commentText = '', int $rate = 0): Comment
{
$commentModel = config('comment.model');

$commentable->comments()->save(new $commentModel([
$comment = new $commentModel([
'comment' => $commentText,
'rate' => $commentable->canBeRated() ? $rate : null,
'approved' => $commentable->mustBeApproved() && !$this->canCommentWithoutApprove() ? false : true,
'commented_id' => $this->primaryId(),
'commented_type' => get_class(),
]));
]);

return $this;
$commentable->comments()->save($comment);

return $comment;
}

public function canCommentWithoutApprove(): bool
Expand Down
10 changes: 10 additions & 0 deletions tests/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Actuallymab\LaravelComment\Tests;

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

/** @test */
public function comment_object_is_returned_after_comment_operation()
{
$user = $this->createUser();
$product = $this->createProduct();

$this->assertInstanceOf(Comment::class, $user->comment($product, $this->faker->sentence));
}

/** @test */
public function comment_can_be_checked()
{
Expand Down

0 comments on commit 7f98851

Please sign in to comment.