Completed
Pull Request — master (#11)
by Tristan
02:02
created

ApiClientTest::testGetBaseUrlWithPlatformId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace LoLApi\Tests;
4
5
use Doctrine\Common\Cache\ArrayCache;
6
use Doctrine\Common\Cache\VoidCache;
7
use GuzzleHttp\Client;
8
use LoLApi\ApiClient;
9
use LoLApi\Result\ApiResult;
10
11
/**
12
 * Class ApiClientTest
13
 *
14
 * @package LoLApi\Tests
15
 */
16
class ApiClientTest extends \PHPUnit_Framework_TestCase
17
{
18
    const REGION = 'euw';
19
    const API_KEY = 'test';
20
21
    /**
22
     * @covers LoLApi\ApiClient::getMatchListApi
23
     * @covers LoLApi\ApiClient::getMatchApi
24
     * @covers LoLApi\ApiClient::getSummonerApi
25
     * @covers LoLApi\ApiClient::getChampionApi
26
     * @covers LoLApi\ApiClient::getFeaturedGamesApi
27
     * @covers LoLApi\ApiClient::getCurrentGameApi
28
     * @covers LoLApi\ApiClient::getStaticDataApi
29
     * @covers LoLApi\ApiClient::getLeagueApi
30
     * @covers LoLApi\ApiClient::getStatusApi
31
     * @covers LoLApi\ApiClient::getChampionMasteryApi
32
     */
33
    public function testApiGetters()
34
    {
35
        $apiClient = new ApiClient(ApiClient::REGION_EUW, 'test');
36
37
        $this->assertInstanceOf('LoLApi\Api\MatchListApi', $apiClient->getMatchListApi());
38
        $this->assertInstanceOf('LoLApi\Api\MatchApi', $apiClient->getMatchApi());
39
        $this->assertInstanceOf('LoLApi\Api\SummonerApi', $apiClient->getSummonerApi());
40
        $this->assertInstanceOf('LoLApi\Api\ChampionApi', $apiClient->getChampionApi());
41
        $this->assertInstanceOf('LoLApi\Api\FeaturedGamesApi', $apiClient->getFeaturedGamesApi());
42
        $this->assertInstanceOf('LoLApi\Api\CurrentGameApi', $apiClient->getCurrentGameApi());
43
        $this->assertInstanceOf('LoLApi\Api\MasteryApi', $apiClient->getMasteriesApi());
44
        $this->assertInstanceOf('LoLApi\Api\RuneApi', $apiClient->getRunesApi());
45
        $this->assertInstanceOf('LoLApi\Api\StaticDataApi', $apiClient->getStaticDataApi());
46
        $this->assertInstanceOf('LoLApi\Api\LeagueApi', $apiClient->getLeagueApi());
47
        $this->assertInstanceOf('LoLApi\Api\StatusApi', $apiClient->getStatusApi());
48
        $this->assertInstanceOf('LoLApi\Api\ChampionMasteryApi', $apiClient->getChampionMasteryApi());
49
    }
50
51
    /**
52
     * @covers LoLApi\ApiClient::getRegion
53
     * @covers LoLApi\ApiClient::getApiKey
54
     * @covers LoLApi\ApiClient::getHttpClient
55
     * @covers LoLApi\ApiClient::getGlobalUrl
56
     */
57
    public function testOtherGetters()
58
    {
59
        $apiClient = new ApiClient(ApiClient::REGION_EUW, 'test');
60
61
        $this->assertEquals(self::REGION, $apiClient->getRegion());
62
        $this->assertEquals(self::API_KEY, $apiClient->getApiKey());
63
        $this->assertInstanceOf('GuzzleHttp\Client', $apiClient->getHttpClient());
64
    }
65
66
    /**
67
     * @covers LoLApi\ApiClient::setCacheProvider
68
     * @covers LoLApi\ApiClient::getCacheProvider
69
     */
70
    public function testCacheProvider()
71
    {
72
        $apiClient = new ApiClient(ApiClient::REGION_EUW, 'test');
73
74
        $this->assertInstanceOf('Doctrine\Common\Cache\VoidCache', $apiClient->getCacheProvider());
75
76
        $apiClient->setCacheProvider(new ArrayCache());
77
78
        $this->assertInstanceOf('Doctrine\Common\Cache\ArrayCache', $apiClient->getCacheProvider());
79
    }
80
81
    /**
82
     * @covers LoLApi\ApiClient::cacheApiResult
83
     */
84
    public function testCacheApiResult()
85
    {
86
        $apiResult  = new ApiResult();
87
        $arrayCache = $this->getMockBuilder('Doctrine\Common\Cache\ArrayCache')->disableOriginalConstructor()->getMock();
88
        $arrayCache->expects($this->once())->method('save')->willReturn($this->equalTo($apiResult));
89
90
        $client = new ApiClient(ApiClient::REGION_EUW, 'test', $arrayCache);
91
92
        $client->cacheApiResult($apiResult);
93
    }
94
95
    /**
96
     * @covers LoLApi\ApiClient::getBaseUrlWithPlatformId
97
     */
98
    public function testGetBaseUrlWithRegion()
99
    {
100
        $apiClient = new ApiClient(ApiClient::REGION_EUW, 'test');
101
        $class     = new \ReflectionClass('LoLApi\ApiClient');
102
        $method    = $class->getMethod('getBaseUrl');
103
        $method->setAccessible(true);
104
105
        $this->assertEquals('https://euw.api.pvp.net', $method->invoke($apiClient, false));
106
    }
107
108
    /**
109
     * @covers LoLApi\ApiClient::getBaseUrlWithPlatformId
110
     */
111
    public function testGetBaseUrlWithPlatformId()
112
    {
113
        $apiClient = new ApiClient(ApiClient::REGION_EUW, 'test', null, null, true);
114
        $class     = new \ReflectionClass('LoLApi\ApiClient');
115
        $method    = $class->getMethod('getBaseUrl');
116
        $method->setAccessible(true);
117
118
        $this->assertEquals('https://euw1.api.riotgames.com', $method->invoke($apiClient, true));
119
    }
120
121
    /**
122
     * @covers LoLApi\ApiClient::__construct
123
     *
124
     * @expectedException \LoLApi\Exception\InvalidRegionException
125
     */
126
    public function testInvalidRegion()
127
    {
128
        new ApiClient('test', 'test');
129
    }
130
131
    /**
132
     * @covers LoLApi\ApiClient::__construct
133
     */
134
    public function testConstructWithClient()
135
    {
136
        $httpClient = new Client();
137
        $apiClient  = new ApiClient(ApiClient::REGION_EUW, 'test', new VoidCache(), $httpClient);
138
139
        $this->assertSame($httpClient, $apiClient->getHttpClient());
140
    }
141
142
    /**
143
     * @covers LoLApi\ApiClient::__construct
144
     */
145
    public function testConstruct()
146
    {
147
        $apiClient = new ApiClient(ApiClient::REGION_EUW, 'test', new VoidCache());
148
149
        $this->assertInstanceOf('GuzzleHttp\Client', $apiClient->getHttpClient());
150
    }
151
}
152