1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Kickbox Bundle. |
5
|
|
|
* |
6
|
|
|
* (c) Abdoul Ndiaye <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Andi\KickBoxBundle\Tests\Http; |
13
|
|
|
|
14
|
|
|
use Andi\KickBoxBundle\Exception\EmptyContentException; |
15
|
|
|
use Andi\KickBoxBundle\Exception\KickBoxApiException; |
16
|
|
|
use Andi\KickBoxBundle\Factory\ResponseFactory; |
17
|
|
|
use Andi\KickBoxBundle\Http\Client; |
18
|
|
|
use Andi\KickBoxBundle\Http\Response; |
19
|
|
|
use Andi\KickBoxBundle\Tests\Factory\ResponseFactoryTest; |
20
|
|
|
use GuzzleHttp\Psr7\Response as HttpResponse; |
21
|
|
|
use GuzzleHttp\Psr7\Request; |
22
|
|
|
use GuzzleHttp\Psr7\Stream; |
23
|
|
|
use Phake; |
24
|
|
|
use GuzzleHttp\Client as HttpClient; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class ClientTest |
28
|
|
|
* |
29
|
|
|
* @author Abdoul Ndiaye <[email protected]> |
30
|
|
|
* |
31
|
|
|
* @see Andi\KickBoxBundle\Http\Client |
32
|
|
|
*/ |
33
|
|
|
class ClientTest extends \PHPUnit_Framework_TestCase |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var HttpClient|\Phake_IMock |
37
|
|
|
*/ |
38
|
|
|
protected $httpClient; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ResponseFactory |
42
|
|
|
*/ |
43
|
|
|
protected $responseFactory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @{@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
protected function setUp() |
49
|
|
|
{ |
50
|
|
|
$this->httpClient = Phake::mock(HttpClient::class); |
51
|
|
|
$this->responseFactory = new ResponseFactory(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testVerify() |
55
|
|
|
{ |
56
|
|
|
Phake::when($this->httpClient)->get( |
|
|
|
|
57
|
|
|
'my_end_point', |
58
|
|
|
[ |
59
|
|
|
'query' => [ |
60
|
|
|
'email' => '[email protected]', |
61
|
|
|
'apikey' => 'my_key', |
62
|
|
|
'timeout' => 7000, |
63
|
|
|
] |
64
|
|
|
] |
65
|
|
|
)->thenReturn($this->getResponse()); |
66
|
|
|
|
67
|
|
|
$client = new Client($this->httpClient, $this->responseFactory, 'my_end_point', 'my_key'); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$this->assertInstanceOf(Response::class, $client->verify('[email protected]', 7000)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testKickboxApiException() |
73
|
|
|
{ |
74
|
|
|
Phake::when($this->httpClient)->get(Phake::anyParameters())->thenThrow( |
|
|
|
|
75
|
|
|
new KickBoxApiException('expectedMessage', new Request('GET', 'error'), $this->getErrorResponse()) |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$this->setExpectedException( |
79
|
|
|
KickBoxApiException::class, |
80
|
|
|
'An error occurred during the api call : exception message' |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$client = new Client($this->httpClient, $this->responseFactory, '', ''); |
|
|
|
|
84
|
|
|
$client->verify(''); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testEmptyContentException() |
88
|
|
|
{ |
89
|
|
|
Phake::when($this->httpClient)->get(Phake::anyParameters())->thenReturn(new HttpResponse(400)); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$this->setExpectedException( |
92
|
|
|
EmptyContentException::class |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$client = new Client($this->httpClient, $this->responseFactory, '', ''); |
|
|
|
|
96
|
|
|
$client->verify(''); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return HttpResponse |
101
|
|
|
*/ |
102
|
|
|
protected function getResponse() |
103
|
|
|
{ |
104
|
|
|
return new HttpResponse( |
105
|
|
|
200, |
106
|
|
|
ResponseFactoryTest::$goodHeaders, |
107
|
|
|
json_encode(ResponseFactoryTest::$goodParameters) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return HttpResponse |
113
|
|
|
*/ |
114
|
|
|
protected function getErrorResponse() |
115
|
|
|
{ |
116
|
|
|
return new HttpResponse( |
117
|
|
|
500, |
118
|
|
|
[], |
119
|
|
|
json_encode(['message' => 'exception message']) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.