Fix tests.

This commit is contained in:
James Cole
2018-07-01 09:27:22 +02:00
parent 0502f2a4a5
commit db149ca6e1
50 changed files with 551 additions and 647 deletions

View File

@@ -696,7 +696,7 @@ class TransactionFactoryTest extends TestCase
}
/**
* Create transfer using minimal data.
* Create reconciliation using minimal data.
*
* @covers \FireflyIII\Factory\TransactionFactory
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait

View File

@@ -46,7 +46,7 @@ class SpectreRoutineTest extends TestCase
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'SRA' . random_int(1, 1000);
$job->key = 'SR1A' . random_int(1, 1000);
$job->status = 'ready_to_run';
$job->stage = 'do-authenticate';
$job->provider = 'spectre';
@@ -77,7 +77,7 @@ class SpectreRoutineTest extends TestCase
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'SRA' . random_int(1, 1000);
$job->key = 'SR2b' . random_int(1, 1000);
$job->status = 'ready_to_run';
$job->stage = 'authenticated';
$job->provider = 'spectre';
@@ -115,7 +115,7 @@ class SpectreRoutineTest extends TestCase
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'SRA' . random_int(1, 1000);
$job->key = 'SR3c' . random_int(1, 1000);
$job->status = 'ready_to_run';
$job->stage = 'go-for-import';
$job->provider = 'spectre';
@@ -154,7 +154,7 @@ class SpectreRoutineTest extends TestCase
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'SRA' . random_int(1, 1000);
$job->key = 'SR4A' . random_int(1, 1000);
$job->status = 'ready_to_run';
$job->stage = 'new';
$job->provider = 'spectre';
@@ -194,7 +194,7 @@ class SpectreRoutineTest extends TestCase
{
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'SRA' . random_int(1, 1000);
$job->key = 'SR5A' . random_int(1, 1000);
$job->status = 'ready_to_run';
$job->stage = 'new';
$job->provider = 'spectre';

View File

@@ -76,6 +76,7 @@ class SandstormTest extends TestCase
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('count')->twice()->andReturn(1);
$repository->shouldReceive('hasRole')->andReturn(true);
$response = $this->get('/_test/sandstorm');
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
@@ -128,6 +129,7 @@ class SandstormTest extends TestCase
$repository->shouldReceive('store')->once()->andReturn($this->user());
$repository->shouldReceive('attachRole')->twice()->andReturn(true);
$repository->shouldReceive('getRole')->once()->andReturn(new Role);
$repository->shouldReceive('hasRole')->andReturn(false);
$response = $this->get('/_test/sandstorm', ['X-Sandstorm-User-Id' => 'abcd']);
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
@@ -156,6 +158,7 @@ class SandstormTest extends TestCase
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('count')->twice()->andReturn(1);
$repository->shouldReceive('first')->once()->andReturn($this->user());
$repository->shouldReceive('hasRole')->andReturn(true);
$response = $this->get('/_test/sandstorm', ['X-Sandstorm-User-Id' => 'abcd']);
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());

View File

@@ -127,12 +127,14 @@ class ChooseAccountsHandlerTest extends TestCase
'account_mapping' => [
'1234' => '456',
],
'apply_rules' => true,
];
$config = [
'accounts' => [
'accounts' => [
0 => ['id' => 1234, 'name' => 'bunq'],
],
'apply-rules' => true,
];
$expected = $config;
$expected['mapping'][1234] = 456;
@@ -179,12 +181,14 @@ class ChooseAccountsHandlerTest extends TestCase
'account_mapping' => [
'1234' => '456',
],
'apply_rules' => true,
];
$config = [
'accounts' => [
'accounts' => [
0 => ['id' => 1235, 'name' => 'bunq'],
],
'apply-rules' => true,
];
$expected = $config;
$expected['mapping'][0] = 456;
@@ -231,12 +235,14 @@ class ChooseAccountsHandlerTest extends TestCase
'account_mapping' => [
'1234' => '456',
],
'apply_rules' => true,
];
$config = [
'accounts' => [
'accounts' => [
0 => ['id' => 1234, 'name' => 'bunq'],
],
'apply-rules' => true,
];
$expected = $config;
$expected['mapping'][1234] = 0;
@@ -279,11 +285,13 @@ class ChooseAccountsHandlerTest extends TestCase
$job->save();
// data:
$data = ['account_mapping' => []];
$data = ['account_mapping' => [], 'apply_rules' => true,];
$config = [
'accounts' => [
0 => ['id' => 1234, 'name' => 'bunq'],
],
'apply-rules' => true,
];
// mock stuff

View File

@@ -48,16 +48,14 @@ class NewBunqJobHandlerTest extends TestCase
$job->save();
// expected config:
$expected = [
'apply-rules' => true,
];
//$expected = [];
// mock stuff
$repository = $this->mock(ImportJobRepositoryInterface::class);
// mock calls
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('getConfiguration')->andReturn([])->once();
$repository->shouldReceive('setConfiguration')->withArgs([Mockery::any(), $expected])->once();
//$repository->shouldReceive('getConfiguration')->andReturn([])->once();
//$repository->shouldReceive('setConfiguration')->withArgs([Mockery::any(), $expected])->once();
$handler = new NewBunqJobHandler();
$handler->setImportJob($job);

View File

@@ -148,11 +148,13 @@ class ChooseAccountsHandlerTest extends TestCase
// data to submit:
$data = [
'account_mapping' => [3131 => 872,],
'apply_rules' => true,
];
// expected configuration:
$config = [
'accounts' => [0 => ['id' => 3131, 'name' => 'Some fake account',],],
'account_mapping' => [3131 => 872,],
'apply-rules' => true,
];
@@ -175,6 +177,65 @@ class ChooseAccountsHandlerTest extends TestCase
$this->assertCount(0, $handler->configureJob($data));
}
/**
* Case: Local account is invalid. Spectre account is invalid.
*
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseAccountsHandler
*/
public function testConfigureJobInvalidBoth(): void
{
// fake job:
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'sca-E' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'spectre';
$job->file_type = '';
$job->configuration = [
'accounts' => [
0 => [
'id' => 3134,
'name' => 'Some fake account',
],
],
];
$job->save();
// data to submit:
$data = [
'account_mapping' => [3131 => 872,],
'apply_rules' => true,
];
// expected configuration:
$config = [
'accounts' => [0 => ['id' => 3134, 'name' => 'Some fake account',],],
'account_mapping' => [0 => 0,],
'apply-rules' => true,
];
// mock repositories:
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$importRepos = $this->mock(ImportJobRepositoryInterface::class);
// mock calls:
$accountRepos->shouldReceive('setUser')->once();
$currencyRepos->shouldReceive('setUser')->once();
$importRepos->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('findNull')->once()->withArgs([872])->andReturn(null);
$importRepos->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $config]);
// call handler:
$handler = new ChooseAccountsHandler();
$handler->setImportJob($job);
$result = $handler->configureJob($data);
$this->assertCount(1, $result);
$this->assertEquals('It seems you have not selected any accounts to import from.', $result->first());
}
/**
* Case: Local account is invalid. Spectre account is valid.
*
@@ -203,11 +264,13 @@ class ChooseAccountsHandlerTest extends TestCase
// data to submit:
$data = [
'account_mapping' => [3131 => 872,],
'apply_rules' => true,
];
// expected configuration:
$config = [
'accounts' => [0 => ['id' => 3131, 'name' => 'Some fake account',],],
'account_mapping' => [3131 => 0,],
'apply-rules' => true,
];
@@ -260,11 +323,13 @@ class ChooseAccountsHandlerTest extends TestCase
// data to submit:
$data = [
'account_mapping' => [3131 => 872,],
'apply_rules' => true,
];
// expected configuration:
$config = [
'accounts' => [0 => ['id' => 3134, 'name' => 'Some fake account',],],
'account_mapping' => [0 => 872,],
'apply-rules' => true,
];
@@ -287,64 +352,6 @@ class ChooseAccountsHandlerTest extends TestCase
$this->assertCount(0, $handler->configureJob($data));
}
/**
* Case: Local account is invalid. Spectre account is invalid.
*
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseAccountsHandler
*/
public function testConfigureJobInvalidBoth(): void
{
// fake job:
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'sca-E' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'spectre';
$job->file_type = '';
$job->configuration = [
'accounts' => [
0 => [
'id' => 3134,
'name' => 'Some fake account',
],
],
];
$job->save();
// data to submit:
$data = [
'account_mapping' => [3131 => 872,],
];
// expected configuration:
$config = [
'accounts' => [0 => ['id' => 3134, 'name' => 'Some fake account',],],
'account_mapping' => [0 => 0,],
];
// mock repositories:
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$importRepos = $this->mock(ImportJobRepositoryInterface::class);
// mock calls:
$accountRepos->shouldReceive('setUser')->once();
$currencyRepos->shouldReceive('setUser')->once();
$importRepos->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('findNull')->once()->withArgs([872])->andReturn(null);
$importRepos->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $config]);
// call handler:
$handler = new ChooseAccountsHandler();
$handler->setImportJob($job);
$result =$handler->configureJob($data);
$this->assertCount(1, $result);
$this->assertEquals('It seems you have not selected any accounts to import from.', $result->first());
}
/**
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseAccountsHandler
*/

View File

@@ -61,8 +61,7 @@ class CurrencyMapperTest extends TestCase
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('findNull')->once()->withArgs([$currency->id])->andReturn(null);
$repository->shouldReceive('store')->once()
->withArgs([['code' => null, 'name' => null, 'symbol' => null, 'decimal_places' => 2]])->andReturn(null);
$mapper = new CurrencyMapper();
$mapper->setUser($this->user());
@@ -161,9 +160,6 @@ class CurrencyMapperTest extends TestCase
// mock data
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('store')->once()
->withArgs([['code' => null, 'name' => null, 'symbol' => null, 'decimal_places' => 2]])->andReturn(null);
$mapper = new CurrencyMapper();
$mapper->setUser($this->user());

View File

@@ -148,6 +148,7 @@ class ImportableConverterTest extends TestCase
$opposingMapper->shouldReceive('map')->once()->withArgs([null, '45.67', $nullAccount])->andReturn($other);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['name' => null, 'code' => null, 'symbol' => null]])->andReturn(null);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['code' => null]])->andReturn(null);
$currencyMapper->shouldReceive('map')->times(2)->withArgs([$euro->id, []])->andReturn($euro);
$converter = new ImportableConverter;
@@ -173,7 +174,7 @@ class ImportableConverterTest extends TestCase
$importable = new ImportTransaction;
$importable->amount = '45.67';
$importable->date = '20180917';
$importable->meta['date-book'] = '2018-01-02';
$importable->meta['date-book'] = '20180102';
$importables = [$importable];
$job = $this->user()->importJobs()->first();
@@ -221,7 +222,7 @@ class ImportableConverterTest extends TestCase
$this->assertEquals($usd->id, $result[0]['transactions'][0]['currency_id']);
$this->assertEquals($revenue->id, $result[0]['transactions'][0]['source_id']);
$this->assertEquals($asset->id, $result[0]['transactions'][0]['destination_id']);
$this->assertEquals($importable->meta['date-book'], $result[0]['book_date']);
$this->assertEquals('2018-01-02', $result[0]['book_date']);
}
@@ -291,8 +292,8 @@ class ImportableConverterTest extends TestCase
$importable = new ImportTransaction;
$importable->amount = '45.67';
$importable->date = '20180917';
$importable->billId = 2; // will be ignored because it's not valid.
$importable->billName = 'Some Bill'; // will be included because bill ID is not valid.
$importable->billId = 2; // will NOT be ignored despite it's not valid.
$importable->billName = 'Some Bill'; // will always be included even when bill ID is not valid.
$importables = [$importable];
$job = $this->user()->importJobs()->first();
@@ -337,7 +338,7 @@ class ImportableConverterTest extends TestCase
$this->assertEquals('transfer', $result[0]['type']);
$this->assertEquals('2018-09-17', $result[0]['date']);
$this->assertEquals([], $result[0]['tags']);
$this->assertNull($result[0]['bill_id']);
$this->assertEquals(2, $result[0]['bill_id']); // will NOT be ignored.
$this->assertEquals($importable->billName, $result[0]['bill_name']);
$this->assertEquals($usd->id, $result[0]['transactions'][0]['currency_id']);
// since amount is positive, $asset recieves the money
@@ -357,7 +358,7 @@ class ImportableConverterTest extends TestCase
$importable->amount = '-45.67';
$importable->date = '20180917';
$importable->billId = 3; // is added to array of valid values, see below.
$importable->billName = 'Some bill'; // will be ignored because ID is valid.
$importable->billName = 'Some bill'; // will be added even when ID is valid.
$importables = [$importable];
$validMappings = [
@@ -408,7 +409,7 @@ class ImportableConverterTest extends TestCase
$this->assertEquals('2018-09-17', $result[0]['date']);
$this->assertEquals([], $result[0]['tags']);
$this->assertEquals(3, $result[0]['bill_id']);
$this->assertNull($result[0]['bill_name']);
$this->assertEquals($importable->billName, $result[0]['bill_name']);
$this->assertEquals($usd->id, $result[0]['transactions'][0]['currency_id']);
// since amount is negative, $asset sends the money
$this->assertEquals($asset->id, $result[0]['transactions'][0]['source_id']);

View File

@@ -97,6 +97,7 @@ class StageNewHandlerTest extends TestCase
// create new customer:
$ncRequest->shouldReceive('setUser')->once();
$ncRequest->shouldReceive('getCustomer')->once()->andReturn($fakeCustomer);
$ncRequest->shouldReceive('call')->once();
// mock calls for repository:
$repository->shouldReceive('setUser')->once();

View File

@@ -36,7 +36,11 @@ class CategoryIsTest extends TestCase
*/
public function testTriggeredJournal(): void
{
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
do {
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
$transactions = $journal->transactions()->count();
} while ($transactions === 0);
$category = $journal->user->categories()->first();
$journal->categories()->detach();
$journal->categories()->save($category);
@@ -52,7 +56,11 @@ class CategoryIsTest extends TestCase
*/
public function testTriggeredNotJournal(): void
{
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
do {
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
$transactions = $journal->transactions()->count();
} while ($transactions === 0);
$category = $journal->user->categories()->first();
$otherCategory = $journal->user->categories()->where('id', '!=', $category->id)->first();
$journal->categories()->detach();
@@ -69,7 +77,11 @@ class CategoryIsTest extends TestCase
*/
public function testTriggeredTransaction(): void
{
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
do {
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
$transactions = $journal->transactions()->count();
} while ($transactions === 0);
$transaction = $journal->transactions()->first();
$category = $journal->user->categories()->first();

View File

@@ -36,9 +36,13 @@ class ToAccountIsTest extends TestCase
*/
public function testTriggered(): void
{
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
$transaction = $journal->transactions()->where('amount', '>', 0)->first();
$account = $transaction->account;
$count = 0;
do {
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
$transaction = $journal->transactions()->where('amount', '>', 0)->first();
$account = $transaction->account;
$count++;
} while ($account === null && $count < 30);
$trigger = ToAccountIs::makeFromStrings($account->name, false);
$result = $trigger->triggered($journal);
@@ -50,7 +54,13 @@ class ToAccountIsTest extends TestCase
*/
public function testTriggeredNot(): void
{
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
$count = 0;
do {
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
$transaction = $journal->transactions()->where('amount', '>', 0)->first();
$account = $transaction->account;
$count++;
} while ($account === null && $count < 30);
$trigger = ToAccountIs::makeFromStrings('some name' . random_int(1, 234), false);
$result = $trigger->triggered($journal);

View File

@@ -68,44 +68,6 @@ class BillTransformerTest extends TestCase
$this->assertTrue($result['active']);
}
/**
* Basic coverage with a note.
*
* @covers \FireflyIII\Transformers\BillTransformer::transform
*/
public function testNote(): void
{
$bill = Bill::create(
[
'user_id' => $this->user()->id,
'name' => 'Some bill ' . random_int(1, 10000),
'match' => 'word,' . random_int(1, 10000),
'amount_min' => 12.34,
'amount_max' => 45.67,
'date' => '2018-01-02',
'transaction_currency_id' => 1,
'repeat_freq' => 'weekly',
'skip' => 0,
'active' => 1,
]
);
$noteText = 'I are a note ' . random_int(1, 10000);
Note::create(
[
'noteable_id' => $bill->id,
'noteable_type' => Bill::class,
'text' => $noteText,
]
);
$transformer = new BillTransformer(new ParameterBag);
$result = $transformer->transform($bill);
$this->assertEquals($bill->name, $result['name']);
$this->assertEquals($noteText, $result['notes']);
$this->assertTrue($result['active']);
}
/**
* Coverage for dates.
*