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
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
|
23
|
|
|
class RuntimeBackendTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
public function testCreateAndPublish(): void |
26
|
|
|
{ |
27
|
|
|
$dispatcher = $this->createMock(EventDispatcherInterface::class); |
28
|
|
|
$backend = new RuntimeBackend($dispatcher); |
29
|
|
|
$message = $backend->createAndPublish('foo', ['message' => 'salut']); |
30
|
|
|
|
31
|
|
|
$this->assertInstanceOf(MessageInterface::class, $message); |
32
|
|
|
|
33
|
|
|
$this->assertSame(MessageInterface::STATE_DONE, $message->getState()); |
34
|
|
|
$this->assertNotNull($message->getCreatedAt()); |
35
|
|
|
$this->assertSame('foo', $message->getType()); |
36
|
|
|
$this->assertSame(['message' => 'salut'], $message->getBody()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testIterator(): void |
40
|
|
|
{ |
41
|
|
|
$dispatcher = $this->createMock(EventDispatcherInterface::class); |
42
|
|
|
$backend = new RuntimeBackend($dispatcher); |
43
|
|
|
|
44
|
|
|
$this->assertInstanceOf('Iterator', $backend->getIterator()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testHandleSuccess(): void |
48
|
|
|
{ |
49
|
|
|
$message = new Message(); |
50
|
|
|
|
51
|
|
|
$dispatcher = $this->createMock(EventDispatcherInterface::class); |
52
|
|
|
$dispatcher->expects($this->once())->method('dispatch'); |
53
|
|
|
|
54
|
|
|
$backend = new RuntimeBackend($dispatcher); |
55
|
|
|
|
56
|
|
|
$backend->handle($message, $dispatcher); |
57
|
|
|
$this->assertSame(MessageInterface::STATE_DONE, $message->getState()); |
58
|
|
|
$this->assertNotNull($message->getCreatedAt()); |
59
|
|
|
$this->assertNotNull($message->getCompletedAt()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testHandleError(): void |
63
|
|
|
{ |
64
|
|
|
$message = new Message(); |
65
|
|
|
|
66
|
|
|
$dispatcher = $this->createMock(EventDispatcherInterface::class); |
67
|
|
|
$dispatcher->expects($this->once())->method('dispatch')->will($this->throwException(new \RuntimeException())); |
68
|
|
|
|
69
|
|
|
$backend = new RuntimeBackend($dispatcher); |
70
|
|
|
|
71
|
|
|
$e = false; |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
$backend->handle($message, $dispatcher); |
75
|
|
|
} catch (HandlingException $e) { |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->assertInstanceOf(HandlingException::class, $e); |
79
|
|
|
|
80
|
|
|
$this->assertSame(MessageInterface::STATE_ERROR, $message->getState()); |
81
|
|
|
$this->assertNotNull($message->getCreatedAt()); |
82
|
|
|
$this->assertNotNull($message->getCompletedAt()); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|