|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright 2025 Anton Smirnov |
| 5 | + * @license MIT https://spdx.org/licenses/MIT.html |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Peso\Debug\Tests\Services; |
| 11 | + |
| 12 | +use Arokettu\Date\Date; |
| 13 | +use Peso\Core\Exceptions\ConversionNotPerformedException; |
| 14 | +use Peso\Core\Exceptions\ExchangeRateNotFoundException; |
| 15 | +use Peso\Core\Exceptions\RequestNotSupportedException; |
| 16 | +use Peso\Core\Requests\CurrentConversionRequest; |
| 17 | +use Peso\Core\Requests\CurrentExchangeRateRequest; |
| 18 | +use Peso\Core\Requests\HistoricalConversionRequest; |
| 19 | +use Peso\Core\Requests\HistoricalExchangeRateRequest; |
| 20 | +use Peso\Core\Responses\ErrorResponse; |
| 21 | +use Peso\Core\Types\Decimal; |
| 22 | +use Peso\Debug\Services\BlackHoleService; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | +use stdClass; |
| 25 | + |
| 26 | +final class BlackHoleServiceTest extends TestCase |
| 27 | +{ |
| 28 | + public function testSupportsAllSupported(): void |
| 29 | + { |
| 30 | + $service = new BlackHoleService(); |
| 31 | + |
| 32 | + self::assertTrue($service->supports(new CurrentExchangeRateRequest('PHP', 'USD'))); |
| 33 | + self::assertTrue($service->supports(new HistoricalExchangeRateRequest('PHP', 'USD', Date::today()))); |
| 34 | + self::assertTrue($service->supports(new CurrentConversionRequest(Decimal::init(1), 'PHP', 'USD'))); |
| 35 | + self::assertTrue($service->supports( |
| 36 | + new HistoricalConversionRequest(Decimal::init(1), 'PHP', 'USD', Date::today()), |
| 37 | + )); |
| 38 | + |
| 39 | + self::assertFalse($service->supports(new stdClass())); |
| 40 | + } |
| 41 | + |
| 42 | + public function testSupportsLimited(): void |
| 43 | + { |
| 44 | + $service = new BlackHoleService( |
| 45 | + CurrentExchangeRateRequest::class, |
| 46 | + HistoricalConversionRequest::class, |
| 47 | + stdClass::class, // still not supported |
| 48 | + ); |
| 49 | + |
| 50 | + self::assertTrue($service->supports(new CurrentExchangeRateRequest('PHP', 'USD'))); |
| 51 | + self::assertFalse($service->supports(new HistoricalExchangeRateRequest('PHP', 'USD', Date::today()))); |
| 52 | + self::assertFalse($service->supports(new CurrentConversionRequest(Decimal::init(1), 'PHP', 'USD'))); |
| 53 | + self::assertTrue($service->supports( |
| 54 | + new HistoricalConversionRequest(Decimal::init(1), 'PHP', 'USD', Date::today()), |
| 55 | + )); |
| 56 | + |
| 57 | + self::assertFalse($service->supports(new stdClass())); |
| 58 | + } |
| 59 | + |
| 60 | + public function testReturnsAppropriateErrors(): void |
| 61 | + { |
| 62 | + $service = new BlackHoleService(); |
| 63 | + $today = Date::today(); |
| 64 | + |
| 65 | + $response = $service->send(new CurrentExchangeRateRequest('PHP', 'USD')); |
| 66 | + self::assertInstanceOf(ErrorResponse::class, $response); |
| 67 | + self::assertInstanceOf(ExchangeRateNotFoundException::class, $response->exception); |
| 68 | + self::assertEquals('Unable to find exchange rate for PHP/USD', $response->exception->getMessage()); |
| 69 | + |
| 70 | + $response = $service->send(new HistoricalExchangeRateRequest('PHP', 'USD', $today)); |
| 71 | + self::assertInstanceOf(ErrorResponse::class, $response); |
| 72 | + self::assertInstanceOf(ExchangeRateNotFoundException::class, $response->exception); |
| 73 | + self::assertEquals( |
| 74 | + 'Unable to find exchange rate for PHP/USD on ' . $today->toString(), |
| 75 | + $response->exception->getMessage(), |
| 76 | + ); |
| 77 | + |
| 78 | + $response = $service->send(new CurrentConversionRequest(Decimal::init(1), 'PHP', 'USD')); |
| 79 | + self::assertInstanceOf(ErrorResponse::class, $response); |
| 80 | + self::assertInstanceOf(ConversionNotPerformedException::class, $response->exception); |
| 81 | + self::assertEquals('Unable to convert 1 PHP to USD', $response->exception->getMessage()); |
| 82 | + |
| 83 | + $response = $service->send(new HistoricalConversionRequest(Decimal::init(1), 'PHP', 'USD', $today)); |
| 84 | + self::assertInstanceOf(ErrorResponse::class, $response); |
| 85 | + self::assertInstanceOf(ConversionNotPerformedException::class, $response->exception); |
| 86 | + self::assertEquals( |
| 87 | + 'Unable to convert 1 PHP to USD on ' . $today->toString(), |
| 88 | + $response->exception->getMessage(), |
| 89 | + ); |
| 90 | + |
| 91 | + $response = $service->send(new stdClass()); |
| 92 | + self::assertInstanceOf(ErrorResponse::class, $response); |
| 93 | + self::assertInstanceOf(RequestNotSupportedException::class, $response->exception); |
| 94 | + self::assertEquals('Unsupported request type: "stdClass"', $response->exception->getMessage()); |
| 95 | + } |
| 96 | + |
| 97 | + public function testReturnsAppropriateErrorsWhenLimited(): void |
| 98 | + { |
| 99 | + $service = new BlackHoleService( |
| 100 | + CurrentExchangeRateRequest::class, |
| 101 | + HistoricalConversionRequest::class, |
| 102 | + stdClass::class, // still not supported |
| 103 | + ); |
| 104 | + $today = Date::today(); |
| 105 | + |
| 106 | + $response = $service->send(new CurrentExchangeRateRequest('PHP', 'USD')); |
| 107 | + self::assertInstanceOf(ErrorResponse::class, $response); |
| 108 | + self::assertInstanceOf(ExchangeRateNotFoundException::class, $response->exception); |
| 109 | + self::assertEquals('Unable to find exchange rate for PHP/USD', $response->exception->getMessage()); |
| 110 | + |
| 111 | + $response = $service->send(new HistoricalExchangeRateRequest('PHP', 'USD', $today)); |
| 112 | + self::assertInstanceOf(ErrorResponse::class, $response); |
| 113 | + self::assertInstanceOf(RequestNotSupportedException::class, $response->exception); |
| 114 | + self::assertEquals(\sprintf( |
| 115 | + 'Unsupported request type: "%s"', |
| 116 | + HistoricalExchangeRateRequest::class, |
| 117 | + ), $response->exception->getMessage()); |
| 118 | + |
| 119 | + $response = $service->send(new CurrentConversionRequest(Decimal::init(1), 'PHP', 'USD')); |
| 120 | + self::assertInstanceOf(ErrorResponse::class, $response); |
| 121 | + self::assertInstanceOf(RequestNotSupportedException::class, $response->exception); |
| 122 | + self::assertEquals(\sprintf( |
| 123 | + 'Unsupported request type: "%s"', |
| 124 | + CurrentConversionRequest::class, |
| 125 | + ), $response->exception->getMessage()); |
| 126 | + |
| 127 | + $response = $service->send(new HistoricalConversionRequest(Decimal::init(1), 'PHP', 'USD', $today)); |
| 128 | + self::assertInstanceOf(ErrorResponse::class, $response); |
| 129 | + self::assertInstanceOf(ConversionNotPerformedException::class, $response->exception); |
| 130 | + self::assertEquals( |
| 131 | + 'Unable to convert 1 PHP to USD on ' . $today->toString(), |
| 132 | + $response->exception->getMessage(), |
| 133 | + ); |
| 134 | + |
| 135 | + $response = $service->send(new stdClass()); |
| 136 | + self::assertInstanceOf(ErrorResponse::class, $response); |
| 137 | + self::assertInstanceOf(RequestNotSupportedException::class, $response->exception); |
| 138 | + self::assertEquals('Unsupported request type: "stdClass"', $response->exception->getMessage()); |
| 139 | + } |
| 140 | +} |
0 commit comments