|
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) { |
|
|
|
|
|
|
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
|
|
|
|