Skip to content

Commit 9960179

Browse files
committed
Test invalid currencies
1 parent d43bdfe commit 9960179

6 files changed

Lines changed: 70 additions & 5 deletions

File tree

tests/CurrentConversionTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,30 @@ public function testCurrentMulticonvSymbols(): void
141141

142142
self::assertCount(1, $http->getRequests());
143143
}
144+
145+
public function testInvalidBaseCurrency(): void
146+
{
147+
$cache = new Psr16Cache(new ArrayAdapter());
148+
$http = MockClient::get();
149+
150+
$service = new CurrencyApiService('xxxpaidxxx', Subscription::Paid, cache: $cache, httpClient: $http);
151+
152+
$response = $service->send(new CurrentConversionRequest(Decimal::init(1), 'XBT', 'USD'));
153+
self::assertInstanceOf(ErrorResponse::class, $response);
154+
self::assertInstanceOf(ConversionNotPerformedException::class, $response->exception);
155+
self::assertEquals('Unable to convert 1 XBT to USD', $response->exception->getMessage());
156+
}
157+
158+
public function testInvalidQuoteCurrency(): void
159+
{
160+
$cache = new Psr16Cache(new ArrayAdapter());
161+
$http = MockClient::get();
162+
163+
$service = new CurrencyApiService('xxxpaidxxx', Subscription::Paid, cache: $cache, httpClient: $http);
164+
165+
$response = $service->send(new CurrentConversionRequest(Decimal::init(1), 'USD', 'XBT'));
166+
self::assertInstanceOf(ErrorResponse::class, $response);
167+
self::assertInstanceOf(ConversionNotPerformedException::class, $response->exception);
168+
self::assertEquals('Unable to convert 1 USD to XBT', $response->exception->getMessage());
169+
}
144170
}

tests/Helpers/MockClient.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,11 @@ static function (RequestInterface $request) {
3838
case 'apikey=xxxfreexxx&base_currency=RUB&currencies=EUR%2CUSD':
3939
return new Response(body: fopen(__DIR__ . '/../data/rates/latest-RUB-EUR,USD.json', 'r'));
4040

41-
case 'apikey=rate_exceeded&base_currency=TRY':
42-
return new Response(429, body: fopen(__DIR__ . '/../data/rate-limit.json', 'r'));
43-
4441
case 'apikey=invalid&base_currency=TRY':
4542
return new Response(401, body: fopen(__DIR__ . '/../data/invalid-key.json', 'r'));
4643

4744
case 'apikey=xxxfreexxx&base_currency=XBT':
48-
return new Response(422, body: fopen(__DIR__ . '/../data/invalid-base.json', 'r'));
45+
return new Response(422, body: fopen(__DIR__ . '/../data/rates/invalid-base.json', 'r'));
4946

5047
default:
5148
throw new \LogicException('Non-mocked query: ' . $query);
@@ -76,7 +73,7 @@ static function (RequestInterface $request) {
7673
return new Response(body: fopen(__DIR__ . '/../data/rates/2025-06-13-RUB-EUR,USD.json', 'r'));
7774

7875
case 'apikey=xxxfreexxx&base_currency=XBT&date=2025-06-13':
79-
return new Response(422, body: fopen(__DIR__ . '/../data/invalid-base.json', 'r'));
76+
return new Response(422, body: fopen(__DIR__ . '/../data/rates/invalid-base.json', 'r'));
8077

8178
default:
8279
throw new \LogicException('Non-mocked query: ' . $query);
@@ -135,6 +132,20 @@ static function (RequestInterface $request) {
135132
case 'apikey=xxxpaidxxx&base_currency=RUB&value=1234.56&currencies=PHP&date=2025-06-13':
136133
return new Response(body: fopen(__DIR__ . '/../data/conv/2025-06-13-RUB-PHP.json', 'r'));
137134

135+
case 'apikey=xxxpaidxxx&base_currency=XBT&value=1&currencies=USD':
136+
case 'apikey=xxxpaidxxx&base_currency=XBT&value=1&currencies=USD&date=2025-06-13':
137+
return new Response(
138+
status: 422,
139+
body: fopen(__DIR__ . '/../data/conv/invalid-base.json', 'r'),
140+
);
141+
142+
case 'apikey=xxxpaidxxx&base_currency=USD&value=1&currencies=XBT':
143+
case 'apikey=xxxpaidxxx&base_currency=USD&value=1&currencies=XBT&date=2025-06-13':
144+
return new Response(
145+
status: 422,
146+
body: fopen(__DIR__ . '/../data/conv/invalid-quote.json', 'r'),
147+
);
148+
138149
default:
139150
throw new \LogicException('Non-mocked query: ' . $query);
140151
}

tests/HistoricalConversionTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,30 @@ public function testCurrentMulticonvSymbols(): void
145145

146146
self::assertCount(1, $http->getRequests());
147147
}
148+
149+
public function testInvalidBaseCurrency(): void
150+
{
151+
$cache = new Psr16Cache(new ArrayAdapter());
152+
$http = MockClient::get();
153+
154+
$service = new CurrencyApiService('xxxpaidxxx', Subscription::Paid, cache: $cache, httpClient: $http);
155+
156+
$response = $service->send(new HistoricalConversionRequest(Decimal::init(1), 'XBT', 'USD', Calendar::parse('2025-06-13')));
157+
self::assertInstanceOf(ErrorResponse::class, $response);
158+
self::assertInstanceOf(ConversionNotPerformedException::class, $response->exception);
159+
self::assertEquals('Unable to convert 1 XBT to USD on 2025-06-13', $response->exception->getMessage());
160+
}
161+
162+
public function testInvalidQuoteCurrency(): void
163+
{
164+
$cache = new Psr16Cache(new ArrayAdapter());
165+
$http = MockClient::get();
166+
167+
$service = new CurrencyApiService('xxxpaidxxx', Subscription::Paid, cache: $cache, httpClient: $http);
168+
169+
$response = $service->send(new HistoricalConversionRequest(Decimal::init(1), 'USD', 'XBT', Calendar::parse('2025-06-13')));
170+
self::assertInstanceOf(ErrorResponse::class, $response);
171+
self::assertInstanceOf(ConversionNotPerformedException::class, $response->exception);
172+
self::assertEquals('Unable to convert 1 USD to XBT on 2025-06-13', $response->exception->getMessage());
173+
}
148174
}

tests/data/conv/invalid-quote.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"message":"Validation error","errors":{"currencies":["The selected currencies is invalid."]},"info":"For more information, see documentation: https:\/\/currencyapi.com\/docs\/status-codes#_422"}

tests/data/rates/invalid-base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"message":"Validation error","errors":{"base_currency":["The selected base currency is invalid."]},"info":"For more information, see documentation: https:\/\/currencyapi.com\/docs\/status-codes#_422"}

0 commit comments

Comments
 (0)