DeviceControllerTest::testNotSave()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 1
eloc 18
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\DeviceController;
13
use Jgut\Pusher\Entity\DeviceEntity;
14
use Jgut\Pusher\Repository\DeviceRepository;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Ramsey\Uuid\Uuid;
17
use Slim\Http\Response;
18
19
/**
20
 * Class DeviceControllerTest
21
 */
22
class DeviceControllerTest extends \PHPUnit_Framework_TestCase
23
{
24
    public function testSave()
25
    {
26
        $repository = $this->getMockBuilder(DeviceRepository::class)->disableOriginalConstructor()->getMock();
27
        $repository->expects(self::once())->method('findOneBy')->will(self::returnValue(null));
28
        $repository->expects(self::once())->method('saveDevice');
29
30
        $parameters = [
31
            'token' => 'aaaa',
32
            'platform' => 'android',
33
        ];
34
35
        $request = $this->getMockBuilder(ServerRequestInterface::class)->disableOriginalConstructor()->getMock();
36
        $request->expects(self::once())->method('getParsedBody')->will(self::returnValue($parameters));
37
38
        $controller = new DeviceController($repository);
39
40
        /** @var Response $response */
41
        $response = $controller->save($request, new Response());
42
43
        self::assertEquals(
44
            json_encode(['uuid' => null, 'token' => 'aaaa', 'platform' => 'android']),
45
            (string) $response->getBody()
46
        );
47
    }
48
49
    public function testNotSave()
50
    {
51
        $uuid = Uuid::uuid4();
52
        $device = (new DeviceEntity())
53
            ->setUuid($uuid)
0 ignored issues
show
Compatibility introduced by
$uuid of type object<Ramsey\Uuid\UuidInterface> is not a sub-type of object<Ramsey\Uuid\Uuid>. It seems like you assume a concrete implementation of the interface Ramsey\Uuid\UuidInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
54
            ->setToken('bbbb')
55
            ->setPlatform('ios');
56
57
        $repository = $this->getMockBuilder(DeviceRepository::class)->disableOriginalConstructor()->getMock();
58
        $repository->expects(self::once())->method('findOneBy')->will(self::returnValue($device));
59
60
        $parameters = [
61
            'token' => 'bbbb',
62
            'platform' => 'ios',
63
        ];
64
65
        $request = $this->getMockBuilder(ServerRequestInterface::class)->disableOriginalConstructor()->getMock();
66
        $request->expects(self::once())->method('getParsedBody')->will(self::returnValue($parameters));
67
68
        $controller = new DeviceController($repository);
69
70
        /** @var Response $response */
71
        $response = $controller->save($request, new Response());
72
73
        self::assertEquals(
74
            json_encode(['uuid' => (string) $uuid, 'token' => 'bbbb', 'platform' => 'ios']),
75
            (string) $response->getBody()
76
        );
77
    }
78
79
    public function testUpdate()
80
    {
81
        $uuid = Uuid::uuid4();
82
        $device = (new DeviceEntity())
83
            ->setUuid($uuid)
0 ignored issues
show
Compatibility introduced by
$uuid of type object<Ramsey\Uuid\UuidInterface> is not a sub-type of object<Ramsey\Uuid\Uuid>. It seems like you assume a concrete implementation of the interface Ramsey\Uuid\UuidInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
84
            ->setToken('bbbb')
85
            ->setPlatform('ios');
86
87
        $repository = $this->getMockBuilder(DeviceRepository::class)->disableOriginalConstructor()->getMock();
88
        $repository->expects(self::once())->method('findOneBy')->will(self::returnValue($device));
89
        $repository->expects(self::once())->method('saveDevice');
90
91
        $parameters = [
92
            'token' => 'cccc',
93
            'previous_token' => 'bbbb',
94
        ];
95
96
        $request = $this->getMockBuilder(ServerRequestInterface::class)->disableOriginalConstructor()->getMock();
97
        $request->expects(self::once())->method('getParsedBody')->will(self::returnValue($parameters));
98
99
        $controller = new DeviceController($repository);
100
101
        /** @var Response $response */
102
        $response = $controller->update($request, new Response());
103
104
        self::assertEquals(
105
            json_encode(['uuid' => (string) $uuid, 'token' => 'cccc', 'platform' => 'ios']),
106
            (string) $response->getBody()
107
        );
108
    }
109
110
    /**
111
     * @expectedException \RuntimeException
112
     */
113
    public function testNoDeviceUpdate()
114
    {
115
        $repository = $this->getMockBuilder(DeviceRepository::class)->disableOriginalConstructor()->getMock();
116
        $repository->expects(self::once())->method('findOneBy')->will(self::returnValue(null));
117
118
        $parameters = [
119
            'token' => 'cccc',
120
            'previous_token' => 'bbbb',
121
        ];
122
123
        $request = $this->getMockBuilder(ServerRequestInterface::class)->disableOriginalConstructor()->getMock();
124
        $request->expects(self::once())->method('getParsedBody')->will(self::returnValue($parameters));
125
126
        $controller = new DeviceController($repository);
127
128
        $controller->update($request, new Response());
129
    }
130
}
131