Skip to content

Firefly III: a personal finances manager

Refactor journal repository and fix tests.

James Cole93f1854be0cb
diff --git a/app/Api/V1/Controllers/TransactionController.php b/app/Api/V1/Controllers/TransactionController.php
index 3d4d3aecf5..f30dce2782 100644
--- a/app/Api/V1/Controllers/TransactionController.php
+++ b/app/Api/V1/Controllers/TransactionController.php
@@ -32,6 +32,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
+use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
use FireflyIII\Support\Http\Api\TransactionFilter;
@@ -61,6 +62,9 @@ class TransactionController extends Controller
/** @var JournalRepositoryInterface The journal repository */
private $repository;
+ /** @var JournalAPIRepositoryInterface Journal API repos */
+ private $journalAPIRepository;
+
/**
* TransactionController constructor.
*
@@ -74,10 +78,12 @@ class TransactionController extends Controller
/** @var User $admin */
$admin = auth()->user();
- $this->repository = app(JournalRepositoryInterface::class);
- $this->groupRepository = app(TransactionGroupRepositoryInterface::class);
+ $this->repository = app(JournalRepositoryInterface::class);
+ $this->groupRepository = app(TransactionGroupRepositoryInterface::class);
+ $this->journalAPIRepository = app(JournalAPIRepositoryInterface::class);
$this->repository->setUser($admin);
$this->groupRepository->setUser($admin);
+ $this->journalAPIRepository->setUser($admin);
return $next($request);
}
@@ -97,7 +103,7 @@ class TransactionController extends Controller
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
- $attachments = $this->repository->getAttachments($transactionJournal);
+ $attachments = $this->journalAPIRepository->getAttachments($transactionJournal);
/** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class);
@@ -206,7 +212,7 @@ class TransactionController extends Controller
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
- $events = $this->repository->getPiggyBankEvents($transactionJournal);
+ $events = $this->journalAPIRepository->getPiggyBankEvents($transactionJournal);
/** @var PiggyBankEventTransformer $transformer */
$transformer = app(PiggyBankEventTransformer::class);
diff --git a/app/Factory/PiggyBankFactory.php b/app/Factory/PiggyBankFactory.php
index 42b1330de0..e0d98fdc6f 100644
--- a/app/Factory/PiggyBankFactory.php
+++ b/app/Factory/PiggyBankFactory.php
@@ -90,7 +90,7 @@ class PiggyBankFactory
*/
public function findByName(string $name): ?PiggyBank
{
- return $this->user->piggyBanks()->where('name', $name)->first();
+ return $this->user->piggyBanks()->where('piggy_banks.name', $name)->first();
}
/**
diff --git a/app/Providers/JournalServiceProvider.php b/app/Providers/JournalServiceProvider.php
index 3b64273b14..d5bbcacc19 100644
--- a/app/Providers/JournalServiceProvider.php
+++ b/app/Providers/JournalServiceProvider.php
@@ -24,6 +24,8 @@ namespace FireflyIII\Providers;
use FireflyIII\Helpers\Collector\GroupCollector;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
+use FireflyIII\Repositories\Journal\JournalAPIRepository;
+use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepository;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepository;
@@ -109,5 +111,19 @@ class JournalServiceProvider extends ServiceProvider
return $repository;
}
);
+
+ // also bind new API repository
+ $this->app->bind(
+ JournalAPIRepositoryInterface::class,
+ function (Application $app) {
+ /** @var JournalAPIRepositoryInterface $repository */
+ $repository = app(JournalAPIRepository::class);
+ if ($app->auth->check()) {
+ $repository->setUser(auth()->user());
+ }
+
+ return $repository;
+ }
+ );
}
}
diff --git a/app/Repositories/Journal/JournalAPIRepository.php b/app/Repositories/Journal/JournalAPIRepository.php
new file mode 100644
index 0000000000..0539760db0
--- /dev/null
+++ b/app/Repositories/Journal/JournalAPIRepository.php
@@ -0,0 +1,105 @@
+<?php
+/**
+ * JournalAPIRepository.php
+ * Copyright (c) 2019 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Repositories\Journal;
+
+use FireflyIII\Models\PiggyBankEvent;
+use FireflyIII\Models\Transaction;
+use FireflyIII\Models\TransactionJournal;
+use FireflyIII\User;
+use Illuminate\Support\Collection;
+use Log;
+
+/**
+ * Class JournalAPIRepository
+ */
+class JournalAPIRepository implements JournalAPIRepositoryInterface
+{
+ /** @var User */
+ private $user;
+
+ /**
+ * Constructor.
+ */
+ public function __construct()
+ {
+ if ('testing' === config('app.env')) {
+ Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
+ }
+ }
+
+ /**
+ * Returns transaction by ID. Used to validate attachments.
+ *
+ * @param int $transactionId
+ *
+ * @return Transaction|null
+ */
+ public function findTransaction(int $transactionId): ?Transaction
+ {
+ $transaction = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
+ ->where('transaction_journals.user_id', $this->user->id)
+ ->where('transactions.id', $transactionId)
+ ->first(['transactions.*']);
+
+ return $transaction;
+ }
+
+ /**
+ * Return all attachments for journal.
+ *
+ * @param TransactionJournal $journal
+ *
+ * @return Collection
+ */
+ public function getAttachments(TransactionJournal $journal): Collection
+ {
+ return $journal->attachments;
+ }
+
+ /**
+ * Get all piggy bank events for a journal.
+ *
+ * @param TransactionJournal $journal
+ *
+ * @return Collection
+ */
+ public function getPiggyBankEvents(TransactionJournal $journal): Collection
+ {
+ /** @var Collection $set */
+ $events = $journal->piggyBankEvents()->get();
+ $events->each(
+ function (PiggyBankEvent $event) {
+ $event->piggyBank = $event->piggyBank()->withTrashed()->first();
+ }
+ );
+
+ return $events;
+ }
+
+ /**
+ * @param User $user
+ */
+ public function setUser(User $user): void
+ {
+ $this->user = $user;
+ }
+}
\ No newline at end of file
diff --git a/app/Repositories/Journal/JournalAPIRepositoryInterface.php b/app/Repositories/Journal/JournalAPIRepositoryInterface.php
new file mode 100644
index 0000000000..db85122487
--- /dev/null
+++ b/app/Repositories/Journal/JournalAPIRepositoryInterface.php
@@ -0,0 +1,65 @@
+<?php
+/**
+ * JournalAPIRepositoryInterface.php
+ * Copyright (c) 2019 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Repositories\Journal;
+
+use FireflyIII\Models\Transaction;
+use FireflyIII\Models\TransactionJournal;
+use FireflyIII\User;
+use Illuminate\Support\Collection;
+
+/**
+ * Interface JournalAPIRepositoryInterface
+ */
+interface JournalAPIRepositoryInterface
+{
+ /**
+ * Returns transaction by ID. Used to validate attachments.
+ *
+ * @param int $transactionId
+ *
+ * @return Transaction|null
+ */
+ public function findTransaction(int $transactionId): ?Transaction;
+
+ /**
+ * Return all attachments for journal.
+ *
+ * @param TransactionJournal $journal
+ *
+ * @return Collection
+ */
+ public function getAttachments(TransactionJournal $journal): Collection;
+
+ /**
+ * Get all piggy bank events for a journal.
+ *
+ * @param TransactionJournal $journal
+ *
+ * @return Collection
+ */
+ public function getPiggyBankEvents(TransactionJournal $journal): Collection;
+
+ /**
+ * @param User $user
+ */
+ public function setUser(User $user);
+}
\ No newline at end of file
diff --git a/app/Repositories/Journal/JournalCLIRepositoryInterface.php b/app/Repositories/Journal/JournalCLIRepositoryInterface.php
new file mode 100644
index 0000000000..ed08f09e98
--- /dev/null
+++ b/app/Repositories/Journal/JournalCLIRepositoryInterface.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * JournalCLIRepositoryInterface.php
+ * Copyright (c) 2019 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Repositories\Journal;
+
+use FireflyIII\User;
+
+/**
+ * Interface JournalCLIRepositoryInterface
+ */
+interface JournalCLIRepositoryInterface
+{
+ /**
+ * @param User $user
+ */
+ public function setUser(User $user);
+
+}
\ No newline at end of file
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index 6cbb046064..b8be7d59fa 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -47,10 +47,6 @@ use stdClass;
/**
* Class JournalRepository.
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @SuppressWarnings(PHPMD.TooManyPublicMethods)
- * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class JournalRepository implements JournalRepositoryInterface
{
@@ -146,21 +142,6 @@ class JournalRepository implements JournalRepositoryInterface
return $this->user->transactionJournals()->where('id', $journalId)->first();
}
- /**
- * @param int $transactionid
- *
- * @return Transaction|null
- */
- public function findTransaction(int $transactionid): ?Transaction
- {
- $transaction = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
- ->where('transaction_journals.user_id', $this->user->id)
- ->where('transactions.id', $transactionid)
- ->first(['transactions.*']);
-
- return $transaction;
- }
-
/**
* Get users first transaction journal or NULL.
*
@@ -178,31 +159,6 @@ class JournalRepository implements JournalRepositoryInterface
return $result;
}
- /**
- * Return all attachments for journal.
- *
- * @param TransactionJournal $journal
- *
- * @return Collection
- */
- public function getAttachments(TransactionJournal $journal): Collection
- {
- return $journal->attachments;
- }
-
- /**
- * Get all attachments connected to the transaction group.
- *
- * @param TransactionJournal $transactionJournal
- *
- * @return Collection
- */
- public function getAttachmentsByJournal(TransactionJournal $transactionJournal): Collection
- {
- // TODO: Implement getAttachmentsByJournal() method.
- throw new NotImplementedException;
- }
-
/**
* Return the ID of the budget linked to the journal (if any) or the transactions (if any).
*
@@ -447,23 +403,7 @@ class JournalRepository implements JournalRepositoryInterface
return $note->text;
}
- /**
- * @param TransactionJournal $journal
- *
- * @return Collection
- */
- public function getPiggyBankEvents(TransactionJournal $journal): Collection
- {
- /** @var Collection $set */
- $events = $journal->piggyBankEvents()->get();
- $events->each(
- function (PiggyBankEvent $event) {
- $event->piggyBank = $event->piggyBank()->withTrashed()->first();
- }
- );
- return $events;
- }
/**
* Returns all journals with more than 2 transactions. Should only return empty collections
diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php
index 64db01974f..4a857299ae 100644
--- a/app/Repositories/Journal/JournalRepositoryInterface.php
+++ b/app/Repositories/Journal/JournalRepositoryInterface.php
@@ -23,16 +23,13 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Journal;
use Carbon\Carbon;
-use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionJournalLink;
use FireflyIII\Models\TransactionJournalMeta;
-use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Support\Collection;
-use Illuminate\Support\MessageBag;
/**
* Interface JournalRepositoryInterface.
@@ -97,15 +94,6 @@ interface JournalRepositoryInterface
*/
public function findNull(int $journalId): ?TransactionJournal;
- /**
- * TODO maybe create API repository?
- *
- * @param int $transactionid
- *
- * @return Transaction|null
- */
- public function findTransaction(int $transactionid): ?Transaction;
-
/**
* Get users very first transaction journal.
*
@@ -113,17 +101,6 @@ interface JournalRepositoryInterface
*/
public function firstNull(): ?TransactionJournal;
- /**
- * TODO maybe create API repository?
- *
- * Return all attachments for journal.
- *
- * @param TransactionJournal $journal
- *
- * @return Collection
- */
- public function getAttachments(TransactionJournal $journal): Collection;
-
/**
* TODO console repository?
*
@@ -235,15 +212,6 @@ interface JournalRepositoryInterface
*/
public function getNoteText(TransactionJournal $journal): ?string;
- /**
- * TODO used only in the API
- *
- * @param TransactionJournal $journal
- *
- * @return Collection
- */
- public function getPiggyBankEvents(TransactionJournal $journal): Collection;
-
/**
* TODO used only on the console.
*
diff --git a/app/Rules/IsValidAttachmentModel.php b/app/Rules/IsValidAttachmentModel.php
index 5cfec6935e..33ff7c0d27 100644
--- a/app/Rules/IsValidAttachmentModel.php
+++ b/app/Rules/IsValidAttachmentModel.php
@@ -30,6 +30,7 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
+use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\User;
use Illuminate\Contracts\Validation\Rule;
@@ -106,8 +107,9 @@ class IsValidAttachmentModel implements Rule
}
if (Transaction::class === $this->model) {
- /** @var JournalRepositoryInterface $repository */
- $repository = app(JournalRepositoryInterface::class);
+ /** @var JournalAPIRepositoryInterface $repository */
+ $repository = app(JournalAPIRepositoryInterface::class);
+
/** @var User $user */
$user = auth()->user();
$repository->setUser($user);
diff --git a/tests/Api/V1/Controllers/TransactionControllerTest.php b/tests/Api/V1/Controllers/TransactionControllerTest.php
index 1082ba252e..fe1f4ff429 100644
--- a/tests/Api/V1/Controllers/TransactionControllerTest.php
+++ b/tests/Api/V1/Controllers/TransactionControllerTest.php
@@ -28,6 +28,7 @@ use Exception;
use FireflyIII\Events\StoredTransactionGroup;
use FireflyIII\Events\UpdatedTransactionGroup;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
+use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
use FireflyIII\Transformers\TransactionGroupTransformer;
@@ -69,11 +70,13 @@ class TransactionControllerTest extends TestCase
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
$validator = $this->mock(AccountValidator::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
@@ -120,10 +123,12 @@ class TransactionControllerTest extends TestCase
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$validator = $this->mock(AccountValidator::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
@@ -173,10 +178,12 @@ class TransactionControllerTest extends TestCase
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$validator = $this->mock(AccountValidator::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
@@ -224,10 +231,13 @@ class TransactionControllerTest extends TestCase
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$validator = $this->mock(AccountValidator::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
+
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
@@ -275,10 +285,12 @@ class TransactionControllerTest extends TestCase
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$validator = $this->mock(AccountValidator::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
@@ -327,10 +339,12 @@ class TransactionControllerTest extends TestCase
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$validator = $this->mock(AccountValidator::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
@@ -381,11 +395,12 @@ class TransactionControllerTest extends TestCase
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$this->mock(AccountValidator::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
-
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
$data = [
'transactions' => [
],
@@ -420,10 +435,12 @@ class TransactionControllerTest extends TestCase
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$validator = $this->mock(AccountValidator::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
@@ -480,10 +497,12 @@ class TransactionControllerTest extends TestCase
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$validator = $this->mock(AccountValidator::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
@@ -542,10 +561,12 @@ class TransactionControllerTest extends TestCase
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$validator = $this->mock(AccountValidator::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['transfer'])->atLeast()->once();
@@ -609,11 +630,13 @@ class TransactionControllerTest extends TestCase
$transformer = $this->mock(TransactionGroupTransformer::class);
$validator = $this->mock(AccountValidator::class);
$collector = $this->mock(GroupCollectorInterface::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
$repository->shouldReceive('setUser')->atLeast()->once();
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
@@ -689,10 +712,12 @@ class TransactionControllerTest extends TestCase
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$validator = $this->mock(AccountValidator::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
$validator->shouldReceive('setTransactionType')->withArgs(['invalid'])->atLeast()->once();
$validator->shouldReceive('validateSource')->withArgs([null, null])->atLeast()->once()->andReturn(true);
@@ -750,6 +775,7 @@ class TransactionControllerTest extends TestCase
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$validator = $this->mock(AccountValidator::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
$validator->shouldReceive('setTransactionType')->withArgs(['deposit'])->atLeast()->once();
@@ -759,6 +785,7 @@ class TransactionControllerTest extends TestCase
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
$data = [
'group_title' => 'Empty',
@@ -815,6 +842,7 @@ class TransactionControllerTest extends TestCase
$transformer = $this->mock(TransactionGroupTransformer::class);
$validator = $this->mock(AccountValidator::class);
$collector = $this->mock(GroupCollectorInterface::class);
+ $apiRepos = $this->mock(JournalAPIRepositoryInterface::class);
$validator->shouldReceive('setTransactionType')->withArgs(['invalid'])->atLeast()->once();
$validator->shouldReceive('validateSource')->withArgs([null, null])->atLeast()->once()->andReturn(true);
@@ -824,6 +852,7 @@ class TransactionControllerTest extends TestCase
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
$repository->shouldReceive('setUser')->atLeast()->once();
+ $apiRepos->shouldReceive('setUser')->atLeast()->once();
// call stuff:
$repository->shouldReceive('update')->atLeast()->once()->andReturn($group);
diff --git a/tests/Unit/Factory/AccountFactoryTest.php b/tests/Unit/Factory/AccountFactoryTest.php
index e034d9061b..1cc9045205 100644
--- a/tests/Unit/Factory/AccountFactoryTest.php
+++ b/tests/Unit/Factory/AccountFactoryTest.php
@@ -410,7 +410,7 @@ class AccountFactoryTest extends TestCase
{
// mock repositories:
$accountRepos = $this->mock(AccountRepositoryInterface::class);
- $groupFactory = $this->mock(TransactionGroupFactory::class);
+ $this->mock(TransactionGroupFactory::class);
$metaFactory = $this->mock(AccountMetaFactory::class);
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
$euro = $this->getEuro();
@@ -429,8 +429,6 @@ class AccountFactoryTest extends TestCase
// mock calls to the repository:
$accountRepos->shouldReceive('getOpeningBalanceGroup')->atLeast()->once()->andReturn(null);
- $groupFactory->shouldReceive('setUser')->atLeast()->once();
- $groupFactory->shouldReceive('create')->atLeast()->once();
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_role', 'defaultAsset'])->atLeast()->once()->andReturnNull();
$metaFactory->shouldReceive('crud')->withArgs([Mockery::any(), 'account_number', ''])->atLeast()->once()->andReturnNull();
diff --git a/tests/Unit/Import/Storage/ImportArrayStorageTest.php b/tests/Unit/Import/Storage/ImportArrayStorageTest.php
index d96790fe17..d79e985a7e 100644
--- a/tests/Unit/Import/Storage/ImportArrayStorageTest.php
+++ b/tests/Unit/Import/Storage/ImportArrayStorageTest.php
@@ -82,7 +82,8 @@ class ImportArrayStorageTest extends TestCase
$language = new Preference;
$language->data = 'en_US';
- Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($language)->atLeast()->once();
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language)->atLeast()->once();
+
// make fake job
@@ -156,7 +157,7 @@ class ImportArrayStorageTest extends TestCase
$language = new Preference;
$language->data = 'en_US';
- Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($language)->atLeast()->once();
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language)->atLeast()->once();
// make fake job
@@ -250,7 +251,7 @@ class ImportArrayStorageTest extends TestCase
$language = new Preference;
$language->data = 'en_US';
- Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($language)->atLeast()->once();
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language)->atLeast()->once();
// make fake job
@@ -358,7 +359,7 @@ class ImportArrayStorageTest extends TestCase
$language = new Preference;
$language->data = 'en_US';
- Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($language)->atLeast()->once();
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language)->atLeast()->once();
// make fake job
@@ -469,7 +470,7 @@ class ImportArrayStorageTest extends TestCase
$language = new Preference;
$language->data = 'en_US';
- Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($language)->atLeast()->once();
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language)->atLeast()->once();
// make fake job
@@ -582,7 +583,7 @@ class ImportArrayStorageTest extends TestCase
$language = new Preference;
$language->data = 'en_US';
- Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($language)->atLeast()->once();
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language)->atLeast()->once();
// make fake job
@@ -700,7 +701,7 @@ class ImportArrayStorageTest extends TestCase
$language = new Preference;
$language->data = 'en_US';
- Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($language)->atLeast()->once();
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language)->atLeast()->once();
// make fake job
@@ -788,7 +789,7 @@ class ImportArrayStorageTest extends TestCase
$language = new Preference;
$language->data = 'en_US';
- Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($language)->atLeast()->once();
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language)->atLeast()->once();
// make fake job
@@ -867,7 +868,7 @@ class ImportArrayStorageTest extends TestCase
$language = new Preference;
$language->data = 'en_US';
- Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($language)->atLeast()->once();
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language)->atLeast()->once();
$meta = new TransactionJournalMeta;
$meta->transaction_journal_id = 1;
@@ -954,7 +955,7 @@ class ImportArrayStorageTest extends TestCase
$language = new Preference;
$language->data = 'en_US';
- Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($language)->atLeast()->once();
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language)->atLeast()->once();
// make fake job
@@ -1047,7 +1048,7 @@ class ImportArrayStorageTest extends TestCase
$language = new Preference;
$language->data = 'en_US';
- Preferences::shouldReceive('get')->withArgs(['language', 'en_US'])->andReturn($language)->atLeast()->once();
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language)->atLeast()->once();
// make fake job
diff --git a/tests/Unit/Rules/IsValidAttachmentModelTest.php b/tests/Unit/Rules/IsValidAttachmentModelTest.php
index d686b19095..b3da7794d3 100644
--- a/tests/Unit/Rules/IsValidAttachmentModelTest.php
+++ b/tests/Unit/Rules/IsValidAttachmentModelTest.php
@@ -28,6 +28,7 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
+use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Rules\IsValidAttachmentModel;
use Log;
@@ -88,11 +89,11 @@ class IsValidAttachmentModelTest extends TestCase
*/
public function testTransaction(): void
{
- $transaction = $this->getRandomWithdrawal()->transactions()->first();
- $journalRepos = $this->mock(JournalRepositoryInterface::class);
+ $transaction = $this->getRandomWithdrawal()->transactions()->first();
+ $apiJournalRepos = $this->mock(JournalAPIRepositoryInterface::class);
- $journalRepos->shouldReceive('setUser')->atLeast()->once();
- $journalRepos->shouldReceive('findTransaction')->atLeast()->once()->withArgs([$transaction->id])->andReturn($transaction);
+ $apiJournalRepos->shouldReceive('setUser')->atLeast()->once();
+ $apiJournalRepos->shouldReceive('findTransaction')->atLeast()->once()->withArgs([$transaction->id])->andReturn($transaction);
$value = $transaction->id;
$attribute = 'not-important';
diff --git a/tests/Unit/TransactionRules/Actions/SetBudgetTest.php b/tests/Unit/TransactionRules/Actions/SetBudgetTest.php
index b31dc936b1..e9e48d7cc2 100644
--- a/tests/Unit/TransactionRules/Actions/SetBudgetTest.php
+++ b/tests/Unit/TransactionRules/Actions/SetBudgetTest.php
@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace Tests\Unit\TransactionRules\Actions;
use FireflyIII\Models\RuleAction;
-use FireflyIII\Models\Transaction;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\TransactionRules\Actions\SetBudget;
use Illuminate\Support\Collection;
@@ -42,9 +41,6 @@ class SetBudgetTest extends TestCase
// get journal, remove all budgets
$journal = $this->getRandomWithdrawal();
$budget = $this->getRandomBudget();
- $budgetRepos = $this->mock(BudgetRepositoryInterface::class);
- $budgetRepos->shouldReceive('setUser');
- $budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]));
$journal->budgets()->sync([]);
$this->assertEquals(0, $journal->budgets()->count());
@@ -65,17 +61,13 @@ class SetBudgetTest extends TestCase
{
// get journal, remove all budgets
$journal = $this->getRandomWithdrawal();
- $budget = $this->getRandomBudget();
- $budgetRepos = $this->mock(BudgetRepositoryInterface::class);
- $budgetRepos->shouldReceive('setUser');
- $budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection);
$journal->budgets()->sync([]);
$this->assertEquals(0, $journal->budgets()->count());
// fire the action:
$ruleAction = new RuleAction;
- $ruleAction->action_value = $budget->name;
+ $ruleAction->action_value = 'non-existing budget #' . $this->randomInt();
$action = new SetBudget($ruleAction);
$result = $action->act($journal);
$this->assertFalse($result);
@@ -91,9 +83,6 @@ class SetBudgetTest extends TestCase
// get journal, remove all budgets
$journal = $this->getRandomDeposit();
$budget = $this->getRandomBudget();
- $budgetRepos = $this->mock(BudgetRepositoryInterface::class);
- $budgetRepos->shouldReceive('setUser');
- $budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]));
$journal->budgets()->detach();
$this->assertEquals(0, $journal->budgets()->count());