mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-22 12:11:19 +00:00
Added some tests to cover an event.
This commit is contained in:
261
tests/helpers/ConnectJournalToPiggyBankTest.php
Normal file
261
tests/helpers/ConnectJournalToPiggyBankTest.php
Normal file
@@ -0,0 +1,261 @@
|
||||
<?php
|
||||
use FireflyIII\Events\JournalCreated;
|
||||
use FireflyIII\Handlers\Events\ConnectJournalToPiggyBank;
|
||||
use FireflyIII\Models\PiggyBankRepetition;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use League\FactoryMuffin\Facade as FactoryMuffin;
|
||||
|
||||
/**
|
||||
* Class ConnectJournalToPiggyBankTest
|
||||
*/
|
||||
class ConnectJournalToPiggyBankTest extends TestCase
|
||||
{
|
||||
//event(new JournalCreated($journal, intval($request->get('piggy_bank_id'))));
|
||||
|
||||
/**
|
||||
* Sets up the fixture, for example, opens a network connection.
|
||||
* This method is called before a test is executed.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tears down the fixture, for example, closes a network connection.
|
||||
* This method is called after a test is executed.
|
||||
*/
|
||||
public function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
|
||||
public function testEmptyPiggyBankId()
|
||||
{
|
||||
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
|
||||
$event = new JournalCreated($journal, 0);
|
||||
$class = new ConnectJournalToPiggyBank();
|
||||
$result = $class->handle($event);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testNoRepetition()
|
||||
{
|
||||
FactoryMuffin::create('FireflyIII\Models\TransactionType');
|
||||
FactoryMuffin::create('FireflyIII\Models\TransactionType');
|
||||
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$this->be($user);
|
||||
|
||||
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
|
||||
$piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
|
||||
$account1 = FactoryMuffin::create('FireflyIII\Models\Account');
|
||||
$account2 = FactoryMuffin::create('FireflyIII\Models\Account');
|
||||
$account1->user_id = $user->id;
|
||||
$account2->user_id = $user->id;
|
||||
$piggyBank->account_id = $account1->id;
|
||||
$account1->save();
|
||||
$account2->save();
|
||||
$piggyBank->save();
|
||||
|
||||
Transaction::create(
|
||||
[
|
||||
'account_id' => $account1->id,
|
||||
'transaction_journal_id' => $journal->id,
|
||||
'amount' => 100
|
||||
]
|
||||
);
|
||||
Transaction::create(
|
||||
[
|
||||
'account_id' => $account2->id,
|
||||
'transaction_journal_id' => $journal->id,
|
||||
'amount' => -100
|
||||
]
|
||||
);
|
||||
|
||||
// two transactions:
|
||||
|
||||
|
||||
$event = new JournalCreated($journal, $piggyBank->id);
|
||||
$class = new ConnectJournalToPiggyBank();
|
||||
$result = $class->handle($event);
|
||||
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testNoSuchPiggy()
|
||||
{
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$this->be($user);
|
||||
|
||||
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
|
||||
$event = new JournalCreated($journal, 1);
|
||||
$class = new ConnectJournalToPiggyBank();
|
||||
$result = $class->handle($event);
|
||||
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testWithRepetition()
|
||||
{
|
||||
FactoryMuffin::create('FireflyIII\Models\TransactionType');
|
||||
FactoryMuffin::create('FireflyIII\Models\TransactionType');
|
||||
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$this->be($user);
|
||||
|
||||
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
|
||||
$piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
|
||||
$account1 = FactoryMuffin::create('FireflyIII\Models\Account');
|
||||
$account2 = FactoryMuffin::create('FireflyIII\Models\Account');
|
||||
$account1->user_id = $user->id;
|
||||
$account2->user_id = $user->id;
|
||||
$piggyBank->account_id = $account1->id;
|
||||
$account1->save();
|
||||
$account2->save();
|
||||
$piggyBank->save();
|
||||
|
||||
$start = clone $journal->date;
|
||||
$end = clone $journal->date;
|
||||
$start->subDay();
|
||||
$end->addDay();
|
||||
|
||||
PiggyBankRepetition::create(
|
||||
[
|
||||
'piggy_bank_id' => $piggyBank->id,
|
||||
'startdate' => $start->format('Y-m-d'),
|
||||
'targetdate' => $end->format('Y-m-d'),
|
||||
'currentamount' => 0,
|
||||
]
|
||||
);
|
||||
|
||||
Transaction::create(
|
||||
[
|
||||
'account_id' => $account1->id,
|
||||
'transaction_journal_id' => $journal->id,
|
||||
'amount' => 100
|
||||
]
|
||||
);
|
||||
Transaction::create(
|
||||
[
|
||||
'account_id' => $account2->id,
|
||||
'transaction_journal_id' => $journal->id,
|
||||
'amount' => -100
|
||||
]
|
||||
);
|
||||
|
||||
$event = new JournalCreated($journal, $piggyBank->id);
|
||||
$class = new ConnectJournalToPiggyBank();
|
||||
$result = $class->handle($event);
|
||||
|
||||
|
||||
$this->assertTrue($result);
|
||||
$this->assertCount(1, $piggyBank->piggyBankEvents()->get());
|
||||
}
|
||||
|
||||
public function testWithRepetitionReversed()
|
||||
{
|
||||
FactoryMuffin::create('FireflyIII\Models\TransactionType');
|
||||
FactoryMuffin::create('FireflyIII\Models\TransactionType');
|
||||
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$this->be($user);
|
||||
|
||||
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
|
||||
$piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
|
||||
$account1 = FactoryMuffin::create('FireflyIII\Models\Account');
|
||||
$account2 = FactoryMuffin::create('FireflyIII\Models\Account');
|
||||
$account1->user_id = $user->id;
|
||||
$account2->user_id = $user->id;
|
||||
$piggyBank->account_id = $account1->id;
|
||||
$account1->save();
|
||||
$account2->save();
|
||||
$piggyBank->save();
|
||||
|
||||
$start = clone $journal->date;
|
||||
$end = clone $journal->date;
|
||||
$start->subDay();
|
||||
$end->addDay();
|
||||
|
||||
PiggyBankRepetition::create(
|
||||
[
|
||||
'piggy_bank_id' => $piggyBank->id,
|
||||
'startdate' => $start->format('Y-m-d'),
|
||||
'targetdate' => $end->format('Y-m-d'),
|
||||
'currentamount' => 0,
|
||||
]
|
||||
);
|
||||
|
||||
Transaction::create(
|
||||
[
|
||||
'account_id' => $account1->id,
|
||||
'transaction_journal_id' => $journal->id,
|
||||
'amount' => -100
|
||||
]
|
||||
);
|
||||
Transaction::create(
|
||||
[
|
||||
'account_id' => $account2->id,
|
||||
'transaction_journal_id' => $journal->id,
|
||||
'amount' => 100
|
||||
]
|
||||
);
|
||||
|
||||
$event = new JournalCreated($journal, $piggyBank->id);
|
||||
$class = new ConnectJournalToPiggyBank();
|
||||
$result = $class->handle($event);
|
||||
|
||||
|
||||
$this->assertTrue($result);
|
||||
$this->assertCount(1, $piggyBank->piggyBankEvents()->get());
|
||||
}
|
||||
|
||||
public function testZeroAmount()
|
||||
{
|
||||
FactoryMuffin::create('FireflyIII\Models\TransactionType');
|
||||
FactoryMuffin::create('FireflyIII\Models\TransactionType');
|
||||
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$this->be($user);
|
||||
|
||||
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
|
||||
$piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
|
||||
$account1 = FactoryMuffin::create('FireflyIII\Models\Account');
|
||||
$account2 = FactoryMuffin::create('FireflyIII\Models\Account');
|
||||
$account1->user_id = $user->id;
|
||||
$account2->user_id = $user->id;
|
||||
$piggyBank->account_id = $account1->id;
|
||||
$account1->save();
|
||||
$account2->save();
|
||||
$piggyBank->save();
|
||||
|
||||
Transaction::create(
|
||||
[
|
||||
'account_id' => $account1->id,
|
||||
'transaction_journal_id' => $journal->id,
|
||||
'amount' => 0
|
||||
]
|
||||
);
|
||||
Transaction::create(
|
||||
[
|
||||
'account_id' => $account2->id,
|
||||
'transaction_journal_id' => $journal->id,
|
||||
'amount' => 0
|
||||
]
|
||||
);
|
||||
|
||||
// two transactions:
|
||||
|
||||
|
||||
$event = new JournalCreated($journal, $piggyBank->id);
|
||||
$class = new ConnectJournalToPiggyBank();
|
||||
$result = $class->handle($event);
|
||||
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user