2020-05-12 11:56:30 +02:00
|
|
|
<?php
|
2020-09-14 13:11:46 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2020-05-12 11:56:30 +02:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
use App\DataMapper\DefaultSettings;
|
|
|
|
use App\Jobs\Util\UpdateExchangeRates;
|
|
|
|
use App\Libraries\Currency\Conversion\CurrencyApi;
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Currency;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use AshAllenDesign\LaravelExchangeRates\Facades\ExchangeRate;
|
|
|
|
use Faker\Factory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Jobs\Util\UpdateExchangeRates
|
|
|
|
*/
|
|
|
|
class UpdateExchangeRatesTest extends TestCase
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use DatabaseTransactions;
|
|
|
|
use MockAccountData;
|
|
|
|
|
|
|
|
public function setUp() :void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->makeTestData();
|
|
|
|
|
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
Model::reguard();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExchangeRate()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! empty(config('ninja.currency_converter_api_key'))) {
|
2020-05-12 11:56:30 +02:00
|
|
|
$cc_endpoint = sprintf('https://openexchangerates.org/api/latest.json?app_id=%s', config('ninja.currency_converter_api_key'));
|
|
|
|
|
|
|
|
$client = new \GuzzleHttp\Client();
|
|
|
|
$response = $client->get($cc_endpoint);
|
|
|
|
|
|
|
|
$currency_api = json_decode($response->getBody());
|
|
|
|
|
|
|
|
UpdateExchangeRates::dispatchNow();
|
|
|
|
|
|
|
|
$currencies = Cache::get('currencies');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-05-12 11:56:30 +02:00
|
|
|
$gbp_currency = $currencies->filter(function ($item) {
|
|
|
|
return $item->id == 2;
|
|
|
|
})->first();
|
|
|
|
|
|
|
|
$this->assertEquals($currency_api->rates->GBP, $gbp_currency->exchange_rate);
|
2020-09-06 11:38:10 +02:00
|
|
|
} else {
|
2020-05-12 11:56:30 +02:00
|
|
|
$this->markTestSkipped('No API Key set');
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-05-12 11:56:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testExchangeRateConversion()
|
|
|
|
{
|
|
|
|
$usd = Currency::find(1);
|
|
|
|
$gbp = Currency::find(2);
|
|
|
|
|
|
|
|
$usd->exchange_rate = 1;
|
|
|
|
$usd->save();
|
|
|
|
|
|
|
|
$gbp->exchange_rate = 0.5;
|
|
|
|
$gbp->save();
|
|
|
|
|
|
|
|
$currency_api = new CurrencyApi();
|
|
|
|
|
|
|
|
$convert_to_gbp = $currency_api->convert(10, 1, 2);
|
|
|
|
|
|
|
|
$this->assertEquals($convert_to_gbp, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSyntheticExchangeRate()
|
|
|
|
{
|
|
|
|
$usd = Currency::find(1);
|
|
|
|
$gbp = Currency::find(2);
|
|
|
|
$aud = Currency::find(12);
|
|
|
|
|
|
|
|
$usd->exchange_rate = 1;
|
|
|
|
$usd->save();
|
|
|
|
|
|
|
|
$gbp->exchange_rate = 0.5;
|
|
|
|
$gbp->save();
|
|
|
|
|
|
|
|
$aud->exchange_rate = 1.5;
|
|
|
|
$aud->save();
|
|
|
|
|
|
|
|
$currency_api = new CurrencyApi();
|
|
|
|
|
|
|
|
$convert_to_aud = $currency_api->convert(10, 1, 12);
|
|
|
|
|
|
|
|
$this->assertEquals($convert_to_aud, 15);
|
|
|
|
|
|
|
|
$synthetic_exchange = $currency_api->exchangeRate($gbp->id, $aud->id);
|
|
|
|
|
|
|
|
$this->assertEquals($synthetic_exchange, 3);
|
|
|
|
}
|
|
|
|
}
|