-
Notifications
You must be signed in to change notification settings - Fork 54
/
ApiClientIntegrationTest.php
329 lines (275 loc) Β· 11.4 KB
/
ApiClientIntegrationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
declare(strict_types=1);
namespace Scheb\YahooFinanceApi\Tests;
use GuzzleHttp\Exception\TransferException;
use PHPUnit\Framework\TestCase;
use Scheb\YahooFinanceApi\ApiClient;
use Scheb\YahooFinanceApi\ApiClientFactory;
use Scheb\YahooFinanceApi\Results\DividendData;
use Scheb\YahooFinanceApi\Results\HistoricalData;
use Scheb\YahooFinanceApi\Results\Option;
use Scheb\YahooFinanceApi\Results\Quote;
use Scheb\YahooFinanceApi\Results\SearchResult;
use Scheb\YahooFinanceApi\Results\SplitData;
class ApiClientIntegrationTest extends TestCase
{
private const APPLE_NAME = 'Apple';
private const APPLE_SYMBOL = 'AAPL';
private const APPLE_SYMBOL_FRANKFURT = 'APC.F';
private const GOOGLE_SYMBOL = 'GOOG';
private const CURRENCY_USD = 'USD';
private const CURRENCY_EUR = 'EUR';
private const TRY_COUNT = 3;
private const RETRY_SLEEP_SECONDS = 1;
private const REGION_US = 'en_US';
private const REGION_GERMANY = 'de_DE';
/**
* @var ApiClient
*/
private $client;
public function setUp(): void
{
$this->client = ApiClientFactory::createApiClient();
}
/**
* @test
*/
public function search_withSearchTerm_returnSearchResults(): void
{
$returnValue = $this->client->search(self::APPLE_NAME);
$this->assertIsArray($returnValue);
$this->assertContainsOnlyInstancesOf(SearchResult::class, $returnValue);
$aaplStock = $this->findApple($returnValue);
$this->assertNotNull($aaplStock, 'Search result must contain AAPL');
$this->assertEquals('Apple Inc.', $aaplStock->getName());
$this->assertEquals('S', $aaplStock->getType());
$this->assertEquals('NASDAQ', $aaplStock->getExchDisp());
$this->assertEquals('Equity', $aaplStock->getTypeDisp());
// Can be either NAS or NMS
$this->assertThat(
$aaplStock->getExch(),
$this->logicalOr(
$this->equalTo('NAS'),
$this->equalTo('NMS')
)
);
}
/**
* @test
*/
public function search_withSearchTermAndRegion_returnSearchResults(): void
{
$returnValue = $this->client->search(self::APPLE_NAME, self::REGION_GERMANY);
$this->assertIsArray($returnValue);
$this->assertContainsOnlyInstancesOf(SearchResult::class, $returnValue);
$aaplStock = $this->findApple($returnValue, self::APPLE_SYMBOL_FRANKFURT);
$this->assertNotNull($aaplStock, 'Search result must contain APC.F');
$this->assertEquals('Apple Inc.', $aaplStock->getName());
$this->assertEquals('S', $aaplStock->getType());
$this->assertEquals('Frankfurt', $aaplStock->getExchDisp());
$this->assertEquals('Aktie', $aaplStock->getTypeDisp());
// Can be either NAS or NMS
$this->assertThat(
$aaplStock->getExch(),
$this->equalTo('FRA')
);
}
/**
* @param SearchResult[] $searchResult
*/
private function findApple($searchResult, $symbol = self::APPLE_SYMBOL): ?SearchResult
{
foreach ($searchResult as $result) {
if ($symbol === $result->getSymbol()) {
return $result;
}
}
return null;
}
/**
* @test
* @dataProvider getTestDataForHistoricalData
*/
public function getHistoricalQuoteData_valuesForInterval_returnHistoricalData($interval, \DateTime $startDate, \DateTime $endDate): void
{
$returnValue = $this->client->getHistoricalQuoteData(self::APPLE_SYMBOL, $interval, $startDate, $endDate);
$this->assertIsArray($returnValue);
$this->assertGreaterThan(0, \count($returnValue));
$this->assertContainsOnlyInstancesOf(HistoricalData::class, $returnValue);
$historicalData = $returnValue[0];
$this->assertInstanceOf(\DateTime::class, $historicalData->getDate());
$this->assertIsFloat($historicalData->getOpen());
$this->assertIsFloat($historicalData->getHigh());
$this->assertIsFloat($historicalData->getLow());
$this->assertIsFloat($historicalData->getClose());
$this->assertIsFloat($historicalData->getAdjClose());
$this->assertIsInt($historicalData->getVolume());
}
/**
* @test
*/
public function getHistoricalQuoteData_valuesForInvalidInterval_throwInvalidArgumentException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Interval must be one of: 1d, 1wk, 1mo');
$this->client->getHistoricalQuoteData(self::APPLE_SYMBOL, 'invalid_interval', new \DateTime('-7 days'), new \DateTime('today'));
}
/**
* @test
*/
public function getHistoricalQuoteData_startDateIsGreaterThanEndDate_throwInvalidArgumentException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Start date must be before end date');
$this->client->getHistoricalQuoteData(self::APPLE_SYMBOL, ApiClient::INTERVAL_1_DAY, new \DateTime('7 days'), new \DateTime('today'));
}
public function getTestDataForHistoricalData(): array
{
return [
[ApiClient::INTERVAL_1_DAY, new \DateTime('-7 days'), new \DateTime('today')],
[ApiClient::INTERVAL_1_WEEK, new \DateTime('-8 weeks'), new \DateTime('today')],
[ApiClient::INTERVAL_1_MONTH, new \DateTime('-12 months'), new \DateTime('today')],
];
}
/**
* @test
*/
public function getHistoricalDividendData_valuesForInterval_returnHistoricalData(): void
{
$returnValue = $this->client->getHistoricalDividendData(self::APPLE_SYMBOL, new \DateTime('2020-01-01'), new \DateTime());
$this->assertIsArray($returnValue);
$this->assertGreaterThanOrEqual(5, \count($returnValue));
$this->assertContainsOnlyInstancesOf(DividendData::class, $returnValue);
// Assert the dividend from Feb 2020
$historicalData = $returnValue[0];
$this->assertInstanceOf(\DateTime::class, $historicalData->getDate());
$this->assertEquals('2020-02-07', $historicalData->getDate()->format('Y-m-d'));
$this->assertIsFloat($historicalData->getDividends());
$this->assertEquals(0.1925, $historicalData->getDividends());
}
/**
* @test
*/
public function getHistoricalDividendData_startDateIsGreaterThanEndDate_throwInvalidArgumentException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Start date must be before end date');
$this->client->getHistoricalDividendData(self::APPLE_SYMBOL, new \DateTime('7 days'), new \DateTime('today'));
}
/**
* @test
*/
public function getHistoricalSplitData_valuesForInterval_returnHistoricalData(): void
{
$returnValue = $this->client->getHistoricalSplitData(self::APPLE_SYMBOL, new \DateTime('2005-01-01'), new \DateTime());
$this->assertIsArray($returnValue);
$this->assertGreaterThanOrEqual(3, \count($returnValue));
$this->assertContainsOnlyInstancesOf(SplitData::class, $returnValue);
// Assert the stop split from Feb 2005
$historicalData = $returnValue[0];
$this->assertInstanceOf(\DateTime::class, $historicalData->getDate());
$this->assertEquals('2005-02-28', $historicalData->getDate()->format('Y-m-d'));
$this->assertEquals('2:1', $historicalData->getStockSplits());
}
/**
* @test
*/
public function getHistoricalSplitData_startDateIsGreaterThanEndDate_throwInvalidArgumentException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Start date must be before end date');
$this->client->getHistoricalSplitData(self::APPLE_SYMBOL, new \DateTime('7 days'), new \DateTime('today'));
}
/**
* @test
*/
public function getQuote_singleSymbol_returnQuote(): void
{
$returnValue = $this->client->getQuote(self::APPLE_SYMBOL);
$this->assertInstanceOf(Quote::class, $returnValue);
$this->assertAppleQuote($returnValue);
}
/**
* @test
*/
public function getQuotes_multipleSymbols_returnListOfQuotes(): void
{
$returnValue = $this->client->getQuotes([self::APPLE_SYMBOL, self::GOOGLE_SYMBOL]);
$this->assertIsArray($returnValue);
$this->assertCount(2, $returnValue);
$this->assertContainsOnlyInstancesOf(Quote::class, $returnValue);
$appleQuote = $returnValue[0];
$this->assertAppleQuote($appleQuote);
}
private function assertAppleQuote(Quote $quote): void
{
$this->assertEquals('AAPL', $quote->getSymbol());
}
/**
* @test
*/
public function getExchangeRate_singleRate_returnExchangeRate(): void
{
$returnValue = $this->client->getExchangeRate(self::CURRENCY_EUR, self::CURRENCY_USD);
$this->assertInstanceOf(Quote::class, $returnValue);
$this->assertEurUsdExchangeRate($returnValue);
}
/**
* @test
*/
public function getExchangeRates_multipleOnes_returnListOfExchangeRates(): void
{
$query = [
[self::CURRENCY_EUR, self::CURRENCY_USD],
[self::CURRENCY_USD, self::CURRENCY_EUR],
];
$returnValue = $this->client->getExchangeRates($query);
$this->assertIsArray($returnValue);
$this->assertCount(2, $returnValue);
$this->assertContainsOnlyInstancesOf(Quote::class, $returnValue);
$exchangeRate = $returnValue[0];
$this->assertEurUsdExchangeRate($exchangeRate);
}
private function assertEurUsdExchangeRate(Quote $exchangeRate): void
{
$expectedSymbol = self::CURRENCY_EUR.self::CURRENCY_USD.ApiClient::CURRENCY_SYMBOL_SUFFIX;
$expectedName = self::CURRENCY_EUR.'/'.self::CURRENCY_USD;
$this->assertEquals($expectedSymbol, $exchangeRate->getSymbol());
$this->assertEquals($expectedName, $exchangeRate->getShortName());
$this->assertInstanceOf(\DateTime::class, $exchangeRate->getRegularMarketTime());
$this->assertIsFloat($exchangeRate->getRegularMarketPrice());
$this->assertIsFloat($exchangeRate->getAsk());
$this->assertIsFloat($exchangeRate->getBid());
}
public function runBare(): void
{
// I'll leave this part to you. PHPUnit supplies methods for parsing annotations.
for ($i = 0; $i < self::TRY_COUNT; ++$i) {
try {
parent::runBare();
return;
} catch (TransferException $e) {
// Catch all Guzzle network exceptions for retry
if ($i < self::TRY_COUNT - 1) {
sleep(self::RETRY_SLEEP_SECONDS);
}
}
}
if ($e) {
throw $e; // Throw the last exception
}
}
public function testStockSummary(): void
{
$returnValue = $this->client->stockSummary(self::APPLE_SYMBOL);
$this->assertIsArray($returnValue);
$this->assertEquals(self::APPLE_SYMBOL, $returnValue[0]['quoteType']['symbol']);
}
public function testGetStockOptions(): void
{
$returnValue = $this->client->getStockOptions(self::APPLE_SYMBOL);
$this->assertIsArray($returnValue);
$this->assertGreaterThan(0, \count($returnValue));
$this->assertContainsOnlyInstancesOf(Option::class, $returnValue);
}
}