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

PostponeRuntimeBackendTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 8
c 3
b 2
f 0
lcom 1
cbo 5
dl 0
loc 127
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B testRecursiveMessage() 0 37 1
B testIteratorContainsPublishedMessages() 0 25 2
A testNoMessagesOnEvent() 0 10 1
B testLiveEnvironment() 0 25 1
A testOnCliPublishHandlesDirectly() 0 11 1
A testStatusIsOk() 0 11 2
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\Backend;
13
14
use Sonata\NotificationBundle\Backend\PostponeRuntimeBackend;
15
use Sonata\NotificationBundle\Consumer\ConsumerEventInterface;
16
use Sonata\NotificationBundle\Model\MessageInterface;
17
use Symfony\Component\EventDispatcher\EventDispatcher;
18
19
/**
20
 * @covers Sonata\NotificationBundle\Backend\PostponeRuntimeBackend
21
 */
22
class PostponeRuntimeBackendTest extends \PHPUnit_Framework_TestCase
23
{
24
    public function testIteratorContainsPublishedMessages()
25
    {
26
        $backend = new PostponeRuntimeBackend($this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'), true);
27
28
        $messages = array();
29
30
        $message = $backend->create('foo', array());
31
        $messages[] = $message;
32
        $backend->publish($message);
33
34
        $message = $backend->create('bar', array());
35
        $messages[] = $message;
36
        $backend->publish($message);
37
38
        $message = $backend->create('baz', array());
39
        $messages[] = $message;
40
        $backend->publish($message);
41
42
        $backend->create('not_published', array());
43
44
        $iterator = $backend->getIterator();
45
        foreach ($iterator as $eachKey => $eachMessage) {
46
            $this->assertSame($messages[$eachKey], $eachMessage);
47
        }
48
    }
49
50
    public function testNoMessagesOnEvent()
51
    {
52
        $backend = $this->getMock('Sonata\NotificationBundle\Backend\PostponeRuntimeBackend', array('handle'), array($this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'), true));
53
        $backend
54
            ->expects($this->never())
55
            ->method('handle')
56
        ;
57
58
        $backend->onEvent();
59
    }
60
61
    public function testLiveEnvironment()
62
    {
63
        $dispatcher = new EventDispatcher();
64
        $backend = new PostponeRuntimeBackend($dispatcher, true);
65
        $dispatcher->addListener('kernel.terminate', array($backend, 'onEvent'));
66
67
        $message = $backend->create('notification.demo', array());
68
        $backend->publish($message);
69
70
        // This message will not be handled.
71
        $backend->create('notification.demo', array());
72
73
        $phpunit = $this;
74
        $phpunit->passed = false;
0 ignored issues
show
Bug introduced by
The property passed does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
75
        $dispatcher->addListener('notification.demo', function (ConsumerEventInterface $event) use ($phpunit, $message) {
76
            $phpunit->assertSame($message, $event->getMessage());
77
78
            $phpunit->passed = true;
79
        });
80
81
        $dispatcher->dispatch('kernel.terminate');
82
83
        $this->assertTrue($phpunit->passed);
84
        $this->assertEquals(MessageInterface::STATE_DONE, $message->getState());
85
    }
86
87
    public function testRecursiveMessage()
88
    {
89
        $dispatcher = new EventDispatcher();
90
        $backend = new PostponeRuntimeBackend($dispatcher, true);
91
        $dispatcher->addListener('kernel.terminate', array($backend, 'onEvent'));
92
93
        $message1 = $backend->create('notification.demo1', array());
94
        $message2 = $backend->create('notification.demo2', array());
95
96
        $backend->publish($message1);
97
98
        $phpunit = $this;
99
        $phpunit->passed1 = false;
0 ignored issues
show
Bug introduced by
The property passed1 does not seem to exist. Did you mean passed?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
100
        $phpunit->passed2 = false;
0 ignored issues
show
Bug introduced by
The property passed2 does not seem to exist. Did you mean passed?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
101
102
        $dispatcher->addListener('notification.demo1', function (ConsumerEventInterface $event) use ($phpunit, $message1, $message2, $backend, $dispatcher) {
103
            $phpunit->assertSame($message1, $event->getMessage());
104
105
            $phpunit->passed1 = true;
0 ignored issues
show
Bug introduced by
The property passed1 does not seem to exist. Did you mean passed?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
106
107
            $backend->publish($message2);
108
        });
109
110
        $dispatcher->addListener('notification.demo2', function (ConsumerEventInterface $event) use ($phpunit, $message2) {
111
            $phpunit->assertSame($message2, $event->getMessage());
112
113
            $phpunit->passed2 = true;
0 ignored issues
show
Bug introduced by
The property passed2 does not seem to exist. Did you mean passed?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
114
        });
115
116
        $dispatcher->dispatch('kernel.terminate');
117
118
        $this->assertTrue($phpunit->passed1);
0 ignored issues
show
Bug introduced by
The property passed1 does not seem to exist. Did you mean passed?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
119
        $this->assertTrue($phpunit->passed2);
0 ignored issues
show
Bug introduced by
The property passed2 does not seem to exist. Did you mean passed?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
120
121
        $this->assertEquals(MessageInterface::STATE_DONE, $message1->getState());
122
        $this->assertEquals(MessageInterface::STATE_DONE, $message2->getState());
123
    }
124
125
    public function testStatusIsOk()
126
    {
127
        if (!class_exists('ZendDiagnostics\Result\Success')) {
128
            $this->markTestSkipped('The class ZendDiagnostics\Result\Success does not exist');
129
        }
130
131
        $backend = new PostponeRuntimeBackend($this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'), true);
132
133
        $status = $backend->getStatus();
134
        $this->assertInstanceOf('ZendDiagnostics\Result\Success', $status);
135
    }
136
137
    public function testOnCliPublishHandlesDirectly()
138
    {
139
        $backend = $this->getMock('Sonata\NotificationBundle\Backend\PostponeRuntimeBackend', array('handle'), array($this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'), false));
140
        $backend
141
            ->expects($this->once())
142
            ->method('handle')
143
        ;
144
145
        $message = $backend->create('notification.demo', array());
146
        $backend->publish($message);
147
    }
148
}
149