Files
firefly-iii/tests/Unit/Helpers/Chart/MetaPieChartTest.php

313 lines
13 KiB
PHP
Raw Normal View History

2017-04-28 20:17:10 +02:00
<?php
/**
* MetaPieChartTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* 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
2017-12-17 14:42:21 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-04-28 20:17:10 +02:00
*/
declare(strict_types=1);
2018-08-25 20:45:42 +02:00
namespace Tests\Unit\Helpers\Chart;
2017-04-28 20:17:10 +02:00
use Carbon\Carbon;
use FireflyIII\Helpers\Chart\MetaPieChart;
2019-06-21 19:10:02 +02:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2017-05-02 20:54:49 +02:00
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-08-25 20:45:42 +02:00
use Log;
2018-09-02 20:27:26 +02:00
use Tests\TestCase;
2017-04-28 20:17:10 +02:00
2017-08-12 10:27:45 +02:00
/**
* Class MetaPieChartTest
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
2017-04-28 20:17:10 +02:00
class MetaPieChartTest extends TestCase
{
2018-08-25 20:45:42 +02:00
/**
*
*/
public function setUp(): void
{
parent::setUp();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-08-25 20:45:42 +02:00
}
2017-04-28 20:17:10 +02:00
/**
2018-03-03 14:24:06 +01:00
* @covers \FireflyIII\Helpers\Chart\MetaPieChart
*/
2018-05-11 19:58:10 +02:00
public function testGenerateExpenseAccount(): void
2018-03-03 14:24:06 +01:00
{
2019-06-21 19:10:02 +02:00
$som = (new Carbon())->startOfMonth();
$eom = (new Carbon())->endOfMonth();
$one = $this->getRandomAsset();
$two = $this->getRandomAsset($one->id);
$array = [
[
'destination_account_id' => $one->id,
'budget_id' => 1,
'category_id' => 1,
'amount' => '10',
],
[
'destination_account_id' => $two->id,
'budget_id' => 2,
'category_id' => 2,
'amount' => '10',
],
2018-03-03 14:24:06 +01:00
];
2019-06-21 19:10:02 +02:00
$collector = $this->mock(GroupCollectorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-03-03 14:24:06 +01:00
// mock collector so the correct set of journals is returned:
// then verify the results.
$collector->shouldReceive('setUser')->andReturnSelf()->once();
$collector->shouldReceive('setAccounts')->andReturnSelf()->once();
$collector->shouldReceive('setRange')->andReturnSelf()->once();
$collector->shouldReceive('setBudgets')->andReturnSelf()->once();
$collector->shouldReceive('setCategories')->andReturnSelf()->once();
2019-06-21 19:10:02 +02:00
2018-03-03 14:24:06 +01:00
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL, TransactionType::TRANSFER]])->andReturnSelf()->once();
2019-06-21 19:10:02 +02:00
$collector->shouldReceive('getExtractedJournals')->andReturn($array)->atLeast()->once();
$collector->shouldReceive('withAccountInformation')->andReturnSelf()->once();
2018-03-03 14:24:06 +01:00
// mock all repositories:
2019-06-21 19:10:02 +02:00
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('findNull')->withArgs([$one->id])->andReturn($one)->atLeast()->once();
$accountRepos->shouldReceive('findNull')->withArgs([$two->id])->andReturn($two)->atLeast()->once();
2018-03-03 14:24:06 +01:00
2019-06-21 19:10:02 +02:00
/** @var MetaPieChart $helper */
$helper = app(MetaPieChart::class);
2018-03-03 14:24:06 +01:00
$helper->setUser($this->user());
$helper->setStart($som);
$helper->setEnd($eom);
$chart = $helper->generate('expense', 'account');
// since the set is pretty basic the result is easy to validate:
$keys = array_keys($chart);
2019-06-21 19:10:02 +02:00
$this->assertContains($keys[0], [$one->name, $two->name]);
$this->assertContains($keys[1], [$one->name, $two->name]);
$this->assertEquals('10.000000000000', $chart[$one->name]);
$this->assertEquals('10.000000000000', $chart[$two->name]);
2018-03-03 14:24:06 +01:00
$this->assertTrue(true);
}
/**
* @covers \FireflyIII\Helpers\Chart\MetaPieChart
*/
2019-06-21 19:10:02 +02:00
public function testGenerateExpenseAccountOthers(): void
2018-03-03 14:24:06 +01:00
{
2019-06-21 19:10:02 +02:00
$som = (new Carbon())->startOfMonth();
$eom = (new Carbon())->endOfMonth();
$one = $this->getRandomAsset();
$two = $this->getRandomAsset($one->id);
$array = [
[
'destination_account_id' => $one->id,
'budget_id' => 1,
'category_id' => 1,
'amount' => '10',
],
[
'destination_account_id' => $two->id,
'budget_id' => 2,
'category_id' => 2,
'amount' => '10',
],
2018-03-03 14:24:06 +01:00
];
2019-06-21 19:10:02 +02:00
$collector = $this->mock(GroupCollectorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-03-03 14:24:06 +01:00
// mock collector so the correct set of journals is returned:
// then verify the results.
2019-06-21 19:10:02 +02:00
2018-03-03 14:24:06 +01:00
$collector->shouldReceive('setUser')->andReturnSelf()->twice();
$collector->shouldReceive('setAccounts')->andReturnSelf()->twice();
$collector->shouldReceive('setRange')->andReturnSelf()->twice();
$collector->shouldReceive('setBudgets')->andReturnSelf()->once();
$collector->shouldReceive('setCategories')->andReturnSelf()->once();
2019-06-21 19:10:02 +02:00
2018-03-03 14:24:06 +01:00
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL, TransactionType::TRANSFER]])->andReturnSelf()->once();
2019-06-21 19:10:02 +02:00
$collector->shouldReceive('getExtractedJournals')->andReturn($array)->atLeast()->once();
$collector->shouldReceive('withAccountInformation')->andReturnSelf()->once();
2018-03-03 14:24:06 +01:00
2019-06-21 19:10:02 +02:00
// also collect others:
2018-03-03 14:24:06 +01:00
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->andReturnSelf()->once();
2019-06-21 19:10:02 +02:00
$collector->shouldReceive('getSum')->atLeast()->once()->andReturn('100');
2018-03-03 14:24:06 +01:00
// mock all repositories:
2019-06-21 19:10:02 +02:00
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('findNull')->withArgs([$one->id])->andReturn($one)->atLeast()->once();
$accountRepos->shouldReceive('findNull')->withArgs([$two->id])->andReturn($two)->atLeast()->once();
2018-03-03 14:24:06 +01:00
2019-06-21 19:10:02 +02:00
/** @var MetaPieChart $helper */
$helper = app(MetaPieChart::class);
2018-03-03 14:24:06 +01:00
$helper->setUser($this->user());
$helper->setStart($som);
$helper->setEnd($eom);
2019-06-21 19:10:02 +02:00
$helper->setCollectOtherObjects(true);
2018-03-03 14:24:06 +01:00
$chart = $helper->generate('expense', 'account');
// since the set is pretty basic the result is easy to validate:
$keys = array_keys($chart);
2019-06-21 19:10:02 +02:00
$this->assertContains($keys[0], [$one->name, $two->name]);
$this->assertContains($keys[1], [$one->name, $two->name]);
$this->assertEquals('10.000000000000', $chart[$one->name]);
$this->assertEquals('10.000000000000', $chart[$two->name]);
2018-03-03 14:24:06 +01:00
$this->assertTrue(true);
}
/**
* @covers \FireflyIII\Helpers\Chart\MetaPieChart
2017-04-28 20:17:10 +02:00
*/
2019-06-21 19:10:02 +02:00
public function testGenerateIncomeAccountOthers(): void
2017-04-28 20:17:10 +02:00
{
2019-06-21 19:10:02 +02:00
$som = (new Carbon())->startOfMonth();
$eom = (new Carbon())->endOfMonth();
$one = $this->getRandomAsset();
$two = $this->getRandomAsset($one->id);
$array = [
[
'destination_account_id' => $one->id,
'budget_id' => 1,
'category_id' => 1,
'amount' => '10',
],
[
'destination_account_id' => $two->id,
'budget_id' => 2,
'category_id' => 2,
'amount' => '10',
],
2017-05-02 20:54:49 +02:00
];
2019-06-21 19:10:02 +02:00
$collector = $this->mock(GroupCollectorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
// mock collector so the correct set of journals is returned:
// then verify the results.
2019-06-21 19:10:02 +02:00
$collector->shouldReceive('setUser')->andReturnSelf()->twice();
$collector->shouldReceive('setAccounts')->andReturnSelf()->twice();
$collector->shouldReceive('setRange')->andReturnSelf()->twice();
2017-09-25 07:01:52 +02:00
$collector->shouldReceive('setBudgets')->andReturnSelf()->once();
$collector->shouldReceive('setCategories')->andReturnSelf()->once();
2019-06-21 19:10:02 +02:00
2017-05-02 20:54:49 +02:00
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT, TransactionType::TRANSFER]])->andReturnSelf()->once();
2019-06-21 19:10:02 +02:00
$collector->shouldReceive('getExtractedJournals')->andReturn($array)->atLeast()->once();
$collector->shouldReceive('withAccountInformation')->andReturnSelf()->once();
2017-05-02 20:54:49 +02:00
2019-06-21 19:10:02 +02:00
// also collect others:
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT]])->andReturnSelf()->once();
$collector->shouldReceive('getSum')->atLeast()->once()->andReturn('100');
2017-05-02 20:54:49 +02:00
2019-06-21 19:10:02 +02:00
// mock all repositories:
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('findNull')->withArgs([$one->id])->andReturn($one)->atLeast()->once();
$accountRepos->shouldReceive('findNull')->withArgs([$two->id])->andReturn($two)->atLeast()->once();
2017-05-02 20:54:49 +02:00
2019-06-21 19:10:02 +02:00
/** @var MetaPieChart $helper */
$helper = app(MetaPieChart::class);
2017-05-02 20:54:49 +02:00
$helper->setUser($this->user());
$helper->setStart($som);
$helper->setEnd($eom);
2019-06-21 19:10:02 +02:00
$helper->setCollectOtherObjects(true);
2017-05-02 20:54:49 +02:00
$chart = $helper->generate('income', 'account');
// since the set is pretty basic the result is easy to validate:
$keys = array_keys($chart);
2019-06-21 19:10:02 +02:00
$this->assertContains($keys[0], [$one->name, $two->name]);
$this->assertContains($keys[1], [$one->name, $two->name]);
$this->assertEquals('10.000000000000', $chart[$one->name]);
$this->assertEquals('10.000000000000', $chart[$two->name]);
2017-05-02 20:54:49 +02:00
$this->assertTrue(true);
}
/**
2018-03-03 14:24:06 +01:00
* @covers \FireflyIII\Helpers\Chart\MetaPieChart
2017-05-02 20:54:49 +02:00
*/
2019-06-21 19:10:02 +02:00
public function testGenerateIncomeAccount(): void
2017-05-02 20:54:49 +02:00
{
2019-06-21 19:10:02 +02:00
$som = (new Carbon())->startOfMonth();
$eom = (new Carbon())->endOfMonth();
$one = $this->getRandomAsset();
$two = $this->getRandomAsset($one->id);
$array = [
[
'destination_account_id' => $one->id,
'budget_id' => 1,
'category_id' => 1,
'amount' => '10',
],
[
'destination_account_id' => $two->id,
'budget_id' => 2,
'category_id' => 2,
'amount' => '10',
],
2017-05-02 20:54:49 +02:00
];
2019-06-21 19:10:02 +02:00
$collector = $this->mock(GroupCollectorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2017-05-02 20:54:49 +02:00
// mock collector so the correct set of journals is returned:
// then verify the results.
2019-06-21 19:10:02 +02:00
$collector->shouldReceive('setUser')->andReturnSelf()->once();
$collector->shouldReceive('setAccounts')->andReturnSelf()->once();
$collector->shouldReceive('setRange')->andReturnSelf()->once();
2017-09-25 07:01:52 +02:00
$collector->shouldReceive('setBudgets')->andReturnSelf()->once();
$collector->shouldReceive('setCategories')->andReturnSelf()->once();
2017-05-02 20:54:49 +02:00
2019-06-21 19:10:02 +02:00
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT, TransactionType::TRANSFER]])->andReturnSelf()->once();
$collector->shouldReceive('getExtractedJournals')->andReturn($array)->atLeast()->once();
$collector->shouldReceive('withAccountInformation')->andReturnSelf()->once();
2017-05-02 20:54:49 +02:00
// mock all repositories:
2019-06-21 19:10:02 +02:00
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('findNull')->withArgs([$one->id])->andReturn($one)->atLeast()->once();
$accountRepos->shouldReceive('findNull')->withArgs([$two->id])->andReturn($two)->atLeast()->once();
2017-05-02 20:54:49 +02:00
2019-06-21 19:10:02 +02:00
/** @var MetaPieChart $helper */
$helper = app(MetaPieChart::class);
2017-04-28 20:17:10 +02:00
$helper->setUser($this->user());
$helper->setStart($som);
$helper->setEnd($eom);
$chart = $helper->generate('income', 'account');
2017-05-02 20:54:49 +02:00
// since the set is pretty basic the result is easy to validate:
$keys = array_keys($chart);
2019-06-21 19:10:02 +02:00
$this->assertContains($keys[0], [$one->name, $two->name]);
$this->assertContains($keys[1], [$one->name, $two->name]);
$this->assertEquals('10.000000000000', $chart[$one->name]);
$this->assertEquals('10.000000000000', $chart[$two->name]);
2017-05-02 20:54:49 +02:00
2017-04-28 20:17:10 +02:00
$this->assertTrue(true);
2017-05-02 20:54:49 +02:00
}
2017-07-07 08:09:42 +02:00
}