Some cleaning up and new tests for the piggy bank controller.

This commit is contained in:
James Cole 2014-08-09 12:23:42 +02:00
parent c06e2380a5
commit b5ae008386
8 changed files with 243 additions and 99 deletions

View File

@ -1,77 +0,0 @@
<?php
use Firefly\Helper\Migration\MigrationHelperInterface as MHI;
/**
* Class MigrationController
*/
class MigrationController extends BaseController
{
protected $_migration;
/**
* @param MHI $migration
*/
public function __construct(MHI $migration)
{
$this->_migration = $migration;
View::share('menu', 'home');
}
/**
* Dev method
*/
public function dev()
{
$file = Config::get('dev.import');
if (file_exists($file)) {
$user = User::find(1);
/** @noinspection PhpParamsInspection */
Auth::login($user);
/** @var Firefly\Helper\Migration\MigrationHelperInterface $migration */
$migration = App::make('Firefly\Helper\Migration\MigrationHelperInterface');
$migration->loadFile($file);
if ($migration->validFile()) {
$migration->migrate();
} else {
throw new \Firefly\Exception\FireflyException('Invalid file.');
}
}
return '<a href="' . route('index') . '">home</a>';
}
/**
* @return \Illuminate\View\View
*/
public function index()
{
return View::make('migrate.index');
}
/**
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
*/
public function postIndex()
{
if (Input::hasFile('exportFile')) {
// get content:
$file = Input::file('exportFile');
$path = $file->getRealPath();
$this->_migration->loadFile($path);
if (!$this->_migration->validFile()) {
return View::make('error')->with('message', 'Invalid JSON content.');
}
$this->_migration->migrate();
return Redirect::route('index');
} else {
return View::make('error')->with('message', 'No file selected');
}
}
}

View File

@ -42,12 +42,6 @@ class HelperServiceProvider extends ServiceProvider
'Firefly\Helper\Email\EmailHelper'
);
// migration:
$this->app->bind(
'Firefly\Helper\Migration\MigrationHelperInterface',
'Firefly\Helper\Migration\MigrationHelper'
);
// settings:
$this->app->bind(
'Firefly\Helper\Preferences\PreferencesHelperInterface',

View File

@ -1,19 +1,20 @@
<?php
use Carbon\Carbon;
use LaravelBook\Ardent\Ardent as Ardent;
/**
* Piggybank
*
* @property integer $id
* @property integer $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property integer $account_id
* @property integer $account_id
* @property \Carbon\Carbon $targetdate
* @property string $name
* @property float $amount
* @property float $target
* @property integer $order
* @property-read \Account $account
* @property string $name
* @property float $amount
* @property float $target
* @property integer $order
* @property-read \Account $account
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereId($value)
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereUpdatedAt($value)
@ -36,6 +37,21 @@ class Piggybank extends Ardent
'order' => 'required:min:1',
];
public static function factory()
{
$start = new Carbon;
$start->endOfMonth();
return [
'name' => 'string',
'account_id' => 'factory|Account',
'targetdate' => $start,
'amount' => 0,
'target' => 100,
'order' => 1
];
}
public function account()
{
return $this->belongsTo('Account');

View File

@ -1,6 +1,37 @@
<?php
use LaravelBook\Ardent\Ardent;
/**
* RecurringTransaction
*
* @property integer $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property integer $user_id
* @property string $name
* @property string $match
* @property float $amount_max
* @property float $amount_min
* @property \Carbon\Carbon $date
* @property boolean $active
* @property boolean $automatch
* @property string $repeat_freq
* @property integer $skip
* @property-read \User $user
* @method static \Illuminate\Database\Query\Builder|\RecurringTransaction whereId($value)
* @method static \Illuminate\Database\Query\Builder|\RecurringTransaction whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\RecurringTransaction whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\RecurringTransaction whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|\RecurringTransaction whereName($value)
* @method static \Illuminate\Database\Query\Builder|\RecurringTransaction whereMatch($value)
* @method static \Illuminate\Database\Query\Builder|\RecurringTransaction whereAmountMax($value)
* @method static \Illuminate\Database\Query\Builder|\RecurringTransaction whereAmountMin($value)
* @method static \Illuminate\Database\Query\Builder|\RecurringTransaction whereDate($value)
* @method static \Illuminate\Database\Query\Builder|\RecurringTransaction whereActive($value)
* @method static \Illuminate\Database\Query\Builder|\RecurringTransaction whereAutomatch($value)
* @method static \Illuminate\Database\Query\Builder|\RecurringTransaction whereRepeatFreq($value)
* @method static \Illuminate\Database\Query\Builder|\RecurringTransaction whereSkip($value)
*/
class RecurringTransaction extends Ardent
{

View File

@ -59,6 +59,10 @@ use LaravelBook\Ardent\Ardent;
* 'Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Category[] $categories
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Category[] $categories
*/
class TransactionJournal extends Ardent
{

View File

@ -33,6 +33,7 @@ use LaravelBook\Ardent\Ardent;
* @method static \Illuminate\Database\Query\Builder|\User whereMigrated($value)
* @property-read \Illuminate\Database\Eloquent\Collection|\TransactionJournal[] $transactionjournals
* @property-read \Illuminate\Database\Eloquent\Collection|\Piggybank[] $piggybanks
* @property-read \Illuminate\Database\Eloquent\Collection|\RecurringTransaction[] $recurringtransactions
*/
class User extends Ardent implements UserInterface, RemindableInterface
{

View File

@ -136,6 +136,7 @@ Route::group(['before' => 'auth'], function () {
// piggy bank controller
Route::get('/piggybanks',['uses' => 'PiggybankController@index','as' => 'piggybanks.index']);
Route::get('/piggybanks/create', ['uses' => 'PiggybankController@create','as' => 'piggybanks.create']);
Route::get('/piggybanks/show/{piggybank}', ['uses' => 'PiggybankController@show','as' => 'piggybanks.show']);
Route::get('/piggybanks/edit/{piggybank}', ['uses' => 'PiggybankController@edit','as' => 'piggybanks.edit']);
Route::get('/piggybanks/delete/{piggybank}', ['uses' => 'PiggybankController@delete','as' => 'piggybanks.delete']);
Route::post('/piggybanks/updateAmount/{piggybank}',['uses' => 'PiggybankController@updateAmount','as' => 'piggybanks.updateAmount']);
@ -165,9 +166,6 @@ Route::group(['before' => 'auth'], function () {
// user controller
Route::get('/logout', ['uses' => 'UserController@logout', 'as' => 'logout']);
// migration controller
Route::get('/migrate', ['uses' => 'MigrationController@index', 'as' => 'migrate']);
}
);
@ -215,17 +213,11 @@ Route::group(['before' => 'csrf|auth'], function () {
Route::post('/transaction/update/{tj}',['uses' => 'TransactionController@update','as' => 'transactions.update']);
Route::post('/transaction/destroy/{tj}',['uses' => 'TransactionController@destroy','as' => 'transactions.destroy']);
// migration controller
Route::post('/migrate', ['uses' => 'MigrationController@postIndex']);
}
);
// guest routes:
Route::group(['before' => 'guest'], function () {
// dev import route:
Route::get('/dev',['uses' => 'MigrationController@dev']);
// user controller
Route::get('/login', ['uses' => 'UserController@login', 'as' => 'login']);
Route::get('/register', ['uses' => 'UserController@register', 'as' => 'register']);

View File

@ -0,0 +1,183 @@
<?php
use Mockery as m;
use Zizaco\FactoryMuff\Facade\FactoryMuff as f;
/**
* Class PiggybankControllerTest
*/
class PiggybankControllerTest extends TestCase
{
protected $_accounts;
protected $_piggybanks;
protected $_user;
public function setUp()
{
parent::setUp();
Artisan::call('migrate');
Artisan::call('db:seed');
$this->_user = m::mock('User', 'Eloquent');
$this->_accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
$this->_piggybanks = $this->mock('Firefly\Storage\Piggybank\PiggybankRepositoryInterface');
}
public function tearDown()
{
m::close();
}
public function testCreate()
{
$this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->once()->andReturn([]);
$this->action('GET', 'PiggybankController@create');
$this->assertResponseOk();
}
public function testDelete()
{
$piggyBank = f::create('Piggybank');
// for binding
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn(
$piggyBank->account()->first()->user_id
);
$this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email');
$this->action('GET', 'PiggybankController@delete', $piggyBank->id);
$this->assertResponseOk();
}
public function testDestroy()
{
$piggyBank = f::create('Piggybank');
// for binding
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn(
$piggyBank->account()->first()->user_id
);
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
$this->action('POST', 'PiggybankController@destroy', $piggyBank->id);
$this->assertResponseStatus(302);
}
public function testEdit()
{
$piggyBank = f::create('Piggybank');
$this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->once()->andReturn([]);
// for binding
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn(
$piggyBank->account()->first()->user_id
);
$this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email');
$this->action('GET', 'PiggybankController@edit', $piggyBank->id);
$this->assertResponseOk();
}
public function testIndex()
{
$aOne = f::create('Account');
$aTwo = f::create('Account');
$one = f::create('Piggybank');
$one->account()->associate($aOne);
$two = f::create('Piggybank');
$two->account()->associate($aOne);
$three = f::create('Piggybank');
$three->account()->associate($aTwo);
$this->_piggybanks->shouldReceive('get')->andReturn([$one,$two,$three]);
$this->_piggybanks->shouldReceive('count')->andReturn(1);
$this->action('GET', 'PiggybankController@index');
$this->assertResponseOk();
}
public function testShow()
{
$piggyBank = f::create('Piggybank');
// for binding
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn(
$piggyBank->account()->first()->user_id
);
$this->_user->shouldReceive('getAttribute')->andReturn('some@email');
$this->action('GET', 'PiggybankController@show', $piggyBank->id);
$this->assertResponseOk();
}
public function testStore()
{
$piggyBank = f::create('Piggybank');
$this->_piggybanks->shouldReceive('store')->andReturn($piggyBank);
$this->action('POST', 'PiggybankController@store');
$this->assertResponseStatus(302);
}
public function testStoreFails()
{
$piggyBank = f::create('Piggybank');
unset($piggyBank->id);
$this->_piggybanks->shouldReceive('store')->andReturn($piggyBank);
$this->action('POST', 'PiggybankController@store');
$this->assertResponseStatus(302);
}
public function testStoreRedirect()
{
$piggyBank = f::create('Piggybank');
$this->_piggybanks->shouldReceive('store')->andReturn($piggyBank);
$this->action('POST', 'PiggybankController@store',['create' => '1']);
$this->assertResponseStatus(302);
}
public function testUpdate()
{
$piggyBank = f::create('Piggybank');
$this->_piggybanks->shouldReceive('update')->andReturn($piggyBank);
// for binding
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->andReturn(
$piggyBank->account()->first()->user_id
);
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
$this->action('POST', 'PiggybankController@update', $piggyBank->id);
$this->assertResponseStatus(302);
}
public function testUpdateAmount()
{
$piggyBank = f::create('Piggybank');
$this->_piggybanks->shouldReceive('updateAmount')->andReturn($piggyBank);
// for binding
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->andReturn(
$piggyBank->account()->first()->user_id
);
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
$this->action('POST', 'PiggybankController@updateAmount', $piggyBank->id);
$this->assertResponseOk();
}
}