Completed
Push — 3.x-dev-kit ( 0a4698 )
by
unknown
13:04
created

RuntimeBackendTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 61
rs 10

4 Methods

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