Test new category code.

This commit is contained in:
James Cole
2019-08-28 17:01:48 +02:00
parent 5fb7635100
commit f4eca2d4ae
4 changed files with 332 additions and 68 deletions

View File

@@ -28,13 +28,15 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Fiscal\FiscalHelperInterface;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Category\NoCategoryRepositoryInterface;
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
use FireflyIII\Support\Chart\Category\WholePeriodChartGenerator;
use Illuminate\Support\Collection;
use Log;
use Preferences;
use Tests\Support\TestDataTrait;
use Tests\TestCase;
/**
@@ -45,6 +47,8 @@ use Tests\TestCase;
*/
class CategoryControllerTest extends TestCase
{
use TestDataTrait;
/**
*
*/
@@ -59,16 +63,19 @@ class CategoryControllerTest extends TestCase
* @dataProvider dateRangeProvider
*
* @param string $range
*
* @throws FireflyException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function testAll(string $range): void
{
Log::info(sprintf('Now in test %s.', __METHOD__));
$repository = $this->mock(CategoryRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$chartGen = $this->mock(WholePeriodChartGenerator::class);
$firstUseDate = null;
switch ($range) {
default:
throw new FireflyException(sprintf('No case for %s', $range));
@@ -98,10 +105,9 @@ class CategoryControllerTest extends TestCase
$this->mockDefaultSession();
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
$repository->shouldReceive('spentInPeriod')->andReturn([])->atLeast()->once();
$repository->shouldReceive('earnedInPeriod')->andReturn([])->atLeast()->once();
$chartGen->shouldReceive('generate')->atLeast()->once()->andReturn([]);
$repository->shouldReceive('firstUseDate')->andReturn($firstUseDate)->once();
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT,AccountType::MORTGAGE]])->andReturn(new Collection)->once();
$generator->shouldReceive('multiSet')->once()->andReturn([]);
$this->be($this->user());
@@ -118,54 +124,24 @@ class CategoryControllerTest extends TestCase
*/
public function testFrontPage(string $range): void
{
$repository = $this->mock(CategoryRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
Log::info(sprintf('Now in test %s.', __METHOD__));
$repository = $this->mock(CategoryRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$noCatRepos = $this->mock(NoCategoryRepositoryInterface::class);
$category = $this->getRandomCategory();
$account = $this->getRandomAsset();
$this->mockDefaultSession();
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
// spent per currency data:
$spentNoCategory = [
1 =>
[
'spent' => '-123.45',
'currency_id' => 1,
'currency_code' => 'X',
'currency_symbol' => 'x',
'currency_decimal_places' => 2,
],
];
$spentData = [
1 => [
'name' => 'Car',
'spent' => [
1 => [
'spent' => '-123.45',
'currency_id' => 2,
'currency_code' => 'a',
'currency_symbol' => 'b',
'currency_decimal_places' => 2,
],
],
],
];
$opsRepos->shouldReceive('sumExpenses')->atLeast()->once()->andReturn($this->categorySumExpenses());
$noCatRepos->shouldReceive('sumExpenses')->atLeast()->once()->andReturn($this->categorySumExpenses());
// grab two categories from the user
$categories = $this->user()->categories()->take(2)->get();
// grab two the users asset accounts:
$accounts = $this->user()->accounts()->where('account_type_id', 3)->take(2)->get();
// repository will return these.
$repository->shouldReceive('getCategories')->andReturn($categories)->once();
$accountRepos->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::ASSET, AccountType::DEFAULT]])->andReturn($accounts);
$repository->shouldReceive('spentInPeriodPerCurrency')->times(2)->andReturn($spentData);
$repository->shouldReceive('spentInPeriodPcWoCategory')->once()->andReturn($spentNoCategory);
$currencyRepos->shouldReceive('findNull')->withArgs([1])->once()->andReturn($this->getEuro());
$repository->shouldReceive('getCategories')->atLeast()->once()->andReturn(new Collection([$category]));
$accountRepos->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::ASSET, AccountType::DEFAULT]])->andReturn(
new Collection([$account])
);
$generator->shouldReceive('multiSet')->andReturn([]);
$this->be($this->user());
@@ -179,18 +155,21 @@ class CategoryControllerTest extends TestCase
*/
public function testReportPeriod(): void
{
Log::info(sprintf('Now in test %s.', __METHOD__));
$repository = $this->mock(CategoryRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$opsRepos = $this->mock(OperationsRepositoryInterface::class);
$date = new Carbon;
$opsRepos->shouldReceive('listExpenses')->atLeast()->once()->andReturn($this->categoryListExpenses());
$opsRepos->shouldReceive('listIncome')->atLeast()->once()->andReturn($this->categoryListIncome());
$this->mockDefaultSession();
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
$repository->shouldReceive('periodExpenses')->andReturn([])->once();
$repository->shouldReceive('periodIncome')->andReturn([])->once();
$generator->shouldReceive('multiSet')->andReturn([])->once();
$this->be($this->user());
@@ -203,18 +182,21 @@ class CategoryControllerTest extends TestCase
*/
public function testReportPeriodNoCategory(): void
{
Log::info(sprintf('Now in test %s.', __METHOD__));
$repository = $this->mock(CategoryRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$noCatRepos = $this->mock(NoCategoryRepositoryInterface::class);
$date = new Carbon;
$noCatRepos->shouldReceive('listExpenses')->atLeast()->once()->andReturn($this->noCategoryListExpenses());
$noCatRepos->shouldReceive('listIncome')->atLeast()->once()->andReturn($this->noCategoryListIncome());
$this->mockDefaultSession();
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
$repository->shouldReceive('periodExpensesNoCategory')->andReturn([])->once();
$repository->shouldReceive('periodIncomeNoCategory')->andReturn([])->once();
$generator->shouldReceive('multiSet')->andReturn([])->once();
$this->be($this->user());
@@ -227,26 +209,29 @@ class CategoryControllerTest extends TestCase
* @dataProvider dateRangeProvider
*
* @param string $range
*
* @throws Exception
*/
public function testSpecificPeriod(string $range): void
{
Log::info(sprintf('Now in test %s.', __METHOD__));
$repository = $this->mock(CategoryRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$account = $this->getRandomAsset();
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
$chartGen = $this->mock(WholePeriodChartGenerator::class);
$account = $this->getRandomAsset();
$date = new Carbon;
$this->mockDefaultSession();
Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
$chartGen->shouldReceive('generate')->atLeast()->once()->andReturn([]);
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection([$account]));
$repository->shouldReceive('spentInPeriod')->andReturn([]);
$repository->shouldReceive('earnedInPeriod')->andReturn([]);
$generator->shouldReceive('multiSet')->andReturn([])->once();
$this->be($this->user());