2018-03-01 20:54:50 +01:00
< ? php
/**
* TransactionFactoryTest . 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\Factory ;
2018-05-11 19:58:10 +02:00
use FireflyIII\Exceptions\FireflyException ;
2018-03-01 20:54:50 +01:00
use FireflyIII\Factory\TransactionFactory ;
2019-03-18 16:52:49 +01:00
use FireflyIII\Models\Account ;
2018-03-01 20:54:50 +01:00
use FireflyIII\Models\AccountType ;
use FireflyIII\Models\TransactionCurrency ;
use FireflyIII\Repositories\Account\AccountRepositoryInterface ;
2019-03-18 16:52:49 +01:00
use FireflyIII\Support\NullArrayObject ;
2018-08-24 07:18:33 +02:00
use Log ;
2018-03-01 20:54:50 +01:00
use Tests\TestCase ;
/**
* Class TransactionFactoryTest
*/
class TransactionFactoryTest extends TestCase
{
2018-08-24 07:18:33 +02:00
/**
*
*/
public function setUp () : void
{
parent :: setUp ();
2018-09-03 18:52:46 +02:00
Log :: info ( sprintf ( 'Now in %s.' , \get_class ( $this )));
2018-08-24 07:18:33 +02:00
}
2018-03-01 20:54:50 +01:00
/**
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testCreateBasic () : void
2018-03-01 20:54:50 +01:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// data used in calls.
$journal = $this -> getRandomWithdrawal ();
$account = $this -> getRandomAsset ();
$euro = TransactionCurrency :: whereCode ( 'EUR' ) -> first ();
$amount = '10' ;
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
2018-03-01 20:54:50 +01:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $journal );
$transaction = $factory -> create ( $account , $euro , $amount );
2018-03-01 20:54:50 +01:00
2019-03-18 16:52:49 +01:00
$this -> assertEquals ( $transaction -> account_id , $account -> id );
2018-03-01 20:54:50 +01:00
}
/**
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testCreateNull () : void
2018-03-01 20:54:50 +01:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// data used in calls.
$journal = $this -> getRandomWithdrawal ();
$account = new Account ;
$euro = TransactionCurrency :: whereCode ( 'EUR' ) -> first ();
$amount = '10' ;
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
2018-03-01 20:54:50 +01:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $journal );
$transaction = $factory -> create ( $account , $euro , $amount );
2018-03-01 20:54:50 +01:00
2019-03-18 16:52:49 +01:00
$this -> assertNull ( $transaction );
2018-03-01 20:54:50 +01:00
}
/**
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testCreatePair () : void
2018-03-01 20:54:50 +01:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// used objects
$withdrawal = $this -> getRandomWithdrawal ();
$asset = $this -> getRandomAsset ();
$expense = $this -> getRandomExpense ();
$currency = TransactionCurrency :: whereCode ( 'EUR' ) -> first ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
$accountRepos -> shouldReceive ( 'findNull' ) -> withArgs ([ 1 ]) -> once () -> andReturn ( $asset );
$accountRepos -> shouldReceive ( 'findByName' ) -> withArgs ([ 'Some destination' , [ AccountType :: EXPENSE ]]) -> once () -> andReturn ( $expense );
$data = new NullArrayObject (
[
'source_id' => 1 ,
'destination_name' => 'Some destination' ,
'amount' => '20' ,
]
);
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
$factory -> setJournal ( $withdrawal );
$pairs = $factory -> createPair ( $data , $currency , null );
$first = $pairs -> first ();
$this -> assertCount ( 2 , $pairs );
$this -> assertEquals ( '-20' , $first -> amount );
$this -> assertEquals ( $currency -> id , $first -> transaction_currency_id );
}
2018-03-01 20:54:50 +01:00
2019-03-18 16:52:49 +01:00
/**
* @ covers \FireflyIII\Factory\TransactionFactory
*/
public function testCreatePairForeign () : void
{
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// used objects:
$withdrawal = $this -> getRandomWithdrawal ();
$expense = $this -> getRandomExpense ();
$asset = $this -> getRandomAsset ();
$currency = TransactionCurrency :: whereCode ( 'EUR' ) -> first ();
$foreign = TransactionCurrency :: whereCode ( 'USD' ) -> first ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
$accountRepos -> shouldReceive ( 'findNull' ) -> withArgs ([ 1 ]) -> once () -> andReturn ( $asset );
$accountRepos -> shouldReceive ( 'findByName' ) -> withArgs ([ 'Some destination' , [ AccountType :: EXPENSE ]]) -> once () -> andReturn ( $expense );
$data = new NullArrayObject (
[
'source_id' => 1 ,
'destination_name' => 'Some destination' ,
'amount' => '20' ,
'foreign_amount' => '20' ,
]
);
2018-03-01 20:54:50 +01:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $withdrawal );
$pairs = $factory -> createPair ( $data , $currency , $foreign );
$first = $pairs -> first ();
$this -> assertCount ( 2 , $pairs );
$this -> assertEquals ( '-20' , $first -> amount );
$this -> assertEquals ( $currency -> id , $first -> transaction_currency_id );
$this -> assertEquals ( $foreign -> id , $first -> foreign_currency_id );
2018-03-01 20:54:50 +01:00
}
/**
2019-03-18 16:52:49 +01:00
* To cover everything , test several combinations .
*
* For the source account , submit a Deposit and an Revenue account ID ( this is OK ) .
* Expected result : the same revenue account .
2018-03-01 20:54:50 +01:00
*
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testDepositSourceAsseRevenueId () : void
2018-03-01 20:54:50 +01:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// data used in calls.
$deposit = $this -> getRandomDeposit ();
$revenue = $this -> getRandomRevenue ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
$accountRepos -> shouldReceive ( 'findNull' ) -> once () -> withArgs ([ $revenue -> id ]) -> andReturn ( $revenue );
2018-03-01 20:54:50 +01:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $deposit );
$result = $factory -> getAccount ( 'source' , null , $revenue -> id , null );
$this -> assertEquals ( $revenue -> id , $result -> id );
2018-03-01 20:54:50 +01:00
}
/**
2019-03-18 16:52:49 +01:00
* To cover everything , test several combinations .
*
* For the source account , submit a Deposit and nothing else ( this is OK ) .
* Expected result : a cash account
2018-03-01 20:54:50 +01:00
*
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testDepositSourceRevenueCash () : void
2018-03-01 20:54:50 +01:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// data used in calls.
$deposit = $this -> getRandomDeposit ();
$revenue = $this -> getRandomRevenue ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
$accountRepos -> shouldReceive ( 'getCashAccount' ) -> once () -> andReturn ( $revenue );
2018-03-01 20:54:50 +01:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $deposit );
$result = $factory -> getAccount ( 'source' , null , null , null );
$this -> assertEquals ( $revenue -> name , $result -> name );
2018-03-01 20:54:50 +01:00
}
/**
2019-03-18 16:52:49 +01:00
* To cover everything , test several combinations .
*
* For the source account , submit a Deposit and an Revenue account name ( this is OK ) .
* Expected result : a new revenue account .
2018-03-01 20:54:50 +01:00
*
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testDepositSourceRevenueNameNew () : void
2018-03-01 20:54:50 +01:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// data used in calls.
$deposit = $this -> getRandomDeposit ();
$name = 'random rev name ' . random_int ( 1 , 100000 );
$revenue = $this -> getRandomRevenue ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
$accountRepos -> shouldReceive ( 'findByName' ) -> once () -> withArgs ([ $name , [ AccountType :: REVENUE ]]) -> andReturnNull ();
// system will automatically expand search:
$accountRepos -> shouldReceive ( 'findByName' ) -> once () -> withArgs (
[ $name , [ AccountType :: REVENUE , AccountType :: CASH , AccountType :: LOAN , AccountType :: DEBT , AccountType :: MORTGAGE , AccountType :: INITIAL_BALANCE ,
AccountType :: RECONCILIATION ]]
) -> andReturnNull ();
// then store new account:
$accountRepos -> shouldReceive ( 'store' ) -> once () -> withArgs (
[[
'account_type_id' => null ,
'accountType' => AccountType :: REVENUE ,
'name' => $name ,
'active' => true ,
'iban' => null ,
]]
) -> andReturn ( $revenue );
2018-03-01 20:54:50 +01:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $deposit );
2018-03-01 20:54:50 +01:00
2019-03-18 16:52:49 +01:00
$result = $factory -> getAccount ( 'source' , null , null , $name );
$this -> assertEquals ( $revenue -> name , $result -> name );
2018-03-01 20:54:50 +01:00
}
/**
2019-03-18 16:52:49 +01:00
* @ throws FireflyException
*/
public function testDramaBasic () : void
{
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
$withdrawal = $this -> getRandomWithdrawal ();
$source = $withdrawal -> transactions () -> where ( 'amount' , '<' , 0 ) -> first ();
$dest = $withdrawal -> transactions () -> where ( 'amount' , '>' , 0 ) -> first ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
$factory -> setJournal ( $withdrawal );
$factory -> makeDramaOverAccountTypes ( $source -> account , $dest -> account );
}
/**
* @ throws FireflyException
2018-03-01 20:54:50 +01:00
*/
2019-03-18 16:52:49 +01:00
public function testDramaNotAllowed () : void
2018-03-01 20:54:50 +01:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
$withdrawal = $this -> getRandomWithdrawal ();
// this is an asset account.
$source = $withdrawal -> transactions () -> where ( 'amount' , '<' , 0 ) -> first ();
// so destiny cannot be also asset account
$dest = $this -> getRandomAsset ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
2018-03-01 20:54:50 +01:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $withdrawal );
2018-05-11 19:58:10 +02:00
try {
2019-03-18 16:52:49 +01:00
$factory -> makeDramaOverAccountTypes ( $source -> account , $dest );
2018-05-11 19:58:10 +02:00
} catch ( FireflyException $e ) {
2019-03-18 16:52:49 +01:00
$this -> assertEquals (
'Journal of type "Withdrawal" has a source account of type "Asset account" and cannot accept a "Asset account"-account as destination, but only accounts of: Expense account, Loan, Debt, Mortgage' ,
$e -> getMessage ()
);
2018-05-11 19:58:10 +02:00
}
2018-03-01 20:54:50 +01:00
}
2018-08-24 07:18:33 +02:00
/**
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testGetAmountBasic () : void
2018-08-24 07:18:33 +02:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
$amount = '10' ;
// data used in calls.
$journal = $this -> getRandomWithdrawal ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
2018-08-24 07:18:33 +02:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $journal );
2018-08-24 07:18:33 +02:00
2019-03-18 16:52:49 +01:00
$result = $factory -> getAmount ( $amount );
$this -> assertEquals ( $amount , $result );
2018-08-24 07:18:33 +02:00
}
2018-03-01 20:54:50 +01:00
/**
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testGetAmountNull () : void
2018-03-01 20:54:50 +01:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
$amount = '' ;
// data used in calls.
$journal = $this -> getRandomWithdrawal ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
2018-03-01 20:54:50 +01:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $journal );
2018-05-11 19:58:10 +02:00
try {
2019-03-18 16:52:49 +01:00
$factory -> getAmount ( $amount );
2018-05-11 19:58:10 +02:00
} catch ( FireflyException $e ) {
2019-03-18 16:52:49 +01:00
$this -> assertEquals ( 'The amount cannot be an empty string: ""' , $e -> getMessage ());
2018-05-11 19:58:10 +02:00
}
2018-03-01 20:54:50 +01:00
}
2018-08-24 07:18:33 +02:00
/**
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testGetAmountZero () : void
2018-08-24 07:18:33 +02:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
$amount = '0.0' ;
// data used in calls.
$journal = $this -> getRandomWithdrawal ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
2018-08-24 07:18:33 +02:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $journal );
2018-08-24 07:18:33 +02:00
try {
2019-03-18 16:52:49 +01:00
$factory -> getAmount ( $amount );
2018-08-24 07:18:33 +02:00
} catch ( FireflyException $e ) {
2019-03-18 16:52:49 +01:00
$this -> assertEquals ( 'The amount seems to be zero: "0.0"' , $e -> getMessage ());
2018-08-24 07:18:33 +02:00
}
2019-03-18 16:52:49 +01:00
}
/**
* @ covers \FireflyIII\Factory\TransactionFactory
*/
public function testGetForeignAmountBasic () : void
{
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
$amount = '10' ;
// data used in calls.
$journal = $this -> getRandomWithdrawal ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
2018-08-24 07:18:33 +02:00
2019-03-18 16:52:49 +01:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
$factory -> setJournal ( $journal );
2018-08-24 07:18:33 +02:00
2019-03-18 16:52:49 +01:00
$result = $factory -> getForeignAmount ( $amount );
$this -> assertEquals ( $amount , $result );
2018-08-24 07:18:33 +02:00
}
/**
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testGetForeignAmountEmpty () : void
2018-08-24 07:18:33 +02:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
$amount = '' ;
// data used in calls.
$journal = $this -> getRandomWithdrawal ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
2018-08-24 07:18:33 +02:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $journal );
$result = $factory -> getForeignAmount ( $amount );
$this -> assertNull ( $result );
}
/**
* @ covers \FireflyIII\Factory\TransactionFactory
*/
public function testGetForeignAmountNull () : void
{
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
$amount = null ;
// data used in calls.
$journal = $this -> getRandomWithdrawal ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
$factory -> setJournal ( $journal );
2018-08-24 07:18:33 +02:00
2019-03-18 16:52:49 +01:00
$result = $factory -> getForeignAmount ( $amount );
$this -> assertNull ( $result );
}
/**
* @ covers \FireflyIII\Factory\TransactionFactory
*/
public function testGetForeignAmountZero () : void
{
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
$amount = '0.0' ;
// data used in calls.
$journal = $this -> getRandomWithdrawal ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
$factory -> setJournal ( $journal );
2018-08-24 07:18:33 +02:00
2019-03-18 16:52:49 +01:00
$result = $factory -> getForeignAmount ( $amount );
$this -> assertNull ( $result );
2018-08-24 07:18:33 +02:00
}
2018-03-01 20:54:50 +01:00
/**
2019-03-18 16:52:49 +01:00
* To cover everything , test several combinations .
*
* For the source account , submit a Withdrawal and an Asset account ID ( this is OK ) .
* Expected result : the same asset account .
2018-03-01 20:54:50 +01:00
*
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testWithdrawalSourceAssetId () : void
2018-03-01 20:54:50 +01:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// data used in calls.
$withdrawal = $this -> getRandomWithdrawal ();
$asset = $this -> getRandomAsset ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
$accountRepos -> shouldReceive ( 'findNull' ) -> once () -> withArgs ([ $asset -> id ]) -> andReturn ( $asset );
2018-03-01 20:54:50 +01:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $withdrawal );
2018-03-01 20:54:50 +01:00
2019-03-18 16:52:49 +01:00
$result = $factory -> getAccount ( 'source' , null , $asset -> id , null );
$this -> assertEquals ( $asset -> id , $result -> id );
2018-03-01 20:54:50 +01:00
}
2018-08-24 07:18:33 +02:00
/**
2019-03-18 16:52:49 +01:00
* To cover everything , test several combinations .
*
* For the source account , submit a Withdrawal and an Asset account ID ( this is OK ) .
* Expected result : find won ' t return anything so we expect a big fat error .
*
2018-08-24 07:18:33 +02:00
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testWithdrawalSourceAssetIdNOK () : void
2018-08-24 07:18:33 +02:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// data used in calls.
$withdrawal = $this -> getRandomWithdrawal ();
$asset = $this -> getRandomAsset ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
$accountRepos -> shouldReceive ( 'findNull' ) -> once () -> withArgs ([ $asset -> id ]) -> andReturn ( $asset );
2018-08-24 07:18:33 +02:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $withdrawal );
2018-08-24 07:18:33 +02:00
try {
2019-03-18 16:52:49 +01:00
$factory -> getAccount ( 'source' , null , $asset -> id , null );
2018-08-24 07:18:33 +02:00
} catch ( FireflyException $e ) {
2019-03-18 16:52:49 +01:00
$this -> assertEquals ( 'TransactionFactory: Cannot create asset account with ID #0 or name "(no name)".' , $e -> getMessage ());
2018-08-24 07:18:33 +02:00
}
2019-03-18 16:52:49 +01:00
}
/**
* To cover everything , test several combinations .
*
* For the source account , submit a Withdrawal and an Asset account name ( this is OK ) .
* Expected result : the same asset account .
*
* @ covers \FireflyIII\Factory\TransactionFactory
*/
public function testWithdrawalSourceAssetName () : void
{
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// data used in calls.
$withdrawal = $this -> getRandomWithdrawal ();
$asset = $this -> getRandomAsset ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
$accountRepos -> shouldReceive ( 'findByName' ) -> once () -> withArgs ([ $asset -> name , [ AccountType :: ASSET ]]) -> andReturn ( $asset );
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
$factory -> setJournal ( $withdrawal );
2018-08-24 07:18:33 +02:00
2019-03-18 16:52:49 +01:00
$result = $factory -> getAccount ( 'source' , null , null , $asset -> name );
$this -> assertEquals ( $asset -> id , $result -> id );
2018-08-24 07:18:33 +02:00
}
/**
2019-03-18 16:52:49 +01:00
* To cover everything , test several combinations .
*
* For the source account , submit a Withdrawal and an Asset account name ( this is OK ) .
* Expected result : the same asset account .
*
* This will initially return NULL and then search again with all possible types .
*
2018-08-24 07:18:33 +02:00
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testWithdrawalSourceAssetName2 () : void
2018-08-24 07:18:33 +02:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// data used in calls.
$withdrawal = $this -> getRandomWithdrawal ();
$asset = $this -> getRandomAsset ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
$accountRepos -> shouldReceive ( 'findByName' ) -> once () -> withArgs ([ $asset -> name , [ AccountType :: ASSET ]]) -> andReturnNull ();
$accountRepos -> shouldReceive ( 'findByName' ) -> once () -> withArgs (
[ $asset -> name , [ AccountType :: ASSET , AccountType :: LOAN , AccountType :: DEBT , AccountType :: MORTGAGE ]]
) -> andReturn ( $asset );
2018-08-24 07:18:33 +02:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $withdrawal );
$result = $factory -> getAccount ( 'source' , null , null , $asset -> name );
$this -> assertEquals ( $asset -> id , $result -> id );
}
/**
* To cover everything , test several combinations .
*
* For the source account , submit a Withdrawal and an Asset account object ( this is OK ) .
* Expected result : the same asset account .
*
* @ covers \FireflyIII\Factory\TransactionFactory
*/
public function testWithdrawalSourceAssetObj () : void
{
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// data used in calls.
$withdrawal = $this -> getRandomWithdrawal ();
$asset = $this -> getRandomAsset ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
2018-08-24 07:18:33 +02:00
2019-03-18 16:52:49 +01:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
$factory -> setJournal ( $withdrawal );
$result = $factory -> getAccount ( 'source' , $asset , null , null );
$this -> assertEquals ( $asset -> id , $result -> id );
2018-08-24 07:18:33 +02:00
}
2018-03-01 20:54:50 +01:00
/**
2019-03-18 16:52:49 +01:00
* To cover everything , test several combinations .
*
* For the source account , submit a Withdrawal and an Expense account object ( this is not OK ) .
* Expected result : big fat error because of missing data .
2018-03-01 20:54:50 +01:00
*
* @ covers \FireflyIII\Factory\TransactionFactory
*/
2019-03-18 16:52:49 +01:00
public function testWithdrawalSourceAssetObjNOK () : void
2018-03-01 20:54:50 +01:00
{
2019-03-18 16:52:49 +01:00
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// data used in calls.
$withdrawal = $this -> getRandomWithdrawal ();
$expense = $this -> getRandomExpense ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
2018-03-01 20:54:50 +01:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
2019-03-18 16:52:49 +01:00
$factory -> setJournal ( $withdrawal );
2018-05-11 19:58:10 +02:00
try {
2019-03-18 16:52:49 +01:00
$factory -> getAccount ( 'source' , $expense , null , null );
2018-05-11 19:58:10 +02:00
} catch ( FireflyException $e ) {
2019-03-18 16:52:49 +01:00
$this -> assertEquals ( 'TransactionFactory: Cannot create asset account with ID #0 or name "(no name)".' , $e -> getMessage ());
2018-05-11 19:58:10 +02:00
}
2019-03-18 16:52:49 +01:00
}
/**
* To cover everything , test several combinations .
*
* For the source account , submit a Withdrawal and Loan account object ( this is OK ) .
* Expected result : the same loan account .
*
* @ covers \FireflyIII\Factory\TransactionFactory
*/
public function testWithdrawalSourceLoanObj () : void
{
// mock classes
$accountRepos = $this -> mock ( AccountRepositoryInterface :: class );
// data used in calls.
$withdrawal = $this -> getRandomWithdrawal ();
$loan = $this -> getRandomLoan ();
// mock calls.
$accountRepos -> shouldReceive ( 'setUser' ) -> once ();
2018-03-01 20:54:50 +01:00
2019-03-18 16:52:49 +01:00
/** @var TransactionFactory $factory */
$factory = app ( TransactionFactory :: class );
$factory -> setUser ( $this -> user ());
$factory -> setJournal ( $withdrawal );
$result = $factory -> getAccount ( 'source' , $loan , null , null );
$this -> assertEquals ( $loan -> id , $result -> id );
2018-03-01 20:54:50 +01:00
}
2019-03-18 16:52:49 +01:00
2018-03-04 15:14:29 +01:00
}