GalantomClientTest::testServiceCommands()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
cc 5
eloc 22
nc 9
nop 2
1
<?php
2
3
namespace GalantomApi\Tests;
4
5
use GalantomApi\GalantomClient;
6
use GuzzleHttp\Command\Guzzle\GuzzleClient;
7
use GuzzleHttp\Handler\MockHandler;
8
use GuzzleHttp\HandlerStack;
9
use GuzzleHttp\Psr7\Response;
10
11
/**
12
 * Class GalantomClientTest
13
 * @package GalantomApi\Tests
14
 */
15
class GalantomClientTest extends AbstractTest
16
{
17
    public function testEmptyFactory()
18
    {
19
        $galantom = GalantomClient::factory();
20
21
        static::assertInstanceOf(GuzzleClient::class, $galantom);
22
        static::assertInstanceOf(GalantomClient::class, $galantom);
23
24
        self::assertEquals('https://galantom.ro/api/', $galantom->getDescription()->getBaseUri()->__toString());
25
    }
26
27
    /**
28
     * @dataProvider providerServiceCommands
29
     * @param $method
30
     * @param $params
31
     */
32
    public function testServiceCommands($method, $params)
33
    {
34
        $queue = new MockHandler([
35
            new Response(200, ['Content-Type' => 'application/json'], '{"response": true}')
36
        ]);
37
        $handler = HandlerStack::create($queue);
38
39
        $client = $this->getClient($handler);
40
41
        $command = $client->getCommand($method, $params);
42
        $client->execute($command);
43
44
        $request = $queue->getLastRequest();
45
46
        $params['api_token'] = 999;
47
48
        //Resource Url
49
        $url = parse_url($request->getUri());
50
51
        $operation = $client->getDescription()->getOperation($method);
52
        //Make sure the url has the right method
53
        static::assertContains($operation->getUri(), $url['path']);
54
55
        $parameters = $operation->getParams();
56
        parse_str($url['query'], $uriParams);
57
58
        foreach ($parameters as $parameter) {
59
            $paramName = $parameter->getName();
60
            $paramValue = isset($params[$paramName]) ? $params[$paramName] : null;
61
            $uriValue = isset($uriParams[$paramName]) ? $uriParams[$paramName] : null;
62
            switch ($parameter->getLocation()) {
63
                case 'query':
64
                    static::assertEquals($paramValue, $uriValue);
65
                    break;
66
67
            }
68
        }
69
    }
70
71
    /**
72
     * Data for service calls
73
     */
74
    public function providerServiceCommands()
75
    {
76
        return [
77
            ['getPageDonations', ['id' => 159]],
78
        ];
79
    }
80
81
    /**
82
     * @param null $handler
83
     * @return GalantomClient
84
     */
85
    protected function getClient($handler = null)
0 ignored issues
show
Coding Style introduced by
getClient uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
86
    {
87
        return GalantomClient::factory([
88
            'api_token' => isset($_ENV['api_token']) ? $_ENV['api_token'] : 999,
89
            'handler' => $handler
90
        ]);
91
    }
92
}
93