AbstractApiTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace LoLApi\Tests;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Handler\MockHandler;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Psr7\Response;
9
use LoLApi\ApiClient;
10
use LoLApi\Handler\ClientExceptionHandler;
11
use Symfony\Component\Cache\Adapter\AdapterInterface;
12
use Symfony\Component\Cache\Adapter\ArrayAdapter;
13
14
/**
15
 * Class AbstractApiTest
16
 *
17
 * @package LoLApi\Test
18
 */
19
abstract class AbstractApiTest extends \PHPUnit_Framework_TestCase
20
{
21
    const REGION = 'euw';
22
    const API_KEY = 'apiKey';
23
24
    /**
25
     * @var ApiClient
26
     */
27
    protected $apiClient;
28
29
    /**
30
     * Initiate ApiClient
31
     */
32
    public function setUp()
33
    {
34
        $this->apiClient = $this->getApiClient(new ArrayAdapter(), $this->getSuccessfulHttpClient());
35
    }
36
37
    /**
38
     * @param AdapterInterface $cache
39
     * @param Client           $httpClient
40
     *
41
     * @return ApiClient
42
     */
43
    protected function getApiClient(AdapterInterface $cache, Client $httpClient)
44
    {
45
        return new ApiClient(self::REGION, self::API_KEY, $cache, $httpClient);
46
    }
47
48
    /**
49
     * @return Client
50
     */
51
    protected function getSuccessfulHttpClient()
52
    {
53
        $mock = new MockHandler([
54
            new Response(200, [], json_encode(['success'])),
55
        ]);
56
57
        $handler = HandlerStack::create($mock);
58
59
        return new Client(['handler' => $handler, 'base_uri' => 'https://' . self::REGION . '.api.pvp.net']);
60
    }
61
62
    /**
63
     * @return Client
64
     */
65
    protected function getRateLimitHttpClient()
66
    {
67
        $mock = new MockHandler([
68
            new Response(
69
                429,
70
                [
71
                    ClientExceptionHandler::HEADER_RATE_LIMIT_TYPE => ClientExceptionHandler::RATE_LIMIT_TYPE_SERVICE,
72
                    ClientExceptionHandler::HEADER_RETRY_AFTER     => 50
73
                ],
74
                json_encode(['failure']
75
                )
76
            ),
77
        ]);
78
79
        $handler = HandlerStack::create($mock);
80
81
        return new Client(['handler' => $handler, 'base_uri' => 'https://' . self::REGION . '.api.pvp.net']);
82
    }
83
}
84