Improve test coverage.

This commit is contained in:
James Cole
2019-08-29 17:53:25 +02:00
parent 91b6b86202
commit 19feefda2d
86 changed files with 3173 additions and 2626 deletions

View File

@@ -34,7 +34,6 @@ use FireflyIII\Factory\PiggyBankFactory;
use FireflyIII\Factory\RecurrenceFactory;
use FireflyIII\Factory\TransactionCurrencyFactory;
use FireflyIII\Factory\TransactionTypeFactory;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Validation\AccountValidator;
@@ -124,11 +123,6 @@ class RecurrenceFactoryTest extends TestCase
'active' => true,
'repeat_until' => null,
],
'meta' => [
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
'repetitions' => [
[
'type' => 'daily',
@@ -154,6 +148,9 @@ class RecurrenceFactoryTest extends TestCase
'budget_name' => 'Some budget',
'category_id' => 2,
'category_name' => 'Some category',
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
],
@@ -167,6 +164,38 @@ class RecurrenceFactoryTest extends TestCase
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
* @covers \FireflyIII\Factory\RecurrenceFactory
*/
public function testCreateBadTransactionType(): void
{
$accountFactory = $this->mock(AccountFactory::class);
$validator = $this->mock(AccountValidator::class);
$typeFactory = $this->mock(TransactionTypeFactory::class);
$data = [
'recurrence' => [
'type' => 'bad type',
],
];
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(null);
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
$factory->setUser($this->user());
$result = null;
Log::warning('The following error is part of a test.');
try {
$result = $factory->create($data);
} catch (FireflyException $e) {
$this->assertEquals('Cannot make a recurring transaction of type "bad type"', $e->getMessage());
$this->assertTrue(true);
}
$this->assertNull($result);
}
/**
* With piggy bank. With tags. With budget. With category.
* Submit account names, not types. This is a withdrawal.
@@ -232,11 +261,6 @@ class RecurrenceFactoryTest extends TestCase
'active' => true,
'repeat_until' => null,
],
'meta' => [
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
'repetitions' => [
[
'type' => 'daily',
@@ -262,6 +286,9 @@ class RecurrenceFactoryTest extends TestCase
'budget_name' => 'Some budget',
'category_id' => 2,
'category_name' => 'Some category',
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
],
@@ -275,6 +302,110 @@ class RecurrenceFactoryTest extends TestCase
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
* Deposit. With piggy bank. With tags. With budget. With category.
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testCreateDeposit(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$source = $this->getRandomRevenue();
$destination = $this->getRandomAsset();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category = $this->user()->categories()->inRandomOrder()->first();
// mock other factories:
$piggyFactory = $this->mock(PiggyBankFactory::class);
$budgetFactory = $this->mock(BudgetFactory::class);
$categoryFactory = $this->mock(CategoryFactory::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
$typeFactory = $this->mock(TransactionTypeFactory::class);
$accountFactory = $this->mock(AccountFactory::class);
$validator = $this->mock(AccountValidator::class);
// mock calls:
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($this->getEuro())->once();
$piggyFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
$accountRepos->shouldReceive('setUser')->twice();
$accountRepos->shouldReceive('findNull')->twice()->andReturn($source, $destination);
$currencyFactory->shouldReceive('find')->once()->withArgs([1, 'EUR'])->andReturn(null);
$currencyFactory->shouldReceive('find')->once()->withArgs([null, null])->andReturn(null);
$budgetFactory->shouldReceive('setUser')->once();
$budgetFactory->shouldReceive('find')->withArgs([1, 'Some budget'])->once()->andReturn($budget);
$categoryFactory->shouldReceive('setUser')->once();
$categoryFactory->shouldReceive('findOrCreate')->withArgs([2, 'Some category'])->once()->andReturn($category);
// validator:
$validator->shouldReceive('setUser')->once();
$validator->shouldReceive('setTransactionType')->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
// data for basic recurrence.
$data = [
'recurrence' => [
'type' => 'deposit',
'first_date' => Carbon::now()->addDay(),
'repetitions' => 0,
'title' => 'Test recurrence' . $this->randomInt(),
'description' => 'Description thing',
'apply_rules' => true,
'active' => true,
'repeat_until' => null,
],
'repetitions' => [
[
'type' => 'daily',
'moment' => '',
'skip' => 0,
'weekend' => 1,
],
],
'transactions' => [
[
'source_id' => 1,
'source_name' => 'Some name',
'destination_id' => 2,
'destination_name' => 'some otjer name',
'currency_id' => 1,
'currency_code' => 'EUR',
'foreign_currency_id' => null,
'foreign_currency_code' => null,
'foreign_amount' => null,
'description' => 'Bla bla bla',
'amount' => '100',
'budget_id' => 1,
'budget_name' => 'Some budget',
'category_id' => 2,
'category_name' => 'Some category',
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
],
];
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(2));
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
* With piggy bank. With tags. With budget. With category.
* Submit account names, not types. Also a withdrawal
@@ -346,11 +477,6 @@ class RecurrenceFactoryTest extends TestCase
'active' => true,
'repeat_until' => null,
],
'meta' => [
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
'repetitions' => [
[
'type' => 'daily',
@@ -376,6 +502,9 @@ class RecurrenceFactoryTest extends TestCase
'budget_name' => 'Some budget',
'category_id' => 2,
'category_name' => 'Some category',
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
],
@@ -389,113 +518,6 @@ class RecurrenceFactoryTest extends TestCase
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
* Deposit. With piggy bank. With tags. With budget. With category.
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testCreateDeposit(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$source = $this->getRandomRevenue();
$destination = $this->getRandomAsset();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category = $this->user()->categories()->inRandomOrder()->first();
// mock other factories:
$piggyFactory = $this->mock(PiggyBankFactory::class);
$budgetFactory = $this->mock(BudgetFactory::class);
$categoryFactory = $this->mock(CategoryFactory::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
$typeFactory = $this->mock(TransactionTypeFactory::class);
$accountFactory = $this->mock(AccountFactory::class);
$validator = $this->mock(AccountValidator::class);
// mock calls:
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($this->getEuro())->once();
$piggyFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
$accountRepos->shouldReceive('setUser')->twice();
$accountRepos->shouldReceive('findNull')->twice()->andReturn($source, $destination);
$currencyFactory->shouldReceive('find')->once()->withArgs([1, 'EUR'])->andReturn(null);
$currencyFactory->shouldReceive('find')->once()->withArgs([null, null])->andReturn(null);
$budgetFactory->shouldReceive('setUser')->once();
$budgetFactory->shouldReceive('find')->withArgs([1, 'Some budget'])->once()->andReturn($budget);
$categoryFactory->shouldReceive('setUser')->once();
$categoryFactory->shouldReceive('findOrCreate')->withArgs([2, 'Some category'])->once()->andReturn($category);
// validator:
$validator->shouldReceive('setUser')->once();
$validator->shouldReceive('setTransactionType')->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
// data for basic recurrence.
$data = [
'recurrence' => [
'type' => 'deposit',
'first_date' => Carbon::now()->addDay(),
'repetitions' => 0,
'title' => 'Test recurrence' . $this->randomInt(),
'description' => 'Description thing',
'apply_rules' => true,
'active' => true,
'repeat_until' => null,
],
'meta' => [
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
'repetitions' => [
[
'type' => 'daily',
'moment' => '',
'skip' => 0,
'weekend' => 1,
],
],
'transactions' => [
[
'source_id' => 1,
'source_name' => 'Some name',
'destination_id' => 2,
'destination_name' => 'some otjer name',
'currency_id' => 1,
'currency_code' => 'EUR',
'foreign_currency_id' => null,
'foreign_currency_code' => null,
'foreign_amount' => null,
'description' => 'Bla bla bla',
'amount' => '100',
'budget_id' => 1,
'budget_name' => 'Some budget',
'category_id' => 2,
'category_name' => 'Some category',
],
],
];
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(2));
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
* No piggy bank. With tags. With budget. With category. Withdrawal.
*
@@ -546,7 +568,7 @@ class RecurrenceFactoryTest extends TestCase
// data for basic recurrence.
$data = [
'recurrence' => [
'recurrence' => [
'type' => 'withdrawal',
'first_date' => Carbon::now()->addDay(),
'repetitions' => 0,
@@ -556,11 +578,7 @@ class RecurrenceFactoryTest extends TestCase
'active' => true,
'repeat_until' => null,
],
'meta' => [
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
'repetitions' => [
[
'type' => 'daily',
@@ -586,6 +604,9 @@ class RecurrenceFactoryTest extends TestCase
'budget_name' => 'Some budget',
'category_id' => 2,
'category_name' => 'Some category',
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
],
@@ -661,11 +682,6 @@ class RecurrenceFactoryTest extends TestCase
'active' => true,
'repeat_until' => null,
],
'meta' => [
'tags' => [],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
'repetitions' => [
[
'type' => 'daily',
@@ -691,6 +707,9 @@ class RecurrenceFactoryTest extends TestCase
'budget_name' => 'Some budget',
'category_id' => 2,
'category_name' => 'Some category',
'tags' => [],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
],
@@ -755,7 +774,7 @@ class RecurrenceFactoryTest extends TestCase
// data for basic recurrence.
$data = [
'recurrence' => [
'recurrence' => [
'type' => 'transfer',
'first_date' => Carbon::now()->addDay(),
'repetitions' => 0,
@@ -765,11 +784,7 @@ class RecurrenceFactoryTest extends TestCase
'active' => true,
'repeat_until' => null,
],
'meta' => [
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
'repetitions' => [
[
'type' => 'daily',
@@ -795,6 +810,9 @@ class RecurrenceFactoryTest extends TestCase
'budget_name' => 'Some budget',
'category_id' => 2,
'category_name' => 'Some category',
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
],
@@ -812,35 +830,4 @@ class RecurrenceFactoryTest extends TestCase
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
* @covers \FireflyIII\Factory\RecurrenceFactory
*/
public function testCreateBadTransactionType(): void
{
$accountFactory = $this->mock(AccountFactory::class);
$validator = $this->mock(AccountValidator::class);
$typeFactory = $this->mock(TransactionTypeFactory::class);
$data = [
'recurrence' => [
'type' => 'bad type',
],
];
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(null);
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
$factory->setUser($this->user());
$result = null;
try {
$result = $factory->create($data);
} catch (FireflyException $e) {
$this->assertEquals('Cannot make a recurring transaction of type "bad type"', $e->getMessage());
$this->assertTrue(true);
}
$this->assertNull($result);
}
}