Removed my own validation source in favour of Ardent.

This commit is contained in:
James Cole 2014-07-15 17:09:59 +02:00
parent a1fb4bdb60
commit aa36e8b7cf
17 changed files with 212 additions and 163 deletions

View File

@ -36,7 +36,6 @@ return [
'Illuminate\Validation\ValidationServiceProvider',
'Illuminate\View\ViewServiceProvider',
'Illuminate\Workbench\WorkbenchServiceProvider',
'Grumpydictator\Gchart\GchartServiceProvider',
'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider',
'Barryvdh\Debugbar\ServiceProvider',
'Firefly\Storage\StorageServiceProvider',

View File

@ -19,11 +19,16 @@ class ChartController extends BaseController
/**
* Show home charts.
*/
public function homeAccount($account = null)
public function homeAccount($id = null)
{
list($start, $end) = tk::getDateRange();
$current = clone $start;
$return = [];
$account = null;
if(!is_null($id)) {
$account = $this->accounts->find($id);
}
if (is_null($account)) {
$accounts = $this->accounts->getActiveDefault();
@ -40,14 +45,8 @@ class ChartController extends BaseController
$current->addDay();
}
} else {
// do something experimental:
$account = $this->accounts->find($account);
if (is_null($account)) {
return View::make('error')->with('message', 'No account found.');
}
$return[0] = ['name' => $account->name, 'data' => []];
while ($current <= $end) {
$return[0]['data'][] = [$current->timestamp * 1000, $account->balance(clone $current)];

View File

@ -61,15 +61,17 @@ class TransactionController extends BaseController
// create journal
/** @var \TransactionJournal $journal */
$journal = $this->tj->createSimpleJournal($account,$beneficiary,$description,$amount,$date);
var_dump($journal);
$journal = $this->tj->createSimpleJournal($account, $beneficiary, $description, $amount, $date);
// attach bud/cat (?)
$journal->budgets()->save($budget);
$journal->categories()->save($category);
if (!is_null($budget)) {
$journal->budgets()->save($budget);
}
if (!is_null($category)) {
$journal->categories()->save($category);
}
Session::flash('success','Transaction saved');
Session::flash('success', 'Transaction saved');
return Redirect::route('index');
}

View File

@ -1,9 +1,11 @@
<?php
namespace Firefly\Database;
abstract class SingleTableInheritanceEntity extends \Elegant
abstract class SingleTableInheritanceEntity extends \LaravelBook\Ardent\Ardent
{
/**
* The field that stores the subclass
@ -13,6 +15,7 @@ abstract class SingleTableInheritanceEntity extends \Elegant
protected $subclassField = null;
/**
* must be overridden and set to true in subclasses
*
* @var bool
*/
protected $isSubclass = false;
@ -67,12 +70,16 @@ abstract class SingleTableInheritanceEntity extends \Elegant
}
// ensure that the subclass field is assigned on save
public function save(array $options = array())
{
public function save(
array $rules = array(),
array $customMessages = array(),
array $options = array(),
\Closure $beforeSave = null,
\Closure $afterSave = null
) {
if ($this->subclassField) {
$this->attributes[$this->subclassField] = get_class($this);
}
return parent::save($options);
return parent::save($rules, $customMessages, $options, $beforeSave, $afterSave);
}
}

View File

@ -91,9 +91,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$journal->completed = false;
$journal->description = $description;
$journal->date = $date;
if (!$journal->isValid()) {
if (!$journal->save()) {
\Log::error('Cannot create valid journal.');
\Log::error('Errors: ' . print_r($journal->validator->messages()->all(), true));
\Log::error('Errors: ' . print_r($journal->errors()->all(), true));
throw new \Firefly\Exception\FireflyException('Cannot create valid journal.');
}
$journal->save();
@ -104,9 +104,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$fromTransaction->transactionJournal()->associate($journal);
$fromTransaction->description = null;
$fromTransaction->amount = $amountFrom;
if (!$fromTransaction->isValid()) {
if (!$fromTransaction->save()) {
\Log::error('Cannot create valid transaction (from) for journal #' . $journal->id);
\Log::error('Errors: ' . print_r($fromTransaction->validator->messages()->all(), true));
\Log::error('Errors: ' . print_r($fromTransaction->errors()->all(), true));
throw new \Firefly\Exception\FireflyException('Cannot create valid transaction (from).');
}
$fromTransaction->save();
@ -116,12 +116,10 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$toTransaction->transactionJournal()->associate($journal);
$toTransaction->description = null;
$toTransaction->amount = $amountTo;
if (!$toTransaction->isValid()) {
if (!$toTransaction->isValid()) {
\Log::error('Cannot create valid transaction (to) for journal #' . $journal->id);
\Log::error('Errors: ' . print_r($toTransaction->validator->messages()->all(), true));
throw new \Firefly\Exception\FireflyException('Cannot create valid transaction (to).');
}
if (!$toTransaction->save()) {
\Log::error('Cannot create valid transaction (to) for journal #' . $journal->id);
\Log::error('Errors: ' . print_r($toTransaction->errors()->all(), true));
throw new \Firefly\Exception\FireflyException('Cannot create valid transaction (to).');
}
$toTransaction->save();
@ -222,7 +220,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$name = $t->account->name;
$amount = floatval($t->amount) < 0 ? floatval($t->amount) * -1 : floatval($t->amount);
$result[$name] = isset($result[$name]) ? $result[$name]+$amount : $amount;
$result[$name] = isset($result[$name]) ? $result[$name] + $amount : $amount;
}
}
}

View File

@ -17,9 +17,9 @@ class EloquentUserRepository implements UserRepositoryInterface
$user->reset = \Str::random(32);
$user->password = \Hash::make(\Str::random(12));
if (!$user->isValid()) {
if (!$user->save()) {
\Log::error('Invalid user');
\Session::flash('error', 'Input invalid, please try again: ' . $user->validator->messages()->first());
\Session::flash('error', 'Input invalid, please try again: ' . $user->errors()->first());
return false;
}
$user->save();

View File

@ -1,9 +1,14 @@
<?php
use LaravelBook\Ardent\Ardent as Ardent;
class Account extends Elegant
class Account extends Ardent
{
/**
* Validation rules.
*
* @var array
*/
public static $rules
= [
'name' => 'required|between:1,100',
@ -13,6 +18,11 @@ class Account extends Elegant
];
/**
* Factory instructions
*
* @var array
*/
public static $factory
= [
'name' => 'string',
@ -21,11 +31,21 @@ class Account extends Elegant
'active' => '1'
];
/**
* Account type.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function accountType()
{
return $this->belongsTo('AccountType');
}
/**
* User
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('User');
@ -42,11 +62,20 @@ class Account extends Elegant
{
$date = is_null($date) ? new \Carbon\Carbon : $date;
return floatval($this->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->sum('transactions.amount'));
return floatval(
$this->transactions()
->leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
)
->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->sum('transactions.amount')
);
}
/**
* Transactions.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function transactions()
{
return $this->hasMany('Transaction');

View File

@ -9,7 +9,6 @@ class Component extends Firefly\Database\SingleTableInheritanceEntity
'user_id' => 'exists:users,id|required',
'name' => 'required|between:1,255',
'class' => 'required',
'component_type_id' => 'required|exists:component_types,id'
];
protected $table = 'components';
protected $subclassField = 'class';

View File

@ -1,18 +0,0 @@
<?php
use \Illuminate\Database\Eloquent\Model as Eloquent;
class Elegant extends Eloquent
{
public static $rules = [];
public $validator;
public function isValid()
{
$validator = Validator::make(
$this->toArray(),
$this::$rules
);
$this->validator = $validator;
return $validator->passes();
}
}

View File

@ -1,19 +1,23 @@
<?php
class Preference extends Elegant
use LaravelBook\Ardent\Ardent;
class Preference extends Ardent
{
public static $rules
= [
'user_id' => 'required|exists:user,id',
'user_id' => 'required|exists:users,id',
'name' => 'required|between:1,255',
'data' => 'required'
];
public static $factory = [
'user_id' => 'factory|User',
'name' => 'string',
'data' => 'string'
];
public static $factory
= [
'user_id' => 'factory|User',
'name' => 'string',
'data' => 'string'
];
public function user()
{

View File

@ -1,14 +1,16 @@
<?php
use LaravelBook\Ardent\Ardent;
class Transaction extends Elegant
class Transaction extends Ardent
{
public static $rules
= [
'account_id' => 'numeric|required|exists:accounts,id',
'transaction_journal_id' => 'numeric|required|exists:transaction_journals,id',
'description' => 'between:1,255',
'amount' => 'required|between:-65536,65536',
'amount' => 'required|between:-65536,65536|not_in:0,0.00',
];
public static $factory

View File

@ -1,15 +1,17 @@
<?php
use LaravelBook\Ardent\Ardent;
class TransactionJournal extends Elegant
class TransactionJournal extends Ardent
{
public static $rules
= [
'transaction_type_id' => 'required|exists:transaction_types,id',
'transaction_currency_id' => 'required|exists:transaction_currencies,id',
'description' => 'between:1,255',
'date' => 'date',
'description' => 'required|between:1,255',
'date' => 'required|date',
'completed' => 'required|between:0,1'
];

View File

@ -4,8 +4,10 @@ use Illuminate\Auth\Reminders\RemindableInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\UserTrait;
use LaravelBook\Ardent\Ardent;
class User extends Elegant implements UserInterface, RemindableInterface
class User extends Ardent implements UserInterface, RemindableInterface
{
use UserTrait, RemindableTrait;
@ -22,7 +24,7 @@ class User extends Elegant implements UserInterface, RemindableInterface
public static $factory
= [
'email' => 'email',
'password' => 'string',
'password' => 'string|60',
'migrated' => '0'
];

View File

@ -1,5 +1,7 @@
<?php
use \League\FactoryMuffin\Facade\FactoryMuffin;
class ChartControllerTest extends TestCase
{
public function setUp()
@ -51,21 +53,7 @@ class ChartControllerTest extends TestCase
public function testHomeAccountWithInput()
{
// save actual account:
$type = new AccountType;
$type->description = 'An account';
$type->save();
$user = new User;
$user->email = 'bla';
$user->migrated = false;
$user->password = 'bla';
$user->save();
$account = new Account;
$account->accountType()->associate($type);
$account->user()->associate($user);
$account->name = 'Hello';
$account->active = true;
$account->save();
$account = FactoryMuffin::create('Account');
// mock preference:
$pref = $this->mock('Preference');
@ -83,54 +71,12 @@ class ChartControllerTest extends TestCase
$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();
}
public function testHomeAccountWithInvalidInput()
{
// save actual account:
$type = new AccountType;
$type->description = 'An account';
$type->save();
$user = new User;
$user->email = 'bla';
$user->migrated = false;
$user->password = 'bla';
$user->save();
$account = new Account;
$account->accountType()->associate($type);
$account->user()->associate($user);
$account->name = 'Hello';
$account->active = true;
$account->save();
// mock preference:
$pref = $this->mock('Preference');
$pref->shouldReceive('getAttribute', 'data')->andReturn('1M');
// mock preferences helper:
$preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface');
$preferences->shouldReceive('get')->with('viewRange', '1M')->once()->andReturn($pref);
// mock toolkit:
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->shouldReceive('getDateRange')->andReturn(null);
// mock account repository:
$accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->shouldReceive('find')->with(1)->andReturn(null);
// call
$this->call('GET', '/chart/home/account/' . $account->id);
// test
$this->assertResponseOk();
$this->assertViewHas('message');
}
public function testhomeBudgets()

View File

@ -1,20 +1,49 @@
<?php
use \League\FactoryMuffin\Facade\FactoryMuffin;
class TransactionControllerTest extends TestCase
{
/**
* Default preparation for each test
*/
public function setUp()
{
parent::setUp();
$this->prepareForTests();
}
/**
* Migrate the database
*/
private function prepareForTests()
{
Artisan::call('migrate');
Artisan::call('db:seed');
}
public function testCreateWithdrawal()
{
$set = [0 => '(no budget)'];
View::shouldReceive('share');
View::shouldReceive('make')->with('transactions.withdrawal')->andReturn(\Mockery::self())
->shouldReceive('with')->once()
->with('accounts', [])
->andReturn(Mockery::self());
->andReturn(Mockery::self())
->shouldReceive('with')->once()
->with('budgets', $set)->andReturn(Mockery::self());
// mock account repository:
$accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]);
// mock budget repository:
$budgets = $this->mock('Firefly\Storage\Budget\BudgetRepositoryInterface');
$budgets->shouldReceive('getAsSelectList')->andReturn($set);
// call
$this->call('GET', '/transactions/add/withdrawal');
@ -22,4 +51,52 @@ class TransactionControllerTest extends TestCase
$this->assertResponseOk();
}
public function testPostCreateWithdrawal()
{
// create objects.
$account = FactoryMuffin::create('Account');
$beneficiary = FactoryMuffin::create('Account');
$category = FactoryMuffin::create('Category');
$budget = FactoryMuffin::create('Budget');
// data to send:
$data = [
'beneficiary' => $beneficiary->name,
'category' => $category->name,
'budget_id' => $budget->id,
'account_id' => $account->id,
'description' => 'Bla',
'amount' => 1.2,
'date' => '2012-01-01'
];
$journal = FactoryMuffin::create('TransactionJournal');
// mock account repository:
$accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->shouldReceive('createOrFindBeneficiary')->with($beneficiary->name)->andReturn($beneficiary);
$accounts->shouldReceive('find')->andReturn($account);
// mock category repository
$categories = $this->mock('Firefly\Storage\Category\CategoryRepositoryInterface');
$categories->shouldReceive('createOrFind')->with($category->name)->andReturn($category);
// mock budget repository
$budgets = $this->mock('Firefly\Storage\Budget\BudgetRepositoryInterface');
$budgets->shouldReceive('createOrFind')->with($budget->name)->andReturn($budget);
$budgets->shouldReceive('find')->andReturn($budget);
// mock transaction journal:
$tj = $this->mock('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$tj->shouldReceive('createSimpleJournal')->once()->andReturn($journal);
$tj->shouldReceive('createSimpleJournal')->with($account, $beneficiary, $data['description'], $data['amount'], new \Carbon\Carbon($data['date']))->once()->andReturn($journal);
// call
$this->call('POST', '/transactions/add/withdrawal', $data);
// test
$this->assertRedirectedToRoute('index');
}
}

View File

@ -1,6 +1,6 @@
<?php
use Zizaco\FactoryMuff\Facade\FactoryMuff;
use \League\FactoryMuffin\Facade\FactoryMuffinin;
class AllModelsTest extends TestCase
{
@ -29,14 +29,14 @@ class AllModelsTest extends TestCase
*/
public function testUser()
{
$user = FactoryMuff::create('User');
$pref = FactoryMuff::create('Preference');
$account = FactoryMuff::create('Account');
$user = FactoryMuffin::create('User');
$pref = FactoryMuffin::create('Preference');
$account = FactoryMuffin::create('Account');
// some more stuff:
$component = FactoryMuff::create('Component');
$budget = FactoryMuff::create('Budget');
$category = FactoryMuff::create('Category');
$component = FactoryMuffin::create('Component');
$budget = FactoryMuffin::create('Budget');
$category = FactoryMuffin::create('Category');
$account->user()->associate($user);
$pref->user()->associate($user);
@ -80,18 +80,18 @@ class AllModelsTest extends TestCase
public function testTransactionJournals()
{
$tj = FactoryMuff::create('TransactionJournal');
$tj = FactoryMuffin::create('TransactionJournal');
$t1 = FactoryMuff::create('Transaction');
$t2 = FactoryMuff::create('Transaction');
$t3 = FactoryMuff::create('Transaction');
$t1 = FactoryMuffin::create('Transaction');
$t2 = FactoryMuffin::create('Transaction');
$t3 = FactoryMuffin::create('Transaction');
$tj->transactions()->save($t1);
$tj->transactions()->save($t2);
$tj->transactions()->save($t3);
$budget = FactoryMuff::create('Budget');
$category = FactoryMuff::create('Category');
$budget = FactoryMuffin::create('Budget');
$category = FactoryMuffin::create('Category');
$tj->components()->save($budget);
$tj->components()->save($category);
@ -102,7 +102,7 @@ class AllModelsTest extends TestCase
$this->assertCount(3, $tj->transactions()->get());
$this->assertTrue($tj->isValid());
$this->assertTrue($tj->validate());
$this->assertEquals($tj->transaction_type_id, $tj->transactionType()->first()->id);
$this->assertEquals($tj->transaction_currency_id, $tj->transactionCurrency()->first()->id);
@ -111,7 +111,7 @@ class AllModelsTest extends TestCase
public function testTransactionJournalScope()
{
$tj = FactoryMuff::create('TransactionJournal');
$tj = FactoryMuffin::create('TransactionJournal');
$tj->date = new \Carbon\Carbon('2012-01-02');
$set = $tj->after(new \Carbon\Carbon)->before(new \Carbon\Carbon)->get();
@ -120,10 +120,10 @@ class AllModelsTest extends TestCase
public function testTransactionType()
{
$j1 = FactoryMuff::create('TransactionJournal');
$j2 = FactoryMuff::create('TransactionJournal');
$j1 = FactoryMuffin::create('TransactionJournal');
$j2 = FactoryMuffin::create('TransactionJournal');
$type = FactoryMuff::create('TransactionType');
$type = FactoryMuffin::create('TransactionType');
$type->transactionjournals()->save($j1);
$type->transactionjournals()->save($j2);
@ -133,10 +133,10 @@ class AllModelsTest extends TestCase
public function testTransactionCurrency()
{
$j1 = FactoryMuff::create('TransactionJournal');
$j2 = FactoryMuff::create('TransactionJournal');
$j1 = FactoryMuffin::create('TransactionJournal');
$j2 = FactoryMuffin::create('TransactionJournal');
$currency = FactoryMuff::create('TransactionCurrency');
$currency = FactoryMuffin::create('TransactionCurrency');
$currency->transactionjournals()->save($j1);
$currency->transactionjournals()->save($j2);
@ -146,9 +146,9 @@ class AllModelsTest extends TestCase
public function testAccountTypes()
{
$type = FactoryMuff::create('AccountType');
$a1 = FactoryMuff::create('Account');
$a2 = FactoryMuff::create('Account');
$type = FactoryMuffin::create('AccountType');
$a1 = FactoryMuffin::create('Account');
$a2 = FactoryMuffin::create('Account');
$type->accounts()->save($a1);
$type->accounts()->save($a2);
@ -158,12 +158,12 @@ class AllModelsTest extends TestCase
public function testTransactions()
{
$transaction = FactoryMuff::create('Transaction');
$transaction = FactoryMuffin::create('Transaction');
$budget = FactoryMuff::create('Budget');
$account = FactoryMuff::create('Account');
$category = FactoryMuff::create('Category');
$journal = FactoryMuff::create('TransactionJournal');
$budget = FactoryMuffin::create('Budget');
$account = FactoryMuffin::create('Account');
$category = FactoryMuffin::create('Category');
$journal = FactoryMuffin::create('TransactionJournal');
$transaction->components()->save($budget);
$transaction->components()->save($category);
@ -180,11 +180,11 @@ class AllModelsTest extends TestCase
public function testComponents()
{
$component = FactoryMuff::create('Component');
$user = FactoryMuff::create('User');
$transaction = FactoryMuff::create('Transaction');
$component = FactoryMuffin::create('Component');
$user = FactoryMuffin::create('User');
$transaction = FactoryMuffin::create('Transaction');
$journal = FactoryMuff::create('TransactionJournal');
$journal = FactoryMuffin::create('TransactionJournal');
$component->transactionjournals()->save($journal);
$component->user()->associate($user);
$component->transactions()->save($transaction);

View File

@ -22,14 +22,15 @@
},
"require": {
"laravel/framework": "4.2.*",
"grumpydictator/gchart": "dev-master"
"laravelbook/ardent": "dev-master"
},
"require-dev": {
"barryvdh/laravel-debugbar": "1.*",
"barryvdh/laravel-ide-helper": "1.*",
"mockery/mockery": "dev-master",
"satooshi/php-coveralls": "dev-master",
"zizaco/factory-muff": "dev-master"
"zizaco/factory-muff": "dev-master",
"doctrine/dbal": "2.4.*"
},
"autoload": {
"classmap": [