2018-05-05 06:53:12 -05:00
< ? php
/**
* ImportArrayStorageTest . 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 );
2018-05-25 16:13:08 -05:00
namespace Tests\Unit\Import\Storage ;
2018-05-05 06:53:12 -05:00
2019-07-26 10:48:24 -05:00
use Amount ;
2018-05-05 06:53:12 -05:00
use FireflyIII\Exceptions\FireflyException ;
2019-07-26 10:48:24 -05:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface ;
2018-05-05 06:53:12 -05:00
use FireflyIII\Import\Storage\ImportArrayStorage ;
use FireflyIII\Models\ImportJob ;
2019-07-26 10:48:24 -05:00
use FireflyIII\Models\Preference ;
use FireflyIII\Models\Transaction ;
2018-05-05 07:40:12 -05:00
use FireflyIII\Models\TransactionJournal ;
2018-05-05 06:53:12 -05:00
use FireflyIII\Models\TransactionJournalMeta ;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface ;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface ;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface ;
use FireflyIII\Repositories\Tag\TagRepositoryInterface ;
2019-07-26 10:48:24 -05:00
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface ;
2018-09-04 09:47:01 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface ;
2019-07-26 10:48:24 -05:00
use FireflyIII\TransactionRules\Processor ;
2018-05-05 06:53:12 -05:00
use Illuminate\Support\Collection ;
2018-09-04 09:47:01 -05:00
use Log ;
2018-05-05 06:53:12 -05:00
use Mockery ;
2019-07-26 10:48:24 -05:00
use Preferences ;
2018-05-05 06:53:12 -05:00
use Tests\TestCase ;
2018-09-04 09:47:01 -05:00
2018-05-05 06:53:12 -05:00
/**
* Class ImportArrayStorageTest
2019-08-17 03:48:28 -05:00
* @ SuppressWarnings ( PHPMD . CouplingBetweenObjects )
* @ SuppressWarnings ( PHPMD . ExcessiveMethodLength )
* @ SuppressWarnings ( PHPMD . TooManyPublicMethods )
2019-08-17 03:54:16 -05:00
* @ SuppressWarnings ( PHPMD . ExcessiveClassLength )
*
2018-05-05 06:53:12 -05:00
*/
class ImportArrayStorageTest extends TestCase
{
2018-09-04 02:52:19 -05:00
/**
*
*/
public function setUp () : void
{
parent :: setUp ();
2019-04-09 13:05:20 -05:00
Log :: info ( sprintf ( 'Now in %s.' , get_class ( $this )));
2018-09-04 02:52:19 -05:00
}
2018-05-05 06:53:12 -05:00
/**
* Very basic storage routine . Doesn ' t call store ()
*
* @ covers \FireflyIII\Import\Storage\ImportArrayStorage
*/
public function testBasic () : void
{
2019-07-26 10:48:24 -05:00
Log :: debug ( sprintf ( 'Now in test %s' , __METHOD__ ));
2018-09-04 09:47:01 -05:00
// mock stuff
2019-07-26 10:48:24 -05:00
$userRepos = $this -> mock ( UserRepositoryInterface :: class );
2018-09-04 09:47:01 -05:00
$repository = $this -> mock ( ImportJobRepositoryInterface :: class );
$journalRepos = $this -> mock ( JournalRepositoryInterface :: class );
2019-07-26 10:48:24 -05:00
$groupRepos = $this -> mock ( TransactionGroupRepositoryInterface :: class );
$this -> mock ( TagRepositoryInterface :: class );
$this -> mock ( Processor :: class );
$this -> mock ( RuleRepositoryInterface :: class );
$this -> mock ( GroupCollectorInterface :: class );
Amount :: shouldReceive ( 'something' );
$language = new Preference ;
$language -> data = 'en_US' ;
2019-08-10 06:42:33 -05:00
Preferences :: shouldReceive ( 'getForUser' ) -> withArgs ([ Mockery :: any (), 'language' , 'en_US' ]) -> andReturn ( $language ) -> atLeast () -> once ();
2019-07-26 10:48:24 -05:00
2018-05-05 06:53:12 -05:00
// make fake job
$job = new ImportJob ;
$job -> user () -> associate ( $this -> user ());
2019-07-26 10:48:24 -05:00
$job -> key = 'a_storage' . $this -> randomInt ();
2018-05-05 06:53:12 -05:00
$job -> status = 'new' ;
$job -> stage = 'new' ;
$job -> provider = 'fake' ;
$job -> file_type = '' ;
$job -> configuration = [];
$job -> transactions = [];
$job -> save ();
2019-07-26 10:48:24 -05:00
// mock user calls
$repository -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$journalRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$groupRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
// mock other calls.
$repository -> shouldReceive ( 'getTransactions' ) -> atLeast () -> once () -> andReturn ([]);
Preferences :: shouldReceive ( 'mark' ) -> atLeast () -> once () -> withNoArgs ();
$userRepos -> shouldReceive ( 'findNull' ) -> atLeast () -> once () -> andReturn ( $this -> user ());
// status changes of the job.
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'storing_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'stored_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linking_to_tag' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linked_to_tag' ]);
2018-05-05 06:53:12 -05:00
$storage = new ImportArrayStorage ;
2018-05-12 08:50:01 -05:00
$storage -> setImportJob ( $job );
2019-07-26 10:48:24 -05:00
try {
$storage -> store ();
} catch ( FireflyException $e ) {
$this -> assertTrue ( false , $e -> getMessage ());
}
2018-05-05 06:53:12 -05:00
}
2018-06-02 13:02:42 -05:00
/**
2019-07-26 10:48:24 -05:00
* Submit a transfer . Mark it as not duplicate .
*
2018-06-02 13:02:42 -05:00
* @ covers \FireflyIII\Import\Storage\ImportArrayStorage
2019-07-26 10:48:24 -05:00
*
2018-06-02 13:02:42 -05:00
*/
2019-07-26 10:48:24 -05:00
public function testTransfer () : void
2018-06-02 13:02:42 -05:00
{
2019-07-26 10:48:24 -05:00
Log :: debug ( sprintf ( 'Now in test %s' , __METHOD__ ));
// data to submit:
$transactions = [
$this -> singleImportTransfer (),
];
// data that is returned:
$withdrawalGroup = $this -> getRandomWithdrawalGroup ();
$tag = $this -> getRandomTag ();
$transfer = $this -> getRandomTransferAsArray ();
// mock stuff
$userRepos = $this -> mock ( UserRepositoryInterface :: class );
$repository = $this -> mock ( ImportJobRepositoryInterface :: class );
$journalRepos = $this -> mock ( JournalRepositoryInterface :: class );
$groupRepos = $this -> mock ( TransactionGroupRepositoryInterface :: class );
$tagRepos = $this -> mock ( TagRepositoryInterface :: class );
$collector = $this -> mock ( GroupCollectorInterface :: class );
$this -> mock ( Processor :: class );
$this -> mock ( RuleRepositoryInterface :: class );
2019-04-09 08:32:48 -05:00
2019-07-26 10:48:24 -05:00
Amount :: shouldReceive ( 'something' );
2018-09-04 09:47:01 -05:00
2019-07-26 10:48:24 -05:00
$language = new Preference ;
$language -> data = 'en_US' ;
2019-08-10 06:42:33 -05:00
Preferences :: shouldReceive ( 'getForUser' ) -> withArgs ([ Mockery :: any (), 'language' , 'en_US' ]) -> andReturn ( $language ) -> atLeast () -> once ();
2018-06-02 13:02:42 -05:00
// make fake job
$job = new ImportJob ;
$job -> user () -> associate ( $this -> user ());
2019-07-26 10:48:24 -05:00
$job -> key = 'a_storage' . $this -> randomInt ();
2018-06-02 13:02:42 -05:00
$job -> status = 'new' ;
$job -> stage = 'new' ;
$job -> provider = 'fake' ;
$job -> file_type = '' ;
2019-07-26 10:48:24 -05:00
$job -> configuration = [];
$job -> transactions = [];
2018-06-02 13:02:42 -05:00
$job -> save ();
2019-07-26 10:48:24 -05:00
// mock user calls
$repository -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$journalRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$groupRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$tagRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
// mock other calls.
$repository -> shouldReceive ( 'getTransactions' ) -> atLeast () -> once () -> andReturn ( $transactions );
Preferences :: shouldReceive ( 'mark' ) -> atLeast () -> once () -> withNoArgs ();
$userRepos -> shouldReceive ( 'findNull' ) -> atLeast () -> once () -> andReturn ( $this -> user ());
// status changes of the job.
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'storing_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'stored_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linking_to_tag' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linked_to_tag' ]);
// calls to validate and import transactions:
$journalRepos -> shouldReceive ( 'findByHash' ) -> withArgs ([ Mockery :: any ()]) -> atLeast () -> once () -> andReturnNull ();
$groupRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $withdrawalGroup );
$tagRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $tag );
$repository -> shouldReceive ( 'setTag' ) -> atLeast () -> once () -> andReturn ( $job );
// also mocks collector:
$collector -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$collector -> shouldReceive ( 'setTypes' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setLimit' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setPage' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setGroup' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'withAccountInformation' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'getExtractedJournals' ) -> atLeast () -> once () -> andReturn ([ $transfer ]);
2018-07-31 13:39:36 -05:00
2018-06-02 13:02:42 -05:00
$storage = new ImportArrayStorage ;
$storage -> setImportJob ( $job );
try {
2019-07-26 10:48:24 -05:00
$storage -> store ();
2018-06-02 13:02:42 -05:00
} catch ( FireflyException $e ) {
$this -> assertTrue ( false , $e -> getMessage ());
}
}
2018-05-05 06:53:12 -05:00
/**
2019-07-26 10:48:24 -05:00
* Submit a transfer , and its not duplicate .
2018-05-05 06:53:12 -05:00
*
* @ covers \FireflyIII\Import\Storage\ImportArrayStorage
2019-07-26 10:48:24 -05:00
*
2018-05-05 06:53:12 -05:00
*/
2019-07-26 10:48:24 -05:00
public function testTransferNotDuplicate () : void
2018-05-05 06:53:12 -05:00
{
2019-07-26 10:48:24 -05:00
Log :: debug ( sprintf ( 'Now in test %s' , __METHOD__ ));
// data to submit:
$transactions = [
$this -> singleImportTransfer (),
];
// data that is returned:
$transferGroup = $this -> getRandomTransferGroup ();
$tag = $this -> getRandomTag ();
$transfer = $this -> getRandomTransferAsArray ();
// make sure the right fields of the transfergroup and the transactions
// are equal, so the duplicate detector is triggered.
// mock stuff
$userRepos = $this -> mock ( UserRepositoryInterface :: class );
$repository = $this -> mock ( ImportJobRepositoryInterface :: class );
$journalRepos = $this -> mock ( JournalRepositoryInterface :: class );
$groupRepos = $this -> mock ( TransactionGroupRepositoryInterface :: class );
$tagRepos = $this -> mock ( TagRepositoryInterface :: class );
$collector = $this -> mock ( GroupCollectorInterface :: class );
$this -> mock ( Processor :: class );
$this -> mock ( RuleRepositoryInterface :: class );
Amount :: shouldReceive ( 'something' );
$language = new Preference ;
$language -> data = 'en_US' ;
2019-08-10 06:42:33 -05:00
Preferences :: shouldReceive ( 'getForUser' ) -> withArgs ([ Mockery :: any (), 'language' , 'en_US' ]) -> andReturn ( $language ) -> atLeast () -> once ();
2019-04-09 08:32:48 -05:00
2018-09-04 09:47:01 -05:00
2018-05-05 06:53:12 -05:00
// make fake job
2019-07-26 10:48:24 -05:00
$job = new ImportJob ;
2018-05-05 06:53:12 -05:00
$job -> user () -> associate ( $this -> user ());
2019-07-26 10:48:24 -05:00
$job -> key = 'a_storage' . $this -> randomInt ();
2018-05-05 06:53:12 -05:00
$job -> status = 'new' ;
$job -> stage = 'new' ;
$job -> provider = 'fake' ;
$job -> file_type = '' ;
2019-07-26 10:48:24 -05:00
$job -> configuration = [];
$job -> transactions = [];
2018-05-05 06:53:12 -05:00
$job -> save ();
2019-07-26 10:48:24 -05:00
// mock user calls
$repository -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$journalRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$groupRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$tagRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
// mock other calls.
$repository -> shouldReceive ( 'getTransactions' ) -> atLeast () -> once () -> andReturn ( $transactions );
Preferences :: shouldReceive ( 'mark' ) -> atLeast () -> once () -> withNoArgs ();
$userRepos -> shouldReceive ( 'findNull' ) -> atLeast () -> once () -> andReturn ( $this -> user ());
// status changes of the job.
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'storing_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'stored_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linking_to_tag' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linked_to_tag' ]);
// calls to validate and import transactions:
$journalRepos -> shouldReceive ( 'findByHash' ) -> withArgs ([ Mockery :: any ()]) -> atLeast () -> once () -> andReturnNull ();
$groupRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $transferGroup );
$tagRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $tag );
$repository -> shouldReceive ( 'setTag' ) -> atLeast () -> once () -> andReturn ( $job );
// also mocks collector:
$collector -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$collector -> shouldReceive ( 'setTypes' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setLimit' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setPage' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setGroup' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'withAccountInformation' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'getExtractedJournals' ) -> atLeast () -> once () -> andReturn ([ $transfer ]);
2018-05-05 06:53:12 -05:00
$storage = new ImportArrayStorage ;
2018-05-12 08:50:01 -05:00
$storage -> setImportJob ( $job );
2018-05-05 06:53:12 -05:00
try {
2019-07-26 10:48:24 -05:00
$storage -> store ();
2018-05-05 06:53:12 -05:00
} catch ( FireflyException $e ) {
$this -> assertTrue ( false , $e -> getMessage ());
}
}
/**
2019-07-26 10:48:24 -05:00
* Submit a transfer , and the amounts match , but the rest doesn ' t .
2018-05-05 06:53:12 -05:00
*
* @ covers \FireflyIII\Import\Storage\ImportArrayStorage
2019-07-26 10:48:24 -05:00
*
2018-05-05 06:53:12 -05:00
*/
2019-07-26 10:48:24 -05:00
public function testTransferNotDuplicateAmount () : void
2018-05-05 06:53:12 -05:00
{
2019-07-26 10:48:24 -05:00
Log :: debug ( sprintf ( 'Now in test %s' , __METHOD__ ));
// data to submit:
$transactions = [
$this -> singleImportTransfer (),
];
// data that is returned:
$transferGroup = $this -> getRandomTransferGroup ();
$tag = $this -> getRandomTag ();
$transfer = $this -> getRandomTransferAsArray ();
// are equal, so the duplicate detector is triggered.
$transactions [ 0 ][ 'transactions' ][ 0 ][ 'amount' ] = '56.78' ;
$transfer [ 'amount' ] = '56.78' ;
//$transferGroup['transactions']['amount'] = '12';
/** @var TransactionJournal $journal */
$journal = $transferGroup -> transactionJournals -> first ();
$journal -> transactions -> each ( static function ( Transaction $t ) {
if ( $t -> amount < 0 ) {
$t -> amount = '-56.78' ;
}
if ( $t -> amount > 0 ) {
$t -> amount = '56.78' ;
}
$t -> save ();
});
$transferGroup -> refresh ();
// mock stuff
$userRepos = $this -> mock ( UserRepositoryInterface :: class );
$repository = $this -> mock ( ImportJobRepositoryInterface :: class );
$journalRepos = $this -> mock ( JournalRepositoryInterface :: class );
$groupRepos = $this -> mock ( TransactionGroupRepositoryInterface :: class );
$tagRepos = $this -> mock ( TagRepositoryInterface :: class );
$collector = $this -> mock ( GroupCollectorInterface :: class );
$this -> mock ( Processor :: class );
$this -> mock ( RuleRepositoryInterface :: class );
Amount :: shouldReceive ( 'something' );
$language = new Preference ;
$language -> data = 'en_US' ;
2019-08-10 06:42:33 -05:00
Preferences :: shouldReceive ( 'getForUser' ) -> withArgs ([ Mockery :: any (), 'language' , 'en_US' ]) -> andReturn ( $language ) -> atLeast () -> once ();
2019-04-10 12:03:33 -05:00
2018-09-04 09:47:01 -05:00
2018-05-05 06:53:12 -05:00
// make fake job
$job = new ImportJob ;
$job -> user () -> associate ( $this -> user ());
2019-07-26 10:48:24 -05:00
$job -> key = 'a_storage' . $this -> randomInt ();
2018-05-05 06:53:12 -05:00
$job -> status = 'new' ;
$job -> stage = 'new' ;
$job -> provider = 'fake' ;
$job -> file_type = '' ;
$job -> configuration = [];
$job -> transactions = [];
$job -> save ();
2019-07-26 10:48:24 -05:00
// mock user calls
$repository -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$journalRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$groupRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$tagRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
// mock other calls.
$repository -> shouldReceive ( 'getTransactions' ) -> atLeast () -> once () -> andReturn ( $transactions );
Preferences :: shouldReceive ( 'mark' ) -> atLeast () -> once () -> withNoArgs ();
$userRepos -> shouldReceive ( 'findNull' ) -> atLeast () -> once () -> andReturn ( $this -> user ());
// status changes of the job.
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'storing_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'stored_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linking_to_tag' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linked_to_tag' ]);
// calls to validate and import transactions:
$journalRepos -> shouldReceive ( 'findByHash' ) -> withArgs ([ Mockery :: any ()]) -> atLeast () -> once () -> andReturnNull ();
$groupRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $transferGroup );
$tagRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $tag );
$repository -> shouldReceive ( 'setTag' ) -> atLeast () -> once () -> andReturn ( $job );
// also mocks collector:
$collector -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$collector -> shouldReceive ( 'setTypes' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setLimit' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setPage' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setGroup' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'withAccountInformation' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'getExtractedJournals' ) -> atLeast () -> once () -> andReturn ([ $transfer ]);
2018-05-05 06:53:12 -05:00
$storage = new ImportArrayStorage ;
2018-05-12 08:50:01 -05:00
$storage -> setImportJob ( $job );
2018-05-05 06:53:12 -05:00
try {
2019-07-26 10:48:24 -05:00
$storage -> store ();
2018-05-05 06:53:12 -05:00
} catch ( FireflyException $e ) {
$this -> assertTrue ( false , $e -> getMessage ());
}
}
/**
2019-07-26 10:48:24 -05:00
* Submit a transfer , and the amounts match , and the description matches , but the rest doesn ' t
2018-05-05 06:53:12 -05:00
*
* @ covers \FireflyIII\Import\Storage\ImportArrayStorage
2019-07-26 10:48:24 -05:00
*
2018-05-05 06:53:12 -05:00
*/
2019-07-26 10:48:24 -05:00
public function testTransferNotDuplicateDescr () : void
2018-05-05 06:53:12 -05:00
{
2019-07-26 10:48:24 -05:00
Log :: debug ( sprintf ( 'Now in test %s' , __METHOD__ ));
// data to submit:
$transactions = [
$this -> singleImportTransfer (),
];
// data that is returned:
$transferGroup = $this -> getRandomTransferGroup ();
$tag = $this -> getRandomTag ();
$transfer = $this -> getRandomTransferAsArray ();
// are equal, so the duplicate detector is triggered.
$transactions [ 0 ][ 'transactions' ][ 0 ][ 'amount' ] = '56.78' ;
$transfer [ 'amount' ] = '56.78' ;
$transactions [ 0 ][ 'transactions' ][ 0 ][ 'description' ] = $transfer [ 'description' ];
//$transferGroup['transactions']['amount'] = '12';
/** @var TransactionJournal $journal */
$journal = $transferGroup -> transactionJournals -> first ();
$journal -> transactions -> each ( static function ( Transaction $t ) {
if ( $t -> amount < 0 ) {
$t -> amount = '-56.78' ;
}
if ( $t -> amount > 0 ) {
$t -> amount = '56.78' ;
}
$t -> save ();
});
$transferGroup -> refresh ();
// mock stuff
$userRepos = $this -> mock ( UserRepositoryInterface :: class );
$repository = $this -> mock ( ImportJobRepositoryInterface :: class );
$journalRepos = $this -> mock ( JournalRepositoryInterface :: class );
$groupRepos = $this -> mock ( TransactionGroupRepositoryInterface :: class );
$tagRepos = $this -> mock ( TagRepositoryInterface :: class );
$collector = $this -> mock ( GroupCollectorInterface :: class );
$this -> mock ( Processor :: class );
$this -> mock ( RuleRepositoryInterface :: class );
Amount :: shouldReceive ( 'something' );
$language = new Preference ;
$language -> data = 'en_US' ;
2019-08-10 06:42:33 -05:00
Preferences :: shouldReceive ( 'getForUser' ) -> withArgs ([ Mockery :: any (), 'language' , 'en_US' ]) -> andReturn ( $language ) -> atLeast () -> once ();
2019-04-09 08:32:48 -05:00
2018-09-04 09:47:01 -05:00
2018-05-05 06:53:12 -05:00
// make fake job
$job = new ImportJob ;
$job -> user () -> associate ( $this -> user ());
2019-07-26 10:48:24 -05:00
$job -> key = 'a_storage' . $this -> randomInt ();
2018-05-05 06:53:12 -05:00
$job -> status = 'new' ;
$job -> stage = 'new' ;
$job -> provider = 'fake' ;
$job -> file_type = '' ;
2019-07-26 10:48:24 -05:00
$job -> configuration = [];
2018-05-05 06:53:12 -05:00
$job -> transactions = [];
$job -> save ();
2019-07-26 10:48:24 -05:00
// mock user calls
$repository -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$journalRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$groupRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$tagRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
// mock other calls.
$repository -> shouldReceive ( 'getTransactions' ) -> atLeast () -> once () -> andReturn ( $transactions );
Preferences :: shouldReceive ( 'mark' ) -> atLeast () -> once () -> withNoArgs ();
$userRepos -> shouldReceive ( 'findNull' ) -> atLeast () -> once () -> andReturn ( $this -> user ());
// status changes of the job.
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'storing_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'stored_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linking_to_tag' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linked_to_tag' ]);
// calls to validate and import transactions:
$journalRepos -> shouldReceive ( 'findByHash' ) -> withArgs ([ Mockery :: any ()]) -> atLeast () -> once () -> andReturnNull ();
$groupRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $transferGroup );
$tagRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $tag );
$repository -> shouldReceive ( 'setTag' ) -> atLeast () -> once () -> andReturn ( $job );
// also mocks collector:
$collector -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$collector -> shouldReceive ( 'setTypes' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setLimit' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setPage' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setGroup' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'withAccountInformation' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'getExtractedJournals' ) -> atLeast () -> once () -> andReturn ([ $transfer ]);
2018-05-05 06:53:12 -05:00
$storage = new ImportArrayStorage ;
2018-05-12 08:50:01 -05:00
$storage -> setImportJob ( $job );
2018-05-05 06:53:12 -05:00
try {
2019-07-26 10:48:24 -05:00
$storage -> store ();
2018-05-05 06:53:12 -05:00
} catch ( FireflyException $e ) {
$this -> assertTrue ( false , $e -> getMessage ());
}
}
/**
2019-07-26 10:48:24 -05:00
* Submit a transfer , and the amounts match , and the description matches ,
* and the date matches , but the rest doesn ' t
*
2018-05-05 06:53:12 -05:00
* @ covers \FireflyIII\Import\Storage\ImportArrayStorage
2019-07-26 10:48:24 -05:00
*
2018-05-05 06:53:12 -05:00
*/
2019-07-26 10:48:24 -05:00
public function testTransferNotDuplicateDate () : void
2018-05-05 06:53:12 -05:00
{
2019-07-26 10:48:24 -05:00
Log :: debug ( sprintf ( 'Now in test %s' , __METHOD__ ));
// data to submit:
$transactions = [
$this -> singleImportTransfer (),
];
// data that is returned:
$transferGroup = $this -> getRandomTransferGroup ();
$tag = $this -> getRandomTag ();
$transfer = $this -> getRandomTransferAsArray ();
// are equal, so the duplicate detector is triggered.
$transactions [ 0 ][ 'transactions' ][ 0 ][ 'amount' ] = '56.78' ;
$transfer [ 'amount' ] = '56.78' ;
$transactions [ 0 ][ 'transactions' ][ 0 ][ 'description' ] = $transfer [ 'description' ];
$transactions [ 0 ][ 'transactions' ][ 0 ][ 'date' ] = $transfer [ 'date' ] -> format ( 'Y-m-d H:i:s' );
//$transferGroup['transactions']['amount'] = '12';
/** @var TransactionJournal $journal */
$journal = $transferGroup -> transactionJournals -> first ();
$journal -> transactions -> each ( static function ( Transaction $t ) {
if ( $t -> amount < 0 ) {
$t -> amount = '-56.78' ;
}
if ( $t -> amount > 0 ) {
$t -> amount = '56.78' ;
}
$t -> save ();
});
$transferGroup -> refresh ();
// mock stuff
$userRepos = $this -> mock ( UserRepositoryInterface :: class );
$repository = $this -> mock ( ImportJobRepositoryInterface :: class );
$journalRepos = $this -> mock ( JournalRepositoryInterface :: class );
$groupRepos = $this -> mock ( TransactionGroupRepositoryInterface :: class );
$tagRepos = $this -> mock ( TagRepositoryInterface :: class );
$collector = $this -> mock ( GroupCollectorInterface :: class );
$this -> mock ( Processor :: class );
$this -> mock ( RuleRepositoryInterface :: class );
Amount :: shouldReceive ( 'something' );
$language = new Preference ;
$language -> data = 'en_US' ;
2019-08-10 06:42:33 -05:00
Preferences :: shouldReceive ( 'getForUser' ) -> withArgs ([ Mockery :: any (), 'language' , 'en_US' ]) -> andReturn ( $language ) -> atLeast () -> once ();
2019-04-09 08:32:48 -05:00
2018-09-04 09:47:01 -05:00
2018-05-05 06:53:12 -05:00
// make fake job
2019-07-26 10:48:24 -05:00
$job = new ImportJob ;
2018-05-05 06:53:12 -05:00
$job -> user () -> associate ( $this -> user ());
2019-07-26 10:48:24 -05:00
$job -> key = 'a_storage' . $this -> randomInt ();
2018-05-05 06:53:12 -05:00
$job -> status = 'new' ;
$job -> stage = 'new' ;
$job -> provider = 'fake' ;
$job -> file_type = '' ;
2019-07-26 10:48:24 -05:00
$job -> configuration = [];
$job -> transactions = [];
2018-05-05 06:53:12 -05:00
$job -> save ();
2019-07-26 10:48:24 -05:00
// mock user calls
$repository -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$journalRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$groupRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$tagRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
// mock other calls.
$repository -> shouldReceive ( 'getTransactions' ) -> atLeast () -> once () -> andReturn ( $transactions );
Preferences :: shouldReceive ( 'mark' ) -> atLeast () -> once () -> withNoArgs ();
$userRepos -> shouldReceive ( 'findNull' ) -> atLeast () -> once () -> andReturn ( $this -> user ());
// status changes of the job.
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'storing_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'stored_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linking_to_tag' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linked_to_tag' ]);
// calls to validate and import transactions:
$journalRepos -> shouldReceive ( 'findByHash' ) -> withArgs ([ Mockery :: any ()]) -> atLeast () -> once () -> andReturnNull ();
$groupRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $transferGroup );
$tagRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $tag );
$repository -> shouldReceive ( 'setTag' ) -> atLeast () -> once () -> andReturn ( $job );
// also mocks collector:
$collector -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$collector -> shouldReceive ( 'setTypes' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setLimit' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setPage' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setGroup' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'withAccountInformation' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'getExtractedJournals' ) -> atLeast () -> once () -> andReturn ([ $transfer ]);
2018-05-05 06:53:12 -05:00
$storage = new ImportArrayStorage ;
2018-05-12 08:50:01 -05:00
$storage -> setImportJob ( $job );
2018-05-05 06:53:12 -05:00
try {
2019-07-26 10:48:24 -05:00
$storage -> store ();
2018-05-05 06:53:12 -05:00
} catch ( FireflyException $e ) {
$this -> assertTrue ( false , $e -> getMessage ());
}
}
2019-07-26 10:48:24 -05:00
2018-05-05 06:53:12 -05:00
/**
2019-07-26 10:48:24 -05:00
* Submit a transfer , and the amounts match , and the description matches ,
* and the date matches , and the accounts match , making it a duplicate .
*
2018-05-05 06:53:12 -05:00
* @ covers \FireflyIII\Import\Storage\ImportArrayStorage
2019-07-26 10:48:24 -05:00
*
2018-05-05 06:53:12 -05:00
*/
2019-07-26 10:48:24 -05:00
public function testTransferNotDuplicateAccounts () : void
2018-05-05 06:53:12 -05:00
{
2019-07-26 10:48:24 -05:00
Log :: debug ( sprintf ( 'Now in test %s' , __METHOD__ ));
// data to submit:
$transactions = [
$this -> singleImportTransfer (),
];
// data that is returned:
$transferGroup = $this -> getRandomTransferGroup ();
$tag = $this -> getRandomTag ();
$transfer = $this -> getRandomTransferAsArray ();
// are equal, so the duplicate detector is triggered.
$transfer [ 'amount' ] = '56.78' ;
$transfer [ 'source_account_id' ] = 0 ;
$transfer [ 'source_account_name' ] = 'x' ;
$transfer [ 'destination_account_id' ] = 0 ;
$transfer [ 'destination_account_name' ] = 'x' ;
$transactions [ 0 ][ 'transactions' ][ 0 ][ 'amount' ] = '56.78' ;
$transactions [ 0 ][ 'transactions' ][ 0 ][ 'description' ] = $transfer [ 'description' ];
$transactions [ 0 ][ 'transactions' ][ 0 ][ 'date' ] = $transfer [ 'date' ] -> format ( 'Y-m-d H:i:s' );
//$transferGroup['transactions']['amount'] = '12';
/** @var TransactionJournal $journal */
$journal = $transferGroup -> transactionJournals -> first ();
$journal -> transactions -> each ( static function ( Transaction $t ) {
if ( $t -> amount < 0 ) {
$t -> amount = '-56.78' ;
}
if ( $t -> amount > 0 ) {
$t -> amount = '56.78' ;
}
$t -> save ();
});
$transferGroup -> refresh ();
// mock stuff
$userRepos = $this -> mock ( UserRepositoryInterface :: class );
$repository = $this -> mock ( ImportJobRepositoryInterface :: class );
$journalRepos = $this -> mock ( JournalRepositoryInterface :: class );
$groupRepos = $this -> mock ( TransactionGroupRepositoryInterface :: class );
$collector = $this -> mock ( GroupCollectorInterface :: class );
$this -> mock ( TagRepositoryInterface :: class );
$this -> mock ( Processor :: class );
$this -> mock ( RuleRepositoryInterface :: class );
Amount :: shouldReceive ( 'something' );
$language = new Preference ;
$language -> data = 'en_US' ;
2019-08-10 06:42:33 -05:00
Preferences :: shouldReceive ( 'getForUser' ) -> withArgs ([ Mockery :: any (), 'language' , 'en_US' ]) -> andReturn ( $language ) -> atLeast () -> once ();
2019-04-09 08:32:48 -05:00
2018-09-04 09:47:01 -05:00
2018-05-05 06:53:12 -05:00
// make fake job
2019-07-26 10:48:24 -05:00
$job = new ImportJob ;
2018-05-05 06:53:12 -05:00
$job -> user () -> associate ( $this -> user ());
2019-07-26 10:48:24 -05:00
$job -> key = 'a_storage' . $this -> randomInt ();
2018-05-05 06:53:12 -05:00
$job -> status = 'new' ;
$job -> stage = 'new' ;
$job -> provider = 'fake' ;
$job -> file_type = '' ;
2019-07-26 10:48:24 -05:00
$job -> configuration = [];
$job -> transactions = [];
2018-05-05 06:53:12 -05:00
$job -> save ();
2019-07-26 10:48:24 -05:00
// mock user calls
$repository -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$journalRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$groupRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
// mock other calls.
$repository -> shouldReceive ( 'getTransactions' ) -> atLeast () -> once () -> andReturn ( $transactions );
Preferences :: shouldReceive ( 'mark' ) -> atLeast () -> once () -> withNoArgs ();
$userRepos -> shouldReceive ( 'findNull' ) -> atLeast () -> once () -> andReturn ( $this -> user ());
// status changes of the job.
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'storing_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'stored_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linking_to_tag' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linked_to_tag' ]);
// calls to validate and import transactions:
$journalRepos -> shouldReceive ( 'findByHash' ) -> withArgs ([ Mockery :: any ()]) -> atLeast () -> once () -> andReturnNull ();
// also mocks collector:
$collector -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$collector -> shouldReceive ( 'setTypes' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setLimit' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'setPage' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'withAccountInformation' ) -> atLeast () -> once () -> andReturnSelf ();
$collector -> shouldReceive ( 'getExtractedJournals' ) -> atLeast () -> once () -> andReturn ([ $transfer ]);
// since a duplicate was found, must register error:
$repository -> shouldReceive ( 'addErrorMessage' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), sprintf ( 'Row #0 ("%s") could not be imported. It already exists.' , $transfer [ 'description' ])]);
$storage = new ImportArrayStorage ;
$storage -> setImportJob ( $job );
try {
$storage -> store ();
} catch ( FireflyException $e ) {
$this -> assertTrue ( false , $e -> getMessage ());
}
}
/**
* Same as testBasic but submits the minimum amount of data required to store a transaction .
* @ covers \FireflyIII\Import\Storage\ImportArrayStorage
*
*/
public function testSimple () : void
{
Log :: debug ( sprintf ( 'Now in test %s' , __METHOD__ ));
// data to submit:
$transactions = [
$this -> singleImportWithdrawal (),
];
// data that is returned:
$withdrawalGroup = $this -> getRandomWithdrawalGroup ();
$tag = $this -> getRandomTag ();
2018-05-05 06:53:12 -05:00
// mock stuff
2019-07-26 10:48:24 -05:00
$userRepos = $this -> mock ( UserRepositoryInterface :: class );
2018-05-05 06:53:12 -05:00
$repository = $this -> mock ( ImportJobRepositoryInterface :: class );
$journalRepos = $this -> mock ( JournalRepositoryInterface :: class );
2019-07-26 10:48:24 -05:00
$groupRepos = $this -> mock ( TransactionGroupRepositoryInterface :: class );
$tagRepos = $this -> mock ( TagRepositoryInterface :: class );
$this -> mock ( Processor :: class );
$this -> mock ( RuleRepositoryInterface :: class );
$this -> mock ( GroupCollectorInterface :: class );
Amount :: shouldReceive ( 'something' );
$language = new Preference ;
$language -> data = 'en_US' ;
2019-08-10 06:42:33 -05:00
Preferences :: shouldReceive ( 'getForUser' ) -> withArgs ([ Mockery :: any (), 'language' , 'en_US' ]) -> andReturn ( $language ) -> atLeast () -> once ();
2019-07-26 10:48:24 -05:00
// make fake job
$job = new ImportJob ;
$job -> user () -> associate ( $this -> user ());
$job -> key = 'a_storage' . $this -> randomInt ();
$job -> status = 'new' ;
$job -> stage = 'new' ;
$job -> provider = 'fake' ;
$job -> file_type = '' ;
$job -> configuration = [];
$job -> transactions = [];
$job -> save ();
// mock user calls
$repository -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$journalRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$groupRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$tagRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
// mock other calls.
$repository -> shouldReceive ( 'getTransactions' ) -> atLeast () -> once () -> andReturn ( $transactions );
Preferences :: shouldReceive ( 'mark' ) -> atLeast () -> once () -> withNoArgs ();
$userRepos -> shouldReceive ( 'findNull' ) -> atLeast () -> once () -> andReturn ( $this -> user ());
// status changes of the job.
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'storing_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'stored_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linking_to_tag' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linked_to_tag' ]);
2018-05-05 06:53:12 -05:00
2019-07-26 10:48:24 -05:00
// calls to validate and import transactions:
$journalRepos -> shouldReceive ( 'findByHash' ) -> withArgs ([ Mockery :: any ()]) -> atLeast () -> once () -> andReturnNull ();
$groupRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $withdrawalGroup );
$tagRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $tag );
$repository -> shouldReceive ( 'setTag' ) -> atLeast () -> once () -> andReturn ( $job );
2018-05-05 06:53:12 -05:00
2018-07-31 13:39:36 -05:00
2018-05-05 06:53:12 -05:00
$storage = new ImportArrayStorage ;
2018-05-12 08:50:01 -05:00
$storage -> setImportJob ( $job );
2018-05-05 06:53:12 -05:00
try {
2019-07-26 10:48:24 -05:00
$storage -> store ();
2018-05-05 06:53:12 -05:00
} catch ( FireflyException $e ) {
$this -> assertTrue ( false , $e -> getMessage ());
}
}
/**
2019-07-26 10:48:24 -05:00
* Same as testBasic but submits the minimum amount of data required to store a transaction .
*
* The one journal in the list is a duplicate .
*
2018-05-05 06:53:12 -05:00
* @ covers \FireflyIII\Import\Storage\ImportArrayStorage
2019-07-26 10:48:24 -05:00
*
2018-05-05 06:53:12 -05:00
*/
2019-07-26 10:48:24 -05:00
public function testSimpleDuplicate () : void
2018-05-05 06:53:12 -05:00
{
2019-07-26 10:48:24 -05:00
Log :: debug ( sprintf ( 'Now in test %s' , __METHOD__ ));
// data to submit:
$transactions = [
$this -> singleImportWithdrawal (),
];
// mock stuff
$userRepos = $this -> mock ( UserRepositoryInterface :: class );
$repository = $this -> mock ( ImportJobRepositoryInterface :: class );
$journalRepos = $this -> mock ( JournalRepositoryInterface :: class );
$groupRepos = $this -> mock ( TransactionGroupRepositoryInterface :: class );
$this -> mock ( TagRepositoryInterface :: class );
$this -> mock ( Processor :: class );
$this -> mock ( RuleRepositoryInterface :: class );
$this -> mock ( GroupCollectorInterface :: class );
Amount :: shouldReceive ( 'something' );
$language = new Preference ;
$language -> data = 'en_US' ;
2019-08-10 06:42:33 -05:00
Preferences :: shouldReceive ( 'getForUser' ) -> withArgs ([ Mockery :: any (), 'language' , 'en_US' ]) -> andReturn ( $language ) -> atLeast () -> once ();
2019-07-26 10:48:24 -05:00
$meta = new TransactionJournalMeta ;
$meta -> transaction_journal_id = 1 ;
2019-04-09 08:32:48 -05:00
2018-09-04 09:47:01 -05:00
2018-05-05 06:53:12 -05:00
// make fake job
2019-07-26 10:48:24 -05:00
$job = new ImportJob ;
2018-05-05 06:53:12 -05:00
$job -> user () -> associate ( $this -> user ());
2019-07-26 10:48:24 -05:00
$job -> key = 'a_storage' . $this -> randomInt ();
2018-05-05 06:53:12 -05:00
$job -> status = 'new' ;
$job -> stage = 'new' ;
$job -> provider = 'fake' ;
$job -> file_type = '' ;
2019-07-26 10:48:24 -05:00
$job -> configuration = [];
$job -> transactions = [];
2018-05-05 06:53:12 -05:00
$job -> save ();
2019-07-26 10:48:24 -05:00
// mock user calls
$repository -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$journalRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$groupRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
// mock other calls.
$repository -> shouldReceive ( 'getTransactions' ) -> atLeast () -> once () -> andReturn ( $transactions );
Preferences :: shouldReceive ( 'mark' ) -> atLeast () -> once () -> withNoArgs ();
$userRepos -> shouldReceive ( 'findNull' ) -> atLeast () -> once () -> andReturn ( $this -> user ());
// status changes of the job.
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'storing_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'stored_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linking_to_tag' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linked_to_tag' ]);
// calls to validate and import transactions:
$journalRepos -> shouldReceive ( 'findByHash' ) -> withArgs ([ Mockery :: any ()]) -> atLeast () -> once () -> andReturn ( $meta );
// errors because of duplicate:
$repository -> shouldReceive ( 'addErrorMessage' ) -> atLeast () -> once ()
-> withArgs ([ Mockery :: any (), 'Row #0 ("") could not be imported. It already exists.' ]);
$storage = new ImportArrayStorage ;
$storage -> setImportJob ( $job );
try {
$storage -> store ();
} catch ( FireflyException $e ) {
$this -> assertTrue ( false , $e -> getMessage ());
}
}
/**
* Same as testBasic but submits the minimum amount of data required to store a transaction .
*
* Also applies the rules , but there are none .
*
* @ covers \FireflyIII\Import\Storage\ImportArrayStorage
*
*/
public function testSimpleApplyNoRules () : void
{
Log :: debug ( sprintf ( 'Now in test %s' , __METHOD__ ));
// data to submit:
$transactions = [
$this -> singleImportWithdrawal (),
];
// data that is returned:
$withdrawalGroup = $this -> getRandomWithdrawalGroup ();
$tag = $this -> getRandomTag ();
2018-05-05 06:53:12 -05:00
// mock stuff
2019-07-26 10:48:24 -05:00
$userRepos = $this -> mock ( UserRepositoryInterface :: class );
2018-05-05 06:53:12 -05:00
$repository = $this -> mock ( ImportJobRepositoryInterface :: class );
$journalRepos = $this -> mock ( JournalRepositoryInterface :: class );
2019-07-26 10:48:24 -05:00
$groupRepos = $this -> mock ( TransactionGroupRepositoryInterface :: class );
$ruleRepos = $this -> mock ( RuleRepositoryInterface :: class );
$tagRepos = $this -> mock ( TagRepositoryInterface :: class );
$this -> mock ( Processor :: class );
$this -> mock ( GroupCollectorInterface :: class );
Amount :: shouldReceive ( 'something' );
$language = new Preference ;
$language -> data = 'en_US' ;
2019-08-10 06:42:33 -05:00
Preferences :: shouldReceive ( 'getForUser' ) -> withArgs ([ Mockery :: any (), 'language' , 'en_US' ]) -> andReturn ( $language ) -> atLeast () -> once ();
2019-07-26 10:48:24 -05:00
// make fake job
$job = new ImportJob ;
$job -> user () -> associate ( $this -> user ());
$job -> key = 'a_storage' . $this -> randomInt ();
$job -> status = 'new' ;
$job -> stage = 'new' ;
$job -> provider = 'fake' ;
$job -> file_type = '' ;
$job -> configuration = [
'apply-rules' => true ,
];
$job -> transactions = [];
$job -> save ();
2018-05-05 06:53:12 -05:00
2019-07-26 10:48:24 -05:00
// mock user calls
$repository -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$journalRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$groupRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$tagRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$ruleRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
// mock other calls.
$repository -> shouldReceive ( 'getTransactions' ) -> atLeast () -> once () -> andReturn ( $transactions );
Preferences :: shouldReceive ( 'mark' ) -> atLeast () -> once () -> withNoArgs ();
$userRepos -> shouldReceive ( 'findNull' ) -> atLeast () -> once () -> andReturn ( $this -> user ());
// status changes of the job.
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'storing_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'stored_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linking_to_tag' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linked_to_tag' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'applying_rules' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'rules_applied' ]);
// calls to validate and import transactions:
$journalRepos -> shouldReceive ( 'findByHash' ) -> withArgs ([ Mockery :: any ()]) -> atLeast () -> once () -> andReturnNull ();
$groupRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $withdrawalGroup );
$tagRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $tag );
$repository -> shouldReceive ( 'setTag' ) -> atLeast () -> once () -> andReturn ( $job );
// calls for application of rules, but returns NO rules.
$ruleRepos -> shouldReceive ( 'getForImport' ) -> once () -> andReturn ( new Collection );
2018-07-31 13:39:36 -05:00
2018-05-05 07:40:12 -05:00
$storage = new ImportArrayStorage ;
2018-05-12 08:50:01 -05:00
$storage -> setImportJob ( $job );
2018-05-05 07:40:12 -05:00
try {
2019-07-26 10:48:24 -05:00
$storage -> store ();
2018-05-05 07:40:12 -05:00
} catch ( FireflyException $e ) {
$this -> assertTrue ( false , $e -> getMessage ());
}
}
2019-07-26 10:48:24 -05:00
2018-05-05 07:40:12 -05:00
/**
2019-07-26 10:48:24 -05:00
* Same as testBasic but submits the minimum amount of data required to store a transaction .
*
* Also applies the rules , but there are none .
*
* @ covers \FireflyIII\Import\Storage\ImportArrayStorage
2018-05-05 07:40:12 -05:00
*
*/
2019-07-26 10:48:24 -05:00
public function testSimpleApplyOneRules () : void
2018-05-05 07:40:12 -05:00
{
2019-07-26 10:48:24 -05:00
Log :: debug ( sprintf ( 'Now in test %s' , __METHOD__ ));
// data to submit:
$transactions = [
$this -> singleImportWithdrawal (),
];
2018-05-05 07:40:12 -05:00
2019-07-26 10:48:24 -05:00
// data that is returned:
$withdrawalGroup = $this -> getRandomWithdrawalGroup ();
$tag = $this -> getRandomTag ();
$rule = $this -> getRandomRule ();
2018-05-05 07:40:12 -05:00
2019-07-26 10:48:24 -05:00
// mock stuff
$userRepos = $this -> mock ( UserRepositoryInterface :: class );
$repository = $this -> mock ( ImportJobRepositoryInterface :: class );
$journalRepos = $this -> mock ( JournalRepositoryInterface :: class );
$groupRepos = $this -> mock ( TransactionGroupRepositoryInterface :: class );
$ruleRepos = $this -> mock ( RuleRepositoryInterface :: class );
$tagRepos = $this -> mock ( TagRepositoryInterface :: class );
$processor = $this -> mock ( Processor :: class );
2018-05-05 07:40:12 -05:00
2019-07-26 10:48:24 -05:00
$this -> mock ( GroupCollectorInterface :: class );
Amount :: shouldReceive ( 'something' );
$language = new Preference ;
$language -> data = 'en_US' ;
2019-08-10 06:42:33 -05:00
Preferences :: shouldReceive ( 'getForUser' ) -> withArgs ([ Mockery :: any (), 'language' , 'en_US' ]) -> andReturn ( $language ) -> atLeast () -> once ();
2019-07-26 10:48:24 -05:00
// make fake job
$job = new ImportJob ;
$job -> user () -> associate ( $this -> user ());
$job -> key = 'a_storage' . $this -> randomInt ();
$job -> status = 'new' ;
$job -> stage = 'new' ;
$job -> provider = 'fake' ;
$job -> file_type = '' ;
$job -> configuration = [
'apply-rules' => true ,
];
$job -> transactions = [];
$job -> save ();
2018-05-05 07:40:12 -05:00
2019-07-26 10:48:24 -05:00
// mock user calls
$repository -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$journalRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$groupRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$tagRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
$ruleRepos -> shouldReceive ( 'setUser' ) -> atLeast () -> once ();
// mock other calls.
$repository -> shouldReceive ( 'getTransactions' ) -> atLeast () -> once () -> andReturn ( $transactions );
Preferences :: shouldReceive ( 'mark' ) -> atLeast () -> once () -> withNoArgs ();
$userRepos -> shouldReceive ( 'findNull' ) -> atLeast () -> once () -> andReturn ( $this -> user ());
// status changes of the job.
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'storing_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'stored_data' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linking_to_tag' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'linked_to_tag' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'applying_rules' ]);
$repository -> shouldReceive ( 'setStatus' ) -> atLeast () -> once () -> withArgs ([ Mockery :: any (), 'rules_applied' ]);
// calls to validate and import transactions:
$journalRepos -> shouldReceive ( 'findByHash' ) -> withArgs ([ Mockery :: any ()]) -> atLeast () -> once () -> andReturnNull ();
$groupRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $withdrawalGroup );
$tagRepos -> shouldReceive ( 'store' ) -> atLeast () -> once () -> andReturn ( $tag );
$repository -> shouldReceive ( 'setTag' ) -> atLeast () -> once () -> andReturn ( $job );
// calls for application of rules, but returns 1 rules.
$ruleRepos -> shouldReceive ( 'getForImport' ) -> once () -> andReturn ( new Collection ([ $rule ]));
$processor -> shouldReceive ( 'make' ) -> atLeast () -> once ();
$processor -> shouldReceive ( 'handleTransactionJournal' ) -> atLeast () -> once ();
$storage = new ImportArrayStorage ;
$storage -> setImportJob ( $job );
try {
$storage -> store ();
} catch ( FireflyException $e ) {
$this -> assertTrue ( false , $e -> getMessage ());
}
2018-05-05 07:40:12 -05:00
}
2018-05-05 06:53:12 -05:00
/**
* @ return array
*/
2019-07-26 10:48:24 -05:00
private function singleImportWithdrawal () : array
2018-05-05 06:53:12 -05:00
{
return
[
2019-07-26 10:48:24 -05:00
'type' => 'withdrawal' ,
2018-05-05 06:53:12 -05:00
'tags' => '' ,
'user' => $this -> user () -> id ,
// all custom fields:
'internal_reference' => null ,
'notes' => null ,
// journal data:
2019-07-26 10:48:24 -05:00
'description' => 'Some TEST withdrawal #1' ,
2018-05-05 06:53:12 -05:00
'piggy_bank_id' => null ,
'piggy_bank_name' => null ,
'bill_id' => null ,
'bill_name' => null ,
// transaction data:
'transactions' => [
[
2019-07-26 10:48:24 -05:00
'date' => '2019-01-01' ,
'type' => 'withdrawal' ,
2018-05-05 06:53:12 -05:00
'currency_id' => null ,
'currency_code' => 'EUR' ,
'description' => null ,
2019-07-26 10:48:24 -05:00
'amount' => '12.34' ,
2018-05-05 06:53:12 -05:00
'budget_id' => null ,
'budget_name' => null ,
'category_id' => null ,
'category_name' => null ,
2019-07-26 10:48:24 -05:00
'source_id' => null ,
'source_name' => 'Checking Account' ,
'destination_id' => null ,
'destination_name' => 'Random TEST expense account #2' ,
2018-05-05 06:53:12 -05:00
'foreign_currency_id' => null ,
'foreign_currency_code' => null ,
'foreign_amount' => null ,
'reconciled' => false ,
'identifier' => 0 ,
],
],
];
}
/**
* @ return array
*/
2019-07-26 10:48:24 -05:00
private function singleImportTransfer () : array
2018-05-05 06:53:12 -05:00
{
return
[
2019-07-26 10:48:24 -05:00
'type' => 'transfer' ,
2018-05-05 06:53:12 -05:00
'tags' => '' ,
'user' => $this -> user () -> id ,
// all custom fields:
'internal_reference' => null ,
'notes' => null ,
// journal data:
2019-07-26 10:48:24 -05:00
'description' => 'Some TEST transfer #1' ,
2018-05-05 06:53:12 -05:00
'piggy_bank_id' => null ,
'piggy_bank_name' => null ,
'bill_id' => null ,
'bill_name' => null ,
// transaction data:
'transactions' => [
[
2019-07-26 10:48:24 -05:00
'date' => '2019-01-01' ,
'type' => 'transfer' ,
2018-05-05 06:53:12 -05:00
'currency_id' => null ,
'currency_code' => 'EUR' ,
'description' => null ,
2019-07-26 10:48:24 -05:00
'amount' => '12.34' ,
2018-05-05 06:53:12 -05:00
'budget_id' => null ,
'budget_name' => null ,
'category_id' => null ,
'category_name' => null ,
'source_id' => null ,
'source_name' => 'Checking Account' ,
'destination_id' => null ,
2019-07-26 10:48:24 -05:00
'destination_name' => 'Random TEST expense account #2' ,
2018-05-05 06:53:12 -05:00
'foreign_currency_id' => null ,
'foreign_currency_code' => null ,
'foreign_amount' => null ,
'reconciled' => false ,
'identifier' => 0 ,
],
],
];
}
2018-05-05 09:51:32 -05:00
}