1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenExRt\Tests; |
4
|
|
|
|
5
|
|
|
use LogicException; |
6
|
|
|
use OpenExRt\Client; |
7
|
|
|
|
8
|
|
|
class ClientTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $appId; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
$this->appId = getenv('APP_ID'); |
18
|
|
|
if (empty($this->appId)) { |
19
|
|
|
throw new LogicException('Undefined APP_ID environment variable'); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testConstructorOptions() |
24
|
|
|
{ |
25
|
|
|
$options = array(Client::OPTION_APP_ID => 'Test'); |
26
|
|
|
$client = new Client($options); |
27
|
|
|
|
28
|
|
|
$this->assertEquals('Test', $client->getAppId()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testSetAndGetAppId() |
32
|
|
|
{ |
33
|
|
|
$testAppId = 'test'; |
34
|
|
|
|
35
|
|
|
$client = new Client(); |
36
|
|
|
$client->setAppId($testAppId); |
37
|
|
|
|
38
|
|
|
$appId = $client->getAppId(); |
39
|
|
|
|
40
|
|
|
$this->assertEquals($testAppId, $appId); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testGetCurrencies() |
44
|
|
|
{ |
45
|
|
|
$client = new Client(); |
46
|
|
|
|
47
|
|
|
$apiResponse = $client->getCurrencies(); |
48
|
|
|
$this->assertInstanceOf('stdClass', $apiResponse); |
49
|
|
|
|
50
|
|
|
// Loop the response object to check it's integrity |
51
|
|
|
foreach($apiResponse as $currencyCode => $currencyName) { |
52
|
|
|
|
53
|
|
|
// Assert the currency code and values are none-empty strings |
54
|
|
|
$this->assertNotEmpty($currencyCode); |
55
|
|
|
$this->assertInternalType('string', $currencyCode); |
56
|
|
|
$this->assertNotEmpty($currencyName); |
57
|
|
|
$this->assertInternalType('string', $currencyName); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testGetLatestWithInvalidAppId() |
62
|
|
|
{ |
63
|
|
|
$client = new Client(); |
64
|
|
|
$client->setAppId('test'); |
65
|
|
|
|
66
|
|
|
$apiResponse = $client->getLatest(); |
67
|
|
|
|
68
|
|
|
$this->assertInvalidApiResponseFormat($apiResponse); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGetLatestWithValidAppId() |
72
|
|
|
{ |
73
|
|
|
$client = new Client(); |
74
|
|
|
$client->setAppId($this->appId); |
75
|
|
|
|
76
|
|
|
$apiResponse = $client->getLatest(); |
77
|
|
|
|
78
|
|
|
$this->assertValidApiRatesResponseFormat($apiResponse); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
public function testGetHistoricalWithInvalidAppId() |
82
|
|
|
{ |
83
|
|
|
$histDate = new \DateTime(); |
84
|
|
|
$histDate->modify('-7 days'); |
85
|
|
|
|
86
|
|
|
$client = new Client(); |
87
|
|
|
$client->setAppId('test'); |
88
|
|
|
|
89
|
|
|
$apiResponse = $client->getHistorical($histDate); |
90
|
|
|
|
91
|
|
|
$this->assertInvalidApiResponseFormat($apiResponse); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public function testGetHistoricalWithValidAppId() |
95
|
|
|
{ |
96
|
|
|
$histDate = new \DateTime(); |
97
|
|
|
$histDate->modify('-7 days'); |
98
|
|
|
|
99
|
|
|
$client = new Client(); |
100
|
|
|
$client->setAppId($this->appId); |
101
|
|
|
|
102
|
|
|
$apiResponse = $client->getHistorical($histDate); |
103
|
|
|
|
104
|
|
|
$this->assertValidApiRatesResponseFormat($apiResponse); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testGetHistoricalWithInvalidDate() |
108
|
|
|
{ |
109
|
|
|
$histDate = new \DateTime(); |
110
|
|
|
$histDate->modify('+7 days'); |
111
|
|
|
|
112
|
|
|
$client = new Client(); |
113
|
|
|
$client->setAppId($this->appId); |
114
|
|
|
|
115
|
|
|
$apiResponse = $client->getHistorical($histDate); |
116
|
|
|
|
117
|
|
|
$this->assertInstanceOf('stdClass', $apiResponse); |
118
|
|
|
|
119
|
|
|
$this->assertTrue($apiResponse->error); |
120
|
|
|
$this->assertEquals(400, $apiResponse->status); |
121
|
|
|
$this->assertEquals('not_available', $apiResponse->message); |
122
|
|
|
$this->assertEquals( |
123
|
|
|
'Historical rates for the requested date are not available - please try a different date, or contact [email protected].', |
124
|
|
|
$apiResponse->description |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function assertInvalidApiResponseFormat($apiResponse) |
129
|
|
|
{ |
130
|
|
|
$this->assertInstanceOf('stdClass', $apiResponse); |
131
|
|
|
|
132
|
|
|
$this->assertTrue($apiResponse->error); |
133
|
|
|
$this->assertEquals(401, $apiResponse->status); |
134
|
|
|
$this->assertEquals('invalid_app_id', $apiResponse->message); |
135
|
|
|
$this->assertEquals( |
136
|
|
|
'Invalid App ID provided. Please sign up at https://openexchangerates.org/signup, or contact [email protected].', |
137
|
|
|
$apiResponse->description |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private function assertValidApiRatesResponseFormat($apiResponse) |
142
|
|
|
{ |
143
|
|
|
$this->assertInstanceOf('stdClass', $apiResponse); |
144
|
|
|
|
145
|
|
|
$this->assertNotEmpty($apiResponse->disclaimer); |
146
|
|
|
$this->assertInternalType('string', $apiResponse->disclaimer); |
147
|
|
|
$this->assertNotEmpty($apiResponse->license); |
148
|
|
|
$this->assertInternalType('string', $apiResponse->license); |
149
|
|
|
|
150
|
|
|
$this->assertInternalType('int', $apiResponse->timestamp); |
151
|
|
|
|
152
|
|
|
$this->assertNotEmpty($apiResponse->base); |
153
|
|
|
$this->assertInternalType('string', $apiResponse->base); |
154
|
|
|
|
155
|
|
|
$this->assertInstanceOf('stdClass', $apiResponse->rates); |
156
|
|
|
|
157
|
|
|
// Loop the set of provided rates |
158
|
|
|
foreach ($apiResponse->rates as $currencyCode => $currencyRate) { |
159
|
|
|
|
160
|
|
|
// Assert the currency code is none-empt string |
161
|
|
|
$this->assertNotEmpty($currencyCode); |
162
|
|
|
$this->assertInternalType('string', $currencyCode); |
163
|
|
|
|
164
|
|
|
// Ensure the currency rate is numeric |
165
|
|
|
$this->assertTrue(is_numeric($currencyRate)); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|