Skip to content

Firefly III: a personal finances manager

Test the currency exchange controller.

James Colef27eb084c7a2
diff --git a/app/Models/CurrencyExchangeRate.php b/app/Models/CurrencyExchangeRate.php
index 6f06cc8862..e77bb2484c 100644
--- a/app/Models/CurrencyExchangeRate.php
+++ b/app/Models/CurrencyExchangeRate.php
@@ -36,6 +36,9 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @property TransactionCurrency $fromCurrency
* @property TransactionCurrency $toCurrency
* @property float $rate
+ * @property Carbon $date
+ * @property int $from_currency_id
+ * @property int $to_currency_id
*
*/
class CurrencyExchangeRate extends Model
diff --git a/tests/Api/V1/Controllers/CurrencyControllerTest.php b/tests/Api/V1/Controllers/CurrencyControllerTest.php
index d6fb9884b3..c90dada382 100644
--- a/tests/Api/V1/Controllers/CurrencyControllerTest.php
+++ b/tests/Api/V1/Controllers/CurrencyControllerTest.php
@@ -42,7 +42,7 @@ class CurrencyControllerTest extends TestCase
/**
*
*/
- public function setUp()
+ public function setUp(): void
{
parent::setUp();
Passport::actingAs($this->user());
diff --git a/tests/Api/V1/Controllers/CurrencyExchangeRateControllerTest.php b/tests/Api/V1/Controllers/CurrencyExchangeRateControllerTest.php
new file mode 100644
index 0000000000..4a0c1c8e04
--- /dev/null
+++ b/tests/Api/V1/Controllers/CurrencyExchangeRateControllerTest.php
@@ -0,0 +1,100 @@
+<?php
+/**
+ * CurrencyExchangeRateControllerTest.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * This file is part of Firefly III.
+ *
+ * Firefly III is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Firefly III is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+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 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::debug(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);
+
+ $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('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' => [
+ 'rate' => 0.5,
+ 'links' => [
+ [
+ 'rel' => 'self',
+ 'uri' => '/currency_exchange_rates/',
+ ],
+ ],
+ ],
+ ]
+ );
+ $response->assertHeader('Content-Type', 'application/vnd.api+json');
+ }
+}
\ No newline at end of file