Skip to content

Firefly III: a personal finances manager

Improve test results.

James Cole214c7a6f3e6f
diff --git a/app/Api/V1/Controllers/TransactionController.php b/app/Api/V1/Controllers/TransactionController.php
index 0cff34a0c8..1d73833931 100644
--- a/app/Api/V1/Controllers/TransactionController.php
+++ b/app/Api/V1/Controllers/TransactionController.php
@@ -26,6 +26,7 @@ namespace FireflyIII\Api\V1\Controllers;
use FireflyIII\Api\V1\Requests\TransactionRequest;
use FireflyIII\Events\StoredTransactionJournal;
+use FireflyIII\Events\UpdatedTransactionJournal;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Helpers\Filter\InternalTransferFilter;
@@ -244,6 +245,8 @@ class TransactionController extends Controller
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
+ event(new UpdatedTransactionJournal($journal));
+
// add include parameter:
$include = $request->get('include') ?? '';
$manager->parseIncludes($include);
diff --git a/app/Factory/AccountFactory.php b/app/Factory/AccountFactory.php
index 3c22db43be..b634671dd2 100644
--- a/app/Factory/AccountFactory.php
+++ b/app/Factory/AccountFactory.php
@@ -41,6 +41,11 @@ use Log;
*/
class AccountFactory
{
+ /** @var User */
+ private $user;
+
+ use AccountServiceTrait;
+
/**
* Constructor.
*/
@@ -51,10 +56,6 @@ class AccountFactory
}
}
- use AccountServiceTrait;
- /** @var User */
- private $user;
-
/**
* @param array $data
*
@@ -151,17 +152,23 @@ class AccountFactory
*/
public function findOrCreate(string $accountName, string $accountType): Account
{
+ Log::debug(sprintf('Searching for "%s" of type "%s"', $accountName, $accountType));
$type = AccountType::whereType($accountType)->first();
$accounts = $this->user->accounts()->where('account_type_id', $type->id)->get(['accounts.*']);
$return = null;
+
+ Log::debug(sprintf('Account type is #%d', $type->id));
+
/** @var Account $object */
foreach ($accounts as $object) {
if ($object->name === $accountName) {
+ Log::debug(sprintf('Found account #%d "%s".', $object->id, $object->name));
$return = $object;
break;
}
}
if (null === $return) {
+ Log::debug('Found nothing. Will create a new one.');
$return = $this->create(
[
'user_id' => $this->user->id,
diff --git a/tests/Api/V1/Controllers/TransactionControllerTest.php b/tests/Api/V1/Controllers/TransactionControllerTest.php
index aa66cc836e..8d3f3f9312 100644
--- a/tests/Api/V1/Controllers/TransactionControllerTest.php
+++ b/tests/Api/V1/Controllers/TransactionControllerTest.php
@@ -24,6 +24,9 @@ declare(strict_types=1);
namespace Tests\Api\V1\Controllers;
+use Exception;
+use FireflyIII\Events\StoredTransactionJournal;
+use FireflyIII\Events\UpdatedTransactionJournal;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\TransactionCollector;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
@@ -1459,6 +1462,12 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$bill = $this->user()->bills()->first();
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
@@ -1500,6 +1509,12 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$bill = $this->user()->bills()->first();
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
@@ -1540,6 +1555,12 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -1581,6 +1602,12 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -1620,6 +1647,12 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -1660,6 +1693,12 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('findByName')->andReturn($account);
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
@@ -1700,6 +1739,12 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -1738,6 +1783,13 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -1778,6 +1830,13 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -1817,6 +1876,13 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -1856,6 +1922,13 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -1892,6 +1965,12 @@ class TransactionControllerTest extends TestCase
$journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$journalRepos->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
@@ -1935,6 +2014,13 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -1981,6 +2067,12 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$name = 'Some new category #' . random_int(1, 10000);
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
@@ -2021,6 +2113,14 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]), new Collection([$opposing]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
+
$name = 'New opposing account #' . random_int(1, 10000);
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
@@ -2061,6 +2161,12 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -2100,6 +2206,13 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]), new Collection([$opposing]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -2140,6 +2253,12 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]), new Collection([$opposing]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -2179,6 +2298,13 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$piggy = $this->user()->piggyBanks()->first();
$data = [
'description' => 'Some deposit #' . random_int(1, 10000),
@@ -2220,6 +2346,12 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$source]), new Collection([$dest]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$piggy = $this->user()->piggyBanks()->first();
$data = [
'description' => 'Some transfer #' . random_int(1, 10000),
@@ -2260,6 +2392,12 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$source]), new Collection([$dest]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$piggy = $this->user()->piggyBanks()->first();
$data = [
'description' => 'Some transfer #' . random_int(1, 10000),
@@ -2297,6 +2435,13 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -2335,6 +2480,13 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -2387,6 +2539,13 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('store')->andReturn($journal)->once();
+
+ try {
+ $this->expectsEvents(StoredTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
@@ -2434,6 +2593,13 @@ class TransactionControllerTest extends TestCase
],
],
];
+
+ try {
+ $this->expectsEvents(UpdatedTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$deposit = $this->getRandomDeposit();
$transaction = $deposit->transactions()->first();
$repository->shouldReceive('setUser');
@@ -2460,6 +2626,12 @@ class TransactionControllerTest extends TestCase
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsById')->withArgs([[$account->id]])->andReturn(new Collection([$account]));
+ try {
+ $this->expectsEvents(UpdatedTransactionJournal::class);
+ } catch (Exception $e) {
+ $this->assertTrue(false, $e->getMessage());
+ }
+
$data = [
'description' => 'Some transaction #' . random_int(1, 10000),
'date' => '2018-01-01',
diff --git a/tests/TestCase.php b/tests/TestCase.php
index 8d7d7255ca..a00b304211 100644
--- a/tests/TestCase.php
+++ b/tests/TestCase.php
@@ -225,7 +225,7 @@ abstract class TestCase extends BaseTestCase
->where('accounts.user_id', $this->user()->id)
->where('account_types.type', $type)
->inRandomOrder()->take(1);
- $result = $query->first();
+ $result = $query->first(['accounts.*']);
return $result;
}
diff --git a/tests/Unit/Factory/AccountFactoryTest.php b/tests/Unit/Factory/AccountFactoryTest.php
index 2642236336..b014718e3c 100644
--- a/tests/Unit/Factory/AccountFactoryTest.php
+++ b/tests/Unit/Factory/AccountFactoryTest.php
@@ -574,10 +574,11 @@ class AccountFactoryTest extends TestCase
public function testFindOrCreate(): void
{
/** @var Account $account */
- $account = $this->getRandomAsset();
+ $account = $this->getRandomRevenue();
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
+ Log::debug(sprintf('Searching for account #%d with name "%s"', $account->id, $account->name));
$result = $factory->findOrCreate($account->name, $account->accountType->type);
$this->assertEquals($result->id, $account->id);