NotificationTestCase::getToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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