More tests!

This commit is contained in:
James Cole 2014-12-20 16:06:25 +01:00
parent 40709c8367
commit 1484621300
6 changed files with 115 additions and 21 deletions

2
.gitignore vendored
View File

@ -21,3 +21,5 @@ tests/acceptance/AcceptanceTester.php
tests/functional/FunctionalTester.php
tests/unit/UnitTester.php
pi.php
tests/_data/db.sqlite
tests/_data/dump.sql

View File

@ -146,7 +146,7 @@ class BudgetController extends BaseController
public function show(Budget $budget, LimitRepetition $repetition = null)
{
if (!is_null($repetition) && $repetition->budgetLimit->budget->id != $budget->id) {
return View::make('error')->with('message','Invalid selection.');
return View::make('error')->with('message', 'Invalid selection.');
}
$hideBudget = true; // used in transaction list.
@ -162,7 +162,8 @@ class BudgetController extends BaseController
*/
public function store()
{
$data = Input::except('_token');
$data = Input::except('_token');
$data['user_id'] = Auth::user()->id;
// always validate:
$messages = $this->_repository->validate($data);
@ -188,11 +189,7 @@ class BudgetController extends BaseController
}
// create another.
if ($data['post_submit_action'] == 'create_another') {
return Redirect::route('budgets.create')->withInput();
}
return Redirect::route('budgets.index');
return Redirect::route('budgets.create')->withInput();
}
@ -204,7 +201,8 @@ class BudgetController extends BaseController
public function update(Budget $budget)
{
$data = Input::except('_token');
$data = Input::except('_token');
$data['user_id'] = Auth::user()->id;
// always validate:
$messages = $this->_repository->validate($data);
@ -230,12 +228,8 @@ class BudgetController extends BaseController
if ($data['post_submit_action'] == 'update') {
return Redirect::route('budgets.index');
}
// go back to update screen.
if ($data['post_submit_action'] == 'return_to_edit') {
return Redirect::route('budgets.edit', $budget->id)->withInput(['post_submit_action' => 'return_to_edit']);
}
return Redirect::route('budgets.index');
return Redirect::route('budgets.edit', $budget->id)->withInput(['post_submit_action' => 'return_to_edit']);
}
/**

View File

@ -48,10 +48,7 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
*/
public function store(array $data)
{
$data['user_id'] = $this->getUser()->id;
$budget = new \Budget($data);
$budget->class = 'Budget';
if (!$budget->isValid()) {
\Log::error('Could not store budget: ' . $budget->getErrors()->toJson());
@ -88,8 +85,9 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
{
$warnings = new MessageBag;
$successes = new MessageBag;
$validator = \Validator::make($model, \Component::$rules);
$errors = $validator->errors();
$budget = new \Budget($model);
$budget->isValid();
$errors = $budget->getErrors();
if (!$errors->has('name')) {
$successes->add('name', 'OK');

View File

@ -2,13 +2,18 @@
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use \Illuminate\Database\Eloquent\Model as Eloquent;
use \Watson\Validating\ValidatingTrait;
/**
* Class Budget
*/
class Budget extends Eloquent
{
use SoftDeletingTrait;
use SoftDeletingTrait, ValidatingTrait;
protected $fillable = ['name', 'user_id'];
protected $rules = [
'user_id' => 'exists:users,id|required',
'name' => 'required|between:1,100|alphabasic',
];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough

View File

@ -109,7 +109,7 @@ class AccountControllerCest
public function storeValidateOnly(FunctionalTester $I)
{
$I->amOnPage('/accounts/create/asset');
$I->wantTo('store a new asset account');
$I->wantTo('validate a new asset account');
$I->see('Create a new asset account');
$I->submitForm(
'#store', ['name' => 'New through tests.', 'what' => 'asset', 'account_role' => 'defaultExpense', 'post_submit_action' => 'validate_only']

View File

@ -129,15 +129,108 @@ class BudgetControllerCest
*/
public function store(FunctionalTester $I)
{
$I->wantTo('store a budget');
$I->amOnPage('/budgets/create');
$I->wantTo('store a new budget');
$I->see('Create a new budget');
$I->submitForm('#store', ['name' => 'New budget.', 'post_submit_action' => 'store']);
$I->seeRecord('budgets', ['name' => 'New budget.']);
resetToClean::clean();
}
/**
* @param FunctionalTester $I
*/
public function storeValidateOnly(FunctionalTester $I)
{
$I->amOnPage('/budgets/create');
$I->wantTo('validate a new budget');
$I->see('Create a new budget');
$I->submitForm('#store', ['name' => 'New budget.', 'post_submit_action' => 'validate_only']);
$I->dontSeeRecord('budgets', ['name' => 'New budget.']);
resetToClean::clean();
}
/**
* @param FunctionalTester $I
*/
public function storeAndCreateAnother(FunctionalTester $I)
{
$I->amOnPage('/budgets/create');
$I->wantTo('store a new budget and create another');
$I->see('Create a new budget');
$I->submitForm('#store', ['name' => 'New budget.', 'post_submit_action' => 'create_another']);
$I->seeRecord('budgets', ['name' => 'New budget.']);
resetToClean::clean();
}
/**
* @param FunctionalTester $I
*/
public function storeFail(FunctionalTester $I)
{
$I->amOnPage('/budgets/create');
$I->wantTo('make storing a new budget fail.');
$I->see('Create a new budget');
$I->submitForm('#store', ['name' => null, 'post_submit_action' => 'validate_only']);
$I->dontSeeRecord('budgets', ['name' => 'New budget.']);
resetToClean::clean();
}
/**
* @param FunctionalTester $I
*/
public function update(FunctionalTester $I)
{
$I->wantTo('update a budget');
$I->amOnPage('/budgets/edit/3');
$I->see('Edit budget "Delete me"');
$I->submitForm('#update', ['name' => 'Update me', 'post_submit_action' => 'update']);
$I->seeRecord('budgets', ['name' => 'Update me']);
resetToClean::clean();
}
/**
* @param FunctionalTester $I
*/
public function failUpdate(FunctionalTester $I)
{
$I->wantTo('update a budget and fail');
$I->amOnPage('/budgets/edit/3');
$I->see('Edit budget "Delete me"');
$I->submitForm('#update', ['name' => '', 'post_submit_action' => 'update']);
$I->seeRecord('budgets', ['name' => 'Delete me']);
}
/**
* @param FunctionalTester $I
*/
public function validateUpdateOnly(FunctionalTester $I)
{
$I->wantTo('update a budget and validate only');
$I->amOnPage('/budgets/edit/3');
$I->see('Edit budget "Delete me"');
$I->submitForm(
'#update', ['name' => 'Validate Only', 'post_submit_action' => 'validate_only']
);
$I->dontSeeRecord('budgets', ['name' => 'Savings accountXX']);
$I->seeRecord('budgets', ['name' => 'Delete me']);
}
/**
* @param FunctionalTester $I
*/
public function updateAndReturn(FunctionalTester $I)
{
$I->wantTo('update a budget and return to form');
$I->amOnPage('/budgets/edit/3');
$I->see('Edit budget "Delete me"');
$I->submitForm(
'#update', ['name' => 'Savings accountXX', 'post_submit_action' => 'return_to_edit']
);
$I->seeRecord('budgets', ['name' => 'Savings accountXX']);
}
/**
@ -145,6 +238,8 @@ class BudgetControllerCest
*/
public function updateIncome(FunctionalTester $I)
{
$I->amOnPage('/budgets/income');
$I->wantTo('update my monthly income');
$I->see('Update (expected) income for ');
}
}