Files
firefly-iii/tests/Unit/Factory/RecurrenceFactoryTest.php

834 lines
36 KiB
PHP
Raw Normal View History

2018-08-24 07:18:33 +02:00
<?php
/**
* RecurrenceFactoryTest.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
2018-08-24 07:18:33 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-08-24 07:18:33 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-08-24 07:18:33 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-08-24 07:18:33 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-08-24 07:18:33 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-08-24 07:18:33 +02:00
*/
declare(strict_types=1);
namespace Tests\Unit\Factory;
use Amount;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
2019-06-16 13:16:46 +02:00
use FireflyIII\Factory\AccountFactory;
2018-08-24 07:18:33 +02:00
use FireflyIII\Factory\BudgetFactory;
use FireflyIII\Factory\CategoryFactory;
use FireflyIII\Factory\PiggyBankFactory;
use FireflyIII\Factory\RecurrenceFactory;
use FireflyIII\Factory\TransactionCurrencyFactory;
use FireflyIII\Factory\TransactionTypeFactory;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2019-06-16 13:16:46 +02:00
use FireflyIII\Validation\AccountValidator;
2018-08-24 07:18:33 +02:00
use Log;
use Tests\TestCase;
/**
2019-06-16 13:16:46 +02:00
*
* Test different combinations:
* Transfer
* Withdrawal
* Deposit
*
* With the correct types.
2018-08-24 07:18:33 +02:00
*
* Class RecurrenceFactoryTest
2019-08-17 10:48:28 +02:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2019-06-16 13:16:46 +02:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2019-08-17 10:48:28 +02:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-08-24 07:18:33 +02:00
*/
class RecurrenceFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-08-24 07:18:33 +02:00
}
/**
* With piggy bank. With tags. With budget. With category.
2019-06-16 13:16:46 +02:00
* This is a withdrawal
2018-08-24 07:18:33 +02:00
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
2019-06-16 13:16:46 +02:00
public function testCreate(): void
2018-08-24 07:18:33 +02:00
{
// objects to return:
2019-06-16 13:16:46 +02:00
$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();
2018-08-24 07:18:33 +02:00
// 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);
2019-06-16 13:16:46 +02:00
$typeFactory = $this->mock(TransactionTypeFactory::class);
$accountFactory = $this->mock(AccountFactory::class);
$validator = $this->mock(AccountValidator::class);
2018-08-24 07:18:33 +02:00
// mock calls:
2019-06-16 13:16:46 +02:00
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro)->once();
$piggyFactory->shouldReceive('setUser')->atLeast()->once();
$budgetFactory->shouldReceive('setUser')->atLeast()->once();
2018-08-24 07:18:33 +02:00
$accountRepos->shouldReceive('setUser')->twice();
2019-06-16 13:16:46 +02:00
$categoryFactory->shouldReceive('setUser')->once();
2018-08-24 07:18:33 +02:00
2019-06-16 13:16:46 +02:00
$piggyFactory->shouldReceive('find')->atLeast()->once()->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
$accountRepos->shouldReceive('findNull')->twice()->andReturn($source, $destination);
2018-08-24 07:18:33 +02:00
$currencyFactory->shouldReceive('find')->once()->withArgs([1, 'EUR'])->andReturn(null);
$currencyFactory->shouldReceive('find')->once()->withArgs([null, null])->andReturn(null);
2019-06-16 13:16:46 +02:00
$budgetFactory->shouldReceive('find')->withArgs([1, 'Some budget'])->once()->andReturn($budget);
$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);
2018-08-24 07:18:33 +02:00
2019-06-16 13:16:46 +02:00
// 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,
],
'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',
2019-08-29 17:53:25 +02:00
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
2019-06-16 13:16:46 +02:00
],
],
];
$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']);
}
2019-08-29 17:53:25 +02:00
/**
* @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);
}
2019-06-16 13:16:46 +02:00
/**
* 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 testCreateByName(): 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();
//$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);
2018-08-24 07:18:33 +02:00
$budgetFactory->shouldReceive('find')->withArgs([1, 'Some budget'])->once()->andReturn($budget);
2019-06-16 13:16:46 +02:00
$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,
],
'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',
2019-08-29 17:53:25 +02:00
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
2019-06-16 13:16:46 +02:00
],
],
];
$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']);
}
2018-08-24 07:18:33 +02:00
2019-06-16 13:16:46 +02:00
/**
2019-08-29 17:53:25 +02:00
* Deposit. With piggy bank. With tags. With budget. With category.
2019-06-16 13:16:46 +02:00
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
2019-08-29 17:53:25 +02:00
public function testCreateDeposit(): void
2019-06-16 13:16:46 +02:00
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
2019-08-29 17:53:25 +02:00
$source = $this->getRandomRevenue();
$destination = $this->getRandomAsset();
2019-06-16 13:16:46 +02:00
$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:
2019-08-29 17:53:25 +02:00
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($this->getEuro())->once();
$piggyFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
2019-06-16 13:16:46 +02:00
$accountRepos->shouldReceive('setUser')->twice();
2019-08-29 17:53:25 +02:00
$accountRepos->shouldReceive('findNull')->twice()->andReturn($source, $destination);
2019-06-16 13:16:46 +02:00
$currencyFactory->shouldReceive('find')->once()->withArgs([1, 'EUR'])->andReturn(null);
$currencyFactory->shouldReceive('find')->once()->withArgs([null, null])->andReturn(null);
2019-08-29 17:53:25 +02:00
$budgetFactory->shouldReceive('setUser')->once();
2019-06-16 13:16:46 +02:00
$budgetFactory->shouldReceive('find')->withArgs([1, 'Some budget'])->once()->andReturn($budget);
2019-08-29 17:53:25 +02:00
$categoryFactory->shouldReceive('setUser')->once();
2018-08-24 07:18:33 +02:00
$categoryFactory->shouldReceive('findOrCreate')->withArgs([2, 'Some category'])->once()->andReturn($category);
2019-06-16 13:16:46 +02:00
// validator:
$validator->shouldReceive('setUser')->once();
$validator->shouldReceive('setTransactionType')->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
2018-08-24 07:18:33 +02:00
// data for basic recurrence.
2019-06-16 13:16:46 +02:00
$data = [
2018-08-24 07:18:33 +02:00
'recurrence' => [
2019-08-29 17:53:25 +02:00
'type' => 'deposit',
2019-03-02 21:18:26 +01:00
'first_date' => Carbon::now()->addDay(),
2018-08-24 07:18:33 +02:00
'repetitions' => 0,
2019-06-16 13:16:46 +02:00
'title' => 'Test recurrence' . $this->randomInt(),
2018-08-24 07:18:33 +02:00
'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,
2019-08-29 17:53:25 +02:00
'destination_name' => 'some otjer name',
2018-08-24 07:18:33 +02:00
'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',
2019-08-29 17:53:25 +02:00
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
2018-08-24 07:18:33 +02:00
],
],
];
2019-08-29 17:53:25 +02:00
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(2));
2019-06-16 13:16:46 +02:00
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
2018-08-24 07:18:33 +02:00
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
2019-08-29 17:53:25 +02:00
* With piggy bank. With tags. With budget. With category.
* Submit account names, not types. Also a withdrawal
2018-08-24 07:18:33 +02:00
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
2019-08-29 17:53:25 +02:00
public function testCreateNewByName(): void
2018-08-24 07:18:33 +02:00
{
// objects to return:
2019-06-16 13:16:46 +02:00
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
2019-08-29 17:53:25 +02:00
$source = $this->getRandomAsset();
$destination = $this->getRandomExpense();
2019-06-16 13:16:46 +02:00
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category = $this->user()->categories()->inRandomOrder()->first();
2019-08-29 17:53:25 +02:00
$euro = $this->getEuro();
2018-08-24 07:18:33 +02:00
// 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);
2019-06-16 13:16:46 +02:00
$typeFactory = $this->mock(TransactionTypeFactory::class);
$accountFactory = $this->mock(AccountFactory::class);
$validator = $this->mock(AccountValidator::class);
2018-08-24 07:18:33 +02:00
// mock calls:
2019-08-29 17:53:25 +02:00
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($euro)->once();
$piggyFactory->shouldReceive('setUser')->atLeast()->once();
$budgetFactory->shouldReceive('setUser')->atLeast()->once();
$categoryFactory->shouldReceive('setUser')->once();
2018-08-24 07:18:33 +02:00
$accountRepos->shouldReceive('setUser')->twice();
2019-08-29 17:53:25 +02:00
$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);
2018-08-24 07:18:33 +02:00
$currencyFactory->shouldReceive('find')->once()->withArgs([1, 'EUR'])->andReturn(null);
$currencyFactory->shouldReceive('find')->once()->withArgs([null, null])->andReturn(null);
2019-08-29 17:53:25 +02:00
$budgetFactory->shouldReceive('find')->withArgs([1, 'Some budget'])->once()->andReturn($budget);
2018-08-24 07:18:33 +02:00
$categoryFactory->shouldReceive('findOrCreate')->withArgs([2, 'Some category'])->once()->andReturn($category);
2019-06-16 13:16:46 +02:00
// validator:
$validator->shouldReceive('setUser')->once();
$validator->shouldReceive('setTransactionType')->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
2018-08-24 07:18:33 +02:00
// data for basic recurrence.
2018-09-02 20:27:26 +02:00
$data = [
2018-08-24 07:18:33 +02:00
'recurrence' => [
2019-08-29 17:53:25 +02:00
'type' => 'withdrawal',
2019-03-02 21:18:26 +01:00
'first_date' => Carbon::now()->addDay(),
2018-08-24 07:18:33 +02:00
'repetitions' => 0,
2019-06-16 13:16:46 +02:00
'title' => 'Test recurrence' . $this->randomInt(),
2018-08-24 07:18:33 +02:00
'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,
2019-08-29 17:53:25 +02:00
'destination_name' => 'some other name',
2018-08-24 07:18:33 +02:00
'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',
2019-08-29 17:53:25 +02:00
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
2018-08-24 07:18:33 +02:00
],
],
];
2019-08-29 17:53:25 +02:00
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(1));
2019-06-16 13:16:46 +02:00
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
2018-08-24 07:18:33 +02:00
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
2019-06-16 13:16:46 +02:00
* No piggy bank. With tags. With budget. With category. Withdrawal.
2018-08-24 07:18:33 +02:00
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
2019-06-16 13:16:46 +02:00
public function testCreateNoPiggybank(): void
2018-08-24 07:18:33 +02:00
{
// objects to return:
2019-06-16 13:16:46 +02:00
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$source = $this->getRandomAsset();
$destination = $this->getRandomExpense();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category = $this->user()->categories()->inRandomOrder()->first();
2018-08-24 07:18:33 +02:00
// 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);
2019-06-16 13:16:46 +02:00
$typeFactory = $this->mock(TransactionTypeFactory::class);
$accountFactory = $this->mock(AccountFactory::class);
$validator = $this->mock(AccountValidator::class);
2018-08-24 07:18:33 +02:00
// mock calls:
2019-08-03 04:46:47 +02:00
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($this->getEuro())->once();
2018-08-24 07:18:33 +02:00
$piggyFactory->shouldReceive('setUser')->once();
2018-09-02 20:27:26 +02:00
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn(null);
2018-08-24 07:18:33 +02:00
$accountRepos->shouldReceive('setUser')->twice();
2019-06-16 13:16:46 +02:00
$accountRepos->shouldReceive('findNull')->twice()->andReturn($source, $destination);
2018-08-24 07:18:33 +02:00
$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);
2019-06-16 13:16:46 +02:00
// validator:
$validator->shouldReceive('setUser')->once();
$validator->shouldReceive('setTransactionType')->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
2018-08-24 07:18:33 +02:00
// data for basic recurrence.
2019-06-16 13:16:46 +02:00
$data = [
2019-08-29 17:53:25 +02:00
'recurrence' => [
2018-09-02 20:27:26 +02:00
'type' => 'withdrawal',
2019-03-02 21:18:26 +01:00
'first_date' => Carbon::now()->addDay(),
2018-08-24 07:18:33 +02:00
'repetitions' => 0,
2019-06-16 13:16:46 +02:00
'title' => 'Test recurrence' . $this->randomInt(),
2018-08-24 07:18:33 +02:00
'description' => 'Description thing',
'apply_rules' => true,
'active' => true,
'repeat_until' => null,
],
2019-08-29 17:53:25 +02:00
2018-08-24 07:18:33 +02:00
'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',
2019-08-29 17:53:25 +02:00
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
2018-08-24 07:18:33 +02:00
],
],
];
2019-06-16 13:16:46 +02:00
2018-09-02 20:27:26 +02:00
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(1));
2019-06-16 13:16:46 +02:00
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
2018-08-24 07:18:33 +02:00
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
2019-06-16 13:16:46 +02:00
* With piggy bank. With tags. With budget. With category. Withdrawal
2018-08-24 07:18:33 +02:00
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
2019-06-16 13:16:46 +02:00
public function testCreateNoTags(): void
2018-08-24 07:18:33 +02:00
{
// objects to return:
2019-06-16 13:16:46 +02:00
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$source = $this->getRandomAsset();
$destination = $this->getRandomExpense();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category = $this->user()->categories()->inRandomOrder()->first();
2018-08-24 07:18:33 +02:00
// 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);
2019-06-16 13:16:46 +02:00
$typeFactory = $this->mock(TransactionTypeFactory::class);
$accountFactory = $this->mock(AccountFactory::class);
$validator = $this->mock(AccountValidator::class);
2018-08-24 07:18:33 +02:00
// mock calls:
2019-08-03 04:46:47 +02:00
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($this->getEuro())->once();
2018-08-24 07:18:33 +02:00
$piggyFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
$accountRepos->shouldReceive('setUser')->twice();
2019-06-16 13:16:46 +02:00
$accountRepos->shouldReceive('findNull')->twice()->andReturn($source, $destination);
2018-08-24 07:18:33 +02:00
$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);
2019-06-16 13:16:46 +02:00
// validator:
$validator->shouldReceive('setUser')->once();
$validator->shouldReceive('setTransactionType')->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
2018-08-24 07:18:33 +02:00
// data for basic recurrence.
2019-06-16 13:16:46 +02:00
$data = [
2018-08-24 07:18:33 +02:00
'recurrence' => [
'type' => 'withdrawal',
2019-03-02 21:18:26 +01:00
'first_date' => Carbon::now()->addDay(),
2018-08-24 07:18:33 +02:00
'repetitions' => 0,
2019-06-16 13:16:46 +02:00
'title' => 'Test recurrence' . $this->randomInt(),
2018-08-24 07:18:33 +02:00
'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',
2019-08-29 17:53:25 +02:00
'tags' => [],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
2018-08-24 07:18:33 +02:00
],
],
];
2019-06-16 13:16:46 +02:00
2018-08-24 07:18:33 +02:00
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(1));
2019-06-16 13:16:46 +02:00
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
2018-08-24 07:18:33 +02:00
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
2018-09-02 20:27:26 +02:00
* Deposit. With piggy bank. With tags. With budget. With category.
2018-08-24 07:18:33 +02:00
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
2019-06-16 13:16:46 +02:00
public function testCreateTransfer(): void
2018-08-24 07:18:33 +02:00
{
// objects to return:
2019-06-16 13:16:46 +02:00
$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);
2018-08-24 07:18:33 +02:00
// 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);
2019-06-16 13:16:46 +02:00
$typeFactory = $this->mock(TransactionTypeFactory::class);
$accountFactory = $this->mock(AccountFactory::class);
2018-08-24 07:18:33 +02:00
// mock calls:
2019-08-03 04:46:47 +02:00
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($this->getEuro())->once();
2018-08-24 07:18:33 +02:00
$piggyFactory->shouldReceive('setUser')->once();
2018-09-02 20:27:26 +02:00
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
2018-08-24 07:18:33 +02:00
$accountRepos->shouldReceive('setUser')->twice();
2019-06-16 13:16:46 +02:00
$accountRepos->shouldReceive('findNull')->twice()->andReturn($source, $destination);
2018-08-24 07:18:33 +02:00
$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);
2019-06-16 13:16:46 +02:00
// validator:
$validator->shouldReceive('setUser')->once();
$validator->shouldReceive('setTransactionType')->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
2018-08-24 07:18:33 +02:00
// data for basic recurrence.
2018-09-02 20:27:26 +02:00
$data = [
2019-08-29 17:53:25 +02:00
'recurrence' => [
2018-09-02 20:27:26 +02:00
'type' => 'transfer',
2019-03-02 21:18:26 +01:00
'first_date' => Carbon::now()->addDay(),
2018-08-24 07:18:33 +02:00
'repetitions' => 0,
2019-06-16 13:16:46 +02:00
'title' => 'Test recurrence' . $this->randomInt(),
2018-08-24 07:18:33 +02:00
'description' => 'Description thing',
'apply_rules' => true,
'active' => true,
'repeat_until' => null,
],
2019-08-29 17:53:25 +02:00
2018-08-24 07:18:33 +02:00
'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',
2019-08-29 17:53:25 +02:00
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
2018-08-24 07:18:33 +02:00
],
],
];
2018-09-02 20:27:26 +02:00
2019-06-16 13:16:46 +02:00
2018-09-02 20:27:26 +02:00
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(3));
2019-06-16 13:16:46 +02:00
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
2018-08-24 07:18:33 +02:00
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($result->title, $data['recurrence']['title']);
}
2018-12-31 07:48:23 +01:00
}