From 36f1b6a834280e8f079f9102bf1c290ffda689f7 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 2 Jan 2017 10:05:02 +0100 Subject: [PATCH] New account tests. --- app/Models/Account.php | 20 +++++--- tests/unit/Models/AccountTest.php | 83 +++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 tests/unit/Models/AccountTest.php diff --git a/app/Models/Account.php b/app/Models/Account.php index a4a2813cb9..4810c6006e 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -69,10 +69,14 @@ class Account extends Model /** * @param array $fields * - * @return Account|null + * @return Account + * @throws FireflyException */ public static function firstOrCreateEncrypted(array $fields) { + if (!isset($fields['user_id'])) { + throw new FireflyException('Missing required field "user_id".'); + } // everything but the name: $query = self::orderBy('id'); $search = $fields; @@ -81,17 +85,21 @@ class Account extends Model foreach ($search as $name => $value) { $query->where($name, $value); } - $set = $query->get(['accounts.*']); + $set = $query->get(['accounts.*']); + + // account must have a name. If not set, use IBAN. + if (!isset($fields['name'])) { + $fields['name'] = $fields['iban']; + } + + + /** @var Account $account */ foreach ($set as $account) { if ($account->name == $fields['name']) { return $account; } } - // account must have a name. If not set, use IBAN. - if (!isset($fields['name'])) { - $fields['name'] = $fields['iban']; - } // create it! $account = self::create($fields); diff --git a/tests/unit/Models/AccountTest.php b/tests/unit/Models/AccountTest.php new file mode 100644 index 0000000000..6be272929b --- /dev/null +++ b/tests/unit/Models/AccountTest.php @@ -0,0 +1,83 @@ + 1, + 'name' => 'Test account #' . rand(1000, 9999), + ]; + $account = Account::firstOrCreateEncrypted($data); + + $this->assertEquals($account->user_id, $data['user_id']); + $this->assertEquals($account->name, $data['name']); + } + + /** + * @covers \FireflyIII\Models\Account::firstOrCreateEncrypted + */ + public function testEncryptedIban() + { + $data = [ + 'user_id' => 1, + 'iban' => 'NL64RABO0133183395', + ]; + $account = Account::firstOrCreateEncrypted($data); + + $this->assertEquals($account->user_id, $data['user_id']); + $this->assertEquals($account->name, $data['iban']); + } + + /** + * @covers \FireflyIII\Models\Account::firstOrCreateEncrypted + * @expectedException \FireflyIII\Exceptions\FireflyException + */ + public function testEncryptedNoId() + { + $data = [ + 'name' => 'Test account', + ]; + $account = Account::firstOrCreateEncrypted($data); + } + + /** + * @covers \FireflyIII\Models\Account::routeBinder + */ + public function testRouteBinder() + { + // not logged in? + $this->be($this->user()); + $this->call('get', route('accounts.show', [1])); + + } + + /** + * One that belongs to another user. + * + * @covers \FireflyIII\Models\Account::routeBinder + */ + public function testRouteBinderError() + { + $account = Account::whereUserId(3)->first(); + $this->be($this->user()); + $this->call('get', route('accounts.show', [$account->id])); + $this->assertResponseStatus(404); + } +} \ No newline at end of file