Improve test coverage.

This commit is contained in:
James Cole
2018-08-24 07:18:33 +02:00
parent b174a06b86
commit 2b54363dd7
42 changed files with 1573 additions and 23 deletions

View File

@@ -132,6 +132,7 @@ class AccountFactory
}
/**
*
* @param string $accountName
* @param string $accountType
*

View File

@@ -65,7 +65,7 @@ class AccountMetaFactory
// if $data has field and $entry is null, create new one:
if (null === $entry) {
Log::debug(sprintf('Created meta-field "%s":"%s" for account #%d ("%s") ', $field, $value, $account->id, $account->name));
$this->create(['account_id' => $account->id, 'name' => $field, 'data' => $value]);
return $this->create(['account_id' => $account->id, 'name' => $field, 'data' => $value]);
}
// if $data has field and $entry is not null, update $entry:
@@ -75,12 +75,13 @@ class AccountMetaFactory
Log::debug(sprintf('Updated meta-field "%s":"%s" for #%d ("%s") ', $field, $value, $account->id, $account->name));
}
}
if ('' === $value && null !== $entry && isset($data[$field])) {
if ('' === $value && null !== $entry) {
try {
$entry->delete();
} catch (Exception $e) {
Log::debug(sprintf('Could not delete entry: %s', $e->getMessage()));
} catch (Exception $e) { // @codeCoverageIgnore
Log::debug(sprintf('Could not delete entry: %s', $e->getMessage())); // @codeCoverageIgnore
}
return null;
}
return $entry;

View File

@@ -59,10 +59,10 @@ class TransactionFactory
$currencyId = isset($data['currency']) ? $data['currency']->id : $currencyId;
if ('' === $data['amount']) {
Log::error('Empty string in data.', $data);
throw new FireflyException('Amount is an empty string, which Firefly III cannot handle. Apologies.'); // @codeCoverageIgnore
throw new FireflyException('Amount is an empty string, which Firefly III cannot handle. Apologies.');
}
if (null === $currencyId) {
throw new FireflyException('Cannot store transaction without currency information.'); // @codeCoverageIgnore
throw new FireflyException('Cannot store transaction without currency information.');
}
$data['foreign_amount'] = '' === (string)$data['foreign_amount'] ? null : $data['foreign_amount'];
Log::debug(sprintf('Create transaction for account #%d ("%s") with amount %s', $data['account']->id, $data['account']->name, $data['amount']));
@@ -154,7 +154,7 @@ class TransactionFactory
]
);
if (null === $source || null === $dest) {
throw new FireflyException('Could not create transactions.');
throw new FireflyException('Could not create transactions.'); // @codeCoverageIgnore
}
// set foreign currency

View File

@@ -28,7 +28,6 @@ namespace FireflyIII\Factory;
use FireflyIII\Models\TransactionType;
/**
* @codeCoverageIgnore
* Class TransactionTypeFactory
*/
class TransactionTypeFactory

View File

@@ -120,7 +120,7 @@ class ChartJsGenerator implements GeneratorInterface
// different sort when values are positive and when they're negative.
asort($data);
$next = next($data);
if (!\is_bool($next) && 1 === bccomp($next, '0')) {
if (!\is_bool($next) && 1 === bccomp((string)$next, '0')) {
// next is positive, sort other way around.
arsort($data);
}
@@ -129,7 +129,7 @@ class ChartJsGenerator implements GeneratorInterface
$index = 0;
foreach ($data as $key => $value) {
// make larger than 0
$chartData['datasets'][0]['data'][] = (float)app('steam')->positive($value);
$chartData['datasets'][0]['data'][] = (float)app('steam')->positive((string)$value);
$chartData['datasets'][0]['backgroundColor'][] = ChartColour::getColour($index);
$chartData['labels'][] = $key;
++$index;

View File

@@ -30,6 +30,8 @@ use Throwable;
/**
* Class MonthReportGenerator.
*
* @codeCoverageIgnore
*/
class MonthReportGenerator implements ReportGeneratorInterface
{

View File

@@ -24,6 +24,8 @@ namespace FireflyIII\Generator\Report\Account;
/**
* Class MultiYearReportGenerator.
*
* @codeCoverageIgnore
*/
class MultiYearReportGenerator extends MonthReportGenerator
{

View File

@@ -24,6 +24,8 @@ namespace FireflyIII\Generator\Report\Account;
/**
* Class YearReportGenerator.
*
* @codeCoverageIgnore
*/
class YearReportGenerator extends MonthReportGenerator
{

View File

@@ -97,6 +97,7 @@ class MonthReportGenerator implements ReportGeneratorInterface
* @param Collection $accounts
*
* @return ReportGeneratorInterface
* @codeCoverageIgnore
*/
public function setAccounts(Collection $accounts): ReportGeneratorInterface
{
@@ -111,6 +112,7 @@ class MonthReportGenerator implements ReportGeneratorInterface
* @param Collection $budgets
*
* @return ReportGeneratorInterface
* @codeCoverageIgnore
*/
public function setBudgets(Collection $budgets): ReportGeneratorInterface
{
@@ -123,6 +125,7 @@ class MonthReportGenerator implements ReportGeneratorInterface
* @param Collection $categories
*
* @return ReportGeneratorInterface
* @codeCoverageIgnore
*/
public function setCategories(Collection $categories): ReportGeneratorInterface
{
@@ -135,6 +138,7 @@ class MonthReportGenerator implements ReportGeneratorInterface
* @param Carbon $date
*
* @return ReportGeneratorInterface
* @codeCoverageIgnore
*/
public function setEndDate(Carbon $date): ReportGeneratorInterface
{
@@ -149,6 +153,7 @@ class MonthReportGenerator implements ReportGeneratorInterface
* @param Collection $expense
*
* @return ReportGeneratorInterface
* @codeCoverageIgnore
*/
public function setExpense(Collection $expense): ReportGeneratorInterface
{
@@ -161,6 +166,7 @@ class MonthReportGenerator implements ReportGeneratorInterface
* @param Carbon $date
*
* @return ReportGeneratorInterface
* @codeCoverageIgnore
*/
public function setStartDate(Carbon $date): ReportGeneratorInterface
{
@@ -175,6 +181,7 @@ class MonthReportGenerator implements ReportGeneratorInterface
* @param Collection $tags
*
* @return ReportGeneratorInterface
* @codeCoverageIgnore
*/
public function setTags(Collection $tags): ReportGeneratorInterface
{

View File

@@ -24,6 +24,7 @@ namespace FireflyIII\Generator\Report\Audit;
/**
* Class MultiYearReportGenerator.
* @codeCoverageIgnore
*/
class MultiYearReportGenerator extends MonthReportGenerator
{

View File

@@ -24,6 +24,7 @@ namespace FireflyIII\Generator\Report\Audit;
/**
* Class YearReportGenerator.
* @codeCoverageIgnore
*/
class YearReportGenerator extends MonthReportGenerator
{

View File

@@ -39,6 +39,8 @@ use Throwable;
/**
* Class MonthReportGenerator.
*
* @codeCoverageIgnore
*/
class MonthReportGenerator extends Support implements ReportGeneratorInterface
{

View File

@@ -24,6 +24,7 @@ namespace FireflyIII\Generator\Report\Budget;
/**
* Class MultiYearReportGenerator.
* @codeCoverageIgnore
*/
class MultiYearReportGenerator extends MonthReportGenerator
{

View File

@@ -24,6 +24,7 @@ namespace FireflyIII\Generator\Report\Budget;
/**
* Class YearReportGenerator.
* @codeCoverageIgnore
*/
class YearReportGenerator extends MonthReportGenerator
{

View File

@@ -40,6 +40,7 @@ use Throwable;
/**
* Class MonthReportGenerator.
* @codeCoverageIgnore
*/
class MonthReportGenerator extends Support implements ReportGeneratorInterface
{

View File

@@ -24,6 +24,7 @@ namespace FireflyIII\Generator\Report\Category;
/**
* Class MultiYearReportGenerator.
* @codeCoverageIgnore
*/
class MultiYearReportGenerator extends MonthReportGenerator
{

View File

@@ -24,6 +24,7 @@ namespace FireflyIII\Generator\Report\Category;
/**
* Class YearReportGenerator.
* @codeCoverageIgnore
*/
class YearReportGenerator extends MonthReportGenerator
{

View File

@@ -27,6 +27,7 @@ use FireflyIII\Exceptions\FireflyException;
/**
* Class ReportGeneratorFactory.
* @codeCoverageIgnore
*/
class ReportGeneratorFactory
{

View File

@@ -31,6 +31,7 @@ use Throwable;
/**
* Class MonthReportGenerator.
* @codeCoverageIgnore
*/
class MonthReportGenerator implements ReportGeneratorInterface
{

View File

@@ -30,6 +30,7 @@ use Throwable;
/**
* Class MonthReportGenerator.
* @codeCoverageIgnore
*/
class MultiYearReportGenerator implements ReportGeneratorInterface
{

View File

@@ -30,6 +30,7 @@ use Throwable;
/**
* Class MonthReportGenerator.
* @codeCoverageIgnore
*/
class YearReportGenerator implements ReportGeneratorInterface
{

View File

@@ -31,6 +31,7 @@ use Illuminate\Support\Collection;
* Class Support.
* @method Collection getExpenses()
* @method Collection getIncome()
* @codeCoverageIgnore
*/
class Support
{

View File

@@ -42,6 +42,7 @@ use Throwable;
/**
* Class MonthReportGenerator.
* @codeCoverageIgnore
*/
class MonthReportGenerator extends Support implements ReportGeneratorInterface
{

View File

@@ -24,6 +24,7 @@ namespace FireflyIII\Generator\Report\Tag;
/**
* Class MultiYearReportGenerator.
* @codeCoverageIgnore
*/
class MultiYearReportGenerator extends MonthReportGenerator
{

View File

@@ -24,6 +24,7 @@ namespace FireflyIII\Generator\Report\Tag;
/**
* Class YearReportGenerator.
* @codeCoverageIgnore
*/
class YearReportGenerator extends MonthReportGenerator
{

View File

@@ -25,9 +25,12 @@ namespace Tests\Unit\Factory;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\AccountFactory;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType;
use Log;
use Tests\TestCase;
/**
@@ -35,6 +38,15 @@ use Tests\TestCase;
*/
class AccountFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* Test minimal set of data to make factory work (asset account).
*
@@ -522,4 +534,53 @@ class AccountFactoryTest extends TestCase
$this->assertEquals($account->id, $existing->id);
}
/**
* Can't find account type.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Factory\AccountMetaFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
public function testCreateNoType(): void
{
$data = [
'account_type_id' => null,
'accountType' => 'bla-bla',
'iban' => null,
'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());
try {
$factory->create($data);
} catch (FireflyException $e) {
$this->assertContains('AccountFactory::create() was unable to find account type #0 ("bla-bla").', $e->getMessage());
}
}
/**
* Test only for existing account because the rest has been covered by other tests.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Factory\AccountMetaFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
public function testFindOrCreate(): void
{
/** @var Account $account */
$account = $this->user()->accounts()->inRandomOrder()->first();
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
$result = $factory->findOrCreate($account->name, $account->accountType->type);
$this->assertEquals($result->id, $account->id);
}
}

View File

@@ -0,0 +1,121 @@
<?php
/**
* AccountMetaFactoryTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Unit\Factory;
use FireflyIII\Factory\AccountMetaFactory;
use Log;
use Tests\TestCase;
/**
*
* Class AccountMetaFactoryTest
*/
class AccountMetaFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Factory\AccountMetaFactory
*/
public function testCreate(): void
{
$account = $this->user()->accounts()->inRandomOrder()->first();
$data = [
'account_id' => $account->id,
'name' => 'Some name',
'data' => 'Some value',
];
$factory = new AccountMetaFactory;
$result = $factory->create($data);
$this->assertEquals($data['name'], $result->name);
}
/**
* @covers \FireflyIII\Factory\AccountMetaFactory
*/
public function testCrudDelete(): void
{
$factory = new AccountMetaFactory;
$account = $this->user()->accounts()->inRandomOrder()->first();
$data = [
'account_id' => $account->id,
'name' => 'Some name ' . random_int(1, 100000),
'data' => 'Some value',
];
$factory->create($data);
// update existing one
$result = $factory->crud($account, $data['name'], '');
$this->assertNull($result);
}
/**
* @covers \FireflyIII\Factory\AccountMetaFactory
*/
public function testCrudExisting(): void
{
$factory = new AccountMetaFactory;
$account = $this->user()->accounts()->inRandomOrder()->first();
$data = [
'account_id' => $account->id,
'name' => 'Some name ' . random_int(1, 100000),
'data' => 'Some value',
];
$existing = $factory->create($data);
// update existing one
$result = $factory->crud($account, $data['name'], 'Some NEW value');
$this->assertNotNull($result);
$this->assertEquals($result->account_id, $account->id);
$this->assertEquals($existing->name, $result->name);
$this->assertEquals('Some NEW value', $result->data);
}
/**
* @covers \FireflyIII\Factory\AccountMetaFactory
*/
public function testCrudNew(): void
{
$account = $this->user()->accounts()->inRandomOrder()->first();
$factory = new AccountMetaFactory;
$result = $factory->crud($account, 'random name ' . random_int(1, 100000), 'Some value');
$this->assertNotNull($result);
$this->assertEquals($result->account_id, $account->id);
}
}

View File

@@ -0,0 +1,71 @@
<?php
/**
* AttachmentFactoryTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Unit\Factory;
use FireflyIII\Factory\AttachmentFactory;
use FireflyIII\Models\TransactionJournal;
use Log;
use Tests\TestCase;
/**
*
* Class AttachmentFactoryTest
*/
class AttachmentFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Factory\AttachmentFactory
*/
public function testCreate(): void
{
$journal = $this->user()->transactionJournals()->inRandomOrder()->first();
$data = [
'model_id' => $journal->id,
'model' => TransactionJournal::class,
'filename' => 'testfile.pdf',
'title' => 'File name',
'notes' => 'Some notes',
];
$factory = new AttachmentFactory;
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($data['title'], $result->title);
$this->assertEquals(1, $result->notes()->count());
}
}

View File

@@ -25,6 +25,7 @@ namespace Tests\Unit\Factory;
use FireflyIII\Factory\BillFactory;
use Log;
use Tests\TestCase;
/**
@@ -33,6 +34,16 @@ use Tests\TestCase;
class BillFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* Create basic bill with minimum data.
*
@@ -77,17 +88,17 @@ class BillFactoryTest extends TestCase
public function testCreateEmptyNotes(): void
{
$data = [
'name' => 'Some new bill #' . random_int(1, 10000),
'amount_min' => '5',
'amount_max' => '10',
'date' => '2018-01-01',
'repeat_freq' => 'monthly',
'currency_id' => 1,
'name' => 'Some new bill #' . random_int(1, 10000),
'amount_min' => '5',
'amount_max' => '10',
'date' => '2018-01-01',
'repeat_freq' => 'monthly',
'currency_id' => 1,
'currency_code' => '',
'skip' => 0,
'automatch' => true,
'active' => true,
'notes' => '',
'skip' => 0,
'automatch' => true,
'active' => true,
'notes' => '',
];
/** @var BillFactory $factory */

View File

@@ -25,6 +25,7 @@ namespace Tests\Unit\Factory;
use FireflyIII\Factory\BudgetFactory;
use Log;
use Tests\TestCase;
/**
@@ -32,6 +33,16 @@ use Tests\TestCase;
*/
class BudgetFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* Put in ID, return it.
*

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace Tests\Unit\Factory;
use FireflyIII\Factory\CategoryFactory;
use Log;
use Tests\TestCase;
/**
@@ -31,6 +32,16 @@ use Tests\TestCase;
*/
class CategoryFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Factory\CategoryFactory
*/

View File

@@ -29,6 +29,7 @@ use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\PiggyBankRepetition;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use Log;
use Tests\TestCase;
/**
@@ -36,6 +37,16 @@ use Tests\TestCase;
*/
class PiggyBankEventFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Factory\PiggyBankEventFactory
*/

View File

@@ -25,6 +25,7 @@ namespace Tests\Unit\Factory;
use FireflyIII\Factory\PiggyBankFactory;
use Log;
use Tests\TestCase;
/**
@@ -32,6 +33,16 @@ use Tests\TestCase;
*/
class PiggyBankFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* Put in ID, return it.
*

View File

@@ -0,0 +1,552 @@
<?php
/**
* RecurrenceFactoryTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Unit\Factory;
use Amount;
use Carbon\Carbon;
use FireflyIII\Factory\BudgetFactory;
use FireflyIII\Factory\CategoryFactory;
use FireflyIII\Factory\PiggyBankFactory;
use FireflyIII\Factory\RecurrenceFactory;
use FireflyIII\Factory\TransactionCurrencyFactory;
use FireflyIII\Factory\TransactionTypeFactory;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Log;
use Tests\TestCase;
/**
*
* Class RecurrenceFactoryTest
*/
class RecurrenceFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* With piggy bank. With tags. With budget. With category.
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testBasic(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$accountA = $this->user()->accounts()->inRandomOrder()->first();
$accountB = $this->user()->accounts()->inRandomOrder()->first();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category= $this->user()->categories()->inRandomOrder()->first();
// mock other factories:
$piggyFactory = $this->mock(PiggyBankFactory::class);
$budgetFactory = $this->mock(BudgetFactory::class);
$categoryFactory = $this->mock(CategoryFactory::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
// mock calls:
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn(TransactionCurrency::find(1))->once();
$piggyFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
$accountRepos->shouldReceive('setUser')->twice();
$accountRepos->shouldReceive('findNull')->twice()->andReturn($accountA, $accountB);
$currencyFactory->shouldReceive('find')->once()->withArgs([1, 'EUR'])->andReturn(null);
$currencyFactory->shouldReceive('find')->once()->withArgs([null, null])->andReturn(null);
$budgetFactory->shouldReceive('setUser')->once();
$budgetFactory->shouldReceive('find')->withArgs([1, 'Some budget'])->once()->andReturn($budget);
$categoryFactory->shouldReceive('setUser')->once();
$categoryFactory->shouldReceive('findOrCreate')->withArgs([2, 'Some category'])->once()->andReturn($category);
// data for basic recurrence.
$data = [
'recurrence' => [
'type' => 'withdrawal',
'first_date' => Carbon::create()->addDay(),
'repetitions' => 0,
'title' => 'Test recurrence' . random_int(1, 100000),
'description' => 'Description thing',
'apply_rules' => true,
'active' => true,
'repeat_until' => null,
],
'meta' => [
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
'repetitions' => [
[
'type' => 'daily',
'moment' => '',
'skip' => 0,
'weekend' => 1,
],
],
'transactions' => [
[
'source_id' => 1,
'source_name' => 'Some name',
'destination_id' => 2,
'destination_name' => 'some otjer name',
'currency_id' => 1,
'currency_code' => 'EUR',
'foreign_currency_id' => null,
'foreign_currency_code' => null,
'foreign_amount' => null,
'description' => 'Bla bla bla',
'amount' => '100',
'budget_id' => 1,
'budget_name' => 'Some budget',
'category_id' => 2,
'category_name' => 'Some category',
],
],
];
$typeFactory = $this->mock(TransactionTypeFactory::class);
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(1));
$factory = new RecurrenceFactory;
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
* Deposit. With piggy bank. With tags. With budget. With category.
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testBasicDeposit(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$accountA = $this->user()->accounts()->inRandomOrder()->first();
$accountB = $this->user()->accounts()->inRandomOrder()->first();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category= $this->user()->categories()->inRandomOrder()->first();
// mock other factories:
$piggyFactory = $this->mock(PiggyBankFactory::class);
$budgetFactory = $this->mock(BudgetFactory::class);
$categoryFactory = $this->mock(CategoryFactory::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
// mock calls:
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn(TransactionCurrency::find(1))->once();
$piggyFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
$accountRepos->shouldReceive('setUser')->twice();
$accountRepos->shouldReceive('findNull')->twice()->andReturn($accountA, $accountB);
$currencyFactory->shouldReceive('find')->once()->withArgs([1, 'EUR'])->andReturn(null);
$currencyFactory->shouldReceive('find')->once()->withArgs([null, null])->andReturn(null);
$budgetFactory->shouldReceive('setUser')->once();
$budgetFactory->shouldReceive('find')->withArgs([1, 'Some budget'])->once()->andReturn($budget);
$categoryFactory->shouldReceive('setUser')->once();
$categoryFactory->shouldReceive('findOrCreate')->withArgs([2, 'Some category'])->once()->andReturn($category);
// data for basic recurrence.
$data = [
'recurrence' => [
'type' => 'deposit',
'first_date' => Carbon::create()->addDay(),
'repetitions' => 0,
'title' => 'Test recurrence' . random_int(1, 100000),
'description' => 'Description thing',
'apply_rules' => true,
'active' => true,
'repeat_until' => null,
],
'meta' => [
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
'repetitions' => [
[
'type' => 'daily',
'moment' => '',
'skip' => 0,
'weekend' => 1,
],
],
'transactions' => [
[
'source_id' => 1,
'source_name' => 'Some name',
'destination_id' => 2,
'destination_name' => 'some otjer name',
'currency_id' => 1,
'currency_code' => 'EUR',
'foreign_currency_id' => null,
'foreign_currency_code' => null,
'foreign_amount' => null,
'description' => 'Bla bla bla',
'amount' => '100',
'budget_id' => 1,
'budget_name' => 'Some budget',
'category_id' => 2,
'category_name' => 'Some category',
],
],
];
$typeFactory = $this->mock(TransactionTypeFactory::class);
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(2));
$factory = new RecurrenceFactory;
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
* Deposit. With piggy bank. With tags. With budget. With category.
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testBasicTransfer(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$accountA = $this->user()->accounts()->inRandomOrder()->first();
$accountB = $this->user()->accounts()->inRandomOrder()->first();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category= $this->user()->categories()->inRandomOrder()->first();
// mock other factories:
$piggyFactory = $this->mock(PiggyBankFactory::class);
$budgetFactory = $this->mock(BudgetFactory::class);
$categoryFactory = $this->mock(CategoryFactory::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
// mock calls:
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn(TransactionCurrency::find(1))->once();
$piggyFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
$accountRepos->shouldReceive('setUser')->twice();
$accountRepos->shouldReceive('findNull')->twice()->andReturn($accountA, $accountB);
$currencyFactory->shouldReceive('find')->once()->withArgs([1, 'EUR'])->andReturn(null);
$currencyFactory->shouldReceive('find')->once()->withArgs([null, null])->andReturn(null);
$budgetFactory->shouldReceive('setUser')->once();
$budgetFactory->shouldReceive('find')->withArgs([1, 'Some budget'])->once()->andReturn($budget);
$categoryFactory->shouldReceive('setUser')->once();
$categoryFactory->shouldReceive('findOrCreate')->withArgs([2, 'Some category'])->once()->andReturn($category);
// data for basic recurrence.
$data = [
'recurrence' => [
'type' => 'transfer',
'first_date' => Carbon::create()->addDay(),
'repetitions' => 0,
'title' => 'Test recurrence' . random_int(1, 100000),
'description' => 'Description thing',
'apply_rules' => true,
'active' => true,
'repeat_until' => null,
],
'meta' => [
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
'repetitions' => [
[
'type' => 'daily',
'moment' => '',
'skip' => 0,
'weekend' => 1,
],
],
'transactions' => [
[
'source_id' => 1,
'source_name' => 'Some name',
'destination_id' => 2,
'destination_name' => 'some otjer name',
'currency_id' => 1,
'currency_code' => 'EUR',
'foreign_currency_id' => null,
'foreign_currency_code' => null,
'foreign_amount' => null,
'description' => 'Bla bla bla',
'amount' => '100',
'budget_id' => 1,
'budget_name' => 'Some budget',
'category_id' => 2,
'category_name' => 'Some category',
],
],
];
$typeFactory = $this->mock(TransactionTypeFactory::class);
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(3));
$factory = new RecurrenceFactory;
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
* With piggy bank. With tags. With budget. With category.
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testBasicNoTags(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$accountA = $this->user()->accounts()->inRandomOrder()->first();
$accountB = $this->user()->accounts()->inRandomOrder()->first();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category= $this->user()->categories()->inRandomOrder()->first();
// mock other factories:
$piggyFactory = $this->mock(PiggyBankFactory::class);
$budgetFactory = $this->mock(BudgetFactory::class);
$categoryFactory = $this->mock(CategoryFactory::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
// mock calls:
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn(TransactionCurrency::find(1))->once();
$piggyFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn($piggyBank);
$accountRepos->shouldReceive('setUser')->twice();
$accountRepos->shouldReceive('findNull')->twice()->andReturn($accountA, $accountB);
$currencyFactory->shouldReceive('find')->once()->withArgs([1, 'EUR'])->andReturn(null);
$currencyFactory->shouldReceive('find')->once()->withArgs([null, null])->andReturn(null);
$budgetFactory->shouldReceive('setUser')->once();
$budgetFactory->shouldReceive('find')->withArgs([1, 'Some budget'])->once()->andReturn($budget);
$categoryFactory->shouldReceive('setUser')->once();
$categoryFactory->shouldReceive('findOrCreate')->withArgs([2, 'Some category'])->once()->andReturn($category);
// data for basic recurrence.
$data = [
'recurrence' => [
'type' => 'withdrawal',
'first_date' => Carbon::create()->addDay(),
'repetitions' => 0,
'title' => 'Test recurrence' . random_int(1, 100000),
'description' => 'Description thing',
'apply_rules' => true,
'active' => true,
'repeat_until' => null,
],
'meta' => [
'tags' => [],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
'repetitions' => [
[
'type' => 'daily',
'moment' => '',
'skip' => 0,
'weekend' => 1,
],
],
'transactions' => [
[
'source_id' => 1,
'source_name' => 'Some name',
'destination_id' => 2,
'destination_name' => 'some otjer name',
'currency_id' => 1,
'currency_code' => 'EUR',
'foreign_currency_id' => null,
'foreign_currency_code' => null,
'foreign_amount' => null,
'description' => 'Bla bla bla',
'amount' => '100',
'budget_id' => 1,
'budget_name' => 'Some budget',
'category_id' => 2,
'category_name' => 'Some category',
],
],
];
$typeFactory = $this->mock(TransactionTypeFactory::class);
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(1));
$factory = new RecurrenceFactory;
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
* No piggy bank. With tags. With budget. With category.
*
* @covers \FireflyIII\Factory\RecurrenceFactory
* @covers \FireflyIII\Services\Internal\Support\RecurringTransactionTrait
*/
public function testBasicNoPiggybank(): void
{
// objects to return:
$piggyBank = $this->user()->piggyBanks()->inRandomOrder()->first();
$accountA = $this->user()->accounts()->inRandomOrder()->first();
$accountB = $this->user()->accounts()->inRandomOrder()->first();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$category= $this->user()->categories()->inRandomOrder()->first();
// mock other factories:
$piggyFactory = $this->mock(PiggyBankFactory::class);
$budgetFactory = $this->mock(BudgetFactory::class);
$categoryFactory = $this->mock(CategoryFactory::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
// mock calls:
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn(TransactionCurrency::find(1))->once();
$piggyFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('find')->withArgs([1, 'Bla bla'])->andReturn(null);
$accountRepos->shouldReceive('setUser')->twice();
$accountRepos->shouldReceive('findNull')->twice()->andReturn($accountA, $accountB);
$currencyFactory->shouldReceive('find')->once()->withArgs([1, 'EUR'])->andReturn(null);
$currencyFactory->shouldReceive('find')->once()->withArgs([null, null])->andReturn(null);
$budgetFactory->shouldReceive('setUser')->once();
$budgetFactory->shouldReceive('find')->withArgs([1, 'Some budget'])->once()->andReturn($budget);
$categoryFactory->shouldReceive('setUser')->once();
$categoryFactory->shouldReceive('findOrCreate')->withArgs([2, 'Some category'])->once()->andReturn($category);
// data for basic recurrence.
$data = [
'recurrence' => [
'type' => 'withdrawal',
'first_date' => Carbon::create()->addDay(),
'repetitions' => 0,
'title' => 'Test recurrence' . random_int(1, 100000),
'description' => 'Description thing',
'apply_rules' => true,
'active' => true,
'repeat_until' => null,
],
'meta' => [
'tags' => ['a', 'b', 'c'],
'piggy_bank_id' => 1,
'piggy_bank_name' => 'Bla bla',
],
'repetitions' => [
[
'type' => 'daily',
'moment' => '',
'skip' => 0,
'weekend' => 1,
],
],
'transactions' => [
[
'source_id' => 1,
'source_name' => 'Some name',
'destination_id' => 2,
'destination_name' => 'some otjer name',
'currency_id' => 1,
'currency_code' => 'EUR',
'foreign_currency_id' => null,
'foreign_currency_code' => null,
'foreign_amount' => null,
'description' => 'Bla bla bla',
'amount' => '100',
'budget_id' => 1,
'budget_name' => 'Some budget',
'category_id' => 2,
'category_name' => 'Some category',
],
],
];
$typeFactory = $this->mock(TransactionTypeFactory::class);
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(TransactionType::find(1));
$factory = new RecurrenceFactory;
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertEquals($result->title, $data['recurrence']['title']);
}
/**
* @covers \FireflyIII\Factory\RecurrenceFactory
*/
public function testCreateBadTransactionType(): void
{
$data = [
'recurrence' => [
'type' => 'bad type',
],
];
$typeFactory = $this->mock(TransactionTypeFactory::class);
$typeFactory->shouldReceive('find')->once()->withArgs([ucfirst($data['recurrence']['type'])])->andReturn(null);
$factory = new RecurrenceFactory;
$factory->setUser($this->user());
$result = $factory->create($data);
$this->assertNull($result);
}
}

View File

@@ -25,6 +25,7 @@ namespace Tests\Unit\Factory;
use FireflyIII\Factory\TagFactory;
use Log;
use Tests\TestCase;
/**
@@ -32,6 +33,16 @@ use Tests\TestCase;
*/
class TagFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Factory\TagFactory
*/

View File

@@ -26,6 +26,7 @@ namespace Tests\Unit\Factory;
use FireflyIII\Factory\TransactionCurrencyFactory;
use FireflyIII\Models\TransactionCurrency;
use Log;
use Tests\TestCase;
/**
@@ -33,6 +34,16 @@ use Tests\TestCase;
*/
class TransactionCurrencyFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Factory\TransactionCurrencyFactory
*/

View File

@@ -35,6 +35,7 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Log;
use Tests\TestCase;
/**
@@ -42,6 +43,16 @@ use Tests\TestCase;
*/
class TransactionFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Factory\TransactionFactory
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait
@@ -617,6 +628,70 @@ class TransactionFactoryTest extends TestCase
$this->assertNull($first->foreign_currency_id);
}
/**
* @covers \FireflyIII\Factory\TransactionFactory
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait
*/
public function testCreatePairEmptyAmount(): void
{
// objects:
$asset = $this->user()->accounts()->where('account_type_id', 3)->first();
$expense = $this->user()->accounts()->where('account_type_id', 4)->first();
$euro = TransactionCurrency::first();
// mocked classes
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetFactory = $this->mock(BudgetFactory::class);
$categoryFactory = $this->mock(CategoryFactory::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
$data = [
'currency_id' => 1,
'currency_code' => null,
'description' => null,
'source_id' => $asset->id,
'source_name' => null,
'destination_id' => $expense->id,
'destination_name' => null,
'amount' => '',
'reconciled' => false,
'identifier' => 0,
'foreign_currency_id' => null,
'foreign_currency_code' => null,
'foreign_amount' => null,
'budget_id' => null,
'budget_name' => null,
'category_id' => null,
'category_name' => null,
];
// mock:
$accountRepos->shouldReceive('setUser');
$budgetFactory->shouldReceive('setUser');
$categoryFactory->shouldReceive('setUser');
// first search action is for the asset account, second is for expense account.
$accountRepos->shouldReceive('findNull')->andReturn($asset, $expense)->atLeast()->once();
// factories return various stuff:
$currencyFactory->shouldReceive('find')->andReturn($euro, null)->atLeast()->once();
/** @var TransactionJournal $withdrawal */
$withdrawal = $this->user()->transactionJournals()->where('transaction_type_id', 1)->first();
$count = $withdrawal->transactions()->count();
/** @var TransactionFactory $factory */
$factory = app(TransactionFactory::class);
$factory->setUser($this->user());
try {
$factory->createPair($withdrawal, $data);
} catch (FireflyException $e) {
$this->assertEquals('Amount is an empty string, which Firefly III cannot handle. Apologies.', $e->getMessage());
}
$newCount = $withdrawal->transactions()->count();
$this->assertEquals($count, $newCount);
}
/**
* @covers \FireflyIII\Factory\TransactionFactory
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait
@@ -695,6 +770,135 @@ class TransactionFactoryTest extends TestCase
$this->assertEquals($foreign->id, $first->foreign_currency_id);
}
/**
* @covers \FireflyIII\Factory\TransactionFactory
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait
*/
public function testCreatePairNoAccounts(): void
{
// objects:
$asset = $this->user()->accounts()->where('account_type_id', 3)->first();
$expense = $this->user()->accounts()->where('account_type_id', 4)->first();
$euro = TransactionCurrency::first();
// mocked classes
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetFactory = $this->mock(BudgetFactory::class);
$categoryFactory = $this->mock(CategoryFactory::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
$data = [
'currency_id' => 1,
'currency_code' => null,
'description' => null,
'source_id' => $asset->id,
'source_name' => null,
'destination_id' => $expense->id,
'destination_name' => null,
'amount' => '10',
'reconciled' => false,
'identifier' => 0,
'foreign_currency_id' => null,
'foreign_currency_code' => null,
'foreign_amount' => null,
'budget_id' => null,
'budget_name' => null,
'category_id' => null,
'category_name' => null,
];
// mock:
$accountRepos->shouldReceive('setUser');
$budgetFactory->shouldReceive('setUser');
$categoryFactory->shouldReceive('setUser');
// first search action is for the asset account, second is for expense account.
$accountRepos->shouldReceive('findNull')->andReturn(null, null)->atLeast()->once();
// factories return various stuff:
$currencyFactory->shouldReceive('find')->andReturn($euro, null)->atLeast()->once();
/** @var TransactionJournal $withdrawal */
$withdrawal = $this->user()->transactionJournals()->where('transaction_type_id', 1)->first();
$count = $withdrawal->transactions()->count();
/** @var TransactionFactory $factory */
$factory = app(TransactionFactory::class);
$factory->setUser($this->user());
try {
$factory->createPair($withdrawal, $data);
} catch (FireflyException $e) {
$this->assertEquals('Could not determine source or destination account.', $e->getMessage());
}
$newCount = $withdrawal->transactions()->count();
$this->assertEquals($count, $newCount);
}
/**
* @covers \FireflyIII\Factory\TransactionFactory
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait
*/
public function testCreatePairNoCurrency(): void
{
// objects:
$asset = $this->user()->accounts()->where('account_type_id', 3)->first();
$expense = $this->user()->accounts()->where('account_type_id', 4)->first();
// mocked classes
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetFactory = $this->mock(BudgetFactory::class);
$categoryFactory = $this->mock(CategoryFactory::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
$data = [
'description' => null,
'source_id' => $asset->id,
'source_name' => null,
'destination_id' => $expense->id,
'currency_id' => null,
'currency_code' => null,
'destination_name' => null,
'amount' => '10',
'reconciled' => false,
'identifier' => 0,
'foreign_currency_id' => null,
'foreign_currency_code' => null,
'foreign_amount' => null,
'budget_id' => null,
'budget_name' => null,
'category_id' => null,
'category_name' => null,
];
// mock:
$accountRepos->shouldReceive('setUser');
$budgetFactory->shouldReceive('setUser');
$categoryFactory->shouldReceive('setUser');
// first search action is for the asset account, second is for expense account.
$accountRepos->shouldReceive('findNull')->andReturn($asset, $expense)->atLeast()->once();
// factories return various stuff:
$currencyFactory->shouldReceive('find')->andReturn(null, null)->atLeast()->once();
/** @var TransactionJournal $withdrawal */
$withdrawal = $this->user()->transactionJournals()->where('transaction_type_id', 1)->first();
$count = $withdrawal->transactions()->count();
/** @var TransactionFactory $factory */
$factory = app(TransactionFactory::class);
$factory->setUser($this->user());
try {
$factory->createPair($withdrawal, $data);
} catch (FireflyException $e) {
$this->assertEquals('Cannot store transaction without currency information.', $e->getMessage());
}
$newCount = $withdrawal->transactions()->count();
$this->assertEquals($count, $newCount);
}
/**
* Create reconciliation using minimal data.
*
@@ -706,8 +910,8 @@ class TransactionFactoryTest extends TestCase
// objects:
$asset = $this->user()->accounts()->where('account_type_id', 3)->first();
$reconAccount = $this->user()->accounts()->where('account_type_id', 10)->first();
$euro = TransactionCurrency::first();
$foreign = TransactionCurrency::where('id', '!=', $euro->id)->first();
$euro = TransactionCurrency::first();
$foreign = TransactionCurrency::where('id', '!=', $euro->id)->first();
// mocked classes
$accountRepos = $this->mock(AccountRepositoryInterface::class);
@@ -776,6 +980,134 @@ class TransactionFactoryTest extends TestCase
$this->assertNull($first->foreign_currency_id);
}
/**
* @covers \FireflyIII\Factory\TransactionFactory
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait
*/
public function testCreatePairSameBadType(): void
{
// objects:
$expense = $this->user()->accounts()->where('account_type_id', 4)->first();
$revenue = $this->user()->accounts()->where('account_type_id', 5)->first();
$euro = TransactionCurrency::first();
// mocked classes
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetFactory = $this->mock(BudgetFactory::class);
$categoryFactory = $this->mock(CategoryFactory::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
$data = [
'currency_id' => 1,
'currency_code' => null,
'description' => null,
'source_id' => $expense->id,
'source_name' => null,
'destination_id' => $revenue->id,
'destination_name' => null,
'amount' => '10',
'reconciled' => false,
'identifier' => 0,
'foreign_currency_id' => null,
'foreign_currency_code' => null,
'foreign_amount' => null,
'budget_id' => null,
'budget_name' => null,
'category_id' => null,
'category_name' => null,
];
// mock:
$accountRepos->shouldReceive('setUser');
$budgetFactory->shouldReceive('setUser');
$categoryFactory->shouldReceive('setUser');
// first search action is for the asset account, second is for expense account.
$accountRepos->shouldReceive('findNull')->andReturn($expense, $revenue);
// factories return various stuff:
$currencyFactory->shouldReceive('find')->andReturn($euro, null)->atLeast()->once();
/** @var TransactionJournal $withdrawal */
$withdrawal = $this->user()->transactionJournals()->where('transaction_type_id', 1)->first();
$count = $withdrawal->transactions()->count();
/** @var TransactionFactory $factory */
$factory = app(TransactionFactory::class);
$factory->setUser($this->user());
try {
$factory->createPair($withdrawal, $data);
} catch (FireflyException $e) {
$this->assertEquals('At least one of the accounts must be an asset account.', $e->getMessage());
}
$newCount = $withdrawal->transactions()->count();
$this->assertEquals($count, $newCount);
}
/**
* @covers \FireflyIII\Factory\TransactionFactory
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait
*/
public function testCreatePairSameType(): void
{
// objects:
$asset = $this->user()->accounts()->where('account_type_id', 3)->first();
$alsoAsset = $this->user()->accounts()->where('account_type_id', 3)->first();
$euro = TransactionCurrency::first();
// mocked classes
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetFactory = $this->mock(BudgetFactory::class);
$categoryFactory = $this->mock(CategoryFactory::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
$data = [
'currency_id' => 1,
'currency_code' => null,
'description' => null,
'source_id' => $asset->id,
'source_name' => null,
'destination_id' => $alsoAsset->id,
'destination_name' => null,
'amount' => '10',
'reconciled' => false,
'identifier' => 0,
'foreign_currency_id' => null,
'foreign_currency_code' => null,
'foreign_amount' => null,
'budget_id' => null,
'budget_name' => null,
'category_id' => null,
'category_name' => null,
];
// mock:
$accountRepos->shouldReceive('setUser');
$budgetFactory->shouldReceive('setUser');
$categoryFactory->shouldReceive('setUser');
// first search action is for the asset account, second is for expense account.
$accountRepos->shouldReceive('findNull')->andReturn($asset, $alsoAsset);
// factories return various stuff:
$currencyFactory->shouldReceive('find')->andReturn($euro, null)->atLeast()->once();
/** @var TransactionJournal $withdrawal */
$withdrawal = $this->user()->transactionJournals()->where('transaction_type_id', 1)->first();
$count = $withdrawal->transactions()->count();
/** @var TransactionFactory $factory */
$factory = app(TransactionFactory::class);
$factory->setUser($this->user());
try {
$factory->createPair($withdrawal, $data);
} catch (FireflyException $e) {
$this->assertEquals('Source and destination account cannot be both of the type "Asset account"', $e->getMessage());
}
$newCount = $withdrawal->transactions()->count();
$this->assertEquals($count, $newCount);
}
/**
* Create reconciliation using minimal (bad) data.
*

View File

@@ -37,6 +37,7 @@ use FireflyIII\Factory\TransactionTypeFactory;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Log;
use Tests\TestCase;
/**
@@ -45,6 +46,15 @@ use Tests\TestCase;
class TransactionJournalFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Factory\TransactionJournalFactory
* @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
@@ -106,6 +116,71 @@ class TransactionJournalFactoryTest extends TestCase
}
/**
* @covers \FireflyIII\Factory\TransactionJournalFactory
* @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
*/
public function testCreateBasicEmptyAmount(): void
{
// mock used classes:
$type = TransactionType::find(1);
$euro = TransactionCurrency::find(1);
$billFactory = $this->mock(BillFactory::class);
$tagFactory = $this->mock(TagFactory::class);
$metaFactory = $this->mock(TransactionJournalMetaFactory::class);
$typeFactory = $this->mock(TransactionTypeFactory::class);
$transactionFactory = $this->mock(TransactionFactory::class);
$piggyFactory = $this->mock(PiggyBankFactory::class);
$eventFactory = $this->mock(PiggyBankEventFactory::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
// mock stuff:
$typeFactory->shouldReceive('find')->andReturn($type);
$currencyRepos->shouldReceive('find')->andReturn($euro);
$metaFactory->shouldReceive('updateOrCreate');
// mock factories:
$transactionFactory->shouldReceive('setUser')->once();
$billFactory->shouldReceive('setUser')->once();
$piggyFactory->shouldReceive('setUser')->once();
$tagFactory->shouldReceive('setUser')->once();
$transactionFactory->shouldReceive('createPair')->once();
$billFactory->shouldReceive('find')->andReturn(null);
$piggyFactory->shouldReceive('find')->andReturn(null);
$data = [
'type' => 'withdrawal',
'user' => $this->user()->id,
'description' => 'I are journal',
'date' => new Carbon('2018-01-01'),
'bill_id' => null,
'bill_name' => null,
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'notes' => 'Hello',
'tags' => [],
'transactions' => [
[
'amount' => '',
]
],
];
/** @var TransactionJournalFactory $factory */
$factory = app(TransactionJournalFactory::class);
$factory->setUser($this->user());
try {
$journal = $factory->create($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals($data['description'], $journal->description);
$this->assertEquals('2018-01-01', $journal->date->format('Y-m-d'));
$this->assertEquals(1, $journal->notes()->count());
}
/**
* Same but with added meta data

View File

@@ -27,6 +27,7 @@ use Carbon\Carbon;
use FireflyIII\Factory\TransactionJournalMetaFactory;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionJournalMeta;
use Log;
use Tests\TestCase;
/**
@@ -34,6 +35,16 @@ use Tests\TestCase;
*/
class TransactionJournalMetaFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Factory\TransactionJournalMetaFactory
*/

View File

@@ -0,0 +1,62 @@
<?php
/**
* TransactionTypeFactoryTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Unit\Factory;
use FireflyIII\Factory\TransactionTypeFactory;
use FireflyIII\Models\TransactionType;
use Log;
use Tests\TestCase;
/**
*
* Class TransactionTypeFactoryTest
*/
class TransactionTypeFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Factory\TransactionTypeFactory
*/
public function testFind(): void
{
/** @var TransactionType $type */
$type = TransactionType::first();
/** @var TransactionTypeFactory $factory */
$factory = app(TransactionTypeFactory::class);
$result = $factory->find($type->type);
$this->assertEquals($result->id, $type->id);
}
}

View File

@@ -0,0 +1,156 @@
<?php
/**
* ChartJsGeneratorTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Unit\Generator\Chart\Basic;
use FireflyIII\Generator\Chart\Basic\ChartJsGenerator;
use Log;
use Tests\TestCase;
/**
*
* Class ChartJsGeneratorTest
*/
class ChartJsGeneratorTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Generator\Chart\Basic\ChartJsGenerator
*/
public function testBasic(): void
{
$data = [
[
'label' => 'Today',
'fill' => '#abcdef',
'yAxisID' => 'a',
'entries' => [
'one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
'five' => 5,
],
],
[
'currency_symbol' => 'X',
'backgroundColor' => '#123456',
'label' => 'Tomorrow',
'entries' => [
'one' => 6,
'two' => 7,
'three' => 8,
'four' => 9,
'five' => 10,
],
],
];
/** @var ChartJsGenerator $generator */
$generator = new ChartJsGenerator();
$result = $generator->multiSet($data);
$this->assertEquals('one', $result['labels'][0]);
$this->assertEquals(2, $result['count']);
$this->assertCount(2, $result['datasets']);
$this->assertEquals('a', $result['datasets'][0]['yAxisID']);
$this->assertEquals('#abcdef', $result['datasets'][0]['fill']);
$this->assertEquals('X', $result['datasets'][1]['currency_symbol']);
$this->assertEquals('#123456', $result['datasets'][1]['backgroundColor']);
}
/**
* @covers \FireflyIII\Generator\Chart\Basic\ChartJsGenerator
*/
public function testPieChart(): void
{
$data = [
'one' => -1,
'two' => -2,
'three' => -3,
];
/** @var ChartJsGenerator $generator */
$generator = new ChartJsGenerator();
$result = $generator->pieChart($data);
$this->assertEquals('three', $result['labels'][0]);
$this->assertEquals(3.0, $result['datasets'][0]['data'][0]);
}
/**
* @covers \FireflyIII\Generator\Chart\Basic\ChartJsGenerator
*/
public function testPieChartReversed(): void
{
$data = [
'one' => 1,
'two' => 2,
'three' => 3,
];
/** @var ChartJsGenerator $generator */
$generator = new ChartJsGenerator();
$result = $generator->pieChart($data);
$this->assertEquals('three', $result['labels'][0]);
$this->assertEquals(3.0, $result['datasets'][0]['data'][0]);
}
/**
* @covers \FireflyIII\Generator\Chart\Basic\ChartJsGenerator
*/
public function testSingleSet(): void
{
$data = [
'one' => '1',
'two' => '2',
'three' => '3',
];
/** @var ChartJsGenerator $generator */
$generator = new ChartJsGenerator();
$result = $generator->singleSet('Some label', $data);
$this->assertEquals('one', $result['labels'][0]);
$this->assertEquals(1.0, $result['datasets'][0]['data'][0]);
}
}

View File

@@ -146,9 +146,10 @@ class ImportableConverterTest extends TestCase
$assetMapper->shouldReceive('map')->once()->withArgs([null, $nullAccount])->andReturn($asset);
$opposingMapper->shouldReceive('map')->once()->withArgs([null, '45.67', $nullAccount])->andReturn($other);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['name' => null, 'code' => null, 'symbol' => null]])->andReturn(null);
$currencyMapper->shouldReceive('map')->once()->withArgs([null, ['code' => null]])->andReturn(null);
$currencyMapper->shouldReceive('map')->times(2)->withArgs([$euro->id, []])->andReturn($euro);
$currencyMapper->shouldReceive('map')->times(1)->withArgs([$euro->id, []])->andReturn($euro);
$converter = new ImportableConverter;