Improve test coverage and quality

This commit is contained in:
James Cole
2019-06-16 13:16:46 +02:00
parent 1ce1a84c9e
commit 72c0d7a874
34 changed files with 3211 additions and 2524 deletions

View File

@@ -26,6 +26,7 @@ namespace Tests\Unit\Factory;
use Amount;
use Carbon\Carbon;
use FireflyIII\Factory\AccountFactory;
use FireflyIII\Factory\BudgetFactory;
use FireflyIII\Factory\CategoryFactory;
use FireflyIII\Factory\PiggyBankFactory;
@@ -35,12 +36,22 @@ use FireflyIII\Factory\TransactionTypeFactory;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Validation\AccountValidator;
use Log;
use Tests\TestCase;
/**
*
*
* Test different combinations:
* Transfer
* Withdrawal
* Deposit
*
* With the correct types.
*
* Class RecurrenceFactoryTest
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
class RecurrenceFactoryTest extends TestCase
{
@@ -55,18 +66,20 @@ class RecurrenceFactoryTest extends TestCase
/**
* With piggy bank. With tags. With budget. With category.
* This is a withdrawal
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testBasic(): void
public function testCreate(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$accountA = $this->user()->accounts()->inRandomOrder()->first();
$accountB = $this->user()->accounts()->inRandomOrder()->first();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category = $this->user()->categories()->inRandomOrder()->first();
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$source = $this->getRandomAsset();
$destination = $this->getRandomExpense();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category = $this->user()->categories()->inRandomOrder()->first();
$euro = $this->getEuro();
// mock other factories:
$piggyFactory = $this->mock(PiggyBankFactory::class);
@@ -74,221 +87,36 @@ class RecurrenceFactoryTest extends TestCase
$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(TransactionCurrency::find(1))->once();
$piggyFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro)->once();
$piggyFactory->shouldReceive('setUser')->atLeast()->once();
$budgetFactory->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('setUser')->twice();
$accountRepos->shouldReceive('findNull')->twice()->andReturn($accountA, $accountB);
$categoryFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->atLeast()->once()->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
$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);
// data for basic recurrence.
$data = [
'recurrence' => [
'type' => 'withdrawal',
'first_date' => Carbon::now()->addDay(),
'repetitions' => 0,
'title' => 'Test recurrence' . random_int(1, 100000),
'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 = $this->mock(TransactionTypeFactory::class);
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(1));
$factory = new RecurrenceFactory;
$factory->setUser($this->user());
$result = $factory->create($data);
$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 testBasicDeposit(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$accountA = $this->user()->accounts()->inRandomOrder()->first();
$accountB = $this->user()->accounts()->inRandomOrder()->first();
$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);
// mock calls:
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn(TransactionCurrency::find(1))->once();
$piggyFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
$accountRepos->shouldReceive('setUser')->twice();
$accountRepos->shouldReceive('findNull')->twice()->andReturn($accountA, $accountB);
$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->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' . random_int(1, 100000),
'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 = $this->mock(TransactionTypeFactory::class);
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(2));
$factory = new RecurrenceFactory;
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
* No piggy bank. With tags. With budget. With category.
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testBasicNoPiggybank(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$accountA = $this->user()->accounts()->inRandomOrder()->first();
$accountB = $this->user()->accounts()->inRandomOrder()->first();
$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);
// mock calls:
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn(TransactionCurrency::find(1))->once();
$piggyFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn(null);
$accountRepos->shouldReceive('setUser')->twice();
$accountRepos->shouldReceive('findNull')->twice()->andReturn($accountA, $accountB);
$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);
// data for basic recurrence.
$data = [
'recurrence' => [
'type' => 'withdrawal',
'first_date' => Carbon::now()->addDay(),
'repetitions' => 0,
'title' => 'Test recurrence' . random_int(1, 100000),
'title' => 'Test recurrence' . $this->randomInt(),
'description' => 'Description thing',
'apply_rules' => true,
'active' => true,
@@ -312,7 +140,7 @@ class RecurrenceFactoryTest extends TestCase
'source_id' => 1,
'source_name' => 'Some name',
'destination_id' => 2,
'destination_name' => 'some otjer name',
'destination_name' => 'some other name',
'currency_id' => 1,
'currency_code' => 'EUR',
'foreign_currency_id' => null,
@@ -328,9 +156,9 @@ class RecurrenceFactoryTest extends TestCase
],
],
];
$typeFactory = $this->mock(TransactionTypeFactory::class);
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(1));
$factory = new RecurrenceFactory;
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
$factory->setUser($this->user());
$result = $factory->create($data);
@@ -339,18 +167,20 @@ class RecurrenceFactoryTest extends TestCase
/**
* With piggy bank. With tags. With budget. With category.
* Submit account names, not types. This is a withdrawal.
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testBasicNoTags(): void
public function testCreateByName(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$accountA = $this->user()->accounts()->inRandomOrder()->first();
$accountB = $this->user()->accounts()->inRandomOrder()->first();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category = $this->user()->categories()->inRandomOrder()->first();
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$source = $this->getRandomAsset();
$destination = $this->getRandomExpense();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category = $this->user()->categories()->inRandomOrder()->first();
$euro = $this->getEuro();
// mock other factories:
$piggyFactory = $this->mock(PiggyBankFactory::class);
@@ -358,6 +188,230 @@ class RecurrenceFactoryTest extends TestCase
$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($euro)->once();
$piggyFactory->shouldReceive('setUser')->atLeast()->once();
$budgetFactory->shouldReceive('setUser')->atLeast()->once();
$categoryFactory->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('setUser')->twice();
//$accountFactory->shouldReceive('setUser')->atLeast()->once();
$piggyFactory->shouldReceive('find')->atLeast()->once()->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
// return NULL for account ID's.
$accountRepos->shouldReceive('findNull')->twice()->andReturn(null, null);
// but find them by name:
$accountRepos->shouldReceive('findByName')->twice()->andReturn($source, $destination);
$currencyFactory->shouldReceive('find')->once()->withArgs([1, 'EUR'])->andReturn(null);
$currencyFactory->shouldReceive('find')->once()->withArgs([null, null])->andReturn(null);
$budgetFactory->shouldReceive('find')->withArgs([1, 'Some budget'])->once()->andReturn($budget);
$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' => 'withdrawal',
'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 other 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(1));
/** @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
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testCreateNewByName(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$source = $this->getRandomAsset();
$destination = $this->getRandomExpense();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category = $this->user()->categories()->inRandomOrder()->first();
$euro = $this->getEuro();
// 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($euro)->once();
$piggyFactory->shouldReceive('setUser')->atLeast()->once();
$budgetFactory->shouldReceive('setUser')->atLeast()->once();
$categoryFactory->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('setUser')->twice();
$piggyFactory->shouldReceive('find')->atLeast()->once()->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
// return NULL for account ID's.
$accountRepos->shouldReceive('findNull')->twice()->andReturn(null, null);
// but find them by name (at least the first one):
$accountRepos->shouldReceive('findByName')->twice()->andReturn($source, null);
// this activates the "create by name" routine (account factory):
$accountFactory->shouldReceive('setUser')->atLeast()->once();
$accountFactory->shouldReceive('findOrCreate')->atLeast()->once()
->andReturn($destination);
$currencyFactory->shouldReceive('find')->once()->withArgs([1, 'EUR'])->andReturn(null);
$currencyFactory->shouldReceive('find')->once()->withArgs([null, null])->andReturn(null);
$budgetFactory->shouldReceive('find')->withArgs([1, 'Some budget'])->once()->andReturn($budget);
$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' => 'withdrawal',
'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 other 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(1));
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
$factory->setUser($this->user());
$result = $factory->create($data);
$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(TransactionCurrency::find(1))->once();
@@ -365,7 +419,7 @@ class RecurrenceFactoryTest extends TestCase
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
$accountRepos->shouldReceive('setUser')->twice();
$accountRepos->shouldReceive('findNull')->twice()->andReturn($accountA, $accountB);
$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);
@@ -376,13 +430,230 @@ class RecurrenceFactoryTest extends TestCase
$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 = [
$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.
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testCreateNoPiggybank(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$source = $this->getRandomAsset();
$destination = $this->getRandomExpense();
$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(TransactionCurrency::find(1))->once();
$piggyFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn(null);
$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' => 'withdrawal',
'first_date' => Carbon::now()->addDay(),
'repetitions' => 0,
'title' => 'Test recurrence' . random_int(1, 100000),
'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(1));
/** @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. Withdrawal
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testCreateNoTags(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$source = $this->getRandomAsset();
$destination = $this->getRandomExpense();
$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(TransactionCurrency::find(1))->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' => 'withdrawal',
'first_date' => Carbon::now()->addDay(),
'repetitions' => 0,
'title' => 'Test recurrence' . $this->randomInt(),
'description' => 'Description thing',
'apply_rules' => true,
'active' => true,
@@ -422,9 +693,10 @@ class RecurrenceFactoryTest extends TestCase
],
],
];
$typeFactory = $this->mock(TransactionTypeFactory::class);
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(1));
$factory = new RecurrenceFactory;
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
$factory->setUser($this->user());
$result = $factory->create($data);
@@ -437,14 +709,15 @@ class RecurrenceFactoryTest extends TestCase
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testBasicTransfer(): void
public function testCreateTransfer(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$accountA = $this->user()->accounts()->inRandomOrder()->first();
$accountB = $this->user()->accounts()->inRandomOrder()->first();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category = $this->user()->categories()->inRandomOrder()->first();
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$source = $this->getRandomAsset();
$destination = $this->getRandomAsset($source->id);
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category = $this->user()->categories()->inRandomOrder()->first();
$validator = $this->mock(AccountValidator::class);
// mock other factories:
$piggyFactory = $this->mock(PiggyBankFactory::class);
@@ -452,6 +725,8 @@ class RecurrenceFactoryTest extends TestCase
$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);
// mock calls:
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn(TransactionCurrency::find(1))->once();
@@ -459,7 +734,7 @@ class RecurrenceFactoryTest extends TestCase
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
$accountRepos->shouldReceive('setUser')->twice();
$accountRepos->shouldReceive('findNull')->twice()->andReturn($accountA, $accountB);
$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);
@@ -470,13 +745,19 @@ class RecurrenceFactoryTest extends TestCase
$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' => 'transfer',
'first_date' => Carbon::now()->addDay(),
'repetitions' => 0,
'title' => 'Test recurrence' . random_int(1, 100000),
'title' => 'Test recurrence' . $this->randomInt(),
'description' => 'Description thing',
'apply_rules' => true,
'active' => true,
@@ -517,10 +798,12 @@ class RecurrenceFactoryTest extends TestCase
],
];
$typeFactory = $this->mock(TransactionTypeFactory::class);
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(3));
$factory = new RecurrenceFactory;
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
$factory->setUser($this->user());
$result = $factory->create($data);
@@ -532,17 +815,21 @@ class RecurrenceFactoryTest extends TestCase
*/
public function testCreateBadTransactionType(): void
{
$data = [
$accountFactory = $this->mock(AccountFactory::class);
$validator = $this->mock(AccountValidator::class);
$typeFactory = $this->mock(TransactionTypeFactory::class);
$data = [
'recurrence' => [
'type' => 'bad type',
],
];
$typeFactory = $this->mock(TransactionTypeFactory::class);
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(null);
$factory = new RecurrenceFactory;
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
$factory->setUser($this->user());
$result = $factory->create($data);