This repository has been archived by the owner on Jul 28, 2022. It is now read-only.
/
MessageControllerTest.php
102 lines (81 loc) · 3.83 KB
/
MessageControllerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\NotificationBundle\Tests\Controller\Api;
use FOS\RestBundle\Request\ParamFetcherInterface;
use PHPUnit\Framework\TestCase;
use Sonata\NotificationBundle\Controller\Api\MessageController;
use Sonata\NotificationBundle\Model\MessageInterface;
use Sonata\NotificationBundle\Model\MessageManagerInterface;
use Sonata\PageBundle\Model\SiteManagerInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* @author Hugo Briand <briand@ekino.com>
*/
class MessageControllerTest extends TestCase
{
public function testGetMessagesAction(): void
{
$messageManager = $this->createMock(MessageManagerInterface::class);
$messageManager->expects($this->once())->method('getPager')->willReturn([]);
$paramFetcher = $this->createMock(ParamFetcherInterface::class);
$paramFetcher->expects($this->exactly(3))->method('get');
$paramFetcher->expects($this->once())->method('all')->willReturn([]);
$this->assertSame([], $this->createMessageController(null, $messageManager)->getMessagesAction($paramFetcher));
}
public function testPostMessageAction(): void
{
$message = $this->createMock(MessageInterface::class);
$messageManager = $this->createMock(MessageManagerInterface::class);
$messageManager->expects($this->once())->method('save')->willReturn($message);
$form = $this->createMock(Form::class);
$form->expects($this->once())->method('handleRequest');
$form->expects($this->once())->method('isValid')->willReturn(true);
$form->expects($this->once())->method('getData')->willReturn($message);
$formFactory = $this->createMock(FormFactoryInterface::class);
$formFactory->expects($this->once())->method('createNamed')->willReturn($form);
$message = $this->createMessageController(null, $messageManager, $formFactory)->postMessageAction(new Request());
$this->assertInstanceOf(MessageInterface::class, $message);
}
public function testPostMessageInvalidAction(): void
{
$message = $this->createMock(MessageInterface::class);
$messageManager = $this->createMock(MessageManagerInterface::class);
$messageManager->expects($this->never())->method('save')->willReturn($message);
$form = $this->createMock(Form::class);
$form->expects($this->once())->method('handleRequest');
$form->expects($this->once())->method('isValid')->willReturn(false);
$formFactory = $this->createMock(FormFactoryInterface::class);
$formFactory->expects($this->once())->method('createNamed')->willReturn($form);
$form = $this->createMessageController(null, $messageManager, $formFactory)->postMessageAction(new Request());
$this->assertInstanceOf(FormInterface::class, $form);
}
/**
* @param $message
* @param $messageManager
* @param $formFactory
*/
public function createMessageController($message = null, $messageManager = null, $formFactory = null): MessageController
{
if (null === $messageManager) {
$messageManager = $this->createMock(SiteManagerInterface::class);
}
if (null !== $message) {
$messageManager->expects($this->once())->method('findOneBy')->willReturn($message);
}
if (null === $formFactory) {
$formFactory = $this->createMock(FormFactoryInterface::class);
}
return new MessageController($messageManager, $formFactory);
}
}