Files

108 lines
3.6 KiB
PHP
Raw Permalink Normal View History

2025-01-28 17:14:49 -06:00
<?php
2025-01-28 17:33:54 -06:00
declare(strict_types=1);
namespace Tests;
use App\Interfaces\MarketData\AlphaVantageMarketData;
2025-01-28 17:14:49 -06:00
use App\Interfaces\MarketData\FallbackInterface;
use App\Interfaces\MarketData\Types\Quote;
2025-01-28 17:14:49 -06:00
use App\Interfaces\MarketData\YahooMarketData;
use Illuminate\Support\Facades\Log;
use Mockery;
class FallbackInterfaceTest extends TestCase
{
2025-01-28 17:14:49 -06:00
protected function setUp(): void
{
parent::setUp();
Log::spy();
}
2025-01-28 17:14:49 -06:00
public function test_fallback_to_next_provider_on_failure()
{
config()->set('investbrain.provider', 'yahoo,alphavantage');
config()->set('investbrain.interfaces', [
'yahoo' => YahooMarketData::class,
'alphavantage' => AlphaVantageMarketData::class,
]);
$yahooMock = Mockery::mock(YahooMarketData::class);
$yahooMock->shouldReceive('quote')
2025-01-28 17:14:49 -06:00
->andThrow(new \Exception('Yahoo failed'));
$alphaMock = Mockery::mock(AlphaVantageMarketData::class);
$alphaMock->shouldReceive('quote')
2025-01-28 17:14:49 -06:00
->andReturn(new Quote(['market_value' => 10]));
$this->app->instance(YahooMarketData::class, $yahooMock);
$this->app->instance(AlphaVantageMarketData::class, $alphaMock);
2025-01-28 17:14:49 -06:00
$fallbackInterface = new FallbackInterface;
$result = $fallbackInterface->quote('ACME');
$this->assertEquals(new Quote(['market_value' => 10]), $result);
Log::shouldHaveReceived('warning')->with('Failed calling method quote (yahoo): Yahoo failed');
}
2025-01-28 17:14:49 -06:00
public function test_all_providers_fail()
{
config()->set('investbrain.provider', 'yahoo,alpha');
config()->set('investbrain.interfaces', [
'yahoo' => YahooMarketData::class,
'alphavantage' => AlphaVantageMarketData::class,
]);
$yahooMock = Mockery::mock(YahooMarketData::class);
$yahooMock->shouldReceive('quote')
2025-01-28 17:14:49 -06:00
->andThrow(new \Exception('Yahoo failed'));
$alphaMock = Mockery::mock(AlphaVantageMarketData::class);
$alphaMock->shouldReceive('quote')
2025-01-28 17:14:49 -06:00
->andThrow(new \Exception('Alpha failed'));
$this->app->instance(YahooMarketData::class, $yahooMock);
$this->app->instance(AlphaVantageMarketData::class, $alphaMock);
2025-01-28 17:14:49 -06:00
$fallbackInterface = new FallbackInterface;
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not get market data: Provider [alpha] is not a valid market data interface.');
$fallbackInterface->quote('AAPL');
Log::shouldHaveReceived('warning')->with('Failed calling method quote (yahoo): Yahoo failed');
Log::shouldHaveReceived('warning')->with('Failed calling method quote (alpha): Alpha failed');
}
public function test_exists_method_fails_without_exception()
{
config()->set('investbrain.provider', 'yahoo,alpha');
config()->set('investbrain.interfaces', [
'yahoo' => YahooMarketData::class,
'alphavantage' => AlphaVantageMarketData::class,
]);
$yahooMock = Mockery::mock(YahooMarketData::class);
$yahooMock->shouldReceive('exists')
->andThrow(new \Exception('Yahoo failed'));
$alphaMock = Mockery::mock(AlphaVantageMarketData::class);
$alphaMock->shouldReceive('exists')
->andThrow(new \Exception('Alpha failed'));
$this->app->instance(YahooMarketData::class, $yahooMock);
$this->app->instance(AlphaVantageMarketData::class, $alphaMock);
$fallbackInterface = new FallbackInterface;
$result = $fallbackInterface->exists('ZZZ');
2025-01-28 20:33:28 -06:00
$this->assertIsBool($result);
$this->assertFalse($result);
}
2025-01-28 17:14:49 -06:00
}