Files
firefly-iii/tests/Feature/Controllers/JavascriptControllerTest.php
Hosh Sadiq 14dd185717 Use php-intl to format currencies
Currently the php function `number_format` is used to format currencies.
This is problematic as we have to figure out different things for
different currencies ourselves. These formats are determined based on
the libc's locale functions.

The issue arises where an OS doesn't have the proper locales installed,
or, in some cases, it's not supported (see below on multiple issues).

This addresses this issue by using the php-intl extensions to format the
numbers based on the locale. The extension is already a requirement in
`composer.json`. The solution does not rely on `LC_MONETARY` from the
underlying libc (which in Alpine Linux's case, which uses musl, is not
supported as of yet).

List of issues that are related and would potentially be fixed using
this PR:

- #2298
- #2946
- #3070
- #3306
- #3519
2020-07-19 18:34:39 +01:00

171 lines
6.2 KiB
PHP

<?php
/**
* JavascriptControllerTest.php
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Feature\Controllers;
use Amount;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Preference;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
use Mockery;
use Preferences;
use Tests\TestCase;
/**
* Class JavascriptControllerTest
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class JavascriptControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Http\Controllers\JavascriptController
*/
public function testAccounts(): void
{
$this->mockDefaultSession();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$account = $this->getRandomAsset();
$euro = $this->getEuro();
$pref = new Preference;
$pref->data = 'EUR';
Preferences::shouldReceive('get')->withArgs(['currencyPreference', 'EUR'])->atLeast()->once()->andReturn($pref);
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection([$account]))->withArgs([[AccountType::DEFAULT, AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD]])->once();
$currencyRepos->shouldReceive('findByCodeNull')->withArgs(['EUR'])->andReturn($euro);
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$this->be($this->user());
$response = $this->get(route('javascript.accounts'));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\JavascriptController
*/
public function testCurrencies(): void
{
$this->mockDefaultSession();
$repository = $this->mock(CurrencyRepositoryInterface::class);
$euro = $this->getEuro();
$repository->shouldReceive('get')->andReturn(new Collection([$euro]));
$this->be($this->user());
$response = $this->get(route('javascript.currencies'));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\JavascriptController
*
* @param string $range
*
* @dataProvider dateRangeProvider
*/
public function testVariables(string $range): void
{
$this->mockDefaultSession();
$account = $this->getRandomAsset();
$euro = $this->getEuro();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$accountRepos->shouldReceive('findNull')->andReturn($account);
$currencyRepos->shouldReceive('findNull')->andReturn($euro);
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('javascript.variables'));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\JavascriptController
*
* @param string $range
*
* @dataProvider dateRangeProvider
*/
public function testVariablesCustom(string $range): void
{
$this->mockDefaultSession();
$account = $this->getRandomAsset();
$euro = $this->getEuro();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$accountRepos->shouldReceive('findNull')->andReturn($account);
$currencyRepos->shouldReceive('findNull')->andReturn($euro);
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$this->session(['is_custom_range' => true]);
$response = $this->get(route('javascript.variables'));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\JavascriptController
*
* @param string $range
*
* @dataProvider dateRangeProvider
*/
public function testVariablesNull(string $range): void
{
$this->mockDefaultSession();
$account = $this->getRandomAsset();
$euro = $this->getEuro();
//Amount::shouldReceive('getDefaultCurrency')->andReturn($euro)->times(2);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$accountRepos->shouldReceive('findNull')->andReturn($account);
$currencyRepos->shouldReceive('findNull')->andReturn(null);
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('javascript.variables'));
$response->assertStatus(200);
}
}