mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-11 17:39:41 +00:00
Improve test coverage.
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user