diff --git a/app/Http/Controllers/Recurring/EditController.php b/app/Http/Controllers/Recurring/EditController.php
index ebae84cf4d..c77733b9c0 100644
--- a/app/Http/Controllers/Recurring/EditController.php
+++ b/app/Http/Controllers/Recurring/EditController.php
@@ -105,10 +105,10 @@ class EditController extends Controller
'times' => (string)trans('firefly.repeat_times'),
];
if (null !== $recurrence->repeat_until) {
- $repetitionEnd = 'until_date';
+ $repetitionEnd = 'until_date'; // @codeCoverageIgnore
}
if ($recurrence->repetitions > 0) {
- $repetitionEnd = 'times';
+ $repetitionEnd = 'times'; // @codeCoverageIgnore
}
$weekendResponses = [
diff --git a/app/Http/Controllers/Report/AccountController.php b/app/Http/Controllers/Report/AccountController.php
index 01b67723af..2f7e9f524e 100644
--- a/app/Http/Controllers/Report/AccountController.php
+++ b/app/Http/Controllers/Report/AccountController.php
@@ -63,10 +63,12 @@ class AccountController extends Controller
$accountReport = $accountTasker->getAccountReport($accounts, $start, $end);
try {
$result = view('reports.partials.accounts', compact('accountReport'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::debug(sprintf('Could not render reports.partials.accounts: %s', $e->getMessage()));
$result = 'Could not render view.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
return $result;
diff --git a/app/Http/Controllers/Report/BalanceController.php b/app/Http/Controllers/Report/BalanceController.php
index 1c08c89f27..0993cf4541 100644
--- a/app/Http/Controllers/Report/BalanceController.php
+++ b/app/Http/Controllers/Report/BalanceController.php
@@ -61,10 +61,12 @@ class BalanceController extends Controller
$balance = $helper->getBalanceReport($accounts, $start, $end);
try {
$result = view('reports.partials.balance', compact('balance'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::debug(sprintf('Could not render reports.partials.balance: %s', $e->getMessage()));
$result = 'Could not render view.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
return $result;
diff --git a/app/Http/Controllers/Report/BudgetController.php b/app/Http/Controllers/Report/BudgetController.php
index 3eddbc2839..d16ba0c81e 100644
--- a/app/Http/Controllers/Report/BudgetController.php
+++ b/app/Http/Controllers/Report/BudgetController.php
@@ -63,10 +63,12 @@ class BudgetController extends Controller
$budgets = $helper->getBudgetReport($start, $end, $accounts);
try {
$result = view('reports.partials.budgets', compact('budgets'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::debug(sprintf('Could not render reports.partials.budgets: %s', $e->getMessage()));
$result = 'Could not render view.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
return $result;
@@ -103,10 +105,12 @@ class BudgetController extends Controller
$periods = app('navigation')->listOfPeriods($start, $end);
try {
$result = view('reports.partials.budget-period', compact('report', 'periods'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage()));
$result = 'Could not render view.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
return $result;
diff --git a/app/Http/Controllers/Report/CategoryController.php b/app/Http/Controllers/Report/CategoryController.php
index 7214d556cb..941a851d78 100644
--- a/app/Http/Controllers/Report/CategoryController.php
+++ b/app/Http/Controllers/Report/CategoryController.php
@@ -67,10 +67,12 @@ class CategoryController extends Controller
$periods = app('navigation')->listOfPeriods($start, $end);
try {
$result = view('reports.partials.category-period', compact('report', 'periods'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage()));
$result = 'An error prevented Firefly III from rendering. Apologies.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
@@ -107,10 +109,12 @@ class CategoryController extends Controller
$periods = app('navigation')->listOfPeriods($start, $end);
try {
$result = view('reports.partials.category-period', compact('report', 'periods'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage()));
$result = 'An error prevented Firefly III from rendering. Apologies.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
return $result;
@@ -160,10 +164,12 @@ class CategoryController extends Controller
try {
$result = view('reports.partials.categories', compact('report'))->render();
$cache->store($result);
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage()));
$result = 'An error prevented Firefly III from rendering. Apologies.';
}
+ // @codeCoverageIgnoreEnd
return $result;
}
diff --git a/app/Http/Controllers/Report/ExpenseController.php b/app/Http/Controllers/Report/ExpenseController.php
index 2e2b3a9429..73de303fe2 100644
--- a/app/Http/Controllers/Report/ExpenseController.php
+++ b/app/Http/Controllers/Report/ExpenseController.php
@@ -107,10 +107,12 @@ class ExpenseController extends Controller
}
try {
$result = view('reports.partials.exp-budgets', compact('together'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::error(sprintf('Could not render category::budget: %s', $e->getMessage()));
$result = 'An error prevented Firefly III from rendering. Apologies.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
return $result;
@@ -170,10 +172,12 @@ class ExpenseController extends Controller
}
try {
$result = view('reports.partials.exp-categories', compact('together'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage()));
$result = 'An error prevented Firefly III from rendering. Apologies.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
return $result;
@@ -220,10 +224,12 @@ class ExpenseController extends Controller
}
try {
$result = view('reports.partials.exp-not-grouped', compact('result'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage()));
$result = 'An error prevented Firefly III from rendering. Apologies.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
return $result;
@@ -271,10 +277,12 @@ class ExpenseController extends Controller
);
try {
$result = view('reports.partials.top-transactions', compact('sorted'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::error(sprintf('Could not render category::topExpense: %s', $e->getMessage()));
$result = 'An error prevented Firefly III from rendering. Apologies.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
return $result;
@@ -320,10 +328,12 @@ class ExpenseController extends Controller
);
try {
$result = view('reports.partials.top-transactions', compact('sorted'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::error(sprintf('Could not render category::topIncome: %s', $e->getMessage()));
$result = 'An error prevented Firefly III from rendering. Apologies.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
return $result;
diff --git a/app/Http/Controllers/Report/OperationsController.php b/app/Http/Controllers/Report/OperationsController.php
index 383629446b..8b8eca74c1 100644
--- a/app/Http/Controllers/Report/OperationsController.php
+++ b/app/Http/Controllers/Report/OperationsController.php
@@ -81,10 +81,12 @@ class OperationsController extends Controller
$type = 'expense-entry';
try {
$result = view('reports.partials.income-expenses', compact('entries', 'type'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::debug(sprintf('Could not render reports.partials.income-expense: %s', $e->getMessage()));
$result = 'Could not render view.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
return $result;
@@ -114,10 +116,12 @@ class OperationsController extends Controller
$type = 'income-entry';
try {
$result = view('reports.partials.income-expenses', compact('entries', 'type'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::debug(sprintf('Could not render reports.partials.income-expenses: %s', $e->getMessage()));
$result = 'Could not render view.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
@@ -166,10 +170,12 @@ class OperationsController extends Controller
);
try {
$result = view('reports.partials.operations', compact('incomeSum', 'expensesSum'))->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::debug(sprintf('Could not render reports.partials.operations: %s', $e->getMessage()));
$result = 'Could not render view.';
}
+ // @codeCoverageIgnoreEnd
$cache->store($result);
return $result;
diff --git a/app/Http/Controllers/Rule/CreateController.php b/app/Http/Controllers/Rule/CreateController.php
index 2cc5c1eb06..3656586d6f 100644
--- a/app/Http/Controllers/Rule/CreateController.php
+++ b/app/Http/Controllers/Rule/CreateController.php
@@ -258,10 +258,13 @@ class CreateController extends Controller
'count' => $index + 1,
]
)->render();
+ // @codeCoverageIgnoreStart
} catch (Throwable $e) {
+
Log::debug(sprintf('Throwable was thrown in getTriggersForBill(): %s', $e->getMessage()));
Log::debug($e->getTraceAsString());
$string = '';
+ // @codeCoverageIgnoreEnd
}
if ('' !== $string) {
$result[] = $string;
diff --git a/app/Http/Controllers/System/CronController.php b/app/Http/Controllers/System/CronController.php
index 46a50c8384..33e57aa517 100644
--- a/app/Http/Controllers/System/CronController.php
+++ b/app/Http/Controllers/System/CronController.php
@@ -49,7 +49,8 @@ class CronController
*/
private function runRecurring(): string
{
- $recurring = new RecurringCronjob;
+ /** @var RecurringCronjob $recurring */
+ $recurring = app(RecurringCronjob::class);
try {
$result = $recurring->fire();
} catch (FireflyException $e) {
diff --git a/app/Http/Requests/RecurrenceFormRequest.php b/app/Http/Requests/RecurrenceFormRequest.php
index a20ffccbe1..c28d22ff58 100644
--- a/app/Http/Requests/RecurrenceFormRequest.php
+++ b/app/Http/Requests/RecurrenceFormRequest.php
@@ -118,7 +118,7 @@ class RecurrenceFormRequest extends Request
// fill in source and destination account data
switch ($this->string('transaction_type')) {
default:
- throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->string('transaction_type')));
+ throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->string('transaction_type'))); // @codeCoverageIgnore
case 'withdrawal':
$return['transactions'][0]['source_id'] = $this->integer('source_id');
$return['transactions'][0]['destination_name'] = $this->string('destination_name');
@@ -227,8 +227,6 @@ class RecurrenceFormRequest extends Request
$rules['title'] = 'required|between:1,255|uniqueObjectForUser:recurrences,title,' . $recurrence->id;
$rules['first_date'] = 'required|date';
}
-
-
return $rules;
}
diff --git a/tests/Api/V1/Controllers/TransactionControllerTest.php b/tests/Api/V1/Controllers/TransactionControllerTest.php
index 876e83f842..f4480aa2d7 100644
--- a/tests/Api/V1/Controllers/TransactionControllerTest.php
+++ b/tests/Api/V1/Controllers/TransactionControllerTest.php
@@ -28,9 +28,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\TransactionCollector;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Helpers\Filter\NegativeAmountFilter;
-use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
-use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;
@@ -46,7 +44,7 @@ class TransactionControllerTest extends TestCase
/**
*
*/
- public function setUp()
+ public function setUp(): void
{
parent::setUp();
Passport::actingAs($this->user());
@@ -1329,16 +1327,8 @@ class TransactionControllerTest extends TestCase
*/
public function testShowDeposit(): void
{
- $loop = 0;
- do {
- /** @var TransactionJournal $journal */
- $journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 2)->whereNull('deleted_at')->first();
- $count = $journal->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
- $transaction = $journal->transactions()->first();
-
-
+ $deposit = $this->getRandomDeposit();
+ $transaction = $deposit->transactions()->first();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsByType')
@@ -1350,7 +1340,7 @@ class TransactionControllerTest extends TestCase
$collector->setUser($this->user());
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
$collector->setAllAssetAccounts();
- $collector->setJournals(new Collection([$journal]));
+ $collector->setJournals(new Collection([$deposit]));
$collector->setLimit(5)->setPage(1);
$transactions = $collector->getTransactions();
@@ -1374,7 +1364,7 @@ class TransactionControllerTest extends TestCase
[
'data' => [[
'attributes' => [
- 'description' => $journal->description,
+ 'description' => $deposit->description,
'type' => 'Deposit',
],
]],
@@ -1393,15 +1383,7 @@ class TransactionControllerTest extends TestCase
*/
public function testShowWithdrawal(): void
{
- $loop = 0;
- do {
- // this is kind of cheating but OK.
- /** @var TransactionJournal $journal */
- $journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first();
- $count = $journal->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
- /** @var Transaction $transaction */
+ $journal = $this->getRandomWithdrawal();
$transaction = $journal->transactions()->first();
$transaction->description = null;
$transaction->save();
@@ -2441,7 +2423,7 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsById')->withArgs([[$account->id]])->andReturn(new Collection([$account]));
- $data = [
+ $data = [
'description' => 'Some deposit #' . random_int(1, 10000),
'date' => '2018-01-01',
'transactions' => [
@@ -2452,12 +2434,7 @@ class TransactionControllerTest extends TestCase
],
],
];
- do {
- /** @var TransactionJournal $deposit */
- $deposit = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 2)->first();
- $count = $deposit->transactions()->count();
- } while ($count !== 2);
-
+ $deposit = $this->getRandomDeposit();
$transaction = $deposit->transactions()->first();
$repository->shouldReceive('setUser');
$repository->shouldReceive('update')->andReturn($deposit)->once();
@@ -2494,13 +2471,8 @@ class TransactionControllerTest extends TestCase
],
],
];
- do {
- /** @var TransactionJournal $withdrawal */
- $withdrawal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 1)->first();
- $count = $withdrawal->transactions()->count();
- } while ($count !== 2);
-
+ $withdrawal = $this->getRandomWithdrawal();
$transaction = $withdrawal->transactions()->first();
$repository->shouldReceive('setUser');
$repository->shouldReceive('update')->andReturn($withdrawal)->once();
diff --git a/tests/Feature/Controllers/Recurring/CreateControllerTest.php b/tests/Feature/Controllers/Recurring/CreateControllerTest.php
index c75543fe52..3fdd63d91a 100644
--- a/tests/Feature/Controllers/Recurring/CreateControllerTest.php
+++ b/tests/Feature/Controllers/Recurring/CreateControllerTest.php
@@ -28,6 +28,7 @@ use Carbon\Carbon;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
+use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
@@ -74,35 +75,189 @@ class CreateControllerTest extends TestCase
$recurringRepos = $this->mock(RecurringRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
+ $currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$tomorrow = Carbon::create()->addDays(2);
- $recurrence = $this->user()->recurrences()->first();
+ $recurrence = $this->user()->recurrences()->first();
$data = [
- 'title' => 'hello',
+ 'title' => 'hello' . random_int(1, 100000),
'first_date' => $tomorrow->format('Y-m-d'),
'repetition_type' => 'daily',
'skip' => 0,
- 'recurring_description' => 'Some descr',
+ 'recurring_description' => 'Some descr' . random_int(1, 100000),
'active' => '1',
'apply_rules' => '1',
+ 'foreign_amount' => '1',
+ 'foreign_currency_id' => '2',
// mandatory for transaction:
'transaction_description' => 'Some descr',
'transaction_type' => 'withdrawal',
'transaction_currency_id' => '1',
'amount' => '30',
+
// mandatory account info:
'source_id' => '1',
- 'source_name' => '',
- 'destination_id' => '',
'destination_name' => 'Some Expense',
// optional fields:
'budget_id' => '1',
'category' => 'CategoryA',
'tags' => 'A,B,C',
+ 'create_another' => '1',
+ 'repetition_end' => 'times',
+ 'repetitions' => 3,
+ ];
- 'repetition_end' => 'times',
- 'repetitions' => 3,
+ $recurringRepos->shouldReceive('store')->andReturn($recurrence)->once();
+
+ $this->be($this->user());
+ $response = $this->post(route('recurring.store'), $data);
+ $response->assertStatus(302);
+ $response->assertSessionHas('success');
+ }
+
+ /**
+ * @covers \FireflyIII\Http\Controllers\Recurring\CreateController
+ * @covers \FireflyIII\Http\Requests\RecurrenceFormRequest
+ */
+ public function testStoreDeposit(): void
+ {
+ $recurringRepos = $this->mock(RecurringRepositoryInterface::class);
+ $budgetRepos = $this->mock(BudgetRepositoryInterface::class);
+ $categoryRepos = $this->mock(CategoryRepositoryInterface::class);
+ $tomorrow = Carbon::create()->addDays(2);
+ $recurrence = $this->user()->recurrences()->first();
+ $data = [
+ 'title' => 'hello' . random_int(1, 100000),
+ 'first_date' => $tomorrow->format('Y-m-d'),
+ 'repetition_type' => 'daily',
+ 'skip' => 0,
+ 'recurring_description' => 'Some descr' . random_int(1, 100000),
+ 'active' => '1',
+ 'apply_rules' => '1',
+ 'foreign_amount' => '1',
+ 'foreign_currency_id' => '2',
+
+ // mandatory for transaction:
+ 'transaction_description' => 'Some descr',
+ 'transaction_type' => 'deposit',
+ 'transaction_currency_id' => '1',
+ 'amount' => '30',
+
+ // mandatory account info:
+ 'source_id' => '2',
+ 'source_name' => 'Some source',
+ 'destination_id' => '1',
+ 'destination_name' => 'Some Expense',
+
+ // optional fields:
+ 'budget_id' => '1',
+ 'category' => 'CategoryA',
+ 'tags' => 'A,B,C',
+ 'create_another' => '1',
+ 'repetition_end' => 'times',
+ 'repetitions' => 3,
+ ];
+
+ $recurringRepos->shouldReceive('store')->andReturn($recurrence)->once();
+
+ $this->be($this->user());
+ $response = $this->post(route('recurring.store'), $data);
+ $response->assertStatus(302);
+ $response->assertSessionHas('success');
+ }
+
+ /**
+ * @covers \FireflyIII\Http\Controllers\Recurring\CreateController
+ * @covers \FireflyIII\Http\Requests\RecurrenceFormRequest
+ */
+ public function testStoreTransfer(): void
+ {
+ $recurringRepos = $this->mock(RecurringRepositoryInterface::class);
+ $budgetRepos = $this->mock(BudgetRepositoryInterface::class);
+ $categoryRepos = $this->mock(CategoryRepositoryInterface::class);
+ $tomorrow = Carbon::create()->addDays(2);
+ $recurrence = $this->user()->recurrences()->first();
+ $data = [
+ 'title' => 'hello' . random_int(1, 100000),
+ 'first_date' => $tomorrow->format('Y-m-d'),
+ 'repetition_type' => 'daily',
+ 'skip' => 0,
+ 'recurring_description' => 'Some descr' . random_int(1, 100000),
+ 'active' => '1',
+ 'apply_rules' => '1',
+ 'foreign_amount' => '1',
+ 'foreign_currency_id' => '2',
+
+ // mandatory for transaction:
+ 'transaction_description' => 'Some descr',
+ 'transaction_type' => 'transfer',
+ 'transaction_currency_id' => '1',
+ 'amount' => '30',
+
+ // mandatory account info:
+ 'source_id' => '2',
+ 'source_name' => 'Some source',
+ 'destination_id' => '1',
+ 'destination_name' => 'Some Expense',
+
+ // optional fields:
+ 'budget_id' => '1',
+ 'category' => 'CategoryA',
+ 'tags' => 'A,B,C',
+ 'create_another' => '1',
+ 'repetition_end' => 'times',
+ 'repetitions' => 3,
+ ];
+
+ $recurringRepos->shouldReceive('store')->andReturn($recurrence)->once();
+
+ $this->be($this->user());
+ $response = $this->post(route('recurring.store'), $data);
+ $response->assertStatus(302);
+ $response->assertSessionHas('success');
+ }
+
+ /**
+ * @covers \FireflyIII\Http\Controllers\Recurring\CreateController
+ * @covers \FireflyIII\Http\Requests\RecurrenceFormRequest
+ */
+ public function testStoreUntilDate(): void
+ {
+ $recurringRepos = $this->mock(RecurringRepositoryInterface::class);
+ $budgetRepos = $this->mock(BudgetRepositoryInterface::class);
+ $categoryRepos = $this->mock(CategoryRepositoryInterface::class);
+ $currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
+ $tomorrow = Carbon::create()->addDays(2);
+ $recurrence = $this->user()->recurrences()->first();
+ $data = [
+ 'title' => 'hello' . random_int(1, 100000),
+ 'first_date' => $tomorrow->format('Y-m-d'),
+ 'repetition_type' => 'daily',
+ 'skip' => 0,
+ 'recurring_description' => 'Some descr' . random_int(1, 100000),
+ 'active' => '1',
+ 'apply_rules' => '1',
+ 'foreign_amount' => '1',
+ 'foreign_currency_id' => '2',
+
+ // mandatory for transaction:
+ 'transaction_description' => 'Some descr',
+ 'transaction_type' => 'withdrawal',
+ 'transaction_currency_id' => '1',
+ 'amount' => '30',
+
+ // mandatory account info:
+ 'source_id' => '1',
+ 'destination_name' => 'Some Expense',
+
+ // optional fields:
+ 'budget_id' => '1',
+ 'category' => 'CategoryA',
+ 'tags' => 'A,B,C',
+ 'create_another' => '1',
+ 'repetition_end' => 'until_date',
+ 'repeat_until' => $tomorrow->format('Y-m-d'),
];
$recurringRepos->shouldReceive('store')->andReturn($recurrence)->once();
diff --git a/tests/Feature/Controllers/Recurring/DeleteControllerTest.php b/tests/Feature/Controllers/Recurring/DeleteControllerTest.php
new file mode 100644
index 0000000000..fadee7b0d3
--- /dev/null
+++ b/tests/Feature/Controllers/Recurring/DeleteControllerTest.php
@@ -0,0 +1,77 @@
+.
+ */
+
+declare(strict_types=1);
+
+namespace Tests\Feature\Controllers\Recurring;
+
+use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
+use Illuminate\Support\Collection;
+use Log;
+use Tests\TestCase;
+
+/**
+ *
+ * Class DeleteControllerTest
+ */
+class DeleteControllerTest extends TestCase
+{
+ /**
+ *
+ */
+ public function setUp(): void
+ {
+ parent::setUp();
+ Log::debug(sprintf('Now in %s.', \get_class($this)));
+ }
+
+ /**
+ * @covers \FireflyIII\Http\Controllers\Recurring\DeleteController
+ */
+ public function testDelete(): void
+ {
+ $recurringRepos = $this->mock(RecurringRepositoryInterface::class);
+ $recurringRepos->shouldReceive('getTransactions')->andReturn(new Collection())->once();
+
+
+ $this->be($this->user());
+ $response = $this->get(route('recurring.delete', [1]));
+ $response->assertStatus(200);
+ $response->assertSee('
');
+ }
+
+ /**
+ * @covers \FireflyIII\Http\Controllers\Recurring\DeleteController
+ */
+ public function testDestroy(): void
+ {
+ $recurringRepos = $this->mock(RecurringRepositoryInterface::class);
+ $recurringRepos->shouldReceive('destroy')->once();
+
+
+ $this->be($this->user());
+ $response = $this->post(route('recurring.destroy', [1]));
+ $response->assertStatus(302);
+ $response->assertSessionHas('success');
+ }
+
+
+}
\ No newline at end of file
diff --git a/tests/Feature/Controllers/Recurring/EditControllerTest.php b/tests/Feature/Controllers/Recurring/EditControllerTest.php
new file mode 100644
index 0000000000..f305c73003
--- /dev/null
+++ b/tests/Feature/Controllers/Recurring/EditControllerTest.php
@@ -0,0 +1,125 @@
+.
+ */
+
+declare(strict_types=1);
+
+namespace Tests\Feature\Controllers\Recurring;
+
+use Carbon\Carbon;
+use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
+use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
+use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
+use Illuminate\Support\Collection;
+use Log;
+use Tests\TestCase;
+
+/**
+ *
+ * Class EditControllerTest
+ */
+class EditControllerTest extends TestCase
+{
+ /**
+ *
+ */
+ public function setUp(): void
+ {
+ parent::setUp();
+ Log::debug(sprintf('Now in %s.', \get_class($this)));
+ }
+
+ /**
+ * @covers \FireflyIII\Http\Controllers\Recurring\EditController
+ */
+ public function testEdit(): void
+ {
+ $recurringRepos = $this->mock(RecurringRepositoryInterface::class);
+ $budgetRepos = $this->mock(BudgetRepositoryInterface::class);
+
+ $recurringRepos->shouldReceive('setUser');
+ $recurringRepos->shouldReceive('getNoteText')->andReturn('Note!');
+ $recurringRepos->shouldReceive('repetitionDescription')->andReturn('dunno');
+ $recurringRepos->shouldReceive('getXOccurrences')->andReturn([]);
+ $budgetRepos->shouldReceive('findNull')->andReturn($this->user()->budgets()->first());
+
+
+ $budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection)->once();
+ //\Amount::shouldReceive('getDefaultCurrency')->andReturn(TransactionCurrency::find(1));
+
+
+ $this->be($this->user());
+ $response = $this->get(route('recurring.edit', [1]));
+ $response->assertStatus(200);
+ $response->assertSee('');
+ }
+
+ /**
+ * @covers \FireflyIII\Http\Controllers\Recurring\EditController
+ * @covers \FireflyIII\Http\Requests\RecurrenceFormRequest
+ */
+ public function testUpdate(): void
+ {
+ $recurringRepos = $this->mock(RecurringRepositoryInterface::class);
+ $budgetRepos = $this->mock(BudgetRepositoryInterface::class);
+ $categoryRepos = $this->mock(CategoryRepositoryInterface::class);
+
+ $recurringRepos->shouldReceive('update')->once();
+
+ $tomorrow = Carbon::create()->addDays(2);
+ $recurrence = $this->user()->recurrences()->first();
+ $data = [
+ 'id' => $recurrence->id,
+ 'title' => 'hello',
+ 'first_date' => $tomorrow->format('Y-m-d'),
+ 'repetition_type' => 'daily',
+ 'skip' => 0,
+ 'recurring_description' => 'Some descr',
+ 'active' => '1',
+ 'apply_rules' => '1',
+ 'return_to_edit' => '1',
+ // mandatory for transaction:
+ 'transaction_description' => 'Some descr',
+ 'transaction_type' => 'withdrawal',
+ 'transaction_currency_id' => '1',
+ 'amount' => '30',
+ // mandatory account info:
+ 'source_id' => '1',
+ 'source_name' => '',
+ 'destination_id' => '',
+ 'destination_name' => 'Some Expense',
+
+ // optional fields:
+ 'budget_id' => '1',
+ 'category' => 'CategoryA',
+ 'tags' => 'A,B,C',
+ 'create_another' => '1',
+ 'repetition_end' => 'times',
+ 'repetitions' => 3,
+ ];
+
+
+ $this->be($this->user());
+ $response = $this->post(route('recurring.update', [1]), $data);
+ $response->assertStatus(302);
+ $response->assertSessionHas('success');
+ }
+
+}
\ No newline at end of file
diff --git a/tests/Feature/Controllers/Recurring/IndexControllerTest.php b/tests/Feature/Controllers/Recurring/IndexControllerTest.php
new file mode 100644
index 0000000000..40f56f5e41
--- /dev/null
+++ b/tests/Feature/Controllers/Recurring/IndexControllerTest.php
@@ -0,0 +1,97 @@
+.
+ */
+
+declare(strict_types=1);
+
+namespace Tests\Feature\Controllers\Recurring;
+
+use FireflyIII\Models\Configuration;
+use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
+use Illuminate\Support\Collection;
+use Log;
+use Tests\TestCase;
+
+/**
+ *
+ * Class IndexControllerTest
+ */
+class IndexControllerTest extends TestCase
+{
+ /**
+ *
+ */
+ public function setUp(): void
+ {
+ parent::setUp();
+ Log::debug(sprintf('Now in %s.', \get_class($this)));
+ }
+
+ /**
+ * @covers \FireflyIII\Http\Controllers\Recurring\IndexController
+ */
+ public function testIndex(): void
+ {
+
+ $repository = $this->mock(RecurringRepositoryInterface::class);
+
+ $config = new Configuration;
+ $config->data = 0;
+
+ $falseConfig = new Configuration;
+ $falseConfig->data = false;
+
+ $collection = $this->user()->recurrences()->take(2)->get();
+
+ // mock cron job config:
+ \FireflyConfig::shouldReceive('get')->withArgs(['last_rt_job', 0])->once()->andReturn($config);
+ \FireflyConfig::shouldReceive('get')->withArgs(['is_demo_site', false])->once()->andReturn($falseConfig);
+
+ $repository->shouldReceive('get')->andReturn($collection)->once();
+ $repository->shouldReceive('setUser');
+ $repository->shouldReceive('getNoteText')->andReturn('Notes');
+ $repository->shouldReceive('repetitionDescription')->andReturn('Bla');
+ $repository->shouldReceive('getXOccurrences')->andReturn([]);
+
+
+ $this->be($this->user());
+ $response = $this->get(route('recurring.index'));
+ $response->assertStatus(200);
+ $response->assertSee('');
+ }
+
+ public function testShow(): void
+ {
+ $repository = $this->mock(RecurringRepositoryInterface::class);
+ $recurrence = $this->user()->recurrences()->first();
+ $repository->shouldReceive('setUser');
+ $repository->shouldReceive('getNoteText')->andReturn('Notes');
+ $repository->shouldReceive('repetitionDescription')->andReturn('Bla');
+ $repository->shouldReceive('getXOccurrences')->andReturn([]);
+ $repository->shouldReceive('getTransactions')->andReturn(new Collection);
+
+ $this->be($this->user());
+ $response = $this->get(route('recurring.show', [$recurrence->id]));
+ $response->assertStatus(200);
+ $response->assertSee('');
+ }
+
+
+}
\ No newline at end of file
diff --git a/tests/Feature/Controllers/Report/ExpenseControllerTest.php b/tests/Feature/Controllers/Report/ExpenseControllerTest.php
index 68dc54e8ce..a00273a45f 100644
--- a/tests/Feature/Controllers/Report/ExpenseControllerTest.php
+++ b/tests/Feature/Controllers/Report/ExpenseControllerTest.php
@@ -231,7 +231,6 @@ class ExpenseControllerTest extends TestCase
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setOpposingAccounts')->andReturnSelf();
$collector->shouldReceive('getTransactions')->andReturn($collection);
- //$collector->shouldReceive('')->andReturnSelf();
$this->be($this->user());
$response = $this->get(route('report-data.expense.expenses', ['1', $expense->id, '20170101', '20170131']));
@@ -248,6 +247,36 @@ class ExpenseControllerTest extends TestCase
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('findByName')->once()->withArgs([$expense->name, [AccountType::REVENUE]])->andReturn($revenue);
+ // fake collection:
+ $transA = new Transaction;
+ $transA->transaction_currency_id = 1;
+ $transA->transaction_category_name = 'Category';
+ $transA->transaction_category_id = 1;
+ $transA->transaction_currency_symbol = 'A';
+ $transA->transaction_currency_dp = 2;
+ $transA->transaction_amount = '100';
+ $transA->opposing_account_id = $expense->id;
+ $transB = new Transaction;
+ $transB->transaction_currency_id = 2;
+ $transB->transaction_category_name = null;
+ $transB->transaction_category_id = 0;
+ $transB->transaction_journal_budget_name = 'Category2';
+ $transB->transaction_journal_budget_id = 2;
+ $transB->transaction_currency_symbol = 'A';
+ $transB->transaction_currency_dp = 2;
+ $transB->transaction_amount = '100';
+ $transB->opposing_account_id = $expense->id;
+ $collection = new Collection([$transA, $transB]);
+
+ $collector = $this->mock(TransactionCollectorInterface::class);
+ $collector->shouldReceive('setRange')->andReturnSelf();
+ $collector->shouldReceive('setTypes')->andReturnSelf();
+ $collector->shouldReceive('setAccounts')->andReturnSelf();
+ $collector->shouldReceive('setOpposingAccounts')->andReturnSelf();
+ $collector->shouldReceive('getTransactions')->andReturn($collection);
+ //$collector->shouldReceive('')->andReturnSelf();
+
+
$this->be($this->user());
$response = $this->get(route('report-data.expense.income', ['1', $expense->id, '20170101', '20170131']));
$response->assertStatus(200);
diff --git a/tests/Feature/Controllers/Rule/CreateControllerTest.php b/tests/Feature/Controllers/Rule/CreateControllerTest.php
index 079807ac20..22de7bb78d 100644
--- a/tests/Feature/Controllers/Rule/CreateControllerTest.php
+++ b/tests/Feature/Controllers/Rule/CreateControllerTest.php
@@ -63,6 +63,23 @@ class CreateControllerTest extends TestCase
$response->assertSee('');
}
+
+ /**
+ * @covers \FireflyIII\Http\Controllers\Rule\CreateController
+ */
+ public function testCreateFromBill(): void
+ {
+ // mock stuff
+ $journalRepos = $this->mock(JournalRepositoryInterface::class);
+ $billRepos = $this->mock(BillRepositoryInterface::class);
+ $journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
+
+ $this->be($this->user());
+ $response = $this->get(route('rules.create-from-bill', [1,1]));
+ $response->assertStatus(200);
+ $response->assertSee('');
+ }
+
/**
* @covers \FireflyIII\Http\Controllers\Rule\CreateController
*/
diff --git a/tests/Feature/Controllers/System/CronControllerTest.php b/tests/Feature/Controllers/System/CronControllerTest.php
new file mode 100644
index 0000000000..7a126fbb5a
--- /dev/null
+++ b/tests/Feature/Controllers/System/CronControllerTest.php
@@ -0,0 +1,117 @@
+.
+ */
+
+declare(strict_types=1);
+
+namespace Tests\Feature\Controllers\System;
+
+use FireflyIII\Exceptions\FireflyException;
+use FireflyIII\Models\Preference;
+use FireflyIII\Repositories\User\UserRepositoryInterface;
+use FireflyIII\Support\Cronjobs\RecurringCronjob;
+use Illuminate\Support\Collection;
+use Log;
+use Mockery;
+use Tests\TestCase;
+
+/**
+ *
+ * Class CronControllerTest
+ */
+class CronControllerTest extends TestCase
+{
+
+ /**
+ *
+ */
+ public function setUp(): void
+ {
+ parent::setUp();
+ Log::debug(sprintf('Now in %s.', \get_class($this)));
+ }
+
+ /**
+ * @covers \FireflyIII\Http\Controllers\System\CronController
+ * @covers \FireflyIII\Support\Binder\CLIToken
+ */
+ public function testCron(): void
+ {
+ $users = new Collection([$this->user()]);
+ $job = $this->mock(RecurringCronjob::class);
+ $preference = new Preference();
+ $preference->data = 'token';
+ $job->shouldReceive('fire')->once()->andReturn(true);
+ $repository = $this->mock(UserRepositoryInterface::class);
+ $repository->shouldReceive('all')->andReturn($users);
+ \Preferences::shouldReceive('getForUser')
+ ->withArgs([Mockery::any(), 'access_token', null])
+ ->andReturn($preference)->once();
+ $response = $this->get(route('cron.cron', ['token']));
+ $response->assertStatus(200);
+ $response->assertSee('The recurring transaction cron job fired successfully.');
+
+ }
+
+ /**
+ * @covers \FireflyIII\Http\Controllers\System\CronController
+ * @covers \FireflyIII\Support\Binder\CLIToken
+ */
+ public function testCronException(): void
+ {
+ $users = new Collection([$this->user()]);
+ $job = $this->mock(RecurringCronjob::class);
+ $preference = new Preference();
+ $preference->data = 'token';
+ $job->shouldReceive('fire')->once()->andThrow(new FireflyException('Exception noted.'));
+ $repository = $this->mock(UserRepositoryInterface::class);
+ $repository->shouldReceive('all')->andReturn($users);
+ \Preferences::shouldReceive('getForUser')
+ ->withArgs([Mockery::any(), 'access_token', null])
+ ->andReturn($preference)->once();
+ $response = $this->get(route('cron.cron', ['token']));
+ $response->assertStatus(200);
+ $response->assertSee('Exception noted.');
+
+ }
+
+ /**
+ * @covers \FireflyIII\Http\Controllers\System\CronController
+ * @covers \FireflyIII\Support\Binder\CLIToken
+ */
+ public function testCronFalse(): void
+ {
+ $users = new Collection([$this->user()]);
+ $job = $this->mock(RecurringCronjob::class);
+ $preference = new Preference();
+ $preference->data = 'token';
+ $job->shouldReceive('fire')->once()->andReturn(false);
+ $repository = $this->mock(UserRepositoryInterface::class);
+ $repository->shouldReceive('all')->andReturn($users);
+ \Preferences::shouldReceive('getForUser')
+ ->withArgs([Mockery::any(), 'access_token', null])
+ ->andReturn($preference)->once();
+ $response = $this->get(route('cron.cron', ['token']));
+ $response->assertStatus(200);
+ $response->assertSee('The recurring transaction cron job did not fire.');
+
+ }
+
+}
\ No newline at end of file
diff --git a/tests/Feature/Controllers/Transaction/ConvertControllerTest.php b/tests/Feature/Controllers/Transaction/ConvertControllerTest.php
index 1ed1f4945a..b397de2619 100644
--- a/tests/Feature/Controllers/Transaction/ConvertControllerTest.php
+++ b/tests/Feature/Controllers/Transaction/ConvertControllerTest.php
@@ -55,7 +55,6 @@ class ConvertControllerTest extends TestCase
/**
- * @covers \FireflyIII\Http\Controllers\Transaction\ConvertController
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController
*/
public function testIndexDepositTransfer(): void
@@ -64,13 +63,7 @@ class ConvertControllerTest extends TestCase
$journalRepos = $this->mock(JournalRepositoryInterface::class);
// find deposit:
- $loop = 0;
- do {
- $deposit = TransactionJournal::where('transaction_type_id', 2)->inRandomOrder()->where('user_id', $this->user()->id)->first();
- $count = $deposit->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
-
+ $deposit = $this->getRandomDeposit();
$journalRepos->shouldReceive('firstNull')->andReturn($deposit);
$journalRepos->shouldReceive('getJournalTotal')->andReturn('1')->once();
$journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection)->once();
@@ -102,13 +95,7 @@ class ConvertControllerTest extends TestCase
$journalRepos = $this->mock(JournalRepositoryInterface::class);
// find deposit:
- $loop = 0;
- do {
- $deposit = TransactionJournal::where('transaction_type_id', 2)->inRandomOrder()->where('user_id', $this->user()->id)->first();
- $count = $deposit->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
-
+ $deposit = $this->getRandomDeposit();
$journalRepos->shouldReceive('firstNull')->andReturn($deposit);
$journalRepos->shouldReceive('getJournalTotal')->andReturn('1')->once();
$journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection)->once();
@@ -135,13 +122,7 @@ class ConvertControllerTest extends TestCase
// mock stuff:
// find deposit:
- $loop = 0;
- do {
- $deposit = TransactionJournal::where('transaction_type_id', 2)->inRandomOrder()->where('user_id', $this->user()->id)->first();
- $count = $deposit->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
-
+ $deposit = $this->getRandomDeposit();
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn($deposit);
$journalRepos->shouldReceive('getJournalTotal')->andReturn('1')->once();
@@ -190,13 +171,7 @@ class ConvertControllerTest extends TestCase
// mock stuff:
// find transfer:
- $loop = 0;
- do {
- $transfer = TransactionJournal::where('transaction_type_id', 3)->inRandomOrder()->where('user_id', $this->user()->id)->first();
- $count = $transfer->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
-
+ $transfer = $this->getRandomTransfer();
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn($transfer);
$journalRepos->shouldReceive('getJournalTotal')->andReturn('1')->once();
@@ -215,13 +190,7 @@ class ConvertControllerTest extends TestCase
public function testIndexTransferWithdrawal(): void
{
// find transfer:
- $loop = 0;
- do {
- $transfer = TransactionJournal::where('transaction_type_id', 3)->inRandomOrder()->where('user_id', $this->user()->id)->first();
- $count = $transfer->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
-
+ $transfer = $this->getRandomTransfer();
// mock stuff:
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
@@ -247,13 +216,7 @@ class ConvertControllerTest extends TestCase
{
// find withdrawal:
- $loop = 0;
- do {
- $withdrawal = TransactionJournal::where('transaction_type_id', 1)->inRandomOrder()->where('user_id', $this->user()->id)->first();
- $count = $withdrawal->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
-
+ $withdrawal = $this->getRandomWithdrawal();
// mock stuff:
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
@@ -278,13 +241,7 @@ class ConvertControllerTest extends TestCase
public function testIndexWithdrawalTransfer(): void
{
// find withdrawal:
- $loop = 0;
- do {
- $withdrawal = TransactionJournal::where('transaction_type_id', 1)->inRandomOrder()->where('user_id', $this->user()->id)->first();
- $count = $withdrawal->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
-
+ $withdrawal = $this->getRandomWithdrawal();
// mock stuff:
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
@@ -379,13 +336,7 @@ class ConvertControllerTest extends TestCase
$account = $this->user()->accounts()->first();
// find withdrawal:
- $loop = 0;
- do {
- $withdrawal = TransactionJournal::where('transaction_type_id', 1)->inRandomOrder()->where('user_id', $this->user()->id)->first();
- $count = $withdrawal->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
-
+ $withdrawal = $this->getRandomWithdrawal();
// mock stuff
$messageBag = new MessageBag;
@@ -459,12 +410,7 @@ class ConvertControllerTest extends TestCase
public function testPostIndexTransferDeposit(): void
{
// find transfer:
- $loop = 0;
- do {
- $transfer = TransactionJournal::where('transaction_type_id', 3)->inRandomOrder()->where('user_id', $this->user()->id)->first();
- $count = $transfer->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
+ $transfer =$this->getRandomTransfer();
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
diff --git a/tests/Feature/Controllers/Transaction/SingleControllerTest.php b/tests/Feature/Controllers/Transaction/SingleControllerTest.php
index 9ded6e76d6..c9d8fa61e6 100644
--- a/tests/Feature/Controllers/Transaction/SingleControllerTest.php
+++ b/tests/Feature/Controllers/Transaction/SingleControllerTest.php
@@ -249,7 +249,9 @@ class SingleControllerTest extends TestCase
// mock new account list:
$currency = TransactionCurrency::first();
$accountRepos->shouldReceive('getAccountsByType')
- ->withArgs([[AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,]])->andReturn(new Collection([$account]))->once();
+ ->withArgs(
+ [[AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,]]
+ )->andReturn(new Collection([$account]))->once();
Amount::shouldReceive('getDefaultCurrency')->andReturn($currency)->times(6);
$this->be($this->user());
@@ -867,13 +869,7 @@ class SingleControllerTest extends TestCase
}
// find withdrawal:
- $loop = 0;
- do {
- $withdrawal = TransactionJournal::where('transaction_type_id', 1)->inRandomOrder()->where('user_id', $this->user()->id)->first();
- $count = $withdrawal->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
-
+ $withdrawal = $this->getRandomWithdrawal();
$journalRepos->shouldReceive('update')->andReturn($withdrawal);
$this->session(['transactions.edit.uri' => 'http://localhost']);
diff --git a/tests/TestCase.php b/tests/TestCase.php
index e9d9b35d3e..f7f1a9f082 100644
--- a/tests/TestCase.php
+++ b/tests/TestCase.php
@@ -24,9 +24,11 @@ declare(strict_types=1);
namespace Tests;
use Carbon\Carbon;
+use DB;
use Exception;
use FireflyIII\Models\Preference;
use FireflyIII\Models\TransactionJournal;
+use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
@@ -41,7 +43,6 @@ use Mockery;
abstract class TestCase extends BaseTestCase
{
-
/**
* @param User $user
* @param string $range
@@ -76,8 +77,6 @@ abstract class TestCase extends BaseTestCase
}
}
- use CreatesApplication;
-
/**
* @return array
*/
@@ -94,6 +93,8 @@ abstract class TestCase extends BaseTestCase
];
}
+ use CreatesApplication;
+
/**
* @return User
*/
@@ -110,6 +111,30 @@ abstract class TestCase extends BaseTestCase
return User::find(2);
}
+ /**
+ * @return TransactionJournal
+ */
+ public function getRandomDeposit(): TransactionJournal
+ {
+ return $this->getRandomJournal(TransactionType::DEPOSIT);
+ }
+
+ /**
+ * @return TransactionJournal
+ */
+ public function getRandomWithdrawal(): TransactionJournal
+ {
+ return $this->getRandomJournal(TransactionType::WITHDRAWAL);
+ }
+
+ /**
+ * @return TransactionJournal
+ */
+ public function getRandomTransfer(): TransactionJournal
+ {
+ return $this->getRandomJournal(TransactionType::TRANSFER);
+ }
+
/**
* @return User
*/
@@ -146,10 +171,38 @@ abstract class TestCase extends BaseTestCase
/**
*
*/
- protected function setUp()
+ protected function setUp(): void
{
parent::setUp();
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('firstNull')->andReturn(new TransactionJournal);
}
+
+ /**
+ * @param string $type
+ *
+ * @return TransactionJournal
+ */
+ private function getRandomJournal(string $type): TransactionJournal
+ {
+ $query = DB::table('transactions')
+ ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
+ ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
+ ->where('transaction_journals.user_id', $this->user()->id)
+ ->whereNull('transaction_journals.deleted_at')
+ ->whereNull('transactions.deleted_at')
+ ->where('transaction_types.type', $type)
+ ->groupBy('transactions.transaction_journal_id')
+ ->having('ct', '=', 2)
+ ->inRandomOrder()->take(1);
+ $result = $query->get(
+ [
+ 'transactions.transaction_journal_id',
+ 'transaction_journalstransaction_type_id',
+ DB::raw('COUNT(transaction_journal_id) as ct'),
+ ]
+ )->first();
+
+ return TransactionJournal::find((int)$result->transaction_journal_id);
+ }
}
diff --git a/tests/Unit/Services/Internal/Update/JournalUpdateServiceTest.php b/tests/Unit/Services/Internal/Update/JournalUpdateServiceTest.php
index b8aa942191..6cfc2e057b 100644
--- a/tests/Unit/Services/Internal/Update/JournalUpdateServiceTest.php
+++ b/tests/Unit/Services/Internal/Update/JournalUpdateServiceTest.php
@@ -137,17 +137,12 @@ class JournalUpdateServiceTest extends TestCase
$service->shouldReceive('setUser');
$service->shouldReceive('updateBudget')->withArgs([Mockery::any(), $budget->id])->twice();
-
- do {
- /** @var TransactionJournal $journal */
- $journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 1)->first();
- $count = $journal->transactions()->count();
- } while ($count !== 2);
+ $withdrawal = $this->getRandomWithdrawal();
// call update service to update budget. Should call transaction service twice.
/** @var JournalUpdateService $service */
$service = app(JournalUpdateService::class);
- $service->updateBudget($journal, $budget->id);
+ $service->updateBudget($withdrawal, $budget->id);
}
/**
@@ -160,16 +155,12 @@ class JournalUpdateServiceTest extends TestCase
$service->shouldReceive('updateCategory')->withArgs([Mockery::any(), 'New category'])->twice();
- do {
- /** @var TransactionJournal $journal */
- $journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 1)->first();
- $count = $journal->transactions()->count();
- } while ($count !== 2);
+ $withdrawal = $this->getRandomWithdrawal();
// call update service to update budget. Should call transaction service twice.
/** @var JournalUpdateService $service */
$service = app(JournalUpdateService::class);
- $service->updateCategory($journal, 'New category');
+ $service->updateCategory($withdrawal, 'New category');
}
diff --git a/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php b/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php
index 96d59797da..8625049961 100644
--- a/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php
+++ b/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php
@@ -27,7 +27,6 @@ use FireflyIII\Models\AccountType;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
-use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\TransactionRules\Actions\SetDestinationAccount;
use Tests\TestCase;
@@ -46,18 +45,11 @@ class SetDestinationAccountTest extends TestCase
*/
public function testActDepositExisting(): void
{
- $accountRepos = $this->mock(AccountRepositoryInterface::class);
- $type = TransactionType::whereType(TransactionType::DEPOSIT)->first();
-
- do {
- /** @var TransactionJournal $journal */
- $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first();
- $count = $journal->transactions()->count();
- } while ($count !== 2);
-
- $destinationTr = $journal->transactions()->where('amount', '>', 0)->first();
+ $accountRepos = $this->mock(AccountRepositoryInterface::class);
+ $deposit = $this->getRandomDeposit();
+ $destinationTr = $deposit->transactions()->where('amount', '>', 0)->first();
$destination = $destinationTr->account;
- $user = $journal->user;
+ $user = $deposit->user;
$accountType = AccountType::whereType(AccountType::ASSET)->first();
$account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $destination->id)->first();
$this->assertNotEquals($destination->id, $account->id);
@@ -70,12 +62,11 @@ class SetDestinationAccountTest extends TestCase
$ruleAction = new RuleAction;
$ruleAction->action_value = $account->name;
$action = new SetDestinationAccount($ruleAction);
- $result = $action->act($journal);
+ $result = $action->act($deposit);
$this->assertTrue($result);
// test journal for new account
- $journal = TransactionJournal::find($journal->id);
- $destinationTr = $journal->transactions()->where('amount', '>', 0)->first();
+ $destinationTr = $deposit->transactions()->where('amount', '>', 0)->first();
$newDestination = $destinationTr->account;
$this->assertNotEquals($destination->id, $newDestination->id);
$this->assertEquals($newDestination->id, $account->id);
@@ -89,13 +80,7 @@ class SetDestinationAccountTest extends TestCase
public function testActDepositNotExisting(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
- $type = TransactionType::whereType(TransactionType::DEPOSIT)->first();
-
- do {
- /** @var TransactionJournal $journal */
- $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first();
- $count = $journal->transactions()->count();
- } while ($count !== 2);
+ $deposit = $this->getRandomDeposit();
// find account? Return account:
$accountRepos->shouldReceive('setUser');
@@ -105,7 +90,7 @@ class SetDestinationAccountTest extends TestCase
$ruleAction = new RuleAction;
$ruleAction->action_value = 'Not existing asset account #' . random_int(1, 10000);
$action = new SetDestinationAccount($ruleAction);
- $result = $action->act($journal);
+ $result = $action->act($deposit);
$this->assertFalse($result);
}
@@ -117,15 +102,8 @@ class SetDestinationAccountTest extends TestCase
public function testActWithDrawalNotExisting(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
- $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
$account = $this->user()->accounts()->inRandomOrder()->where('account_type_id', 4)->first();
-
-
- do {
- /** @var TransactionJournal $journal */
- $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first();
- $count = $journal->transactions()->count();
- } while ($count !== 2);
+ $withdrawal = $this->getRandomWithdrawal();
// find account? Return account:
$accountRepos->shouldReceive('setUser');
@@ -136,7 +114,7 @@ class SetDestinationAccountTest extends TestCase
$ruleAction = new RuleAction;
$ruleAction->action_value = 'Not existing expense account #' . random_int(1, 10000);
$action = new SetDestinationAccount($ruleAction);
- $result = $action->act($journal);
+ $result = $action->act($withdrawal);
$this->assertTrue($result);
}
@@ -148,19 +126,11 @@ class SetDestinationAccountTest extends TestCase
*/
public function testActWithdrawalExisting(): void
{
- $accountRepos = $this->mock(AccountRepositoryInterface::class);
- $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
-
- do {
- /** @var TransactionJournal $journal */
- $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first();
- $count = $journal->transactions()->count();
- } while ($count !== 2);
-
-
- $destinationTr = $journal->transactions()->where('amount', '>', 0)->first();
+ $accountRepos = $this->mock(AccountRepositoryInterface::class);
+ $withdrawal = $this->getRandomWithdrawal();
+ $destinationTr = $withdrawal->transactions()->where('amount', '>', 0)->first();
$destination = $destinationTr->account;
- $user = $journal->user;
+ $user = $withdrawal->user;
$accountType = AccountType::whereType(AccountType::EXPENSE)->first();
$account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $destination->id)->first();
$this->assertNotEquals($destination->id, $account->id);
@@ -173,12 +143,11 @@ class SetDestinationAccountTest extends TestCase
$ruleAction = new RuleAction;
$ruleAction->action_value = $account->name;
$action = new SetDestinationAccount($ruleAction);
- $result = $action->act($journal);
+ $result = $action->act($withdrawal);
$this->assertTrue($result);
// test journal for new account
- $journal = TransactionJournal::find($journal->id);
- $destinationTr = $journal->transactions()->where('amount', '>', 0)->first();
+ $destinationTr = $withdrawal->transactions()->where('amount', '>', 0)->first();
$newDestination = $destinationTr->account;
$this->assertNotEquals($destination->id, $newDestination->id);
$this->assertEquals($newDestination->id, $account->id);
diff --git a/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php b/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php
index b56271d315..2b68331a0a 100644
--- a/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php
+++ b/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php
@@ -27,7 +27,6 @@ use FireflyIII\Models\AccountType;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
-use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\TransactionRules\Actions\SetSourceAccount;
use Tests\TestCase;
@@ -45,21 +44,12 @@ class SetSourceAccountTest extends TestCase
public function testActDepositExistingUpdated(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
-
-
- $type = TransactionType::whereType(TransactionType::DEPOSIT)->first();
-
- do {
- /** @var TransactionJournal $journal */
- $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first();
- $count = $journal->transactions()->count();
- } while ($count !== 2);
-
- $sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
- $source = $sourceTr->account;
- $user = $journal->user;
- $accountType = AccountType::whereType(AccountType::REVENUE)->first();
- $account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $source->id)->first();
+ $deposit = $this->getRandomDeposit();
+ $sourceTr = $deposit->transactions()->where('amount', '<', 0)->first();
+ $source = $sourceTr->account;
+ $user = $deposit->user;
+ $accountType = AccountType::whereType(AccountType::REVENUE)->first();
+ $account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $source->id)->first();
$this->assertNotEquals($source->id, $account->id);
// find account? Return account:
@@ -70,12 +60,11 @@ class SetSourceAccountTest extends TestCase
$ruleAction = new RuleAction;
$ruleAction->action_value = $account->name;
$action = new SetSourceAccount($ruleAction);
- $result = $action->act($journal);
+ $result = $action->act($deposit);
$this->assertTrue($result);
// test journal for new account
- $journal = TransactionJournal::find($journal->id);
- $sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
+ $sourceTr = $deposit->transactions()->where('amount', '<', 0)->first();
$newSource = $sourceTr->account;
$this->assertNotEquals($source->id, $newSource->id);
$this->assertEquals($newSource->id, $account->id);
@@ -89,14 +78,8 @@ class SetSourceAccountTest extends TestCase
public function testActDepositRevenue(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
- $type = TransactionType::whereType(TransactionType::DEPOSIT)->first();
$account = $this->user()->accounts()->inRandomOrder()->where('account_type_id', 5)->first();
-
- do {
- /** @var TransactionJournal $journal */
- $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first();
- $count = $journal->transactions()->count();
- } while ($count !== 2);
+ $deposit = $this->getRandomDeposit();
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('findByName')->andReturn(null);
@@ -106,7 +89,7 @@ class SetSourceAccountTest extends TestCase
$ruleAction = new RuleAction;
$ruleAction->action_value = 'Some new revenue #' . random_int(1, 10000);
$action = new SetSourceAccount($ruleAction);
- $result = $action->act($journal);
+ $result = $action->act($deposit);
$this->assertTrue($result);
}
@@ -118,17 +101,11 @@ class SetSourceAccountTest extends TestCase
public function testActWithdrawalExistingUpdated(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
- $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
+ $withdrawal = $this->getRandomWithdrawal();
- do {
- /** @var TransactionJournal $journal */
- $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first();
- $count = $journal->transactions()->count();
- } while ($count !== 2);
-
- $sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
+ $sourceTr = $withdrawal->transactions()->where('amount', '<', 0)->first();
$source = $sourceTr->account;
- $user = $journal->user;
+ $user = $withdrawal->user;
$accountType = AccountType::whereType(AccountType::ASSET)->first();
$account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $source->id)->first();
$this->assertNotEquals($source->id, $account->id);
@@ -141,12 +118,11 @@ class SetSourceAccountTest extends TestCase
$ruleAction = new RuleAction;
$ruleAction->action_value = $account->name;
$action = new SetSourceAccount($ruleAction);
- $result = $action->act($journal);
+ $result = $action->act($withdrawal);
$this->assertTrue($result);
// test journal for new account
- $journal = TransactionJournal::find($journal->id);
- $sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
+ $sourceTr = $withdrawal->transactions()->where('amount', '<', 0)->first();
$newSource = $sourceTr->account;
$this->assertNotEquals($source->id, $newSource->id);
$this->assertEquals($newSource->id, $account->id);
@@ -160,13 +136,7 @@ class SetSourceAccountTest extends TestCase
public function testActWithdrawalNotExisting(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
- $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
-
- do {
- /** @var TransactionJournal $journal */
- $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first();
- $count = $journal->transactions()->count();
- } while ($count !== 2);
+ $withdrawal = $this->getRandomWithdrawal();
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('findByName')->andReturn(null);
@@ -175,7 +145,7 @@ class SetSourceAccountTest extends TestCase
$ruleAction = new RuleAction;
$ruleAction->action_value = 'Some new account #' . random_int(1, 10000);
$action = new SetSourceAccount($ruleAction);
- $result = $action->act($journal);
+ $result = $action->act($withdrawal);
$this->assertFalse($result);
}
diff --git a/tests/Unit/TransactionRules/Triggers/BudgetIsTest.php b/tests/Unit/TransactionRules/Triggers/BudgetIsTest.php
index eb8544d405..eefcf800ad 100644
--- a/tests/Unit/TransactionRules/Triggers/BudgetIsTest.php
+++ b/tests/Unit/TransactionRules/Triggers/BudgetIsTest.php
@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace Tests\Unit\TransactionRules\Triggers;
use FireflyIII\Models\Transaction;
-use FireflyIII\Models\TransactionJournal;
use FireflyIII\TransactionRules\Triggers\BudgetIs;
use Illuminate\Support\Collection;
use Tests\TestCase;
@@ -38,18 +37,14 @@ class BudgetIsTest extends TestCase
*/
public function testTriggeredJournal(): void
{
- do {
- $journal = TransactionJournal::inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first();
- $count = $journal->transactions()->count();
- } while ($count !== 2);
-
- $budget = $journal->user->budgets()->first();
- $journal->budgets()->detach();
- $journal->budgets()->save($budget);
- $this->assertEquals(1, $journal->budgets()->count());
+ $withdrawal = $this->getRandomWithdrawal();
+ $budget = $withdrawal->user->budgets()->first();
+ $withdrawal->budgets()->detach();
+ $withdrawal->budgets()->save($budget);
+ $this->assertEquals(1, $withdrawal->budgets()->count());
$trigger = BudgetIs::makeFromStrings($budget->name, false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertTrue($result);
}
@@ -58,19 +53,15 @@ class BudgetIsTest extends TestCase
*/
public function testTriggeredNotJournal(): void
{
- do {
- $journal = TransactionJournal::inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first();
- $count = $journal->transactions()->count();
- } while ($count !== 2);
-
- $budget = $journal->user->budgets()->first();
- $otherBudget = $journal->user->budgets()->where('id', '!=', $budget->id)->first();
- $journal->budgets()->detach();
- $journal->budgets()->save($budget);
- $this->assertEquals(1, $journal->budgets()->count());
+ $withdrawal = $this->getRandomWithdrawal();
+ $budget = $withdrawal->user->budgets()->first();
+ $otherBudget = $withdrawal->user->budgets()->where('id', '!=', $budget->id)->first();
+ $withdrawal->budgets()->detach();
+ $withdrawal->budgets()->save($budget);
+ $this->assertEquals(1, $withdrawal->budgets()->count());
$trigger = BudgetIs::makeFromStrings($otherBudget->name, false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertFalse($result);
}
@@ -79,16 +70,12 @@ class BudgetIsTest extends TestCase
*/
public function testTriggeredTransaction(): void
{
- do {
- $journal = TransactionJournal::inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first();
- $count = $journal->transactions()->count();
- } while ($count !== 2);
-
+ $withdrawal = $this->getRandomWithdrawal();
/** @var Collection $transactions */
- $transactions = $journal->transactions()->get();
- $budget = $journal->user->budgets()->first();
+ $transactions = $withdrawal->transactions()->get();
+ $budget = $withdrawal->user->budgets()->first();
- $journal->budgets()->detach();
+ $withdrawal->budgets()->detach();
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$transaction->budgets()->detach();
@@ -96,10 +83,10 @@ class BudgetIsTest extends TestCase
$this->assertEquals(1, $transaction->budgets()->count());
}
- $this->assertEquals(0, $journal->budgets()->count());
+ $this->assertEquals(0, $withdrawal->budgets()->count());
$trigger = BudgetIs::makeFromStrings($budget->name, false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertTrue($result);
}
diff --git a/tests/Unit/TransactionRules/Triggers/CategoryIsTest.php b/tests/Unit/TransactionRules/Triggers/CategoryIsTest.php
index cf68876b9c..c65ef7d4ae 100644
--- a/tests/Unit/TransactionRules/Triggers/CategoryIsTest.php
+++ b/tests/Unit/TransactionRules/Triggers/CategoryIsTest.php
@@ -36,18 +36,14 @@ class CategoryIsTest extends TestCase
*/
public function testTriggeredJournal(): void
{
- do {
- $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
- $transactions = $journal->transactions()->count();
- } while ($transactions !== 2);
-
- $category = $journal->user->categories()->first();
- $journal->categories()->detach();
- $journal->categories()->save($category);
- $this->assertEquals(1, $journal->categories()->count());
+ $withdrawal = $this->getRandomWithdrawal();
+ $category = $withdrawal->user->categories()->first();
+ $withdrawal->categories()->detach();
+ $withdrawal->categories()->save($category);
+ $this->assertEquals(1, $withdrawal->categories()->count());
$trigger = CategoryIs::makeFromStrings($category->name, false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertTrue($result);
}
@@ -56,19 +52,15 @@ class CategoryIsTest extends TestCase
*/
public function testTriggeredNotJournal(): void
{
- do {
- $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
- $transactions = $journal->transactions()->count();
- } while ($transactions !== 2);
-
- $category = $journal->user->categories()->first();
- $otherCategory = $journal->user->categories()->where('id', '!=', $category->id)->first();
- $journal->categories()->detach();
- $journal->categories()->save($category);
- $this->assertEquals(1, $journal->categories()->count());
+ $withdrawal = $this->getRandomWithdrawal();
+ $category = $withdrawal->user->categories()->first();
+ $otherCategory = $withdrawal->user->categories()->where('id', '!=', $category->id)->first();
+ $withdrawal->categories()->detach();
+ $withdrawal->categories()->save($category);
+ $this->assertEquals(1, $withdrawal->categories()->count());
$trigger = CategoryIs::makeFromStrings($otherCategory->name, false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertFalse($result);
}
@@ -77,22 +69,18 @@ class CategoryIsTest extends TestCase
*/
public function testTriggeredTransaction(): void
{
- do {
- $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
- $transactions = $journal->transactions()->count();
- } while ($transactions !== 2);
+ $withdrawal = $this->getRandomWithdrawal();
+ $transaction = $withdrawal->transactions()->first();
+ $category = $withdrawal->user->categories()->first();
- $transaction = $journal->transactions()->first();
- $category = $journal->user->categories()->first();
-
- $journal->categories()->detach();
+ $withdrawal->categories()->detach();
$transaction->categories()->detach();
$transaction->categories()->save($category);
- $this->assertEquals(0, $journal->categories()->count());
+ $this->assertEquals(0, $withdrawal->categories()->count());
$this->assertEquals(1, $transaction->categories()->count());
$trigger = CategoryIs::makeFromStrings($category->name, false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertTrue($result);
}
diff --git a/tests/Unit/TransactionRules/Triggers/HasAnyBudgetTest.php b/tests/Unit/TransactionRules/Triggers/HasAnyBudgetTest.php
index 7c86027ba4..71d015d1be 100644
--- a/tests/Unit/TransactionRules/Triggers/HasAnyBudgetTest.php
+++ b/tests/Unit/TransactionRules/Triggers/HasAnyBudgetTest.php
@@ -38,21 +38,15 @@ class HasAnyBudgetTest extends TestCase
*/
public function testTriggered(): void
{
- $loop = 0;
- do {
- /** @var TransactionJournal $journal */
- $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
- $count = $journal->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
+ $withdrawal = $this->getRandomWithdrawal();
- $budget = $journal->user->budgets()->first();
- $journal->budgets()->detach();
- $journal->budgets()->save($budget);
+ $budget = $withdrawal->user->budgets()->first();
+ $withdrawal->budgets()->detach();
+ $withdrawal->budgets()->save($budget);
- $this->assertEquals(1, $journal->budgets()->count());
+ $this->assertEquals(1, $withdrawal->budgets()->count());
$trigger = HasAnyBudget::makeFromStrings('', false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertTrue($result);
}
@@ -62,25 +56,18 @@ class HasAnyBudgetTest extends TestCase
*/
public function testTriggeredNot(): void
{
- $loop = 0;
- do {
- /** @var TransactionJournal $journal */
- $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
- $count = $journal->transactions()->count();
- $loop++;
- } while ($count !== 2 && $loop < 30);
-
- $journal->budgets()->detach();
- $this->assertEquals(0, $journal->budgets()->count());
+ $withdrawal = $this->getRandomWithdrawal();
+ $withdrawal->budgets()->detach();
+ $this->assertEquals(0, $withdrawal->budgets()->count());
// also detach all transactions:
/** @var Transaction $transaction */
- foreach ($journal->transactions()->get() as $transaction) {
+ foreach ($withdrawal->transactions()->get() as $transaction) {
$transaction->budgets()->detach();
}
$trigger = HasAnyBudget::makeFromStrings('', false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertFalse($result);
}
@@ -90,28 +77,17 @@ class HasAnyBudgetTest extends TestCase
public function testTriggeredTransactions(): void
{
Log::debug('Now in testTriggeredTransactions()');
- $loop = 0;
- do {
- Log::debug(sprintf('Loop is now at #%d', $loop));
- /** @var TransactionJournal $journal */
- $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
- $count = $journal->transactions()->count();
+ $withdrawal = $this->getRandomWithdrawal();
- Log::debug(sprintf('Found journal #%d with %d transactions', $journal->id, $count));
-
- $loop++;
- } while ($count !== 2 && $loop < 30);
- Log::debug('end of loop!');
-
- $budget = $journal->user->budgets()->first();
+ $budget = $withdrawal->user->budgets()->first();
Log::debug(sprintf('First budget is %d ("%s")', $budget->id, $budget->name));
- $journal->budgets()->detach();
- $this->assertEquals(0, $journal->budgets()->count());
+ $withdrawal->budgets()->detach();
+ $this->assertEquals(0, $withdrawal->budgets()->count());
Log::debug('Survived the assumption.');
// append to transaction
Log::debug('Do transaction loop.');
- foreach ($journal->transactions()->get() as $index => $transaction) {
+ foreach ($withdrawal->transactions()->get() as $index => $transaction) {
Log::debug(sprintf('Now at index #%d, transaction #%d', $index, $transaction->id));
$transaction->budgets()->detach();
if (0 === $index) {
@@ -121,7 +97,7 @@ class HasAnyBudgetTest extends TestCase
}
Log::debug('Done with loop, make trigger');
$trigger = HasAnyBudget::makeFromStrings('', false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertTrue($result);
}
diff --git a/tests/Unit/TransactionRules/Triggers/HasAnyCategoryTest.php b/tests/Unit/TransactionRules/Triggers/HasAnyCategoryTest.php
index cd41aa337d..9043523267 100644
--- a/tests/Unit/TransactionRules/Triggers/HasAnyCategoryTest.php
+++ b/tests/Unit/TransactionRules/Triggers/HasAnyCategoryTest.php
@@ -73,27 +73,21 @@ class HasAnyCategoryTest extends TestCase
*/
public function testTriggeredTransactions(): void
{
- $count = 0;
- $journal = null;
- while ($count === 0) {
- /** @var TransactionJournal $journal */
- $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
- $count = $journal->transactions()->count();
- }
+ $withdrawal = $this->getRandomWithdrawal();
- $category = $journal->user->categories()->first();
- $journal->categories()->detach();
- $this->assertEquals(0, $journal->categories()->count());
+ $category = $withdrawal->user->categories()->first();
+ $withdrawal->categories()->detach();
+ $this->assertEquals(0, $withdrawal->categories()->count());
// append to transaction, not to journal.
- foreach ($journal->transactions()->get() as $index => $transaction) {
+ foreach ($withdrawal->transactions()->get() as $index => $transaction) {
$transaction->categories()->sync([$category->id]);
$this->assertEquals(1, $transaction->categories()->count());
}
- $this->assertEquals(0, $journal->categories()->count());
+ $this->assertEquals(0, $withdrawal->categories()->count());
$trigger = HasAnyCategory::makeFromStrings('', false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertTrue($result);
}
diff --git a/tests/Unit/TransactionRules/Triggers/HasAttachmentTest.php b/tests/Unit/TransactionRules/Triggers/HasAttachmentTest.php
index 6ee912cbda..ab67f5551d 100644
--- a/tests/Unit/TransactionRules/Triggers/HasAttachmentTest.php
+++ b/tests/Unit/TransactionRules/Triggers/HasAttachmentTest.php
@@ -36,17 +36,14 @@ class HasAttachmentTest extends TestCase
*/
public function testTriggered(): void
{
- do {
- $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
- $count = $journal->attachments()->count();
- } while ($count !== 0);
+ $withdrawal = $this->getRandomWithdrawal();
- $attachment = $journal->user->attachments()->first();
- $journal->attachments()->save($attachment);
- $this->assertEquals(1, $journal->attachments()->count());
+ $attachment = $withdrawal->user->attachments()->first();
+ $withdrawal->attachments()->save($attachment);
+ $this->assertEquals(1, $withdrawal->attachments()->count());
$trigger = HasAttachment::makeFromStrings('1', false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertTrue($result);
}
@@ -55,16 +52,12 @@ class HasAttachmentTest extends TestCase
*/
public function testTriggeredFalse(): void
{
- do {
- // this is kind of cheating but OK.
- $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
- $count = $journal->attachments()->count();
- } while ($count !== 0);
+ $withdrawal = $this->getRandomWithdrawal();
- $this->assertEquals(0, $journal->attachments()->count());
+ $this->assertEquals(0, $withdrawal->attachments()->count());
$trigger = HasAttachment::makeFromStrings('1', false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertFalse($result);
}
diff --git a/tests/Unit/TransactionRules/Triggers/HasNoBudgetTest.php b/tests/Unit/TransactionRules/Triggers/HasNoBudgetTest.php
index fae5c36dcc..511779aa0b 100644
--- a/tests/Unit/TransactionRules/Triggers/HasNoBudgetTest.php
+++ b/tests/Unit/TransactionRules/Triggers/HasNoBudgetTest.php
@@ -72,25 +72,21 @@ class HasNoBudgetTest extends TestCase
*/
public function testTriggeredTransaction(): void
{
- $loopCount = 0;
- do {
- $journal = $this->user()->transactionJournals()->inRandomOrder()->whereNull('deleted_at')->first();
- $count = $journal->transactions()->count();
- } while ($loopCount < 30 && $count !== 2);
+ $withdrawal = $this->getRandomWithdrawal();
- $transactions = $journal->transactions()->get();
- $budget = $journal->user->budgets()->first();
+ $transactions = $withdrawal->transactions()->get();
+ $budget = $withdrawal->user->budgets()->first();
- $journal->budgets()->detach();
+ $withdrawal->budgets()->detach();
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$transaction->budgets()->sync([$budget->id]);
$this->assertEquals(1, $transaction->budgets()->count());
}
- $this->assertEquals(0, $journal->budgets()->count());
+ $this->assertEquals(0, $withdrawal->budgets()->count());
$trigger = HasNoBudget::makeFromStrings('', false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertFalse($result);
}
diff --git a/tests/Unit/TransactionRules/Triggers/HasNoCategoryTest.php b/tests/Unit/TransactionRules/Triggers/HasNoCategoryTest.php
index 1fb833e8d0..9f12927fd5 100644
--- a/tests/Unit/TransactionRules/Triggers/HasNoCategoryTest.php
+++ b/tests/Unit/TransactionRules/Triggers/HasNoCategoryTest.php
@@ -75,21 +75,17 @@ class HasNoCategoryTest extends TestCase
*/
public function testTriggeredTransaction(): void
{
- $count = 0;
- while ($count === 0) {
- $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
- $count = $journal->transactions()->count();
- }
- $transaction = $journal->transactions()->first();
- $category = $journal->user->categories()->first();
+ $withdrawal = $this->getRandomWithdrawal();
+ $transaction = $withdrawal->transactions()->first();
+ $category = $withdrawal->user->categories()->first();
- $journal->categories()->detach();
+ $withdrawal->categories()->detach();
$transaction->categories()->sync([$category->id]);
- $this->assertEquals(0, $journal->categories()->count());
+ $this->assertEquals(0, $withdrawal->categories()->count());
$this->assertEquals(1, $transaction->categories()->count());
$trigger = HasNoCategory::makeFromStrings('', false);
- $result = $trigger->triggered($journal);
+ $result = $trigger->triggered($withdrawal);
$this->assertFalse($result);
}