mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-12 01:42:32 +00:00
Update tests and code.
This commit is contained in:
@@ -30,6 +30,7 @@ use FireflyIII\Factory\AccountFactory;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountMeta;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use Log;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -67,6 +68,7 @@ class AccountFactoryTest extends TestCase
|
||||
'accountRole' => 'defaultAsset',
|
||||
];
|
||||
|
||||
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($this->user());
|
||||
@@ -84,6 +86,12 @@ class AccountFactoryTest extends TestCase
|
||||
$meta = $account->accountMeta()->where('name', 'accountRole')->first();
|
||||
$this->assertNotNull($meta);
|
||||
$this->assertEquals('defaultAsset', $meta->data);
|
||||
|
||||
// get the currency ID:
|
||||
/** @var AccountMeta $meta */
|
||||
$currencyId = $account->accountMeta()->where('name', 'currency_id')->first();
|
||||
$this->assertNotNull($currencyId);
|
||||
$this->assertEquals('1', $currencyId->data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -507,6 +515,100 @@ class AccountFactoryTest extends TestCase
|
||||
$this->assertEquals('Hello!', $note->text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test minimal set of data to make factory work (asset account).
|
||||
*
|
||||
* @covers \FireflyIII\Factory\AccountFactory
|
||||
* @covers \FireflyIII\Factory\AccountMetaFactory
|
||||
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
|
||||
*/
|
||||
public function testCreateCurrencyCode(): void
|
||||
{
|
||||
$currency = TransactionCurrency::where('code', 'CAD')->first();
|
||||
$data = [
|
||||
'account_type_id' => null,
|
||||
'accountType' => 'asset',
|
||||
'iban' => null,
|
||||
'currency_code' => $currency->code,
|
||||
'name' => 'Basic asset account #' . random_int(1, 10000),
|
||||
'virtualBalance' => null,
|
||||
'active' => true,
|
||||
'accountRole' => 'defaultAsset',
|
||||
];
|
||||
|
||||
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($this->user());
|
||||
$account = $factory->create($data);
|
||||
|
||||
// assert stuff about account:
|
||||
$this->assertEquals($account->name, $data['name']);
|
||||
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
|
||||
$this->assertEquals('', $account->iban);
|
||||
$this->assertTrue($account->active);
|
||||
$this->assertEquals('0', $account->virtual_balance);
|
||||
|
||||
// get the role:
|
||||
/** @var AccountMeta $meta */
|
||||
$meta = $account->accountMeta()->where('name', 'accountRole')->first();
|
||||
$this->assertNotNull($meta);
|
||||
$this->assertEquals('defaultAsset', $meta->data);
|
||||
|
||||
// get the currency ID:
|
||||
/** @var AccountMeta $meta */
|
||||
$currencyId = $account->accountMeta()->where('name', 'currency_id')->first();
|
||||
$this->assertNotNull($currencyId);
|
||||
$this->assertEquals($currency->id, $currencyId->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test minimal set of data to make factory work (asset account).
|
||||
*
|
||||
* @covers \FireflyIII\Factory\AccountFactory
|
||||
* @covers \FireflyIII\Factory\AccountMetaFactory
|
||||
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
|
||||
*/
|
||||
public function testCreateCurrencyId(): void
|
||||
{
|
||||
$currency = TransactionCurrency::where('code', 'USD')->first();
|
||||
$data = [
|
||||
'account_type_id' => null,
|
||||
'accountType' => 'asset',
|
||||
'iban' => null,
|
||||
'currency_id' => $currency->id,
|
||||
'name' => 'Basic asset account #' . random_int(1, 10000),
|
||||
'virtualBalance' => null,
|
||||
'active' => true,
|
||||
'accountRole' => 'defaultAsset',
|
||||
];
|
||||
|
||||
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($this->user());
|
||||
$account = $factory->create($data);
|
||||
|
||||
// assert stuff about account:
|
||||
$this->assertEquals($account->name, $data['name']);
|
||||
$this->assertEquals(AccountType::ASSET, $account->accountType->type);
|
||||
$this->assertEquals('', $account->iban);
|
||||
$this->assertTrue($account->active);
|
||||
$this->assertEquals('0', $account->virtual_balance);
|
||||
|
||||
// get the role:
|
||||
/** @var AccountMeta $meta */
|
||||
$meta = $account->accountMeta()->where('name', 'accountRole')->first();
|
||||
$this->assertNotNull($meta);
|
||||
$this->assertEquals('defaultAsset', $meta->data);
|
||||
|
||||
// get the currency ID:
|
||||
/** @var AccountMeta $meta */
|
||||
$currencyId = $account->accountMeta()->where('name', 'currency_id')->first();
|
||||
$this->assertNotNull($currencyId);
|
||||
$this->assertEquals($currency->id, $currencyId->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should return existing account.
|
||||
*
|
||||
|
||||
@@ -25,8 +25,11 @@ namespace Tests\Unit\Factory;
|
||||
|
||||
|
||||
use FireflyIII\Factory\BillFactory;
|
||||
use FireflyIII\Factory\TransactionCurrencyFactory;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use Log;
|
||||
use Tests\TestCase;
|
||||
use Amount;
|
||||
|
||||
/**
|
||||
* Class BillFactoryTest
|
||||
@@ -52,7 +55,8 @@ class BillFactoryTest extends TestCase
|
||||
*/
|
||||
public function testCreateBasic(): void
|
||||
{
|
||||
$data = [
|
||||
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
||||
$data = [
|
||||
'name' => 'Some new bill #' . random_int(1, 10000),
|
||||
'amount_min' => '5',
|
||||
'currency_id' => 1,
|
||||
@@ -66,6 +70,10 @@ class BillFactoryTest extends TestCase
|
||||
'notes' => 'Hello!',
|
||||
];
|
||||
|
||||
$currencyFactory->shouldReceive('find')->atLeast()->once()
|
||||
->withArgs([1, ''])->andReturn(TransactionCurrency::find(1));
|
||||
|
||||
|
||||
/** @var BillFactory $factory */
|
||||
$factory = app(BillFactory::class);
|
||||
$factory->setUser($this->user());
|
||||
@@ -73,6 +81,88 @@ class BillFactoryTest extends TestCase
|
||||
|
||||
$this->assertEquals($data['name'], $bill->name);
|
||||
$this->assertEquals($data['amount_min'], $bill->amount_min);
|
||||
$this->assertEquals(1, $bill->transaction_currency_id);
|
||||
$this->assertEquals($data['repeat_freq'], $bill->repeat_freq);
|
||||
$note = $bill->notes()->first();
|
||||
$this->assertEquals($data['notes'], $note->text);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create basic bill with minimum data.
|
||||
*
|
||||
* @covers \FireflyIII\Factory\BillFactory
|
||||
* @covers \FireflyIII\Services\Internal\Support\BillServiceTrait
|
||||
*/
|
||||
public function testCreateDifferentCurrency(): void
|
||||
{
|
||||
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
||||
$data = [
|
||||
'name' => 'Some new bill #' . random_int(1, 10000),
|
||||
'amount_min' => '5',
|
||||
'currency_code' => 'USD',
|
||||
'amount_max' => '10',
|
||||
'date' => '2018-01-01',
|
||||
'repeat_freq' => 'monthly',
|
||||
'skip' => 0,
|
||||
'automatch' => true,
|
||||
'active' => true,
|
||||
'notes' => 'Hello!',
|
||||
];
|
||||
|
||||
$currencyFactory->shouldReceive('find')->atLeast()->once()
|
||||
->withArgs([0, 'USD'])->andReturn(TransactionCurrency::find(5));
|
||||
|
||||
|
||||
/** @var BillFactory $factory */
|
||||
$factory = app(BillFactory::class);
|
||||
$factory->setUser($this->user());
|
||||
$bill = $factory->create($data);
|
||||
|
||||
$this->assertEquals($data['name'], $bill->name);
|
||||
$this->assertEquals($data['amount_min'], $bill->amount_min);
|
||||
$this->assertEquals(5, $bill->transaction_currency_id);
|
||||
$this->assertEquals($data['repeat_freq'], $bill->repeat_freq);
|
||||
$note = $bill->notes()->first();
|
||||
$this->assertEquals($data['notes'], $note->text);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create basic bill with minimum data.
|
||||
*
|
||||
* @covers \FireflyIII\Factory\BillFactory
|
||||
* @covers \FireflyIII\Services\Internal\Support\BillServiceTrait
|
||||
*/
|
||||
public function testCreateNoCurrency(): void
|
||||
{
|
||||
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
||||
$data = [
|
||||
'name' => 'Some new bill #' . random_int(1, 10000),
|
||||
'amount_min' => '5',
|
||||
'amount_max' => '10',
|
||||
'date' => '2018-01-01',
|
||||
'repeat_freq' => 'monthly',
|
||||
'skip' => 0,
|
||||
'automatch' => true,
|
||||
'active' => true,
|
||||
'notes' => 'Hello!',
|
||||
];
|
||||
|
||||
$currencyFactory->shouldReceive('find')->atLeast()->once()
|
||||
->withArgs([0, ''])->andReturnNull();
|
||||
|
||||
Amount::shouldReceive('getDefaultCurrencyByUser')->atLeast()->once()->andReturn(TransactionCurrency::find(3));
|
||||
|
||||
|
||||
/** @var BillFactory $factory */
|
||||
$factory = app(BillFactory::class);
|
||||
$factory->setUser($this->user());
|
||||
$bill = $factory->create($data);
|
||||
|
||||
$this->assertEquals($data['name'], $bill->name);
|
||||
$this->assertEquals($data['amount_min'], $bill->amount_min);
|
||||
$this->assertEquals(3, $bill->transaction_currency_id);
|
||||
$this->assertEquals($data['repeat_freq'], $bill->repeat_freq);
|
||||
$note = $bill->notes()->first();
|
||||
$this->assertEquals($data['notes'], $note->text);
|
||||
@@ -87,7 +177,8 @@ class BillFactoryTest extends TestCase
|
||||
*/
|
||||
public function testCreateEmptyNotes(): void
|
||||
{
|
||||
$data = [
|
||||
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
||||
$data = [
|
||||
'name' => 'Some new bill #' . random_int(1, 10000),
|
||||
'amount_min' => '5',
|
||||
'amount_max' => '10',
|
||||
@@ -101,12 +192,17 @@ class BillFactoryTest extends TestCase
|
||||
'notes' => '',
|
||||
];
|
||||
|
||||
$currencyFactory->shouldReceive('find')->atLeast()->once()
|
||||
->withArgs([1, ''])->andReturn(TransactionCurrency::find(1));
|
||||
|
||||
|
||||
/** @var BillFactory $factory */
|
||||
$factory = app(BillFactory::class);
|
||||
$factory->setUser($this->user());
|
||||
$bill = $factory->create($data);
|
||||
|
||||
$this->assertEquals($data['name'], $bill->name);
|
||||
$this->assertEquals(1, $bill->transaction_currency_id);
|
||||
$this->assertEquals($data['amount_min'], $bill->amount_min);
|
||||
$this->assertEquals($data['repeat_freq'], $bill->repeat_freq);
|
||||
$this->assertEquals(0, $bill->notes()->count());
|
||||
@@ -121,7 +217,8 @@ class BillFactoryTest extends TestCase
|
||||
*/
|
||||
public function testFindById(): void
|
||||
{
|
||||
$existing = $this->user()->piggyBanks()->first();
|
||||
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
||||
$existing = $this->user()->piggyBanks()->first();
|
||||
/** @var BillFactory $factory */
|
||||
$factory = app(BillFactory::class);
|
||||
$factory->setUser($this->user());
|
||||
@@ -137,7 +234,8 @@ class BillFactoryTest extends TestCase
|
||||
*/
|
||||
public function testFindByName(): void
|
||||
{
|
||||
$existing = $this->user()->bills()->first();
|
||||
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
||||
$existing = $this->user()->bills()->first();
|
||||
/** @var BillFactory $factory */
|
||||
$factory = app(BillFactory::class);
|
||||
$factory->setUser($this->user());
|
||||
@@ -154,6 +252,7 @@ class BillFactoryTest extends TestCase
|
||||
*/
|
||||
public function testFindByUnknownName(): void
|
||||
{
|
||||
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
||||
/** @var BillFactory $factory */
|
||||
$factory = app(BillFactory::class);
|
||||
$factory->setUser($this->user());
|
||||
@@ -170,6 +269,7 @@ class BillFactoryTest extends TestCase
|
||||
*/
|
||||
public function testFindNull(): void
|
||||
{
|
||||
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
||||
/** @var BillFactory $factory */
|
||||
$factory = app(BillFactory::class);
|
||||
$factory->setUser($this->user());
|
||||
|
||||
Reference in New Issue
Block a user