Some new tests and fixes. [skip ci]

This commit is contained in:
James Cole 2014-07-23 06:57:51 +02:00
parent 9d142aa8b0
commit cc71a1116a
15 changed files with 397 additions and 97 deletions

View File

@ -87,7 +87,7 @@ class BudgetController extends BaseController
'repeats' => intval(Input::get('repeats'))
];
$budget = $this->_budgets->create($data);
$this->_budgets->store($data);
Session::flash('success', 'Budget created!');
return Redirect::route('budgets.index');
}
@ -106,31 +106,29 @@ class BudgetController extends BaseController
$return[$month][] = $entry;
}
$str = '';
foreach ($return as $month => $set) {
echo '<h1>' . $month . '</h1>';
$str .= '<h1>' . $month . '</h1>';
/** @var \TransactionJournal $tj */
$sum = 0;
foreach ($set as $tj) {
echo '#' . $tj->id . ' ' . $tj->description . ': ';
$str .= '#' . $tj->id . ' ' . $tj->description . ': ';
foreach ($tj->transactions as $index => $t) {
echo $t->amount . ', ';
$str .= $t->amount . ', ';
if ($index == 0) {
$sum += $t->amount;
}
}
echo '<br>';
$str .= '<br>';
}
echo 'sum: ' . $sum . '<br><br>';
$str .= 'sum: ' . $sum . '<br><br>';
}
exit;
return View::make('budgets.show');
return $str;
}

View File

@ -1,7 +1,8 @@
<?php
use Firefly\Exception\FireflyException;
use Firefly\Helper\Preferences\PreferencesHelperInterface as PHI;
use Firefly\Helper\Toolkit\Toolkit as tk;
use Firefly\Helper\Toolkit\ToolkitInterface as tk;
use Firefly\Storage\Account\AccountRepositoryInterface as ARI;
use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface as TJRI;
@ -36,6 +37,7 @@ class ChartController extends BaseController
public function homeAccount($accountId = null)
{
list($start, $end) = $this->_tk->getDateRange();
\Log::debug('Start is (cannot clone?): ' . $start);
$current = clone $start;
$return = [];
$account = null;
@ -73,8 +75,10 @@ class ChartController extends BaseController
}
} else {
$return[0] = ['name' => $account->name, 'id' => $account->id, 'data' => []];
\Log::debug('Start is: '.$start);
\Log::debug('End is: '.$end);
while ($current <= $end) {
\Log::debug('Current: ' . $current.' is smaller or equal to ' . $end);
if ($current > $today) {
$return[0]['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)];
} else {

View File

@ -5,7 +5,7 @@
App::before(
function ($request) {
if (Auth::check()) {
$toolkit = App::make('\Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit = App::make('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->getDateRange();
}
Event::fire('app.before');

View File

@ -27,6 +27,11 @@ class HelperServiceProvider extends ServiceProvider
'Firefly\Helper\Preferences\PreferencesHelperInterface',
'Firefly\Helper\Preferences\PreferencesHelper'
);
// settings:
$this->app->bind(
'Firefly\Helper\Toolkit\ToolkitInterface',
'Firefly\Helper\Toolkit\Toolkit'
);
}
}

View File

@ -2,7 +2,11 @@
namespace Firefly\Helper\Toolkit;
/**
* Class Toolkit
*
* @package Firefly\Helper\Toolkit
*/
class Toolkit implements ToolkitInterface
{
@ -16,6 +20,7 @@ class Toolkit implements ToolkitInterface
*/
public function getDateRange()
{
\Log::debug('Should be mocked!');
$preferences = \App::make('Firefly\Helper\Preferences\PreferencesHelperInterface');
$viewRange = $preferences->get('viewRange', '1M');

View File

@ -8,7 +8,7 @@ interface BudgetRepositoryInterface
public function getAsSelectList();
public function get();
public function create($data);
public function store($data);
public function find($id);

View File

@ -18,7 +18,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
return $return;
}
public function create($data)
public function store($data)
{
$budget = new \Budget;
$budget->name = $data['name'];

View File

@ -5,25 +5,25 @@ use LaravelBook\Ardent\Ardent as Ardent;
/**
* Limit
*
* @property integer $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property integer $component_id
* @property \Carbon\Carbon $startdate
* @property float $amount
* @property boolean $repeats
* @property string $repeat_freq
* @property-read \Component $component
* @property-read \Budget $budget
* @property integer $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property integer $component_id
* @property \Carbon\Carbon $startdate
* @property float $amount
* @property boolean $repeats
* @property string $repeat_freq
* @property-read \Component $component
* @property-read \Budget $budget
* @property-read \Illuminate\Database\Eloquent\Collection|\LimitRepetition[] $limitrepetitions
* @method static \Illuminate\Database\Query\Builder|\Limit whereId($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereComponentId($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereStartdate($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereAmount($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereRepeats($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereRepeatFreq($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereId($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereComponentId($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereStartdate($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereAmount($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereRepeats($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereRepeatFreq($value)
*/
class Limit extends Ardent
{
@ -38,17 +38,23 @@ class Limit extends Ardent
];
public static $factory
= [
public static function factory()
{
$start = new Carbon\Carbon;
$start->startOfMonth();
return [
'component_id' => 'factory|Budget',
'startdate' => 'date',
'enddate' => 'date',
'amount' => '100'
'startdate' => $start,
'amount' => '100',
'repeats' => 0,
'repeat_freq' => 'monthly'
];
}
public function component()
{
return $this->belongsTo('Component','component_id');
return $this->belongsTo('Component', 'component_id');
}
public function budget()
@ -56,7 +62,8 @@ class Limit extends Ardent
return $this->belongsTo('Budget', 'component_id');
}
public function limitrepetitions() {
public function limitrepetitions()
{
return $this->hasMany('LimitRepetition');
}

View File

@ -31,13 +31,19 @@ class LimitRepetition extends Ardent
'amount' => 'numeric|required|min:0.01',
];
public static $factory
= [
public static function factory()
{
$start = new \Carbon\Carbon;
$start->startOfMonth();
$end = clone $start;
$end->endOfMonth();
return [
'limit_id' => 'factory|Limit',
'startdate' => 'date',
'enddate' => 'date',
'amount' => 'integer'
'startdate' => $start,
'enddate' => $end,
'amount' => 100
];
}
public function limit()
{

View File

@ -32,13 +32,13 @@ use LaravelBook\Ardent\Ardent;
* @method static \Illuminate\Database\Query\Builder|\TransactionJournal whereDate($value)
* @method static \TransactionJournal after($date)
* @method static \TransactionJournal before($date)
* @property integer $user_id
* @property-read \User $user
* @property integer $user_id
* @property-read \User $user
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Category[] $categories
* @method static \Illuminate\Database\Query\Builder|\TransactionJournal whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|\TransactionJournal whereUserId($value)
*/
class TransactionJournal extends Ardent
{
@ -52,15 +52,18 @@ class TransactionJournal extends Ardent
'completed' => 'required|between:0,1'
];
public static $factory
= [
public static function factory()
{
$date = new \Carbon\Carbon;
return [
'transaction_type_id' => 'factory|TransactionType',
'transaction_currency_id' => 'factory|TransactionCurrency',
'description' => 'string',
'completed' => '1',
'user_id' => 'factory|User',
'date' => 'date|Y-m-d'
'date' => $date
];
}
public function transactionType()
{

View File

@ -27,8 +27,8 @@ Route::group(['before' => 'auth'], function () {
Route::get('/accounts/{account}', ['uses' => 'AccountController@show', 'as' => 'accounts.show']);
// budget controller:
Route::get('/budgets/{group?}',['uses' => 'BudgetController@index','as' => 'budgets.index']);
Route::get('/budget/create',['uses' => 'BudgetController@create', 'as' => 'budgets.create']);
Route::get('/budgets/{group?}',['uses' => 'BudgetController@index','as' => 'budgets.index']);
Route::get('/budget/show/{id}',['uses' => 'BudgetController@show', 'as' => 'budgets.show']);
// limit controller:

View File

@ -0,0 +1,121 @@
<?php
use \League\FactoryMuffin\Facade\FactoryMuffin;
/**
* Class BudgetControllerTest
*/
class BudgetControllerTest extends TestCase
{
public function setUp()
{
parent::setUp();
Artisan::call('migrate');
Artisan::call('db:seed');
}
public function testIndex()
{
// create some objects:
$budget = FactoryMuffin::create('Budget');
$limit = FactoryMuffin::create('Limit');
$rep = FactoryMuffin::create('LimitRepetition');
$limit->limitrepetitions()->save($rep);
$budget->limits()->save($limit);
// mock budget repository:
$budgets = $this->mock('Firefly\Storage\Budget\BudgetRepositoryInterface');
$budgets->shouldReceive('get')->once()->andReturn([$budget]);
// call
$this->call('GET', '/budgets/date');
// test
$this->assertResponseOk();
}
public function testIndexBudget()
{
// create some objects:
$budget = FactoryMuffin::create('Budget');
$limit = FactoryMuffin::create('Limit');
$rep = FactoryMuffin::create('LimitRepetition');
$limit->limitrepetitions()->save($rep);
$budget->limits()->save($limit);
// mock budget repository:
$budgets = $this->mock('Firefly\Storage\Budget\BudgetRepositoryInterface');
$budgets->shouldReceive('get')->once()->andReturn([$budget]);
// call
$this->call('GET', '/budgets/budget');
// test
$this->assertResponseOk();
}
public function testCreate()
{
// call
$this->call('GET', '/budget/create');
// test
$this->assertResponseOk();
}
public function testStore()
{
$data = [
'name' => 'X',
'amount' => 100,
'period' => 'monthly',
'repeats' => 0
];
$return = $data;
$return['repeat_freq'] = 'monthly';
unset($return['period']);
// mock budget repository:
$budgets = $this->mock('Firefly\Storage\Budget\BudgetRepositoryInterface');
$budgets->shouldReceive('store')->with($return)->once()->andReturn(true);
// call
$this->call('POST', '/budget/store',$data);
// test
$this->assertResponseStatus(302);
$this->assertSessionHas('success');
}
public function testShow()
{
$budget = FactoryMuffin::create('Budget');
$journal = FactoryMuffin::create('TransactionJournal');
$transaction = FactoryMuffin::create('Transaction');
$journal->transactions()->save($transaction);
$budget->transactionjournals()->save($journal);
// mock budget repository:
$budgets = $this->mock('Firefly\Storage\Budget\BudgetRepositoryInterface');
$budgets->shouldReceive('find')->with($budget->id)->once()->andReturn($budget);
// call
$this->call('GET', '/budget/show/'.$budget->id);
// test
$this->assertResponseOk();
}
public function tearDown()
{
Mockery::close();
}
}

View File

@ -14,20 +14,21 @@ class ChartControllerTest extends TestCase
public function testHomeAccount()
{
// mock preference:
$pref = $this->mock('Preference');
$pref->shouldReceive('getAttribute', 'data')->andReturn('1M');
$pref2 = $this->mock('Preference');
$pref2->shouldReceive('getAttribute', 'data')->andReturn([]);
// mock preferences helper:
$preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface');
$preferences->shouldReceive('get')->with('viewRange', '1M')->once()->andReturn($pref);
$preferences->shouldReceive('get')->with('frontpageAccounts', [])->once()->andReturn($pref2);
// mock toolkit:
$start = new Carbon\Carbon;
$start->startOfMonth();
$end = new \Carbon\Carbon;
$end->endOfMonth();
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->shouldReceive('getDateRange')->andReturn(null);
$toolkit->shouldReceive('getDateRange')->with()->once()->andReturn([$start, $end]);
// create a semi-mocked collection of accounts:
@ -55,22 +56,65 @@ class ChartControllerTest extends TestCase
$this->assertResponseOk();
}
public function testHomeAccountWithPref()
{
// 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]);
// mock preference:
$pref2 = $this->mock('Preference');
$pref2->shouldReceive('getAttribute', 'data')->andReturn([$one->id]);
// mock preferences helper:
$preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface');
$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');
// mock preference:
$pref = $this->mock('Preference');
$pref->shouldReceive('getAttribute', 'data')->andReturn('week');
// mock preferences helper:
$preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface');
$preferences->shouldReceive('get')->with('viewRange', '1M')->once()->andReturn($pref);
// mock toolkit:
$start = new Carbon\Carbon;
$start->startOfMonth();
$end = new \Carbon\Carbon;
$end->endOfMonth();
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->shouldReceive('getDateRange')->andReturn(null);
$toolkit->shouldReceive('getDateRange')->with()->once()->andReturn([$start, $end]);
// mock account repository:
$accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
@ -89,30 +133,17 @@ class ChartControllerTest extends TestCase
$start = new \Carbon\Carbon;
$end = new \Carbon\Carbon;
// mock preferences & pref:
$pref = $this->mock('Preference');
$pref->shouldReceive('getAttribute', 'data')->andReturn([]);
// 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]);
// mock toolkit
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->shouldReceive('getDateRange')->andReturn([$start, $end]);
//
// // mock preference?
$preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface');
$preferences->shouldReceive('get')->with('viewRange', '1M')->once()->andReturn($pref);
//
//
// // mock toolkit:
//// $class = $this->getMockClass('Foo', array('baz'));
//// $toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
//// $toolkit::static ->shouldReceive('getDateRange')->once()->andReturn([$start, $end]);
//
//// // mock transaction journal
//// $tj = $this->mock('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
//// $tj->shouldReceive('getByDateRange')->with($start, $end)->andReturn([]);
////
// call
$this->call('GET', '/chart/home/categories');
@ -121,6 +152,64 @@ class ChartControllerTest extends TestCase
$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]);
// call
$this->call('GET', '/chart/home/categories');
// test
$this->assertResponseOk();
}
public function testHomeAccountInfo()
{
$account = FactoryMuffin::create('Account');
$second = FactoryMuffin::create('Account');
$date = \Carbon\Carbon::createFromDate('2012','01','01');
$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'));
// test
$this->assertResponseOk();
} //($name, $day, $month, $year)
public function tearDown()
{
Mockery::close();

View File

@ -1,10 +1,17 @@
<?php
use League\FactoryMuffin\Facade\FactoryMuffin;
/**
* Class HomeControllerTest
*/
class HomeControllerTest extends TestCase
{
public function setUp()
{
parent::setUp();
Artisan::call('migrate');
Artisan::call('db:seed');
}
public function testIndex()
@ -12,12 +19,12 @@ class HomeControllerTest extends TestCase
// mock:
View::shouldReceive('share');
View::shouldReceive('make')->with('index')->once()->andReturn(\Mockery::self())
->shouldReceive('with')->once() // Pass a 'with' parameter
->shouldReceive('with')->once() // Pass a 'with' parameter
->with('count', 0)->andReturn(\Mockery::self())
->shouldReceive('with')->once() // Pass a 'with' parameter
->shouldReceive('with')->once() // Pass a 'with' parameter
->with('transactions', [])->andReturn(\Mockery::self());
// pass another
// pass another
Auth::shouldReceive('check')->andReturn(true);
@ -40,6 +47,46 @@ class HomeControllerTest extends TestCase
$this->assertResponseOk();
}
public function testIndexWithAccounts()
{
// mock:
View::shouldReceive('share');
View::shouldReceive('make')->with('index')->once()->andReturn(\Mockery::self())
->shouldReceive('with')->once() // Pass a 'with' parameter
->with('count', 1)->andReturn(\Mockery::self())
->shouldReceive('with')->once() // Pass a 'with' parameter
->with('transactions', [])->andReturn(\Mockery::self());
// pass another
Auth::shouldReceive('check')->andReturn(true);
// make account:
$account = FactoryMuffin::create('Account');
// mock account repository
$accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->shouldReceive('count')->andReturn(1);
$accounts->shouldReceive('getByIds')->with([$account->id])->andReturn([$account]);
// mock transaction journal repository:
$tj = $this->mock('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$tj->shouldReceive('getByAccount')->with($account,15)->andReturn([]);
// mock preferences & pref:
$pref = $this->mock('Preference');
$pref->shouldReceive('getAttribute', 'data')->andReturn([$account->id]);
$preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface');
$preferences->shouldReceive('get')->with('frontpageAccounts', [])->once()->andReturn($pref);
// call
$this->call('GET', '/');
// test
$this->assertResponseOk();
}
public function tearDown()
{
Mockery::close();

View File

@ -119,14 +119,6 @@
</div>
</div>
<!-- ALWAYS SHOW SUBMit -->
<div class="form-group">
<label for="submit" class="col-sm-4 control-label">&nbsp;</label>
<div class="col-sm-8">
<input type="submit" name="submit" value="Create {{$what}}" class="btn btn-info" />
</div>
</div>
</div>
<div class="col-lg-6 col-md-12 col-sm-12">
<h4>Optional fields</h4>
@ -154,8 +146,31 @@
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<!-- add another after this one? -->
<div class="form-group">
<label for="submit" class="col-sm-4 control-label">&nbsp;</label>
<div class="col-sm-8">
<div class="checkbox">
<label>
<input type="checkbox" value="1" name="create">
Create another (return to this form)
</label>
</div>
</div>
</div>
<!-- ALWAYS SHOW SUBMit -->
<div class="form-group">
<label for="submit" class="col-sm-4 control-label">&nbsp;</label>
<div class="col-sm-8">
<input type="submit" name="submit" value="Create {{$what}}" class="btn btn-info" />
</div>
</div>
</div>
</div>
@stop
@section('scripts')