Passed
Push — main ( 31df87...01449a )
by Evgenii
02:07
created

MindBoxClientTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 30
c 1
b 0
f 0
dl 0
loc 47
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A testRequestSendsSuccess() 0 44 1
1
<?php
2
3
use floor12\MindBox\MindBoxClient;
4
use floor12\MindBox\Requests\CustomRequest;
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Handler\MockHandler;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Middleware;
9
use GuzzleHttp\Psr7\Request;
10
use GuzzleHttp\Psr7\Response;
11
use PHPUnit\Framework\TestCase;
12
13
class MindBoxClientTest extends TestCase
14
{
15
16
    public function testRequestSendsSuccess()
17
    {
18
        // Prepare test data
19
        $mindBoxSecretKey = 'test-secret-key';
20
        $mindBoxEndpoint = 'test.endpoint';
21
        $operationName = 'test.operation';
22
        $deviceUUID = 'test-device-uuid';
23
        $body = ['test' => 'body'];
24
        $expectedUri = MindBoxClient::ASYNC_MINDBOX_API_URL . '?' .
25
            http_build_query([
26
                'endpointId' => $mindBoxEndpoint,
27
                'operation' => $operationName,
28
                'deviceUUID' => $deviceUUID,
29
            ]);
30
31
        // Setting up Guzzle Client Mock
32
        $container = [];
33
        $history = Middleware::history($container);
34
        $mock = new MockHandler([new Response(200, ['Content-Length' => 0]),]);
35
        $handlerStack = HandlerStack::create($mock);
36
        $handlerStack->push($history);
37
        $httpClient = new Client([
38
            'handler' => $handlerStack,
39
            'base_uri' => MindBoxClient::ASYNC_MINDBOX_API_URL
40
        ]);
41
42
        // Create  and send MindBox request
43
        $request = new CustomRequest($operationName, $body, 0, $deviceUUID);
44
        $mindBoxClient = new MindBoxClient(
45
            $mindBoxSecretKey,
46
            $mindBoxEndpoint,
47
            $httpClient
48
        );
49
        $mindBoxClient->sendData($request);
50
51
52
        // Checking results
53
        /** @var Request $request */
54
        $request = $container[0]['request'];
55
        $bodyContent = (string)$request->getBody()->getContents();
56
57
        $this->assertEquals($expectedUri, (string)$request->getUri());
58
        $this->assertEquals($body, json_decode($bodyContent, true));
59
        $this->assertEquals('application/json; charset=utf-8', $request->getHeader('Content-type')[0]);
60
    }
61
62
63
}
64