Completed
Push — master ( cd8958...94944b )
by dan
02:07
created

NotificationTestCase::getTestMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace IrishDan\NotificationBundle\Test;
4
5
use IrishDan\NotificationBundle\Test\Notification\TestNotification;
6
use IrishDan\NotificationBundle\Dispatcher\MessageDispatcherInterface;
7
use IrishDan\NotificationBundle\Formatter\MessageFormatterInterface;
8
use IrishDan\NotificationBundle\Message\Message;
9
use IrishDan\NotificationBundle\Test\Entity\User;
10
use Symfony\Component\Yaml\Yaml;
11
12
class NotificationTestCase extends \PHPUnit_Framework_TestCase
13
{
14
    protected $testKernel;
15
    protected $parameters = [];
16
17
    protected function bootSymfony()
18
    {
19
        require_once __DIR__ . '/AppKernel.php';
20
21
        $this->testKernel = new \AppKernel('test', true);
22
        $this->testKernel->boot();
23
    }
24
25
    protected function getTestUser()
26
    {
27
        $user = new User();
28
29
        return $user;
30
    }
31
32
    protected function getTestNotification()
33
    {
34
        $notification = new TestNotification();
35
36
        return $notification;
37
    }
38
39
    protected function getNotificationWithUser()
40
    {
41
        $user         = $this->getTestUser();
42
        $notification = $this->getTestNotification();
43
44
        $notification->setNotifiable($user);
45
46
        return $notification;
47
    }
48
49
    protected function getContainer()
50
    {
51
        if (empty($this->testKernel)) {
52
            $this->bootSymfony();
53
        }
54
55
        return $this->testKernel->getContainer();
56
    }
57
58
    protected function getMockFormatter($withMessage = false)
59
    {
60
        $formatter = $this->getMockBuilder(MessageFormatterInterface::class)
61
                          ->disableOriginalConstructor()
62
                          ->getMock();
63
64
        if ($withMessage) {
65
            $formatter->expects($this->once())
66
                      ->method('format')
67
                      ->will($this->returnValue($this->getTestMessage()));
68
        }
69
70
        return $formatter;
71
    }
72
73
    protected function getMockDispatcher($withDispatch = false, $returnValue = true)
74
    {
75
        $dispatcher = $this->getMockBuilder(MessageDispatcherInterface::class)
76
                           ->disableOriginalConstructor()
77
                           ->getMock();
78
79
        if ($withDispatch) {
80
            $dispatcher->expects($this->once())
81
                       ->method('dispatch')
82
                       ->will($this->returnValue($returnValue));
83
        }
84
85
        return $dispatcher;
86
    }
87
88
    protected function getTestMessage()
89
    {
90
        $message = new Message();
91
        $message->setDispatchData(['mail' => '[email protected]']);
92
        $message->setMessageData(
93
            [
94
                'title' => 'Hi!',
95
                'body'  => 'Hi Jim, this is a notification',
96
            ]
97
        );
98
99
        return $message;
100
    }
101
102
    protected function getService($serviceName)
103
    {
104
        $container = $this->getContainer();
105
106
        return $container->get($serviceName);
107
    }
108
109
    protected function getParametersFromContainer($parameter)
110
    {
111
        $container = $this->getContainer();
112
113
        return $container->getParameter($parameter);
114
    }
115
116
    protected function getParameters($key = '')
117
    {
118
        if (empty($this->parameters)) {
119
            $path             = __DIR__ . '/config_test.yml';
120
            $this->parameters = Yaml::parse(file_get_contents($path));
121
        }
122
123
        if (empty($key)) {
124
            return $this->parameters;
125
        }
126
127
        return empty($this->parameters[$key]) ? [] : $this->parameters[$key];
128
    }
129
}