1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stp\SndApi\Common\Test; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleHttpClient; |
6
|
|
|
use GuzzleHttp\Message\Response; |
7
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
8
|
|
|
use Stp\SndApi\Common\Client; |
9
|
|
|
|
10
|
|
|
class ClientTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
use InvokeInaccessibleMethodTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @return Client|PHPUnit_Framework_MockObject_MockObject |
16
|
|
|
*/ |
17
|
|
|
private function getStub() |
18
|
|
|
{ |
19
|
|
|
return $this->getMockForAbstractClass(Client::class, ['apikey', 'apisecret', 'sa']); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function jsonAcceptationProvider() |
23
|
|
|
{ |
24
|
|
|
return [ |
25
|
|
|
[true], |
26
|
|
|
[false] |
27
|
|
|
]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testApiSecret() |
31
|
|
|
{ |
32
|
|
|
$stub = $this->getStub(); |
33
|
|
|
|
34
|
|
|
$stub->setApiSecret('test'); |
|
|
|
|
35
|
|
|
$this->assertEquals('test', $stub->getApiSecret()); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testApiKey() |
39
|
|
|
{ |
40
|
|
|
$stub = $this->getStub(); |
41
|
|
|
|
42
|
|
|
$stub->setApiKey('test'); |
|
|
|
|
43
|
|
|
$this->assertEquals('test', $stub->getApiKey()); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testValidPublicationId() |
47
|
|
|
{ |
48
|
|
|
$stub = $this->getStub(); |
49
|
|
|
|
50
|
|
|
$stub->setPublicationId('sa'); |
|
|
|
|
51
|
|
|
$this->assertEquals('sa', $stub->getPublicationId()); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @expectedException \Stp\SndApi\Common\Exception\InvalidPublicationIdException |
56
|
|
|
*/ |
57
|
|
|
public function testInvalidPublicationId() |
58
|
|
|
{ |
59
|
|
|
$stub = $this->getStub(); |
60
|
|
|
|
61
|
|
|
$stub->setPublicationId('invalidpublicationid'); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
public function testSignRequest() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$stub = $this->getStub(); |
67
|
|
|
|
68
|
|
|
$client = new GuzzleHttpClient(); |
69
|
|
|
$request = $client->createRequest('GET', 'http://www.example.org/'); |
70
|
|
|
|
71
|
|
|
$this->invokeMethod($stub, 'signRequest', [$request]); |
72
|
|
|
|
73
|
|
|
$this->assertNotNull($request->getHeader('X-Snd-ApiSignature')); |
74
|
|
|
$this->assertNotNull($request->getHeader('X-Snd-ApiKey')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function testSignRequestWithEmptyApiKey() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$stub = $this->getStub(); |
80
|
|
|
$stub->setApiKey(''); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$client = new GuzzleHttpClient(); |
83
|
|
|
$request = $client->createRequest('GET', 'http://www.example.org/'); |
84
|
|
|
|
85
|
|
|
$this->invokeMethod($stub, 'signRequest', [$request]); |
86
|
|
|
|
87
|
|
|
$this->assertEmpty($request->getHeader('X-Snd-ApiKey')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @dataProvider jsonAcceptationProvider |
92
|
|
|
* @param bool $isJsonAccepted |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function testBuildRequest($isJsonAccepted) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$stub = $this->getStub(); |
97
|
|
|
|
98
|
|
|
$client = new GuzzleHttpClient(); |
99
|
|
|
$request = $client->createRequest('GET', 'http://www.example.org/'); |
100
|
|
|
|
101
|
|
|
$this->invokeMethod($stub, 'buildRequest', [$request, $isJsonAccepted]); |
102
|
|
|
|
103
|
|
|
if ($isJsonAccepted) { |
104
|
|
|
$this->assertNotNull($request->getHeader('Accept')); |
105
|
|
|
} else { |
106
|
|
|
$this->assertEmpty($request->getHeader('Accept')); |
107
|
|
|
} |
108
|
|
|
$this->assertNotNull($request->getHeader('Accept-Charset')); |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
public function testApiGet() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$validResponse = new Response(200); |
115
|
|
|
|
116
|
|
|
$client = $this->getMock(GuzzleHttpClient::class, ['send']); |
117
|
|
|
$client->expects($this->once()) |
118
|
|
|
->method('send') |
119
|
|
|
->willReturn($validResponse); |
120
|
|
|
|
121
|
|
|
$stub = $this->getStub(); |
122
|
|
|
$stub->setClient($client); |
|
|
|
|
123
|
|
|
|
124
|
|
|
$response = $this->invokeMethod($stub, 'apiGet', ['']); |
125
|
|
|
$this->assertEquals($validResponse, $response); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: