Passed
Push — main ( 01449a...f97206 )
by Evgenii
02:10
created

MindBoxClientTest::testRequestSendsSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
c 0
b 0
f 0
dl 0
loc 47
rs 9.456
cc 1
nc 1
nop 0
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\Requests\CustomRequest;
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 CustomRequest($operationName, $body, MindBoxClient::MODE_ASYNCHRONOUS, $deviceUUID);
61
62
        $mindBoxClient = new MindBoxClient(
63
            $mindBoxSecretKey,
64
            $mindBoxEndpoint,
65
            $httpClient
66
        );
67
68
        $mindBoxClient->sendData($request);
69
70
71
        // Checking results
72
        /** @var Request $request */
73
        $request = $container[0]['request'];
74
        $bodyContent = (string)$request->getBody()->getContents();
75
76
        $this->assertEquals($expectedUri, (string)$request->getUri());
77
        $this->assertEquals($body, json_decode($bodyContent, true));
78
        $this->assertEquals('application/json; charset=utf-8', $request->getHeader('Content-type')[0]);
79
    }
80
81
82
}
83