. */ declare(strict_types=1); namespace Tests\Api\V1\Controllers; use Carbon\Carbon; use FireflyIII\Models\CurrencyExchangeRate; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Services\Currency\ExchangeRateInterface; use FireflyIII\Transformers\CurrencyExchangeRateTransformer; use Laravel\Passport\Passport; use Log; use Tests\TestCase; /** * * Class CurrencyExchangeRateControllerTest */ class CurrencyExchangeRateControllerTest extends TestCase { /** * */ public function setUp(): void { parent::setUp(); Passport::actingAs($this->user()); Log::info(sprintf('Now in %s.', \get_class($this))); } /** * @covers \FireflyIII\Api\V1\Controllers\CurrencyExchangeRateController */ public function testIndex(): void { // mock repository $repository = $this->mock(CurrencyRepositoryInterface::class); $service = $this->mock(ExchangeRateInterface::class); $transformer = $this->mock(CurrencyExchangeRateTransformer::class); $rate = new CurrencyExchangeRate(); $rate->date = new Carbon(); $rate->updated_at = new Carbon(); $rate->created_at = new Carbon(); $rate->rate = '0.5'; $rate->to_currency_id = 1; $rate->from_currency_id = 2; // mock transformer $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf(); $transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]); $transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]); $transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]); // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('findByCodeNull')->withArgs(['EUR'])->andReturn(TransactionCurrency::whereCode('EUR')->first())->once(); $repository->shouldReceive('findByCodeNull')->withArgs(['USD'])->andReturn(TransactionCurrency::whereCode('USD')->first())->once(); $repository->shouldReceive('getExchangeRate')->andReturn(null)->once(); $service->shouldReceive('setUser')->once(); $service->shouldReceive('getRate')->once()->andReturn($rate); // test API $params = [ 'from' => 'EUR', 'to' => 'USD', 'date' => '2018-01-01', ]; $response = $this->get('/api/v1/cer?' . http_build_query($params)); $response->assertStatus(200); $response->assertJson( ['data' => [ 'type' => 'currency_exchange_rates', ], ] ); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } /** * @covers \FireflyIII\Api\V1\Controllers\CurrencyExchangeRateController */ public function testIndexBadDestination(): void { // mock repository $repository = $this->mock(CurrencyRepositoryInterface::class); $service = $this->mock(ExchangeRateInterface::class); $transformer = $this->mock(CurrencyExchangeRateTransformer::class); $rate = new CurrencyExchangeRate(); $rate->date = new Carbon(); $rate->updated_at = new Carbon(); $rate->created_at = new Carbon(); $rate->rate = '0.5'; $rate->to_currency_id = 1; $rate->from_currency_id = 2; // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('findByCodeNull')->withArgs(['EUR'])->andReturn(TransactionCurrency::whereCode('USD')->first())->once(); $repository->shouldReceive('findByCodeNull')->withArgs(['USD'])->andReturn(null)->once(); // test API $params = [ 'from' => 'EUR', 'to' => 'USD', 'date' => '2018-01-01', ]; $response = $this->get('/api/v1/cer?' . http_build_query($params), ['Accept' => 'application/json']); $response->assertStatus(500); $response->assertSee('Unknown destination currency.'); $response->assertHeader('Content-Type', 'application/json'); } /** * @covers \FireflyIII\Api\V1\Controllers\CurrencyExchangeRateController */ public function testIndexBadSource(): void { // mock repository $repository = $this->mock(CurrencyRepositoryInterface::class); $service = $this->mock(ExchangeRateInterface::class); $transformer = $this->mock(CurrencyExchangeRateTransformer::class); $rate = new CurrencyExchangeRate(); $rate->date = new Carbon(); $rate->updated_at = new Carbon(); $rate->created_at = new Carbon(); $rate->rate = '0.5'; $rate->to_currency_id = 1; $rate->from_currency_id = 2; // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('findByCodeNull')->withArgs(['EUR'])->andReturn(null)->once(); $repository->shouldReceive('findByCodeNull')->withArgs(['USD'])->andReturn(TransactionCurrency::whereCode('USD')->first())->once(); // test API $params = [ 'from' => 'EUR', 'to' => 'USD', 'date' => '2018-01-01', ]; $response = $this->get('/api/v1/cer?' . http_build_query($params), ['Accept' => 'application/json']); $response->assertStatus(500); $response->assertSee('Unknown source currency.'); $response->assertHeader('Content-Type', 'application/json'); } }