NotifyWhenCommentPostedTest.php 5.47 KB
<?php

namespace FootyRoom\Tests\Integration\Notification;

use FootyRoom\Core\Post\Post;
use FootyRoom\Tests\TestCase;
use FootyRoom\Core\Wall\Wall;
use FootyRoom\Core\Comment\Author;
use FootyRoom\Core\Comment\Comment;
use FootyRoom\Support\UrlGenerator;
use FootyRoom\Core\ForumPost\ForumPost;
use FootyRoom\Support\SqlCommentNotifier;
use FootyRoom\Core\Comment\CommentPosted;
use FootyRoom\Repositories\Match\MatchRepository;
use FootyRoom\Core\Notification\NotifyWhenCommentPosted;

class NotifyWhenCommentPostedTest extends TestCase
{
    public function setUp()
    {
        parent::setUp();

        $this->mockSqlCommentNotifier = $this->getMockBuilder(SqlCommentNotifier::class)
                            ->disableOriginalConstructor()
                            ->setMethods([
                                'notifyForumReply',
                                'notifyProfileWallComment',
                                'notifyProfileWallReply',
                                'notifyReply',
                                ])
                            ->getMock();

        $this->mockUrlGenerator = $this->getMockBuilder(UrlGenerator::class)
                            ->disableOriginalConstructor()
                            ->setMethods([
                                'fromPost',
                                ])
                            ->getMock();

        $this->notifyWhenCommentPosted = new NotifyWhenCommentPosted($this->mockSqlCommentNotifier, $this->mockUrlGenerator);
    }

    public function testNotifyForumReplyWasCalledIfCommentPostedInForum()
    {
        $postId = 1;
        $author = new Author(41301, 'chanBC');
        $content = 'test content';
        $html = '';

        $comment = new Comment(
            $postId,
            $author,
            $content,
            $html
        );

        $title = 'title';
        $newCategoryId = 61;
        $userId = 41301;

        $forumPost = new ForumPost(
            $title,
            $newCategoryId,
            $userId
        );

        $commentPosted = new CommentPosted($comment, $forumPost);

        $this->mockSqlCommentNotifier->expects($this->once())
                                    ->method('notifyForumReply');

        $this->notifyWhenCommentPosted->handle($commentPosted);
    }

    public function testNotifyProfileWallCommentWasCalledIfCommentPostedInWallAndCommentParantIsEmpty()
    {
        $postId = 1;
        $author = new Author(41301, 'chanBC');
        $content = 'test content';
        $html = '';

        $comment = new Comment(
            $postId,
            $author,
            $content,
            $html
        );

        $userId = 41301;
        $username = 'Denis';

        $wall = new Wall(
            $userId,
            $username
        );

        $commentPosted = new CommentPosted($comment, $wall);

        $this->mockSqlCommentNotifier->expects($this->once())
                                    ->method('notifyProfileWallComment');

        $this->notifyWhenCommentPosted->handle($commentPosted);
    }

    public function testNotifyProfileWallReplyWasCalledIfCommentPostedInWallAndCommentParantIsNotEmpty()
    {
        $postId = 1;
        $author = new Author(41301, 'chanBC');
        $content = 'test content';
        $html = '';
        $parentId = 2;

        $comment = new Comment(
            $postId,
            $author,
            $content,
            $html,
            $parentId
        );

        $userId = 41301;
        $username = 'Denis';

        $wall = new Wall(
            $userId,
            $username
        );

        $commentPosted = new CommentPosted($comment, $wall);

        $this->mockSqlCommentNotifier->expects($this->once())
                                    ->method('notifyProfileWallReply');

        $this->notifyWhenCommentPosted->handle($commentPosted);
    }

    public function testNotifyReplyWasCalledIfCommentPostedInCommentParantIsNotEmptyAndNotPostInForumAndWall()
    {
        $postId = 1;
        $author = new Author(41301, 'chanBC');
        $content = 'test content';
        $html = '';
        $parentId = 2;

        $comment = new Comment(
            $postId,
            $author,
            $content,
            $html,
            $parentId
        );

        $userId = 41301;

        $post = new Post();

        $commentPosted = new CommentPosted($comment, $post);

        $this->mockSqlCommentNotifier->expects($this->once())
                                    ->method('notifyReply');

        $this->notifyWhenCommentPosted->handle($commentPosted);
    }

    public function testNotificationTitleForMatchComment()
    {
        $discussionId = 'match:1';
        $author = new Author(1, 'John');
        $content = 'test content';
        $html = 'test content';
        $parent = 1;

        $comment = new Comment($discussionId, $author, $content, $html, $parent);

        $commentable = app(MatchRepository::class)->findById(1957);

        $commentPosted = new CommentPosted($comment, $commentable);

        $this->mockSqlCommentNotifier
            ->expects($this->once())
            ->method('notifyReply')
            ->with(
                $this->anything(),
                $this->anything(),
                $this->anything(),
                $this->callback(function ($payloadJson) {
                    $payload = json_decode($payloadJson);

                    return $payload->title === 'Manchester City vs Everton';
                })
            );

        $this->notifyWhenCommentPosted->handle($commentPosted);
    }
}