firefly-iii/tests/controllers/AccountControllerTest.php

307 lines
10 KiB
PHP
Raw Normal View History

2015-03-28 00:48:38 -05:00
<?php
use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Preference;
2015-04-02 15:39:31 -05:00
use FireflyIII\Models\TransactionCurrency;
2015-04-07 11:48:34 -05:00
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
2015-04-03 01:31:50 -05:00
use League\FactoryMuffin\Facade as FactoryMuffin;
2015-03-28 00:48:38 -05:00
/**
2015-04-05 03:36:28 -05:00
* Class AccountControllerTest
2015-03-28 00:48:38 -05:00
*/
class AccountControllerTest extends TestCase
{
2015-04-04 14:52:56 -05:00
/** @var Account */
public $account;
2015-03-28 00:48:38 -05:00
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
public function setUp()
{
parent::setUp();
2015-04-04 14:52:56 -05:00
$this->createAccount();
2015-03-28 00:48:38 -05:00
}
2015-04-04 14:52:56 -05:00
/**
* 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();
}
2015-03-28 00:48:38 -05:00
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
public function tearDown()
{
parent::tearDown();
2015-03-28 00:48:38 -05:00
}
2015-04-04 14:52:56 -05:00
public function createAccount()
{
if (is_null($this->account)) {
$this->account = FactoryMuffin::create('FireflyIII\Models\Account');
2015-04-04 14:52:56 -05:00
Log::debug('Created a new account.');
//$this->account->accountType->type = 'Asset account';
//$this->account->accountType->save();
}
}
2015-03-28 00:48:38 -05:00
public function testCreate()
{
2015-04-03 01:31:50 -05:00
$pref = FactoryMuffin::create('FireflyIII\Models\Preference');
2015-03-31 12:21:49 -05:00
$pref->data = '1M';
2015-04-03 01:31:50 -05:00
$this->be($pref->user);
2015-03-31 12:21:49 -05:00
Preferences::shouldReceive('get', 'viewRange')->andReturn($pref);
2015-04-03 01:55:24 -05:00
// CURRENCY:
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
Amount::shouldReceive('getDefaultCurrency')->once()->andReturn($currency);
Amount::shouldReceive('getAllCurrencies')->once()->andReturn([$currency]);
2015-03-31 12:21:49 -05:00
2015-04-03 01:31:50 -05:00
$this->call('GET', '/accounts/create/asset');
2015-03-28 00:48:38 -05:00
$this->assertResponseOk();
2015-03-31 12:21:49 -05:00
2015-03-28 00:48:38 -05:00
$this->assertViewHas('subTitle', 'Create a new asset account');
$this->assertViewHas('subTitleIcon', 'fa-money');
$this->assertViewHas('what', 'asset');
}
public function testDelete()
{
2015-04-03 01:31:50 -05:00
$this->be($this->account->user);
$this->call('GET', '/accounts/delete/' . $this->account->id);
2015-04-03 01:31:50 -05:00
$this->assertResponseOk();
$this->assertViewHas('subTitle', 'Delete ' . strtolower(e($this->account->accountType->type)) . ' "' . e($this->account->name) . '"');
2015-04-03 01:31:50 -05:00
2015-03-28 00:48:38 -05:00
}
public function testDestroy()
{
2015-04-03 01:55:24 -05:00
// fake an account.
2015-04-04 14:52:56 -05:00
$account = FactoryMuffin::create('FireflyIII\Models\Account');
$this->be($account->user);
2015-04-07 11:48:34 -05:00
// mock:
$repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$repository->shouldReceive('destroy')->andReturn(true);
2015-04-03 01:55:24 -05:00
// post it!
2015-04-04 14:52:56 -05:00
$this->call('POST', '/accounts/destroy/' . $account->id, ['_token' => 'replaceme']);
2015-04-03 01:55:24 -05:00
$this->assertSessionHas('success');
2015-04-07 11:48:34 -05:00
$this->assertResponseStatus(302);
2015-03-28 00:48:38 -05:00
}
public function testEdit()
{
2015-04-03 01:55:24 -05:00
// fake an account.
$this->be($this->account->user);
$this->assertCount(1, DB::table('accounts')->where('id', $this->account->id)->whereNull('deleted_at')->get());
2015-04-03 01:55:24 -05:00
// create a transaction journal that will act as opening balance:
$openingBalance = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
$repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$repository->shouldReceive('openingBalanceTransaction')->andReturn($openingBalance);
// create a transaction that will be returned for the opening balance transaction:
$openingBalanceTransaction = FactoryMuffin::create('FireflyIII\Models\Transaction');
$repository->shouldReceive('getFirstTransaction')->andReturn($openingBalanceTransaction);
2015-04-03 01:55:24 -05:00
// CURRENCY:
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
Amount::shouldReceive('getDefaultCurrency')->once()->andReturn($currency);
Amount::shouldReceive('getAllCurrencies')->once()->andReturn([$currency]);
// get edit page:
$this->call('GET', '/accounts/edit/' . $this->account->id);
2015-04-03 01:55:24 -05:00
// assert stuff:
$this->assertResponseOk();
$this->assertSessionHas('preFilled');
$this->assertViewHas('subTitle', 'Edit ' . strtolower(e($this->account->accountType->type)) . ' "' . e($this->account->name) . '"');
2015-04-03 01:55:24 -05:00
2015-03-28 00:48:38 -05:00
}
public function testIndex()
{
// an account:
$this->be($this->account->user);
$collection = new Collection;
$collection->push($this->account);
$repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$repository->shouldReceive('getAccounts')->andReturn($collection);
$repository->shouldReceive('countAccounts')->andReturn(1);
$repository->shouldReceive('getLastActivity')->andReturn(null);
Amount::shouldReceive('format')->andReturn('');
Amount::shouldReceive('getCurrencyCode')->once()->andReturn('A');
// put stuff in session:
$this->session(['start' => new Carbon, 'end' => new Carbon]);
// get edit page:
$this->call('GET', '/accounts/asset');
$this->assertResponseOk();
$this->assertViewHas('what', 'asset');
2015-03-28 00:48:38 -05:00
}
public function testShow()
{
// an account:
2015-04-04 14:52:56 -05:00
$this->be($this->account->user);
2015-04-07 11:48:34 -05:00
// mock!
Amount::shouldReceive('getCurrencyCode')->once()->andReturn('A');
2015-04-07 11:48:34 -05:00
$repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
2015-04-07 11:52:15 -05:00
$repository->shouldReceive('getJournals')->andReturn(new LengthAwarePaginator([], 0, 10));
// get edit page:
2015-04-04 14:52:56 -05:00
$this->call('GET', '/accounts/show/' . $this->account->id);
$this->assertResponseOk();
2015-03-28 00:48:38 -05:00
}
public function testStore()
{
// an account:
$this->be($this->account->user);
$data = [
'name' => 'New test account ' . rand(1, 1000),
'what' => 'asset',
'virtualBalance' => 0,
'accountRole' => 'defaultAsset',
'openingBalance' => 20,
'openingBalanceDate' => date('Y-m-d'),
'openingBalanceCurrency' => 1,
'_token' => 'replaceme'
];
// fake validation routine:
$request = $this->mock('FireflyIII\Http\Requests\AccountFormRequest');
$request->shouldReceive('input')->andReturn('');
// fake store routine:
$repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$repository->shouldReceive('store')->andReturn($this->account);
$this->call('POST', '/accounts/store', $data);
$this->assertResponseStatus(302);
$this->assertSessionHas('success');
}
public function testStoreAndRedirect()
{
// an account:
$this->be($this->account->user);
$data = [
'name' => 'New test account ' . rand(1, 1000),
'what' => 'asset',
'virtualBalance' => 0,
'accountRole' => 'defaultAsset',
'openingBalance' => 20,
'openingBalanceDate' => date('Y-m-d'),
'openingBalanceCurrency' => 1,
'_token' => 'replaceme',
'create_another' => 1,
];
// fake validation routine:
$request = $this->mock('FireflyIII\Http\Requests\AccountFormRequest');
$request->shouldReceive('input')->andReturn('');
// fake store routine:
$repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$repository->shouldReceive('store')->andReturn($this->account);
$this->call('POST', '/accounts/store', $data);
$this->assertResponseStatus(302);
$this->assertSessionHas('success');
2015-03-28 00:48:38 -05:00
}
public function testUpdate()
{
// an account:
$this->be($this->account->user);
$data = [
'name' => 'Edited test account ' . rand(1, 1000),
'active' => 1,
'accountRole' => 'defaultAsset',
'virtualBalance' => 0,
'openingBalance' => 25,
'openingBalanceDate' => date('Y-m-d'),
'openingBalanceCurrency' => 1,
'_token' => 'replaceme'
];
// fake validation routine:
$request = $this->mock('FireflyIII\Http\Requests\AccountFormRequest');
$request->shouldReceive('input')->andReturn('');
// fake update routine:
$repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$repository->shouldReceive('update')->andReturn($this->account);
$this->call('POST', '/accounts/update/' . $this->account->id, $data);
$this->assertResponseStatus(302);
$this->assertSessionHas('success');
}
public function testUpdateAndRedirect()
{
// an account:
$this->be($this->account->user);
$data = [
'name' => 'Edited test account ' . rand(1, 1000),
'active' => 1,
'accountRole' => 'defaultAsset',
'virtualBalance' => 0,
'openingBalance' => 25,
'openingBalanceDate' => date('Y-m-d'),
'openingBalanceCurrency' => 1,
'_token' => 'replaceme',
'return_to_edit' => 1,
];
// fake validation routine:
$request = $this->mock('FireflyIII\Http\Requests\AccountFormRequest');
$request->shouldReceive('input')->andReturn('');
// fake update routine:
$repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$repository->shouldReceive('update')->andReturn($this->account);
$this->call('POST', '/accounts/update/' . $this->account->id, $data);
$this->assertResponseStatus(302);
$this->assertSessionHas('success');
2015-03-28 00:48:38 -05:00
}
}