|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace floor12\MindBox\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use floor12\MindBox\Exceptions\EmptyApiEndPointException; |
|
6
|
|
|
use floor12\MindBox\Exceptions\EmptyApiKeyException; |
|
7
|
|
|
use floor12\MindBox\MindBoxClient; |
|
8
|
|
|
use floor12\MindBox\MindBoxRequest; |
|
9
|
|
|
use GuzzleHttp\Client; |
|
10
|
|
|
use GuzzleHttp\Handler\MockHandler; |
|
11
|
|
|
use GuzzleHttp\HandlerStack; |
|
12
|
|
|
use GuzzleHttp\Middleware; |
|
13
|
|
|
use GuzzleHttp\Psr7\Request; |
|
14
|
|
|
use GuzzleHttp\Psr7\Response; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
|
|
17
|
|
|
class MindBoxClientTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
public function testNoApiKey() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->expectException(EmptyApiKeyException::class); |
|
23
|
|
|
new MindBoxClient('', 'test'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testNoEndpointKey() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->expectException(EmptyApiEndPointException::class); |
|
29
|
|
|
new MindBoxClient('key', ''); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testRequestSendsSuccess() |
|
33
|
|
|
{ |
|
34
|
|
|
// Prepare test data |
|
35
|
|
|
$mindBoxSecretKey = 'test-secret-key'; |
|
36
|
|
|
$mindBoxEndpoint = 'test.endpoint'; |
|
37
|
|
|
$operationName = 'test.operation'; |
|
38
|
|
|
$deviceUUID = 'test-device-uuid'; |
|
39
|
|
|
$body = ['test' => 'body']; |
|
40
|
|
|
|
|
41
|
|
|
$expectedUri = MindBoxClient::ASYNC_MINDBOX_API_URL . '?' . |
|
42
|
|
|
http_build_query([ |
|
43
|
|
|
'endpointId' => $mindBoxEndpoint, |
|
44
|
|
|
'operation' => $operationName, |
|
45
|
|
|
'deviceUUID' => $deviceUUID, |
|
46
|
|
|
]); |
|
47
|
|
|
|
|
48
|
|
|
// Setting up Guzzle Client Mock |
|
49
|
|
|
$container = []; |
|
50
|
|
|
$history = Middleware::history($container); |
|
51
|
|
|
$mock = new MockHandler([new Response(200, ['Content-Length' => 0]),]); |
|
52
|
|
|
$handlerStack = HandlerStack::create($mock); |
|
53
|
|
|
$handlerStack->push($history); |
|
54
|
|
|
$httpClient = new Client([ |
|
55
|
|
|
'handler' => $handlerStack, |
|
56
|
|
|
'base_uri' => MindBoxClient::ASYNC_MINDBOX_API_URL |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
|
|
// Create and send MindBox request |
|
60
|
|
|
$request = new MindBoxRequest(); |
|
61
|
|
|
$request->setOperationName($operationName); |
|
62
|
|
|
$request->setBody($body); |
|
63
|
|
|
$request->setMode(MindBoxClient::MODE_ASYNCHRONOUS); |
|
64
|
|
|
$request->setDeviceUUID($deviceUUID); |
|
65
|
|
|
|
|
66
|
|
|
$mindBoxClient = new MindBoxClient( |
|
67
|
|
|
$mindBoxSecretKey, |
|
68
|
|
|
$mindBoxEndpoint, |
|
69
|
|
|
$httpClient |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
$mindBoxClient->sendData($request); |
|
73
|
|
|
|
|
74
|
|
|
// Checking results |
|
75
|
|
|
/** @var Request $request */ |
|
76
|
|
|
$request = $container[0]['request']; |
|
77
|
|
|
$bodyContent = (string)$request->getBody()->getContents(); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertEquals($expectedUri, (string)$request->getUri()); |
|
80
|
|
|
$this->assertEquals($body, json_decode($bodyContent, true)); |
|
81
|
|
|
$this->assertEquals('application/json; charset=utf-8', $request->getHeader('Content-type')[0]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|