firefly-iii/tests/Unit/Transformers/AccountTransformerTest.php

404 lines
19 KiB
PHP
Raw Normal View History

2018-02-16 15:14:34 -06:00
<?php
/**
* AccountTransformerTest.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
2018-02-16 15:14:34 -06:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-02-16 15:14:34 -06:00
*
* 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.
2018-02-16 15:14:34 -06:00
*
* This program is distributed in the hope that it will be useful,
2018-02-16 15:14:34 -06:00
* 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.
2018-02-16 15:14:34 -06:00
*
* 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/>.
2018-02-16 15:14:34 -06:00
*/
declare(strict_types=1);
namespace Tests\Unit\Transformers;
use Carbon\Carbon;
2018-12-18 12:57:23 -06:00
use FireflyIII\Models\TransactionCurrency;
2018-02-28 13:18:47 -06:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-02-16 15:14:34 -06:00
use FireflyIII\Transformers\AccountTransformer;
use Log;
2018-03-22 23:31:45 -05:00
use Mockery;
2018-12-18 12:57:23 -06:00
use Steam;
2018-02-16 15:14:34 -06:00
use Symfony\Component\HttpFoundation\ParameterBag;
use Tests\TestCase;
/**
* Class AccountTransformerTest
2019-08-17 03:48:28 -05:00
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-02-16 15:14:34 -06:00
*/
class AccountTransformerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
2019-04-09 13:05:20 -05:00
Log::info(sprintf('Now in %s.', get_class($this)));
}
2018-02-16 15:14:34 -06:00
/**
2018-12-18 12:57:23 -06:00
* Check balance on a different date.
2018-02-16 15:14:34 -06:00
*
2018-08-09 13:17:15 -05:00
* @covers \FireflyIII\Transformers\AccountTransformer
2018-02-16 15:14:34 -06:00
*/
2018-12-18 12:57:23 -06:00
public function testBalanceDate(): void
2018-02-16 15:14:34 -06:00
{
2018-12-18 12:57:23 -06:00
// mock stuff and get object:
$account = $this->getRandomAsset();
2019-08-02 21:46:47 -05:00
$euro = $this->getEuro();
2018-02-28 13:18:47 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-02-16 15:14:34 -06:00
2018-12-18 12:57:23 -06:00
$parameters = new ParameterBag;
$parameters->set('date', new Carbon('2018-01-01'));
2018-12-18 12:57:23 -06:00
$transformer = app(AccountTransformer::class);
$transformer->setParameters($parameters);
2018-12-18 12:57:23 -06:00
// following calls are expected:
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getAccountType')->andReturn('Asset account')->atLeast()->once();
$accountRepos->shouldReceive('getAccountCurrency')->andReturn($euro)->atLeast()->once();
$accountRepos->shouldReceive('getNoteText')->andReturn('I am a note')->atLeast()->once();
2018-12-18 12:57:23 -06:00
// get all kinds of meta values:
2019-08-02 21:46:47 -05:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_role'])->andReturn('defaultAsset')->atLeast()->once();
2018-12-18 12:57:23 -06:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1')->atLeast()->once();
2019-08-02 21:46:47 -05:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_number'])->andReturn('12345')->atLeast()->once();
2018-12-18 12:57:23 -06:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('NL5X')->atLeast()->once();
2018-02-16 15:14:34 -06:00
2018-12-18 12:57:23 -06:00
// opening balance:
$accountRepos->shouldReceive('getOpeningBalanceAmount')->withArgs([Mockery::any()])->andReturnNull()->atLeast()->once();
$accountRepos->shouldReceive('getOpeningBalanceDate')->withArgs([Mockery::any()])->andReturnNull()->atLeast()->once();
2018-02-16 15:14:34 -06:00
2018-12-18 12:57:23 -06:00
// steam is also called for the account balance:
Steam::shouldReceive('balance')->andReturn('123.45')->atLeast()->once();
2018-02-16 15:14:34 -06:00
2018-12-18 12:57:23 -06:00
$result = $transformer->transform($account);
// verify all fields.
$this->assertEquals($account->id, $result['id']);
$this->assertEquals($account->active, $result['active']);
2018-02-16 15:14:34 -06:00
$this->assertEquals($account->name, $result['name']);
2018-12-18 12:57:23 -06:00
$this->assertEquals('asset', $result['type']);
$this->assertEquals('defaultAsset', $result['account_role']);
2018-02-16 15:14:34 -06:00
$this->assertEquals(1, $result['currency_id']);
$this->assertEquals('EUR', $result['currency_code']);
2018-12-18 12:57:23 -06:00
$this->assertEquals('€', $result['currency_symbol']);
$this->assertEquals(2, $result['currency_decimal_places']);
// date given, so it must match.
$this->assertEquals('2018-01-01', $result['current_balance_date']);
$this->assertEquals(123.45, $result['current_balance']);
$this->assertEquals('I am a note', $result['notes']);
$this->assertNull($result['monthly_payment_date']);
$this->assertNull($result['credit_card_type']);
$this->assertEquals('12345', $result['account_number']);
$this->assertEquals($account->iban, $result['iban']);
$this->assertEquals('NL5X', $result['bic']);
$this->assertNull($result['liability_type']);
$this->assertNull($result['liability_amount']);
$this->assertNull($result['liability_start_date']);
$this->assertNull($result['interest']);
$this->assertNull($result['interest_period']);
$this->assertTrue($result['include_net_worth']);
2018-02-16 15:14:34 -06:00
}
/**
2018-12-18 12:57:23 -06:00
* Load a basic asset account, and verify the result in the transformer.
2018-02-16 15:14:34 -06:00
*
2018-08-09 13:17:15 -05:00
* @covers \FireflyIII\Transformers\AccountTransformer
2018-02-16 15:14:34 -06:00
*/
2018-12-18 12:57:23 -06:00
public function testBasicAsset(): void
2018-02-16 15:14:34 -06:00
{
2018-12-18 12:57:23 -06:00
// mock stuff and get object:
$account = $this->getRandomAsset();
2019-08-02 21:46:47 -05:00
$euro = $this->getEuro();
2018-12-18 12:57:23 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-03-22 23:31:30 -05:00
2018-12-18 12:57:23 -06:00
$transformer = app(AccountTransformer::class);
$transformer->setParameters(new ParameterBag);
2018-02-16 15:14:34 -06:00
2018-12-18 12:57:23 -06:00
// following calls are expected:
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getAccountType')->andReturn('Asset account')->atLeast()->once();
$accountRepos->shouldReceive('getAccountCurrency')->andReturn($euro)->atLeast()->once();
$accountRepos->shouldReceive('getNoteText')->andReturn('I am a note')->atLeast()->once();
// get all kinds of meta values:
2019-08-02 21:46:47 -05:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_role'])->andReturn('defaultAsset')->atLeast()->once();
2018-12-18 12:57:23 -06:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1')->atLeast()->once();
2019-08-02 21:46:47 -05:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_number'])->andReturn('12345')->atLeast()->once();
2018-12-18 12:57:23 -06:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('NL5X')->atLeast()->once();
// opening balance:
$accountRepos->shouldReceive('getOpeningBalanceAmount')->withArgs([Mockery::any()])->andReturnNull()->atLeast()->once();
$accountRepos->shouldReceive('getOpeningBalanceDate')->withArgs([Mockery::any()])->andReturnNull()->atLeast()->once();
// steam is also called for the account balance:
Steam::shouldReceive('balance')->andReturn('123.45')->atLeast()->once();
2018-02-16 15:14:34 -06:00
2018-12-18 12:57:23 -06:00
$result = $transformer->transform($account);
// verify all fields.
$this->assertEquals($account->id, $result['id']);
$this->assertEquals($account->active, $result['active']);
2018-02-16 15:14:34 -06:00
$this->assertEquals($account->name, $result['name']);
2018-12-18 12:57:23 -06:00
$this->assertEquals('asset', $result['type']);
$this->assertEquals('defaultAsset', $result['account_role']);
2018-02-16 15:14:34 -06:00
$this->assertEquals(1, $result['currency_id']);
$this->assertEquals('EUR', $result['currency_code']);
2018-12-18 12:57:23 -06:00
$this->assertEquals('€', $result['currency_symbol']);
$this->assertEquals(2, $result['currency_decimal_places']);
// no date given, so must be today:
$this->assertEquals(date('Y-m-d'), $result['current_balance_date']);
$this->assertEquals(123.45, $result['current_balance']);
$this->assertEquals('I am a note', $result['notes']);
2018-02-16 15:14:34 -06:00
$this->assertNull($result['monthly_payment_date']);
$this->assertNull($result['credit_card_type']);
2018-12-18 12:57:23 -06:00
$this->assertEquals('12345', $result['account_number']);
$this->assertEquals($account->iban, $result['iban']);
$this->assertEquals('NL5X', $result['bic']);
$this->assertNull($result['liability_type']);
$this->assertNull($result['liability_amount']);
$this->assertNull($result['liability_start_date']);
$this->assertNull($result['interest']);
$this->assertNull($result['interest_period']);
$this->assertTrue($result['include_net_worth']);
2018-02-16 15:14:34 -06:00
}
/**
2018-12-18 12:57:23 -06:00
* Credit card asset has some extra fields
2018-02-16 15:14:34 -06:00
*
2018-08-09 13:17:15 -05:00
* @covers \FireflyIII\Transformers\AccountTransformer
2018-02-16 15:14:34 -06:00
*/
2018-12-18 12:57:23 -06:00
public function testCreditCardAsset(): void
2018-02-16 15:14:34 -06:00
{
2018-12-18 12:57:23 -06:00
// mock stuff and get object:
$account = $this->getRandomAsset();
2019-08-02 21:46:47 -05:00
$euro = $this->getEuro();
2018-02-28 13:18:47 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-02-16 15:14:34 -06:00
2018-12-18 12:57:23 -06:00
$transformer = app(AccountTransformer::class);
$transformer->setParameters(new ParameterBag);
// following calls are expected:
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getAccountType')->andReturn('Asset account')->atLeast()->once();
$accountRepos->shouldReceive('getAccountCurrency')->andReturn($euro)->atLeast()->once();
$accountRepos->shouldReceive('getNoteText')->andReturn('I am a note')->atLeast()->once();
// get all kinds of meta values:
2019-08-02 21:46:47 -05:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_role'])->andReturn('ccAsset')->atLeast()->once();
2018-12-18 12:57:23 -06:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1')->atLeast()->once();
2019-08-02 21:46:47 -05:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_number'])->andReturn('12345')->atLeast()->once();
2018-12-18 12:57:23 -06:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('NL5X')->atLeast()->once();
// credit card fields:
2019-08-02 21:46:47 -05:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'cc_type'])->andReturn('monthlyFull')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'cc_monthly_payment_date'])->andReturn('2018-01-01')->atLeast()->once();
2018-12-18 12:57:23 -06:00
// opening balance:
$accountRepos->shouldReceive('getOpeningBalanceAmount')->withArgs([Mockery::any()])->andReturnNull()->atLeast()->once();
$accountRepos->shouldReceive('getOpeningBalanceDate')->withArgs([Mockery::any()])->andReturnNull()->atLeast()->once();
// steam is also called for the account balance:
Steam::shouldReceive('balance')->andReturn('123.45')->atLeast()->once();
$result = $transformer->transform($account);
// verify all fields.
$this->assertEquals($account->id, $result['id']);
$this->assertEquals($account->active, $result['active']);
2018-02-16 15:14:34 -06:00
$this->assertEquals($account->name, $result['name']);
2018-12-18 12:57:23 -06:00
$this->assertEquals('asset', $result['type']);
$this->assertEquals('ccAsset', $result['account_role']);
$this->assertEquals(1, $result['currency_id']);
$this->assertEquals('EUR', $result['currency_code']);
$this->assertEquals('€', $result['currency_symbol']);
$this->assertEquals(2, $result['currency_decimal_places']);
// no date given, so must be today:
$this->assertEquals(date('Y-m-d'), $result['current_balance_date']);
$this->assertEquals(123.45, $result['current_balance']);
$this->assertEquals('I am a note', $result['notes']);
// cc fields must be filled in:
$this->assertEquals('2018-01-01', $result['monthly_payment_date']);
$this->assertEquals('monthlyFull', $result['credit_card_type']);
$this->assertEquals('12345', $result['account_number']);
$this->assertEquals($account->iban, $result['iban']);
$this->assertEquals('NL5X', $result['bic']);
$this->assertNull($result['liability_type']);
$this->assertNull($result['liability_amount']);
$this->assertNull($result['liability_start_date']);
$this->assertNull($result['interest']);
$this->assertNull($result['interest_period']);
$this->assertTrue($result['include_net_worth']);
2018-02-16 15:14:34 -06:00
}
/**
2018-12-18 12:57:23 -06:00
* Liability also has some extra fields.
2018-02-16 15:14:34 -06:00
*
2018-08-09 13:17:15 -05:00
* @covers \FireflyIII\Transformers\AccountTransformer
2018-02-16 15:14:34 -06:00
*/
2018-12-18 12:57:23 -06:00
public function testLiability(): void
2018-02-16 15:14:34 -06:00
{
2018-12-18 12:57:23 -06:00
// mock stuff and get object:
$account = $this->getRandomAsset();
2019-08-02 21:46:47 -05:00
$euro = $this->getEuro();
2018-02-28 13:18:47 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-02-16 15:14:34 -06:00
2018-12-18 12:57:23 -06:00
$transformer = app(AccountTransformer::class);
$transformer->setParameters(new ParameterBag);
// following calls are expected:
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getAccountType')->andReturn('Mortgage')->atLeast()->once();
$accountRepos->shouldReceive('getAccountCurrency')->andReturn($euro)->atLeast()->once();
$accountRepos->shouldReceive('getNoteText')->andReturn('I am a note')->atLeast()->once();
// get all kinds of meta values:
2019-08-02 21:46:47 -05:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_role'])->andReturn('')->atLeast()->once();
2018-12-18 12:57:23 -06:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1')->atLeast()->once();
2019-08-02 21:46:47 -05:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_number'])->andReturn('12345')->atLeast()->once();
2018-12-18 12:57:23 -06:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('NL5X')->atLeast()->once();
// data for liability
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest'])->andReturn('3')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest_period'])->andReturn('monthly')->atLeast()->once();
// opening balance:
$accountRepos->shouldReceive('getOpeningBalanceAmount')->withArgs([Mockery::any()])->andReturn('-1000')->atLeast()->once();
$accountRepos->shouldReceive('getOpeningBalanceDate')->withArgs([Mockery::any()])->andReturn('2018-01-01')->atLeast()->once();
// steam is also called for the account balance:
Steam::shouldReceive('balance')->andReturn('123.45')->atLeast()->once();
$result = $transformer->transform($account);
// verify all fields.
$this->assertEquals($account->id, $result['id']);
$this->assertEquals($account->active, $result['active']);
2018-02-16 15:14:34 -06:00
$this->assertEquals($account->name, $result['name']);
2018-12-18 12:57:23 -06:00
$this->assertEquals('liabilities', $result['type']);
$this->assertNull($result['account_role']);
2018-02-16 15:14:34 -06:00
$this->assertEquals(1, $result['currency_id']);
$this->assertEquals('EUR', $result['currency_code']);
2018-12-18 12:57:23 -06:00
$this->assertEquals('€', $result['currency_symbol']);
$this->assertEquals(2, $result['currency_decimal_places']);
// no date given, so must be today:
$this->assertEquals(date('Y-m-d'), $result['current_balance_date']);
$this->assertEquals(123.45, $result['current_balance']);
$this->assertEquals('I am a note', $result['notes']);
$this->assertNull($result['monthly_payment_date']);
$this->assertNull($result['credit_card_type']);
$this->assertEquals('12345', $result['account_number']);
$this->assertEquals($account->iban, $result['iban']);
$this->assertEquals('NL5X', $result['bic']);
// liability fields
$this->assertEquals('mortgage', $result['liability_type']);
$this->assertEquals('-1000', $result['liability_amount']);
$this->assertEquals('2018-01-01', $result['liability_start_date']);
$this->assertEquals('3', $result['interest']);
$this->assertEquals('monthly', $result['interest_period']);
$this->assertTrue($result['include_net_worth']);
2018-02-16 15:14:34 -06:00
}
/**
2018-12-18 12:57:23 -06:00
* If the account is not an asset account, the role must always be NULL.
2018-02-16 15:14:34 -06:00
*
2018-08-09 13:17:15 -05:00
* @covers \FireflyIII\Transformers\AccountTransformer
2018-02-16 15:14:34 -06:00
*/
2018-12-18 12:57:23 -06:00
public function testRoleEmpty(): void
2018-02-16 15:14:34 -06:00
{
2018-12-18 12:57:23 -06:00
// mock stuff and get object:
$account = $this->getRandomExpense();
2019-08-02 21:46:47 -05:00
$euro = $this->getEuro();
2018-03-22 23:31:45 -05:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-12-18 12:57:23 -06:00
$transformer = app(AccountTransformer::class);
$transformer->setParameters(new ParameterBag);
// following calls are expected:
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('getAccountType')->andReturn('Expense account')->atLeast()->once();
$accountRepos->shouldReceive('getAccountCurrency')->andReturn($euro)->atLeast()->once();
$accountRepos->shouldReceive('getNoteText')->andReturn('I am a note')->atLeast()->once();
// get all kinds of meta values:
2019-08-02 21:46:47 -05:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_role'])->andReturn('defaultAsset')->atLeast()->once();
2018-12-18 12:57:23 -06:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1')->atLeast()->once();
2019-08-02 21:46:47 -05:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'account_number'])->andReturn('12345')->atLeast()->once();
2018-12-18 12:57:23 -06:00
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('NL5X')->atLeast()->once();
2018-03-22 23:31:45 -05:00
2018-12-18 12:57:23 -06:00
// steam is also called for the account balance:
Steam::shouldReceive('balance')->andReturn('123.45')->atLeast()->once();
2018-03-22 23:31:45 -05:00
2018-02-16 15:14:34 -06:00
2018-12-18 12:57:23 -06:00
$result = $transformer->transform($account);
// verify all fields.
$this->assertEquals($account->id, $result['id']);
$this->assertEquals($account->active, $result['active']);
2018-02-16 15:14:34 -06:00
$this->assertEquals($account->name, $result['name']);
2018-12-18 12:57:23 -06:00
$this->assertEquals('expense', $result['type']);
$this->assertNull($result['account_role']);
2018-02-16 15:14:34 -06:00
$this->assertEquals(1, $result['currency_id']);
$this->assertEquals('EUR', $result['currency_code']);
2018-12-18 12:57:23 -06:00
$this->assertEquals('€', $result['currency_symbol']);
$this->assertEquals(2, $result['currency_decimal_places']);
2018-02-16 15:14:34 -06:00
2018-12-18 12:57:23 -06:00
// no date given, so must be today:
$this->assertEquals(date('Y-m-d'), $result['current_balance_date']);
$this->assertEquals(123.45, $result['current_balance']);
2018-02-16 15:14:34 -06:00
2018-12-18 12:57:23 -06:00
$this->assertEquals('I am a note', $result['notes']);
$this->assertNull($result['monthly_payment_date']);
$this->assertNull($result['credit_card_type']);
$this->assertEquals('12345', $result['account_number']);
$this->assertEquals($account->iban, $result['iban']);
$this->assertEquals('NL5X', $result['bic']);
$this->assertNull($result['liability_type']);
$this->assertNull($result['liability_amount']);
$this->assertNull($result['liability_start_date']);
$this->assertNull($result['interest']);
$this->assertNull($result['interest_period']);
$this->assertTrue($result['include_net_worth']);
}
2018-03-04 08:14:29 -06:00
}