PHPushbulletTestBase::setUp()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 2
eloc 17
nc 2
nop 0
1
<?php
2
3
use GuzzleHttp\Client;
4
use PHPushbullet\Connection;
5
use PHPushbullet\PHPushbullet;
6
7
class PHPushbulletTestBase extends PHPUnit_Framework_TestCase
8
{
9
    protected $pushbullet;
10
11
    protected $client;
12
13
    protected $history;
14
15
    protected $guzzle_6;
16
17
    protected $mock_handler;
18
19
    public function setUp()
20
    {
21
        $this->guzzle_6 = version_compare(Client::VERSION, 6, '>=');
22
23
        if ($this->guzzle_6) {
24
            $this->mock_handler = new \GuzzleHttp\Handler\MockHandler();
25
            $this->history      = [];
26
27
            $history = \GuzzleHttp\Middleware::history($this->history);
28
            $stack   = \GuzzleHttp\HandlerStack::create($this->mock_handler);
29
            $stack->push($history);
30
            $client  = new Client(['handler' => $stack]);
31
        } else {
32
            $client        = new Client();
33
            $this->history = new \GuzzleHttp\Subscriber\History();
34
            $this->mock_handler = new \GuzzleHttp\Subscriber\Mock();
35
36
            $client->getEmitter()->attach($this->history);
37
            $client->getEmitter()->attach($this->mock_handler);
38
        }
39
40
        $connection = new Connection('random', $client);
41
42
        $this->pushbullet = new PHPushbullet('random', $connection);
43
    }
44
45
    protected function okResponse($body)
46
    {
47
        $body = json_encode($body);
48
49
        if ($this->guzzle_6) {
50
            return new \GuzzleHttp\Psr7\Response(200, ['Content-Length' => strlen($body)], $body);
51
        }
52
53
        $response = [
54
            'HTTP/1.1 200 OK',
55
            'Content-Length: ' . strlen($body),
56
            '',
57
            $body,
58
        ];
59
60
        return implode("\r\n", $response);
61
    }
62
63
    protected function mock(array $mocks)
64
    {
65
        foreach ($mocks as $mock) {
66
            if ($this->guzzle_6) {
67
                $this->mock_handler->append($mock);
68
            } else {
69
                $this->mock_handler->addResponse($mock);
0 ignored issues
show
Bug introduced by
The method addResponse() does not seem to exist on object<GuzzleHttp\Handler\MockHandler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
            }
71
        }
72
    }
73
74
    protected function pushResponse($vars)
75
    {
76
        $standard = [
77
            'iden'                      => 'ubdpjxxxOK0sKG',
78
            'created'                   => 1399253701.9746201,
79
            'modified'                  => 1399253701.9744401,
80
            'active'                    => true,
81
            'dismissed'                 => false,
82
            'owner_iden'                => 'ubd',
83
            'target_device_iden'        => 'ubddjAy95rgBxc',
84
            'sender_iden'               => 'ubd',
85
            'sender_email'              => '[email protected]',
86
            'sender_email_normalized'   => '[email protected]',
87
            'receiver_iden'             => 'ubd',
88
            'receiver_email'            => '[email protected]',
89
            'receiver_email_normalized' => '[email protected]'
90
        ];
91
92
        return array_merge($vars, $standard);
93
    }
94
95
    protected function getDevice($type)
96
    {
97
        $devices = [
98
            'android' => [
99
                'iden'         => 'u1qSJddxeKwOGuGW',
100
                'push_token'   => 'u1qSJddxeKwOGuGWu1qdxeKwOGuGWu1qSJddxeK',
101
                'app_version'  => 74,
102
                'fingerprint'  => '{"mac_address":"57:2e:5c:38:39:81","android_id":"ao2cdb9gmf485e96"}',
103
                'active'       => true,
104
                'nickname'     => 'Galaxy S4',
105
                'manufacturer' => 'samsung',
106
                'type'         => 'android',
107
                'created'      => 1394748080.0139201,
108
                'modified'     => 1399008037.8487799,
109
                'model'        => 'SCH-I545',
110
                'pushable'     => true
111
            ],
112
            'chrome' => [
113
                'iden'         => 'u1qSJddxeKwOGuG1',
114
                'push_token'   => 'u1qSJddxeKwOGuGWu1qdxeKwOGuGWu1qSJddxe3',
115
                'app_version'  => 74,
116
                'fingerprint'  => '{"mac_address":"57:2e:5c:38:39:81","android_id":"ao2cdb9gmf485e96"}',
117
                'active'       => true,
118
                'nickname'     => 'Chrome',
119
                'manufacturer' => 'google',
120
                'type'         => 'chrome',
121
                'created'      => 1394748080.0139201,
122
                'modified'     => 1399008037.8487799,
123
                'model'        => 'SCH-I745',
124
                'pushable'     => true
125
            ]
126
        ];
127
128
        return $devices[$type];
129
    }
130
131
    protected function getRuquestUrls()
132
    {
133
        if ($this->guzzle_6) {
134
            return array_map(function ($request) {
135
                return (string) $request['request']->getUri();
136
            }, $this->history);
137
        }
138
139
        return array_map(function ($request) {
140
            return $request->getUrl();
141
        }, $this->history->getRequests());
142
    }
143
144
    protected function assertRequestHistory(array $expected)
145
    {
146
        $this->assertSame($expected, $this->getRuquestUrls());
147
    }
148
149
    /** @test **/
150
151
    public function nothing()
152
    {
153
    }
154
}
155