Files
firefly-iii/app/tests/controllers/ChartControllerTest.php

212 lines
6.8 KiB
PHP
Raw Normal View History

2014-07-08 15:17:18 +02:00
<?php
2014-07-20 21:56:20 +02:00
use League\FactoryMuffin\Facade\FactoryMuffin;
2014-07-08 15:17:18 +02:00
class ChartControllerTest extends TestCase
{
public function setUp()
{
parent::setUp();
Artisan::call('migrate');
Artisan::call('db:seed');
}
public function testHomeAccount()
{
// mock preference:
2014-07-20 21:56:20 +02:00
$pref2 = $this->mock('Preference');
$pref2->shouldReceive('getAttribute', 'data')->andReturn([]);
2014-07-08 15:17:18 +02:00
// mock preferences helper:
$preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface');
2014-07-20 21:56:20 +02:00
$preferences->shouldReceive('get')->with('frontpageAccounts', [])->once()->andReturn($pref2);
2014-07-08 15:17:18 +02:00
// mock toolkit:
2014-07-23 06:57:51 +02:00
$start = new Carbon\Carbon;
$start->startOfMonth();
$end = new \Carbon\Carbon;
$end->endOfMonth();
2014-07-08 15:17:18 +02:00
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
2014-07-23 06:57:51 +02:00
$toolkit->shouldReceive('getDateRange')->with()->once()->andReturn([$start, $end]);
2014-07-08 15:17:18 +02:00
// create a semi-mocked collection of accounts:
// mock account(s)
$personal = $this->mock('AccountType');
$personal->shouldReceive('jsonSerialize')->andReturn('');
$one = $this->mock('Account');
$one->shouldReceive('getAttribute')->andReturn($personal);
$one->shouldReceive('balance')->andReturn(0);
2014-07-20 21:56:20 +02:00
$one->shouldReceive('predict')->andReturn(null);
2014-07-08 15:17:18 +02:00
// collection:
$c = new \Illuminate\Database\Eloquent\Collection([$one]);
// mock account repository:
$accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->shouldReceive('getActiveDefault')->andReturn($c);
// call
$this->call('GET', '/chart/home/account');
// test
$this->assertResponseOk();
}
2014-07-23 06:57:51 +02:00
public function testHomeAccountWithPref()
2014-07-08 15:17:18 +02:00
{
2014-07-23 06:57:51 +02:00
// mock toolkit:
$start = new Carbon\Carbon;
$start->startOfMonth();
$end = new \Carbon\Carbon;
$end->endOfMonth();
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->shouldReceive('getDateRange')->with()->once()->andReturn([$start, $end]);
// mock account(s)
$personal = $this->mock('AccountType');
$personal->shouldReceive('jsonSerialize')->andReturn('');
$one = $this->mock('Account');
$one->shouldReceive('getAttribute')->andReturn($personal);
$one->shouldReceive('balance')->andReturn(0);
$one->shouldReceive('predict')->andReturn(null);
// collection:
$c = new \Illuminate\Database\Eloquent\Collection([$one]);
2014-07-08 15:17:18 +02:00
// mock preference:
2014-07-23 06:57:51 +02:00
$pref2 = $this->mock('Preference');
$pref2->shouldReceive('getAttribute', 'data')->andReturn([$one->id]);
2014-07-08 15:17:18 +02:00
// mock preferences helper:
$preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface');
2014-07-23 06:57:51 +02:00
$preferences->shouldReceive('get')->with('frontpageAccounts', [])->once()->andReturn($pref2);
// create a semi-mocked collection of accounts:
// mock account repository:
$accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->shouldReceive('getByIds')->andReturn($c);
// call
$this->call('GET', '/chart/home/account');
// test
$this->assertResponseOk();
}
public function testHomeAccountWithInput()
{
// save actual account:
$account = FactoryMuffin::create('Account');
2014-07-08 15:17:18 +02:00
// mock toolkit:
2014-07-23 06:57:51 +02:00
$start = new Carbon\Carbon;
$start->startOfMonth();
$end = new \Carbon\Carbon;
$end->endOfMonth();
2014-07-08 15:17:18 +02:00
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
2014-07-23 06:57:51 +02:00
$toolkit->shouldReceive('getDateRange')->with()->once()->andReturn([$start, $end]);
2014-07-08 15:17:18 +02:00
2014-07-08 15:31:59 +02:00
// mock account repository:
2014-07-08 15:17:18 +02:00
$accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->shouldReceive('find')->with(1)->andReturn($account);
// call
$this->call('GET', '/chart/home/account/' . $account->id);
// test
$this->assertResponseOk();
}
2014-07-09 12:56:06 +02:00
public function testhomeCategories()
{
$start = new \Carbon\Carbon;
$end = new \Carbon\Carbon;
2014-07-23 06:57:51 +02:00
// mock journals:
$transaction = FactoryMuffin::create('Transaction');
$journal = FactoryMuffin::create('TransactionJournal');
$journal->transactions()->save($transaction);
$tj = $this->mock('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$tj->shouldReceive('getByDateRange')->once()->with($start, $end)->andReturn([$journal]);
2014-07-09 12:56:06 +02:00
// mock toolkit
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->shouldReceive('getDateRange')->andReturn([$start, $end]);
2014-07-09 12:56:06 +02:00
2014-07-23 06:57:51 +02:00
// call
$this->call('GET', '/chart/home/categories');
// test
$this->assertResponseOk();
}
/**
* @expectedException \Firefly\Exception\FireflyException
*/
public function testhomeCategoriesException()
{
$start = new \Carbon\Carbon;
$end = new \Carbon\Carbon;
// mock journals:
$journal = FactoryMuffin::create('TransactionJournal');
$tj = $this->mock('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$tj->shouldReceive('getByDateRange')->once()->with($start, $end)->andReturn([$journal]);
// mock toolkit
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->shouldReceive('getDateRange')->andReturn([$start, $end]);
2014-07-09 12:56:06 +02:00
// call
$this->call('GET', '/chart/home/categories');
// test
$this->assertResponseOk();
}
2014-07-23 06:57:51 +02:00
public function testHomeAccountInfo()
{
$account = FactoryMuffin::create('Account');
$second = FactoryMuffin::create('Account');
$date = \Carbon\Carbon::createFromDate('2012', '01', '01');
2014-07-23 06:57:51 +02:00
$transaction = FactoryMuffin::create('Transaction');
$transaction->account()->associate($second);
$journal = FactoryMuffin::create('TransactionJournal');
$journal->transactions()->save($transaction);
// mock account repository:
$accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->shouldReceive('findByName')->with($account->name)->andReturn($account);
// mock journal:
$tj = $this->mock('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$tj->shouldReceive('getByAccountAndDate')->once()->andReturn([$journal]);
// call
$this->call('GET', '/chart/home/info/' . $account->name . '/' . $date->format('d/m/Y'));
2014-07-23 06:57:51 +02:00
// test
$this->assertResponseOk();
} //($name, $day, $month, $year)
public function tearDown()
{
Mockery::close();
}
2014-07-08 15:17:18 +02:00
}