Completed
Push — master ( e6067c...97d58e )
by
unknown
41:27
created

tests/Backend/RuntimeBackendTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NotificationBundle\Tests\Notification;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\NotificationBundle\Backend\RuntimeBackend;
18
use Sonata\NotificationBundle\Exception\HandlingException;
19
use Sonata\NotificationBundle\Model\MessageInterface;
20
use Sonata\NotificationBundle\Tests\Entity\Message;
21
22
class RuntimeBackendTest extends TestCase
23
{
24
    public function testCreateAndPublish(): void
25
    {
26
        $dispatcher = $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
27
        $backend = new RuntimeBackend($dispatcher);
28
        $message = $backend->createAndPublish('foo', ['message' => 'salut']);
29
30
        $this->assertInstanceOf('Sonata\\NotificationBundle\\Model\\MessageInterface', $message);
31
32
        $this->assertEquals(MessageInterface::STATE_DONE, $message->getState());
33
        $this->assertNotNull($message->getCreatedAt());
34
        $this->assertEquals('foo', $message->getType());
35
        $this->assertEquals(['message' => 'salut'], $message->getBody());
36
    }
37
38
    public function testIterator(): void
39
    {
40
        $dispatcher = $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
41
        $backend = new RuntimeBackend($dispatcher);
42
43
        $this->assertInstanceOf('Iterator', $backend->getIterator());
44
    }
45
46
    public function testHandleSuccess(): void
47
    {
48
        $message = new Message();
49
50
        $dispatcher = $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
51
        $dispatcher->expects($this->once())->method('dispatch');
52
53
        $backend = new RuntimeBackend($dispatcher);
54
55
        $backend->handle($message, $dispatcher);
56
        $this->assertEquals(MessageInterface::STATE_DONE, $message->getState());
57
        $this->assertNotNull($message->getCreatedAt());
58
        $this->assertNotNull($message->getCompletedAt());
59
    }
60
61
    public function testHandleError(): void
62
    {
63
        $message = new Message();
64
65
        $dispatcher = $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
66
        $dispatcher->expects($this->once())->method('dispatch')->will($this->throwException(new \RuntimeException()));
67
68
        $backend = new RuntimeBackend($dispatcher);
69
70
        $e = false;
71
72
        try {
73
            $backend->handle($message, $dispatcher);
74
        } catch (HandlingException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
75
        }
76
77
        $this->assertInstanceOf('Sonata\NotificationBundle\Exception\HandlingException', $e);
78
79
        $this->assertEquals(MessageInterface::STATE_ERROR, $message->getState());
80
        $this->assertNotNull($message->getCreatedAt());
81
        $this->assertNotNull($message->getCompletedAt());
82
    }
83
}
84