Covered currency controller in tests.

This commit is contained in:
James Cole 2014-12-24 14:02:21 +01:00
parent cb670bb27d
commit eae96cd2af
7 changed files with 224 additions and 25 deletions

View File

@ -2,6 +2,10 @@
use FireflyIII\Database\TransactionCurrency\TransactionCurrency as Repository;
/**
*
* @SuppressWarnings("CamelCase") // I'm fine with this.
* @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
*
* Class CurrencyController
*/
class CurrencyController extends BaseController
@ -46,6 +50,8 @@ class CurrencyController extends BaseController
$currencyPreference = $preferences->get('currencyPreference', 'EUR');
$currencyPreference->data = $currency->code;
$currencyPreference->save();
Session::flash('success', $currency->name.' is now the default currency.');
Cache::forget('FFCURRENCYSYMBOL');
Cache::forget('FFCURRENCYCODE');
@ -139,7 +145,7 @@ class CurrencyController extends BaseController
public function update(TransactionCurrency $currency)
{
$data = Input::except('_token');
$data = Input::except('_token');
// always validate:
$messages = $this->_repository->validate($data);

View File

@ -10,9 +10,8 @@ class TransactionCurrencySeeder extends Seeder
{
DB::table('transaction_currencies')->delete();
TransactionCurrency::create(
['code' => 'EUR']
);
TransactionCurrency::create(['code' => 'EUR','name' => 'Euro','symbol' => '€']);
TransactionCurrency::create(['code' => 'USD','name' => 'US Dollar','symbol' => '$']);
}
}

View File

@ -35,7 +35,9 @@ class TransactionCurrency implements TransactionCurrencyInterface, CommonDatabas
public function store(array $data)
{
$currency = new \TransactionCurrency($data);
\Log::debug('Is valid? ' . boolstr($currency->isValid()));
$currency->save();
return $currency;
}
@ -48,8 +50,9 @@ class TransactionCurrency implements TransactionCurrencyInterface, CommonDatabas
public function update(Eloquent $model, array $data)
{
$model->symbol = $data['symbol'];
$model->code = $data['code'];
$model->name = $data['name'];
$model->code = $data['code'];
$model->name = $data['name'];
\Log::debug('Is valid? ' . boolstr($model->isValid()));
$model->save();
return true;
@ -67,11 +70,16 @@ class TransactionCurrency implements TransactionCurrencyInterface, CommonDatabas
{
$warnings = new MessageBag;
$successes = new MessageBag;
\Log::debug('Now in TransactionCurrency::validate()');
$currency = new \TransactionCurrency($model);
$currency->isValid();
$errors = $currency->getErrors();
\Log::debug('Data: ' . json_encode($model));
\Log::debug('Error-content: ' . json_encode($errors->all()));
\Log::debug('Error count is: ' . $errors->count());
$fields = ['name', 'code', 'symbol'];
foreach ($fields as $field) {
if (!$errors->has($field)) {

View File

@ -12,20 +12,10 @@ class TransactionCurrency extends Eloquent
use SoftDeletingTrait, ValidatingTrait;
protected $fillable = ['name', 'symbol', 'code'];
protected $rules
= [
'creating' => [
'code' => 'required|min:3|max:3|alpha|unique:transaction_currencies,code',
'name' => 'required|min:3|max:48|unique:transaction_currencies,name',
'symbol' => 'required|min:1|max:8|unique:transaction_currencies,symbol',
],
'updating' => [
'code' => 'required|min:3|max:3|alpha',
'name' => 'required|min:3|max:48',
'symbol' => 'required|min:1|max:8',
],
protected $rules = [
'code' => 'required|alpha|between:3,3|min:3|max:3',
'name' => 'required|between:3,48|min:3|max:48',
'symbol' => 'required|between:1,8|min:1|max:8',
];
/**

View File

@ -1,7 +1,7 @@
@extends('layouts.default')
@section('content')
{{ Breadcrumbs::renderIfExists(Route::getCurrentRoute()->getName()) }}
{{Form::model($currency, ['class' => 'form-horizontal','id' => 'store','url' => route('currency.update',$currency->id)])}}
{{Form::model($currency, ['class' => 'form-horizontal','id' => 'update','url' => route('currency.update',$currency->id)])}}
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12">
<div class="panel panel-primary">

View File

@ -89,6 +89,7 @@ class BudgetControllerCest
{
$I->wantTo('show all budgets');
$I->amOnPage('/budgets');
$I->see('Budgets');
}
/**
@ -147,7 +148,6 @@ class BudgetControllerCest
$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();
}
/**
@ -160,7 +160,6 @@ class BudgetControllerCest
$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();
}
/**
@ -171,10 +170,11 @@ class BudgetControllerCest
$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->submitForm('#store', ['name' => null, 'post_submit_action' => 'store']);
$I->dontSeeRecord('budgets', ['name' => 'New budget.']);
resetToClean::clean();
}
/**
* @param FunctionalTester $I
*/

View File

@ -0,0 +1,196 @@
<?php
/**
* Class CurrencyControllerCest
*/
class CurrencyControllerCest
{
/**
* @param FunctionalTester $I
*/
public function _after(FunctionalTester $I)
{
}
/**
* @param FunctionalTester $I
*/
public function _before(FunctionalTester $I)
{
$I->amLoggedAs(['email' => 'thegrumpydictator@gmail.com', 'password' => 'james']);
}
/**
* @param FunctionalTester $I
*/
public function create(FunctionalTester $I)
{
$I->wantTo('create a currency');
$I->amOnRoute('currency.create');
$I->see('Create a new currency');
}
/**
* @param FunctionalTester $I
*/
public function delete(FunctionalTester $I)
{
$I->wantTo('delete a currency');
$I->amOnPage('/currency/delete/2');
$I->see('Delete currency "US Dollar"');
}
/**
* @param FunctionalTester $I
*/
public function destroy(FunctionalTester $I)
{
$I->wantTo('destroy a currency');
$I->amOnPage('/currency/delete/2');
$I->see('Delete currency "US Dollar"');
$I->submitForm('#destroy', []);
$I->see('Currency &quot;US Dollar&quot; deleted');
}
public function defaultCurrency(FunctionalTester $I)
{
$I->wantTo('make US Dollar the default currency');
$I->amOnPage('/currency/default/2');
$I->see('US Dollar is now the default currency.');
}
/**
* @param FunctionalTester $I
*/
public function destroyFail(FunctionalTester $I)
{
$I->wantTo('destroy a currency currently in use');
$I->amOnPage('/currency/delete/1');
$I->see('Cannot delete Euro because there are still transactions attached to it.');
}
/**
* @param FunctionalTester $I
*/
public function edit(FunctionalTester $I)
{
$I->wantTo('edit a currency');
$I->amOnPage('/currency/edit/2');
$I->see('Edit currency "US Dollar"');
}
/**
* @param FunctionalTester $I
*/
public function index(FunctionalTester $I)
{
$I->wantTo('show all currencies');
$I->amOnPage('/currency');
$I->see('fa-usd');
}
/**
* @param FunctionalTester $I
*/
public function store(FunctionalTester $I)
{
$I->amOnPage('/currency/create');
$I->wantTo('store a new currency');
$I->see('Create a new currency');
$I->submitForm('#store', ['name' => 'New currency.', 'symbol' => 'C', 'code' => 'CXX', 'post_submit_action' => 'store']);
$I->seeRecord('transaction_currencies', ['name' => 'New currency.']);
}
/**
* @param FunctionalTester $I
*/
public function storeAndCreateAnother(FunctionalTester $I)
{
$I->amOnPage('/currency/create');
$I->wantTo('store a new currency and create another');
$I->see('Create a new currency');
$I->submitForm('#store', ['name' => 'Store and create another.', 'symbol' => 'C', 'code' => 'CXX', 'post_submit_action' => 'create_another']);
$I->seeRecord('transaction_currencies', ['name' => 'Store and create another.']);
}
/**
* @param FunctionalTester $I
*/
public function storeFail(FunctionalTester $I)
{
$I->amOnPage('/currency/create');
$I->wantTo('make storing a new currency fail.');
$I->see('Create a new currency');
$I->submitForm('#store', ['name' => 'Store and fail', 'symbol' => null, 'code' => '123', 'post_submit_action' => 'store']);
$I->dontSeeRecord('transaction_currencies', ['name' => 'Store and fail']);
}
/**
* @param FunctionalTester $I
*/
public function storeValidateOnly(FunctionalTester $I)
{
$I->amOnPage('/currency/create');
$I->wantTo('validate a new currency');
$I->see('Create a new currency');
$I->submitForm('#store', ['name' => 'Store validate only.', 'symbol' => 'C', 'code' => 'CXX', 'post_submit_action' => 'validate_only']);
$I->dontSeeRecord('transaction_currencies', ['name' => 'Store validate only.']);
}
/**
* @param FunctionalTester $I
*/
public function update(FunctionalTester $I)
{
$I->wantTo('update a currency');
$I->amOnPage('/currency/edit/2');
$I->see('Edit currency "US Dollar"');
$I->submitForm('#update', ['name' => 'Successful update', 'symbol' => '$', 'code' => 'USD', 'post_submit_action' => 'update']);
$I->seeRecord('transaction_currencies', ['name' => 'Successful update']);
resetToClean::clean();
}
/**
* @param FunctionalTester $I
*/
public function failUpdate(FunctionalTester $I)
{
$I->wantTo('update a currency and fail');
$I->amOnPage('/currency/edit/2');
$I->see('Edit currency "US Dollar"');
$I->submitForm('#update', ['name' => 'Failed update', 'code' => '123', 'post_submit_action' => 'update']);
$I->dontSeeRecord('transaction_currencies', ['name' => 'Failed update']);
}
/**
* @param FunctionalTester $I
*/
public function validateUpdateOnly(FunctionalTester $I)
{
$I->wantTo('update a currency and validate only');
$I->amOnPage('/currency/edit/2');
$I->see('Edit currency "US Dollar"');
$I->submitForm('#update', ['name' => 'Update Validate Only', 'post_submit_action' => 'validate_only']);
$I->dontSeeRecord('transaction_currencies', ['name' => 'Update Validate Only']);
$I->seeRecord('transaction_currencies', ['name' => 'US Dollar']);
}
/**
* @param FunctionalTester $I
*/
public function updateAndReturn(FunctionalTester $I)
{
$I->wantTo('update a currency and return to form');
$I->amOnPage('/currency/edit/2');
$I->see('Edit currency "US Dollar"');
$I->submitForm(
'#update', ['name' => 'US DollarXXX', 'symbol' => '$', 'code' => 'USD', 'post_submit_action' => 'return_to_edit']
);
$I->seeRecord('transaction_currencies', ['name' => 'US DollarXXX']);
}
}