NotificationControllerTest::testSend()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 37
rs 8.8571
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
/**
3
 * Push notification server example (http://github.com/juliangut/tify_example)
4
 *
5
 * @link https://github.com/juliangut/tify_example for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify_example/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Pusher\Tests\Entity;
11
12
use Jgut\Pusher\Controller\NotificationController;
13
use Jgut\Pusher\Entity\DeviceEntity;
14
use Jgut\Pusher\Repository\DeviceRepository;
15
use Jgut\Tify\Service;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Slim\Http\Response;
18
19
/**
20
 * Class NotificationControllerTest
21
 */
22
class NotificationControllerTest extends \PHPUnit_Framework_TestCase
23
{
24
    public function testNoDevices()
25
    {
26
        $repository = $this->getMockBuilder(DeviceRepository::class)->disableOriginalConstructor()->getMock();
27
        $repository->expects(self::once())->method('findAll')->will(self::returnValue(null));
28
29
        $service = $this->getMockBuilder(Service::class)->disableOriginalConstructor()->getMock();
30
        $service->expects(self::once())->method('push')->will(self::returnValue([]));
31
32
        $controller = new NotificationController($repository, $service);
33
34
        $parameters = [
35
            'title' => 'Title',
36
            'body' => 'Body',
37
            'data' => [
38
                'param1' => 'value1',
39
            ],
40
        ];
41
        $request = $this->getMockBuilder(ServerRequestInterface::class)->disableOriginalConstructor()->getMock();
42
        $request->expects(self::once())->method('getParsedBody')->will(self::returnValue($parameters));
43
44
        /** @var Response $response */
45
        $response = $controller->send($request, new Response());
46
47
        self::assertEquals(
48
            json_encode([]),
49
            (string) $response->getBody()
50
        );
51
    }
52
53
    public function testSend()
54
    {
55
        $devices = [
56
            (new DeviceEntity())->setToken('aaaa')->setPlatform('android'),
57
            (new DeviceEntity())
58
                ->setToken('0000000000000000000000000000000000000000000000000000000000000000')
59
                ->setPlatform('ios'),
60
            new DeviceEntity(),
61
        ];
62
63
        $repository = $this->getMockBuilder(DeviceRepository::class)->disableOriginalConstructor()->getMock();
64
        $repository->expects(self::once())->method('findBy')->will(self::returnValue($devices));
65
66
        $service = $this->getMockBuilder(Service::class)->disableOriginalConstructor()->getMock();
67
        $service->expects(self::once())->method('push')->will(self::returnValue([]));
68
69
        $controller = new NotificationController($repository, $service);
70
71
        $parameters = [
72
            'title' => 'Title',
73
            'body' => 'Body',
74
            'platform' => 'android',
75
            'data' => [
76
                'param1' => 'value1',
77
            ],
78
        ];
79
        $request = $this->getMockBuilder(ServerRequestInterface::class)->disableOriginalConstructor()->getMock();
80
        $request->expects(self::once())->method('getParsedBody')->will(self::returnValue($parameters));
81
82
        /** @var Response $response */
83
        $response = $controller->send($request, new Response());
84
85
        self::assertEquals(
86
            json_encode([]),
87
            (string) $response->getBody()
88
        );
89
    }
90
}
91