Fix tests

This commit is contained in:
James Cole 2018-06-02 20:02:42 +02:00
parent 2f824ba1a8
commit 332d32c319
3 changed files with 91 additions and 88 deletions

View File

@ -30,6 +30,7 @@ use FireflyIII\Models\TransactionJournal;
/**
* Class TransactionJournalMeta.
* @property string $name
* @property int $transaction_journal_id
*/
class TransactionJournalMeta extends Model
{

View File

@ -156,10 +156,10 @@ class TagControllerTest extends TestCase
$collector = $this->mock(JournalCollectorInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('spentInPeriod')->andReturn('-1')->times(2);
$repository->shouldReceive('spentInPeriod')->andReturn('-1')->times(3);
$repository->shouldReceive('firstUseDate')->andReturn(new Carbon)->once();
$repository->shouldReceive('lastUseDate')->andReturn(new Carbon)->once();
$repository->shouldReceive('earnedInPeriod')->andReturn('1')->times(2);
$repository->shouldReceive('earnedInPeriod')->andReturn('1')->times(3);
$repository->shouldReceive('sumsOfTag')->andReturn($amounts)->once();
$collector->shouldReceive('removeFilter')->andReturnSelf()->once();
@ -225,10 +225,10 @@ class TagControllerTest extends TestCase
$collector = $this->mock(JournalCollectorInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('spentInPeriod')->andReturn('-1')->times(2);
$repository->shouldReceive('spentInPeriod')->andReturn('-1')->times(3);
$repository->shouldReceive('firstUseDate')->andReturn(new Carbon)->once();
$repository->shouldReceive('lastUseDate')->andReturn(new Carbon)->once();
$repository->shouldReceive('earnedInPeriod')->andReturn('1')->times(2);
$repository->shouldReceive('earnedInPeriod')->andReturn('1')->times(3);
$collector->shouldReceive('removeFilter')->andReturnSelf()->once();
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->once();

View File

@ -79,6 +79,87 @@ class ImportArrayStorageTest extends TestCase
$storage->setImportJob($job);
}
/**
* @covers \FireflyIII\Import\Storage\ImportArrayStorage
*/
public function testBasicStoreDoubleTransferWithRules(): void
{
// get a transfer:
/** @var TransactionJournal $transfer */
$transfer = $this->user()->transactionJournals()
->inRandomOrder()->where('transaction_type_id', 3)
->first();
// get transfer as a collection, so the compare routine works.
$journalCollector = new JournalCollector();
$journalCollector->setUser($this->user());
$journalCollector->setJournals(new Collection([$transfer]));
$transferCollection = $journalCollector->withOpposingAccount()->getJournals();
// make fake job
$job = new ImportJob;
$job->user()->associate($this->user());
$job->key = 'h_storage' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = ['apply-rules' => true];
$job->transactions = [$this->singleTransfer(), $this->singleWithdrawal(), $this->basedOnTransfer($transfer)];
$job->save();
// get some stuff:
$tag = $this->user()->tags()->inRandomOrder()->first();
$journal = $this->user()->transactionJournals()->inRandomOrder()->first();
$ruleOne = new Rule;
$ruleOne->stop_processing = false;
$ruleTwo = new Rule;
$ruleTwo->stop_processing = true;
// mock stuff
$repository = $this->mock(ImportJobRepositoryInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$tagRepos = $this->mock(TagRepositoryInterface::class);
$ruleRepos = $this->mock(RuleRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
// mock calls:
$collector->shouldReceive('setUser')->once();
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('setStatus')->withAnyArgs();
$ruleRepos->shouldReceive('setUser')->once();
$tagRepos->shouldReceive('setUser')->once();
$tagRepos->shouldReceive('store')->once()->andReturn($tag);
$repository->shouldReceive('setTag')->once();
$ruleRepos->shouldReceive('getForImport')->andReturn(new Collection([$ruleOne, $ruleTwo]));
$journalRepos->shouldReceive('setUser')->once();
$journalRepos->shouldReceive('store')->twice()->andReturn($journal);
$journalRepos->shouldReceive('findByHash')->andReturn(null)->times(5);
$repository->shouldReceive('addErrorMessage')->withArgs(
[Mockery::any(), 'Row #2 ("' . $transfer->description . '") could not be imported. Such a transfer already exists.']
)->once();
// mock collector so it will return some transfers:
$collector->shouldReceive('setAllAssetAccounts')->once()->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::TRANSFER]])->once()->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->once()->andReturnSelf();
$collector->shouldReceive('ignoreCache')->once()->andReturnSelf();
$collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->once()->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn($transferCollection);
$storage = new ImportArrayStorage;
$storage->setImportJob($job);
$result = new Collection;
try {
$result = $storage->store();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertCount(2, $result);
}
/**
* Two withdrawals, one of which is duplicated.
*
@ -126,7 +207,7 @@ class ImportArrayStorageTest extends TestCase
$ruleRepos->shouldReceive('getForImport')->andReturn(new Collection([$ruleOne, $ruleTwo]));
$journalRepos->shouldReceive('setUser')->once();
$journalRepos->shouldReceive('store')->once()->andReturn($journal);
$journalRepos->shouldReceive('findByHash')->andReturn(null, $meta)->twice();
$journalRepos->shouldReceive('findByHash')->andReturn(null, $meta, null)->times(3);
$repository->shouldReceive('addErrorMessage')->once()
->withArgs([Mockery::any(), 'Row #1 ("' . $transactions[1]['description'] . '") could not be imported. It already exists.']);
@ -262,7 +343,7 @@ class ImportArrayStorageTest extends TestCase
$ruleRepos->shouldReceive('getForImport')->andReturn(new Collection);
$journalRepos->shouldReceive('setUser')->once();
$journalRepos->shouldReceive('store')->once()->andReturn($journal);
$journalRepos->shouldReceive('findByHash')->andReturn(null)->once();
$journalRepos->shouldReceive('findByHash')->andReturn(null)->times(2);
$storage = new ImportArrayStorage;
$storage->setImportJob($job);
@ -317,7 +398,7 @@ class ImportArrayStorageTest extends TestCase
$ruleRepos->shouldReceive('getForImport')->andReturn(new Collection([$ruleOne, $ruleTwo]));
$journalRepos->shouldReceive('setUser')->once();
$journalRepos->shouldReceive('store')->once()->andReturn($journal);
$journalRepos->shouldReceive('findByHash')->andReturn(null)->once();
$journalRepos->shouldReceive('findByHash')->andReturn(null)->times(2);
$storage = new ImportArrayStorage;
$storage->setImportJob($job);
@ -384,91 +465,12 @@ class ImportArrayStorageTest extends TestCase
$ruleRepos->shouldReceive('getForImport')->andReturn(new Collection([$ruleOne, $ruleTwo]));
$journalRepos->shouldReceive('setUser')->once();
$journalRepos->shouldReceive('store')->twice()->andReturn($journal);
$journalRepos->shouldReceive('findByHash')->andReturn(null)->twice();
// mock collector so it will return some transfers:
$collector->shouldReceive('setAllAssetAccounts')->once()->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::TRANSFER]])->once()->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->once()->andReturnSelf();
$collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->once()->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn($transferCollection);
$storage = new ImportArrayStorage;
$storage->setImportJob($job);
$result = new Collection;
try {
$result = $storage->store();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertCount(2, $result);
}
/**
* @covers \FireflyIII\Import\Storage\ImportArrayStorage
*/
public function testBasicStoreDoubleTransferWithRules(): void
{
// get a transfer:
/** @var TransactionJournal $transfer */
$transfer = $this->user()->transactionJournals()
->inRandomOrder()->where('transaction_type_id', 3)
->first();
// get transfer as a collection, so the compare routine works.
$journalCollector = new JournalCollector();
$journalCollector->setUser($this->user());
$journalCollector->setJournals(new Collection([$transfer]));
$transferCollection = $journalCollector->withOpposingAccount()->getJournals();
// make fake job
$job = new ImportJob;
$job->user()->associate($this->user());
$job->key = 'h_storage' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'fake';
$job->file_type = '';
$job->configuration = ['apply-rules' => true];
$job->transactions = [$this->singleTransfer(), $this->singleWithdrawal(), $this->basedOnTransfer($transfer)];
$job->save();
// get some stuff:
$tag = $this->user()->tags()->inRandomOrder()->first();
$journal = $this->user()->transactionJournals()->inRandomOrder()->first();
$ruleOne = new Rule;
$ruleOne->stop_processing = false;
$ruleTwo = new Rule;
$ruleTwo->stop_processing = true;
// mock stuff
$repository = $this->mock(ImportJobRepositoryInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$tagRepos = $this->mock(TagRepositoryInterface::class);
$ruleRepos = $this->mock(RuleRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
// mock calls:
$collector->shouldReceive('setUser')->once();
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('setStatus')->withAnyArgs();
$ruleRepos->shouldReceive('setUser')->once();
$tagRepos->shouldReceive('setUser')->once();
$tagRepos->shouldReceive('store')->once()->andReturn($tag);
$repository->shouldReceive('setTag')->once();
$ruleRepos->shouldReceive('getForImport')->andReturn(new Collection([$ruleOne, $ruleTwo]));
$journalRepos->shouldReceive('setUser')->once();
$journalRepos->shouldReceive('store')->twice()->andReturn($journal);
$journalRepos->shouldReceive('findByHash')->andReturn(null)->times(3);
$repository->shouldReceive('addErrorMessage')->withArgs(
[Mockery::any(), 'Row #2 ("' . $transfer->description . '") could not be imported. Such a transfer already exists.']
)->once();
$journalRepos->shouldReceive('findByHash')->andReturn(null)->times(4);
// mock collector so it will return some transfers:
$collector->shouldReceive('setAllAssetAccounts')->once()->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::TRANSFER]])->once()->andReturnSelf();
$collector->shouldReceive('ignoreCache')->once()->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->once()->andReturnSelf();
$collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->once()->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn($transferCollection);