<?php

namespace FD\PrivateMessageBundle\Tests\EventSubscriber;

use Doctrine\ORM\EntityManager;
use FD\PrivateMessageBundle\Entity\Conversation;
use FD\PrivateMessageBundle\Event\ConversationEvent;
use FD\PrivateMessageBundle\EventSubscriber\LeaveConversationSubscriber;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * Class LeaveConversationSubscriberTest.
 */
class LeaveConversationSubscriberTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Test onConversationLeft with a conversation having no more recipients.
     */
    public function testOnConversationLeftRemove()
    {
        /** @var Request $request */
        $request = $this->getMock(Request::class);
        /** @var UserInterface $user */
        $user    = $this->getMock(UserInterface::class);
        $em = $this->getMockBuilder(EntityManager::class)
            ->disableOriginalConstructor()
            ->getMock();

        $em->expects($this->once())->method('remove');
        $em->expects($this->once())->method('flush');

        $service      = new LeaveConversationSubscriber($em);
        $conversation = new Conversation();
        $event        = new ConversationEvent($conversation, $request, $user);

        $service->onConversationLeft($event);
    }

    /**
     * Test onConversationLeft with a conversation still having recipients.
     */
    public function testOnConversationLeft()
    {
        /** @var Request $request */
        $request = $this->getMock(Request::class);
        /** @var UserInterface $user */
        $user    = $this->getMock(UserInterface::class);
        $em = $this->getMockBuilder(EntityManager::class)
            ->disableOriginalConstructor()
            ->getMock();

        $em->expects($this->never())->method('remove');
        $em->expects($this->never())->method('flush');

        $conversation = new Conversation();
        $conversation->addRecipient($user);

        $service      = new LeaveConversationSubscriber($em);
        $event        = new ConversationEvent($conversation, $request, $user);

        $service->onConversationLeft($event);
    }
}

Add unit test for LeaveConversationSubscriber
Add unit test for LeaveConversationSubscriber
