mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-05-03 20:56:21 +00:00
Included bill controller.
This commit is contained in:
249
tests/controllers/BillControllerTest.php
Normal file
249
tests/controllers/BillControllerTest.php
Normal file
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\FactoryMuffin\Facade as FactoryMuffin;
|
||||
|
||||
/**
|
||||
* Class BillControllerTest
|
||||
*/
|
||||
class BillControllerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Sets up the fixture, for example, opens a network connection.
|
||||
* This method is called before a test is executed.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called before the first test of this test class is run.
|
||||
*
|
||||
* @since Method available since Release 3.4.0
|
||||
*/
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
parent::setUpBeforeClass();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 testAdd()
|
||||
{
|
||||
// create a bill:
|
||||
$bill = FactoryMuffin::create('FireflyIII\Models\Bill');
|
||||
$this->be($bill->user);
|
||||
|
||||
// create an expense account:
|
||||
$expense = FactoryMuffin::create('FireflyIII\Models\Account');
|
||||
// fix the name of the expense account to match one of the words
|
||||
// in the bill:
|
||||
$words = explode(',', $bill->match);
|
||||
$word = $words[1];
|
||||
$expense->name = $word;
|
||||
$expense->save();
|
||||
|
||||
// mock repository:
|
||||
$repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
|
||||
$repository->shouldReceive('getAccounts')->andReturn([$expense]);
|
||||
|
||||
// go!
|
||||
$this->call('GET', '/bills/add/' . $bill->id);
|
||||
$this->assertSessionHas('preFilled');
|
||||
$this->assertRedirectedToRoute('transactions.create', ['withdrawal']);
|
||||
}
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
// go!
|
||||
$bill = FactoryMuffin::create('FireflyIII\Models\Bill');
|
||||
$this->be($bill->user);
|
||||
|
||||
// CURRENCY:
|
||||
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
|
||||
Amount::shouldReceive('getDefaultCurrency')->once()->andReturn($currency);
|
||||
Amount::shouldReceive('getAllCurrencies')->once()->andReturn([$currency]);
|
||||
|
||||
$this->call('GET', '/bills/create');
|
||||
$this->assertViewHas('subTitle', 'Create new');
|
||||
$this->assertResponseOk();
|
||||
|
||||
}
|
||||
|
||||
public function testDelete()
|
||||
{
|
||||
$bill = FactoryMuffin::create('FireflyIII\Models\Bill');
|
||||
$this->be($bill->user);
|
||||
$this->call('GET', '/bills/delete/' . $bill->id);
|
||||
$this->assertViewHas('subTitle', 'Delete "' . e($bill->name) . '"');
|
||||
$this->assertResponseOk();
|
||||
}
|
||||
|
||||
public function testDestroy()
|
||||
{
|
||||
$bill = FactoryMuffin::create('FireflyIII\Models\Bill');
|
||||
$this->be($bill->user);
|
||||
|
||||
|
||||
$this->call('POST', '/bills/destroy/' . $bill->id, ['_token' => 'replaceMe']);
|
||||
$this->assertSessionHas('success', 'The bill was deleted.');
|
||||
$this->assertResponseStatus(302);
|
||||
}
|
||||
|
||||
public function testEdit()
|
||||
{
|
||||
$bill = FactoryMuffin::create('FireflyIII\Models\Bill');
|
||||
$this->be($bill->user);
|
||||
|
||||
// CURRENCY:
|
||||
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
|
||||
Amount::shouldReceive('getDefaultCurrency')->once()->andReturn($currency);
|
||||
Amount::shouldReceive('getAllCurrencies')->once()->andReturn([$currency]);
|
||||
|
||||
$this->call('GET', '/bills/edit/' . $bill->id);
|
||||
$this->assertViewHas('subTitle', 'Edit "' . e($bill->name) . '"');
|
||||
$this->assertResponseOk();
|
||||
}
|
||||
|
||||
public function testIndex()
|
||||
{
|
||||
$bill = FactoryMuffin::create('FireflyIII\Models\Bill');
|
||||
$this->be($bill->user);
|
||||
|
||||
$collection = new Collection;
|
||||
$collection->push($bill);
|
||||
|
||||
Amount::shouldReceive('format')->andReturn('XX');
|
||||
|
||||
$repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface');
|
||||
$repository->shouldReceive('getBills')->once()->andReturn($collection);
|
||||
$repository->shouldReceive('nextExpectedMatch')->with($bill)->andReturn(new Carbon);
|
||||
$repository->shouldReceive('lastFoundMatch')->with($bill)->andReturn(new Carbon);
|
||||
|
||||
$this->call('GET', '/bills');
|
||||
$this->assertResponseOk();
|
||||
|
||||
}
|
||||
|
||||
public function testRescan()
|
||||
{
|
||||
$bill = FactoryMuffin::create('FireflyIII\Models\Bill');
|
||||
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
|
||||
$collection = new Collection;
|
||||
$this->be($bill->user);
|
||||
$collection->push($journal);
|
||||
|
||||
$repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface');
|
||||
$repository->shouldReceive('getPossiblyRelatedJournals')->once()->andReturn($collection);
|
||||
$repository->shouldReceive('scan');
|
||||
|
||||
$this->call('GET', '/bills/rescan/' . $bill->id);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success', 'Rescanned everything.');
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function testRescanInactive()
|
||||
{
|
||||
$bill = FactoryMuffin::create('FireflyIII\Models\Bill');
|
||||
$bill->active = 0;
|
||||
$bill->save();
|
||||
$this->be($bill->user);
|
||||
|
||||
$this->call('GET', '/bills/rescan/' . $bill->id);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('warning', 'Inactive bills cannot be scanned.');
|
||||
|
||||
}
|
||||
|
||||
public function testShow()
|
||||
{
|
||||
$bill = FactoryMuffin::create('FireflyIII\Models\Bill');
|
||||
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
|
||||
$collection = new Collection;
|
||||
|
||||
$bill->save();
|
||||
$this->be($bill->user);
|
||||
$collection->push($journal);
|
||||
|
||||
|
||||
$repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface');
|
||||
$repository->shouldReceive('getJournals')->once()->andReturn($collection);
|
||||
$repository->shouldReceive('nextExpectedMatch')->once()->andReturn(new Carbon);
|
||||
|
||||
Amount::shouldReceive('format')->andReturn('XX');
|
||||
Amount::shouldReceive('getCurrencyCode')->andReturn('XX');
|
||||
|
||||
$this->call('GET', '/bills/show/' . $bill->id);
|
||||
}
|
||||
|
||||
public function testStore()
|
||||
{
|
||||
$bill = FactoryMuffin::create('FireflyIII\Models\Bill');
|
||||
$repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface');
|
||||
$request = $this->mock('FireflyIII\Http\Requests\BillFormRequest');
|
||||
|
||||
$this->be($bill->user);
|
||||
$request->shouldReceive('getBillData')->once()->andReturn([]);
|
||||
$repository->shouldReceive('store')->with([])->andReturn($bill);
|
||||
|
||||
$this->call('POST', '/bills/store', ['_token' => 'replaceMe']);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success', 'Bill "' . e($bill->name) . '" stored.');
|
||||
}
|
||||
|
||||
public function testStoreAndRedirect()
|
||||
{
|
||||
$bill = FactoryMuffin::create('FireflyIII\Models\Bill');
|
||||
$repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface');
|
||||
$request = $this->mock('FireflyIII\Http\Requests\BillFormRequest');
|
||||
|
||||
$this->be($bill->user);
|
||||
$request->shouldReceive('getBillData')->once()->andReturn([]);
|
||||
$repository->shouldReceive('store')->with([])->andReturn($bill);
|
||||
|
||||
$this->call('POST', '/bills/store', ['_token' => 'replaceMe', 'create_another' => 1]);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success', 'Bill "' . e($bill->name) . '" stored.');
|
||||
}
|
||||
|
||||
public function testUpdate()
|
||||
{
|
||||
$bill = FactoryMuffin::create('FireflyIII\Models\Bill');
|
||||
$repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface');
|
||||
$request = $this->mock('FireflyIII\Http\Requests\BillFormRequest');
|
||||
|
||||
$this->be($bill->user);
|
||||
$request->shouldReceive('getBillData')->once()->andReturn([]);
|
||||
$repository->shouldReceive('update')->andReturn($bill);
|
||||
|
||||
$this->call('POST', '/bills/update/' . $bill->id, ['_token' => 'replaceMe']);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success', 'Bill "' . e($bill->name) . '" updated.');
|
||||
}
|
||||
|
||||
public function testUpdateAndRedirect()
|
||||
{
|
||||
$bill = FactoryMuffin::create('FireflyIII\Models\Bill');
|
||||
$repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface');
|
||||
$request = $this->mock('FireflyIII\Http\Requests\BillFormRequest');
|
||||
|
||||
$this->be($bill->user);
|
||||
$request->shouldReceive('getBillData')->once()->andReturn([]);
|
||||
$repository->shouldReceive('update')->andReturn($bill);
|
||||
|
||||
$this->call('POST', '/bills/update/' . $bill->id, ['_token' => 'replaceMe', 'return_to_edit' => 1]);
|
||||
$this->assertResponseStatus(302);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -27,109 +27,147 @@ if (!class_exists('RandomString')) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
FactoryMuffin::define(
|
||||
'FireflyIII\Models\Account', [
|
||||
'user_id' => 'factory|FireflyIII\User',
|
||||
'account_type_id' => 'factory|FireflyIII\Models\AccountType',
|
||||
'name' => 'word',
|
||||
'active' => 'boolean',
|
||||
'encrypted' => 'boolean',
|
||||
'virtual_balance' => 0
|
||||
]
|
||||
'FireflyIII\Models\Bill',
|
||||
[
|
||||
'user_id' => 'factory|FireflyIII\User',
|
||||
'name' => 'sentence',
|
||||
'match' => function () {
|
||||
$words = [];
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
$words[] = RandomString::generateRandomString(5);
|
||||
}
|
||||
|
||||
return join(',', $words);
|
||||
},
|
||||
'amount_min' => 10,
|
||||
'amount_max' => 20,
|
||||
'date' => 'date',
|
||||
'active' => 1,
|
||||
'automatch' => 1,
|
||||
'repeat_freq' => 'monthly',
|
||||
'skip' => 0,
|
||||
'name_encrypted' => 1,
|
||||
'match_encrypted' => 1,
|
||||
|
||||
]
|
||||
);
|
||||
|
||||
FactoryMuffin::define(
|
||||
'FireflyIII\Models\Budget', [
|
||||
'user_id' => 'factory|FireflyIII\User',
|
||||
'name' => 'sentence',
|
||||
'active' => 'boolean',
|
||||
'encrypted' => 1,
|
||||
]
|
||||
'FireflyIII\Models\Account',
|
||||
[
|
||||
'user_id' => 'factory|FireflyIII\User',
|
||||
'account_type_id' => 'factory|FireflyIII\Models\AccountType',
|
||||
'name' => 'word',
|
||||
'active' => 'boolean',
|
||||
'encrypted' => 'boolean',
|
||||
'virtual_balance' => 0
|
||||
]
|
||||
);
|
||||
|
||||
FactoryMuffin::define(
|
||||
'FireflyIII\Models\LimitRepetition', [
|
||||
'budget_limit_id' => 'factory|FireflyIII\Models\BudgetLimit',
|
||||
'startdate' => 'date',
|
||||
'enddate' => 'date',
|
||||
'amount' => 'integer',
|
||||
]
|
||||
'FireflyIII\Models\Budget',
|
||||
[
|
||||
'user_id' => 'factory|FireflyIII\User',
|
||||
'name' => 'sentence',
|
||||
'active' => 'boolean',
|
||||
'encrypted' => 1,
|
||||
]
|
||||
);
|
||||
|
||||
FactoryMuffin::define(
|
||||
'FireflyIII\Models\BudgetLimit', [
|
||||
'budget_id' => 'factory|FireflyIII\Models\Budget',
|
||||
'startdate' => 'date',
|
||||
'amount' => 'integer',
|
||||
'repeats' => 'false',
|
||||
'repeat_freq' => 'monthly',
|
||||
'FireflyIII\Models\LimitRepetition',
|
||||
[
|
||||
'budget_limit_id' => 'factory|FireflyIII\Models\BudgetLimit',
|
||||
'startdate' => 'date',
|
||||
'enddate' => 'date',
|
||||
'amount' => 'integer',
|
||||
]
|
||||
);
|
||||
|
||||
]
|
||||
FactoryMuffin::define(
|
||||
'FireflyIII\Models\BudgetLimit',
|
||||
[
|
||||
'budget_id' => 'factory|FireflyIII\Models\Budget',
|
||||
'startdate' => 'date',
|
||||
'amount' => 'integer',
|
||||
'repeats' => 'false',
|
||||
'repeat_freq' => 'monthly',
|
||||
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
FactoryMuffin::define(
|
||||
'FireflyIII\Models\Preference', [
|
||||
'name' => 'word',
|
||||
'data' => 'sentence',
|
||||
'user_id' => 'factory|FireflyIII\User',
|
||||
]
|
||||
'FireflyIII\Models\Preference',
|
||||
[
|
||||
'name' => 'word',
|
||||
'data' => 'sentence',
|
||||
'user_id' => 'factory|FireflyIII\User',
|
||||
]
|
||||
);
|
||||
|
||||
FactoryMuffin::define(
|
||||
'FireflyIII\Models\AccountType', [
|
||||
'type' => function () {
|
||||
$types = ['Expense account', 'Revenue account', 'Asset account'];
|
||||
$count = DB::table('account_types')->count();
|
||||
'FireflyIII\Models\AccountType',
|
||||
[
|
||||
'type' => function () {
|
||||
$types = ['Expense account', 'Revenue account', 'Asset account'];
|
||||
$count = DB::table('account_types')->count();
|
||||
|
||||
return $types[$count];
|
||||
},
|
||||
'editable' => 1,
|
||||
]
|
||||
return $types[$count];
|
||||
},
|
||||
'editable' => 1,
|
||||
]
|
||||
);
|
||||
|
||||
FactoryMuffin::define(
|
||||
'FireflyIII\Models\TransactionCurrency', [
|
||||
'code' => function () {
|
||||
return RandomString::generateRandomString(3);
|
||||
},
|
||||
'symbol' => function () {
|
||||
return RandomString::generateRandomString(1);
|
||||
},
|
||||
'name' => 'word'
|
||||
]
|
||||
'FireflyIII\Models\TransactionCurrency',
|
||||
[
|
||||
'code' => function () {
|
||||
return RandomString::generateRandomString(3);
|
||||
},
|
||||
'symbol' => function () {
|
||||
return RandomString::generateRandomString(1);
|
||||
},
|
||||
'name' => 'word'
|
||||
]
|
||||
);
|
||||
|
||||
FactoryMuffin::define(
|
||||
'FireflyIII\User', [
|
||||
'email' => 'email',
|
||||
'password' => bcrypt('james'),
|
||||
]
|
||||
'FireflyIII\User',
|
||||
[
|
||||
'email' => 'email',
|
||||
'password' => bcrypt('james'),
|
||||
]
|
||||
);
|
||||
|
||||
FactoryMuffin::define(
|
||||
'FireflyIII\Models\Transaction', [
|
||||
'transaction_journal_id' => 'factory|FireflyIII\Models\TransactionJournal',
|
||||
'amount' => 'integer',
|
||||
'account_id' => 'factory|FireflyIII\Models\Account'
|
||||
]
|
||||
'FireflyIII\Models\Transaction',
|
||||
[
|
||||
'transaction_journal_id' => 'factory|FireflyIII\Models\TransactionJournal',
|
||||
'amount' => 'integer',
|
||||
'account_id' => 'factory|FireflyIII\Models\Account'
|
||||
]
|
||||
);
|
||||
|
||||
FactoryMuffin::define(
|
||||
'FireflyIII\Models\TransactionType', [
|
||||
'type' => 'word',
|
||||
]
|
||||
'FireflyIII\Models\TransactionType',
|
||||
[
|
||||
'type' => 'word',
|
||||
]
|
||||
);
|
||||
|
||||
FactoryMuffin::define(
|
||||
'FireflyIII\Models\TransactionJournal', [
|
||||
'user_id' => 'factory|FireflyIII\User',
|
||||
'transaction_type_id' => 'factory|FireflyIII\Models\TransactionType',
|
||||
'transaction_currency_id' => 'factory|FireflyIII\Models\TransactionCurrency',
|
||||
'description' => 'sentence',
|
||||
'completed' => '1',
|
||||
'date' => 'date',
|
||||
'encrypted' => '1',
|
||||
'order' => '0',
|
||||
]
|
||||
'FireflyIII\Models\TransactionJournal',
|
||||
[
|
||||
'user_id' => 'factory|FireflyIII\User',
|
||||
'transaction_type_id' => 'factory|FireflyIII\Models\TransactionType',
|
||||
'transaction_currency_id' => 'factory|FireflyIII\Models\TransactionCurrency',
|
||||
'description' => 'sentence',
|
||||
'completed' => '1',
|
||||
'date' => 'date',
|
||||
'encrypted' => '1',
|
||||
'order' => '0',
|
||||
]
|
||||
);
|
||||
Reference in New Issue
Block a user