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

400 lines
19 KiB
PHP
Raw Normal View History

2018-02-16 22:14:34 +01:00
<?php
/**
* AccountTransformerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Unit\Transformers;
use Carbon\Carbon;
2018-12-18 19:57:23 +01:00
use FireflyIII\Models\TransactionCurrency;
2018-02-28 20:18:47 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-02-16 22:14:34 +01:00
use FireflyIII\Transformers\AccountTransformer;
use Log;
2018-03-23 05:31:45 +01:00
use Mockery;
2018-12-18 19:57:23 +01:00
use Steam;
2018-02-16 22:14:34 +01:00
use Symfony\Component\HttpFoundation\ParameterBag;
use Tests\TestCase;
/**
* Class AccountTransformerTest
*/
class AccountTransformerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', \get_class($this)));
}
2018-02-16 22:14:34 +01:00
/**
2018-12-18 19:57:23 +01:00
* Check balance on a different date.
2018-02-16 22:14:34 +01:00
*
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Transformers\AccountTransformer
2018-02-16 22:14:34 +01:00
*/
2018-12-18 19:57:23 +01:00
public function testBalanceDate(): void
2018-02-16 22:14:34 +01:00
{
2018-12-18 19:57:23 +01:00
// mock stuff and get object:
$account = $this->getRandomAsset();
$euro = TransactionCurrency::find(1);
2018-02-28 20:18:47 +01:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-02-16 22:14:34 +01:00
2018-12-18 19:57:23 +01:00
$parameters = new ParameterBag;
$parameters->set('date', new Carbon('2018-01-01'));
2018-12-18 19:57:23 +01:00
$transformer = app(AccountTransformer::class);
$transformer->setParameters($parameters);
2018-12-18 19:57:23 +01: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 19:57:23 +01:00
// get all kinds of meta values:
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('defaultAsset')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('12345')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('NL5X')->atLeast()->once();
2018-02-16 22:14:34 +01:00
2018-12-18 19:57:23 +01:00
// opening balance:
$accountRepos->shouldReceive('getOpeningBalanceAmount')->withArgs([Mockery::any()])->andReturnNull()->atLeast()->once();
$accountRepos->shouldReceive('getOpeningBalanceDate')->withArgs([Mockery::any()])->andReturnNull()->atLeast()->once();
2018-02-16 22:14:34 +01:00
2018-12-18 19:57:23 +01:00
// steam is also called for the account balance:
Steam::shouldReceive('balance')->andReturn('123.45')->atLeast()->once();
2018-02-16 22:14:34 +01:00
2018-12-18 19:57:23 +01:00
$result = $transformer->transform($account);
// verify all fields.
$this->assertEquals($account->id, $result['id']);
$this->assertEquals($account->active, $result['active']);
2018-02-16 22:14:34 +01:00
$this->assertEquals($account->name, $result['name']);
2018-12-18 19:57:23 +01:00
$this->assertEquals('asset', $result['type']);
$this->assertEquals('defaultAsset', $result['account_role']);
2018-02-16 22:14:34 +01:00
$this->assertEquals(1, $result['currency_id']);
$this->assertEquals('EUR', $result['currency_code']);
2018-12-18 19:57:23 +01: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 22:14:34 +01:00
}
/**
2018-12-18 19:57:23 +01:00
* Load a basic asset account, and verify the result in the transformer.
2018-02-16 22:14:34 +01:00
*
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Transformers\AccountTransformer
2018-02-16 22:14:34 +01:00
*/
2018-12-18 19:57:23 +01:00
public function testBasicAsset(): void
2018-02-16 22:14:34 +01:00
{
2018-12-18 19:57:23 +01:00
// mock stuff and get object:
$account = $this->getRandomAsset();
$euro = TransactionCurrency::find(1);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-03-23 05:31:30 +01:00
2018-12-18 19:57:23 +01:00
$transformer = app(AccountTransformer::class);
$transformer->setParameters(new ParameterBag);
2018-02-16 22:14:34 +01:00
2018-12-18 19:57:23 +01: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:
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('defaultAsset')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('12345')->atLeast()->once();
$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 22:14:34 +01:00
2018-12-18 19:57:23 +01:00
$result = $transformer->transform($account);
// verify all fields.
$this->assertEquals($account->id, $result['id']);
$this->assertEquals($account->active, $result['active']);
2018-02-16 22:14:34 +01:00
$this->assertEquals($account->name, $result['name']);
2018-12-18 19:57:23 +01:00
$this->assertEquals('asset', $result['type']);
$this->assertEquals('defaultAsset', $result['account_role']);
2018-02-16 22:14:34 +01:00
$this->assertEquals(1, $result['currency_id']);
$this->assertEquals('EUR', $result['currency_code']);
2018-12-18 19:57:23 +01: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 22:14:34 +01:00
$this->assertNull($result['monthly_payment_date']);
$this->assertNull($result['credit_card_type']);
2018-12-18 19:57:23 +01: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 22:14:34 +01:00
}
/**
2018-12-18 19:57:23 +01:00
* Credit card asset has some extra fields
2018-02-16 22:14:34 +01:00
*
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Transformers\AccountTransformer
2018-02-16 22:14:34 +01:00
*/
2018-12-18 19:57:23 +01:00
public function testCreditCardAsset(): void
2018-02-16 22:14:34 +01:00
{
2018-12-18 19:57:23 +01:00
// mock stuff and get object:
$account = $this->getRandomAsset();
$euro = TransactionCurrency::find(1);
2018-02-28 20:18:47 +01:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-02-16 22:14:34 +01:00
2018-12-18 19:57:23 +01: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:
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('ccAsset')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('12345')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('NL5X')->atLeast()->once();
// credit card fields:
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'ccType'])->andReturn('monthlyFull')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'ccMonthlyPaymentDate'])->andReturn('2018-01-01')->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();
$result = $transformer->transform($account);
// verify all fields.
$this->assertEquals($account->id, $result['id']);
$this->assertEquals($account->active, $result['active']);
2018-02-16 22:14:34 +01:00
$this->assertEquals($account->name, $result['name']);
2018-12-18 19:57:23 +01: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 22:14:34 +01:00
}
/**
2018-12-18 19:57:23 +01:00
* Liability also has some extra fields.
2018-02-16 22:14:34 +01:00
*
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Transformers\AccountTransformer
2018-02-16 22:14:34 +01:00
*/
2018-12-18 19:57:23 +01:00
public function testLiability(): void
2018-02-16 22:14:34 +01:00
{
2018-12-18 19:57:23 +01:00
// mock stuff and get object:
$account = $this->getRandomAsset();
$euro = TransactionCurrency::find(1);
2018-02-28 20:18:47 +01:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-02-16 22:14:34 +01:00
2018-12-18 19:57:23 +01: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:
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('12345')->atLeast()->once();
$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 22:14:34 +01:00
$this->assertEquals($account->name, $result['name']);
2018-12-18 19:57:23 +01:00
$this->assertEquals('liabilities', $result['type']);
$this->assertNull($result['account_role']);
2018-02-16 22:14:34 +01:00
$this->assertEquals(1, $result['currency_id']);
$this->assertEquals('EUR', $result['currency_code']);
2018-12-18 19:57:23 +01: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 22:14:34 +01:00
}
/**
2018-12-18 19:57:23 +01:00
* If the account is not an asset account, the role must always be NULL.
2018-02-16 22:14:34 +01:00
*
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Transformers\AccountTransformer
2018-02-16 22:14:34 +01:00
*/
2018-12-18 19:57:23 +01:00
public function testRoleEmpty(): void
2018-02-16 22:14:34 +01:00
{
2018-12-18 19:57:23 +01:00
// mock stuff and get object:
$account = $this->getRandomExpense();
$euro = TransactionCurrency::find(1);
2018-03-23 05:31:45 +01:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-12-18 19:57:23 +01: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:
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('defaultAsset')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn('1')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('12345')->atLeast()->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('NL5X')->atLeast()->once();
2018-03-23 05:31:45 +01:00
2018-12-18 19:57:23 +01:00
// steam is also called for the account balance:
Steam::shouldReceive('balance')->andReturn('123.45')->atLeast()->once();
2018-03-23 05:31:45 +01:00
2018-02-16 22:14:34 +01:00
2018-12-18 19:57:23 +01:00
$result = $transformer->transform($account);
// verify all fields.
$this->assertEquals($account->id, $result['id']);
$this->assertEquals($account->active, $result['active']);
2018-02-16 22:14:34 +01:00
$this->assertEquals($account->name, $result['name']);
2018-12-18 19:57:23 +01:00
$this->assertEquals('expense', $result['type']);
$this->assertNull($result['account_role']);
2018-02-16 22:14:34 +01:00
$this->assertEquals(1, $result['currency_id']);
$this->assertEquals('EUR', $result['currency_code']);
2018-12-18 19:57:23 +01:00
$this->assertEquals('€', $result['currency_symbol']);
$this->assertEquals(2, $result['currency_decimal_places']);
2018-02-16 22:14:34 +01:00
2018-12-18 19:57:23 +01: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 22:14:34 +01:00
2018-12-18 19:57:23 +01: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 15:14:29 +01:00
}