Completed
Push — master ( 705569...50fbec )
by dan
02:16
created

NotificationTestCase::getService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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 getMockFormatter($withMessage = false)
50
    {
51
        $formatter = $this->getMockBuilder(MessageFormatterInterface::class)
52
                          ->disableOriginalConstructor()
53
                          ->getMock();
54
55
        if ($withMessage) {
56
            $formatter->expects($this->once())
57
                      ->method('format')
58
                      ->will($this->returnValue($this->getTestMessage()));
59
        }
60
61
        return $formatter;
62
    }
63
64
    protected function getMockDispatcher($withDispatch = false, $returnValue = true)
65
    {
66
        $dispatcher = $this->getMockBuilder(MessageDispatcherInterface::class)
67
                           ->disableOriginalConstructor()
68
                           ->getMock();
69
70
        if ($withDispatch) {
71
            $dispatcher->expects($this->once())
72
                       ->method('dispatch')
73
                       ->will($this->returnValue($returnValue));
74
        }
75
76
        return $dispatcher;
77
    }
78
79
    protected function getTestMessage()
80
    {
81
        $message = new Message();
82
        $message->setDispatchData(['mail' => '[email protected]']);
83
        $message->setMessageData(
84
            [
85
                'title' => 'Hi!',
86
                'body'  => 'Hi Jim, this is a notifiation',
87
            ]
88
        );
89
90
        return $message;
91
    }
92
93
    protected function getService($serviceName)
94
    {
95
        if (empty($this->testKernel)) {
96
            $this->bootSymfony();
97
        }
98
99
        $container = $this->testKernel->getContainer();
100
101
        return $container->get($serviceName);
102
    }
103
104
    protected function getParameters($key = '')
105
    {
106
        if (empty($this->parameters)) {
107
            $path             = __DIR__ . '/config_test.yml';
108
            $this->parameters = Yaml::parse(file_get_contents($path));
109
        }
110
111
        if (empty($key)) {
112
            return $this->parameters;
113
        }
114
115
        return empty($this->parameters[$key]) ? [] : $this->parameters[$key];
116
    }
117
}