From aa36e8b7cfce29b7bd0dc72a488aa69943bf2980 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 15 Jul 2014 17:09:59 +0200 Subject: [PATCH] Removed my own validation source in favour of Ardent. --- app/config/app.php | 1 - app/controllers/ChartController.php | 13 ++- app/controllers/TransactionController.php | 14 ++-- .../Database/SingleTableInheritanceEntity.php | 17 ++-- .../EloquentTransactionJournalRepository.php | 20 +++-- .../Storage/User/EloquentUserRepository.php | 4 +- app/models/Account.php | 39 +++++++-- app/models/Component.php | 1 - app/models/Elegant.php | 18 ----- app/models/Preference.php | 18 +++-- app/models/Transaction.php | 6 +- app/models/TransactionJournal.php | 8 +- app/models/User.php | 6 +- app/tests/controllers/ChartControllerTest.php | 60 +------------- .../controllers/TransactionControllerTest.php | 79 ++++++++++++++++++- app/tests/models/AllModelsTest.php | 66 ++++++++-------- composer.json | 5 +- 17 files changed, 212 insertions(+), 163 deletions(-) delete mode 100644 app/models/Elegant.php diff --git a/app/config/app.php b/app/config/app.php index f03bcefd6b..d0b412010f 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -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', diff --git a/app/controllers/ChartController.php b/app/controllers/ChartController.php index 403f0f0b5f..369fb7c79c 100644 --- a/app/controllers/ChartController.php +++ b/app/controllers/ChartController.php @@ -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)]; diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.php index 1647ca25f8..7da2b3e37d 100644 --- a/app/controllers/TransactionController.php +++ b/app/controllers/TransactionController.php @@ -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'); } diff --git a/app/lib/Firefly/Database/SingleTableInheritanceEntity.php b/app/lib/Firefly/Database/SingleTableInheritanceEntity.php index 244710539f..11b6f81eea 100644 --- a/app/lib/Firefly/Database/SingleTableInheritanceEntity.php +++ b/app/lib/Firefly/Database/SingleTableInheritanceEntity.php @@ -1,9 +1,11 @@ subclassField) { $this->attributes[$this->subclassField] = get_class($this); } - return parent::save($options); + return parent::save($rules, $customMessages, $options, $beforeSave, $afterSave); } } \ No newline at end of file diff --git a/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php index 4b100b5629..2dbddc44ea 100644 --- a/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php +++ b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php @@ -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; } } } diff --git a/app/lib/Firefly/Storage/User/EloquentUserRepository.php b/app/lib/Firefly/Storage/User/EloquentUserRepository.php index 35365aaaca..2a502cd2e3 100644 --- a/app/lib/Firefly/Storage/User/EloquentUserRepository.php +++ b/app/lib/Firefly/Storage/User/EloquentUserRepository.php @@ -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(); diff --git a/app/models/Account.php b/app/models/Account.php index aaa9c62e82..319eb1b215 100644 --- a/app/models/Account.php +++ b/app/models/Account.php @@ -1,9 +1,14 @@ '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'); diff --git a/app/models/Component.php b/app/models/Component.php index 1775d77562..34a70bc2be 100644 --- a/app/models/Component.php +++ b/app/models/Component.php @@ -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'; diff --git a/app/models/Elegant.php b/app/models/Elegant.php deleted file mode 100644 index ebe6805cfe..0000000000 --- a/app/models/Elegant.php +++ /dev/null @@ -1,18 +0,0 @@ -toArray(), - $this::$rules - ); - $this->validator = $validator; - return $validator->passes(); - } -} \ No newline at end of file diff --git a/app/models/Preference.php b/app/models/Preference.php index cd5f58161e..047355887d 100644 --- a/app/models/Preference.php +++ b/app/models/Preference.php @@ -1,19 +1,23 @@ '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() { diff --git a/app/models/Transaction.php b/app/models/Transaction.php index 40d323d970..30b3d86e8b 100644 --- a/app/models/Transaction.php +++ b/app/models/Transaction.php @@ -1,14 +1,16 @@ '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 diff --git a/app/models/TransactionJournal.php b/app/models/TransactionJournal.php index 3fc4aab322..f5c73ea7ca 100644 --- a/app/models/TransactionJournal.php +++ b/app/models/TransactionJournal.php @@ -1,15 +1,17 @@ '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' ]; diff --git a/app/models/User.php b/app/models/User.php index 0f7886e2b8..a9856114d6 100644 --- a/app/models/User.php +++ b/app/models/User.php @@ -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' ]; diff --git a/app/tests/controllers/ChartControllerTest.php b/app/tests/controllers/ChartControllerTest.php index 20aaf8c18d..4192dc9603 100644 --- a/app/tests/controllers/ChartControllerTest.php +++ b/app/tests/controllers/ChartControllerTest.php @@ -1,5 +1,7 @@ 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() diff --git a/app/tests/controllers/TransactionControllerTest.php b/app/tests/controllers/TransactionControllerTest.php index 823c42be47..130393d55d 100644 --- a/app/tests/controllers/TransactionControllerTest.php +++ b/app/tests/controllers/TransactionControllerTest.php @@ -1,20 +1,49 @@ 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'); + } + } \ No newline at end of file diff --git a/app/tests/models/AllModelsTest.php b/app/tests/models/AllModelsTest.php index 219a04ef11..773f244e50 100644 --- a/app/tests/models/AllModelsTest.php +++ b/app/tests/models/AllModelsTest.php @@ -1,6 +1,6 @@ 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); diff --git a/composer.json b/composer.json index 5ec7d12d65..bcf6764194 100644 --- a/composer.json +++ b/composer.json @@ -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": [