mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 10:33:30 +00:00
New account tests.
This commit is contained in:
@@ -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;
|
||||
@@ -82,16 +86,20 @@ class Account extends Model
|
||||
$query->where($name, $value);
|
||||
}
|
||||
$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);
|
||||
|
83
tests/unit/Models/AccountTest.php
Normal file
83
tests/unit/Models/AccountTest.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/**
|
||||
* AccountTest.php
|
||||
* Copyright (c) 2016 thegrumpydictator@gmail.com
|
||||
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
|
||||
*
|
||||
* See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class AccountTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Models\Account::firstOrCreateEncrypted
|
||||
*/
|
||||
public function testEncrypted()
|
||||
{
|
||||
$data = [
|
||||
'user_id' => 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user