mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-05-04 13:16:31 +00:00
Improve test coverage.
This commit is contained in:
@@ -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