diff --git a/app/Api/V1/Controllers/AccountController.php b/app/Api/V1/Controllers/AccountController.php index 5a0208c481..89a0272498 100644 --- a/app/Api/V1/Controllers/AccountController.php +++ b/app/Api/V1/Controllers/AccountController.php @@ -29,7 +29,6 @@ use FireflyIII\Helpers\Filter\InternalTransferFilter; use FireflyIII\Models\Account; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; -use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Support\Http\Api\AccountFilter; use FireflyIII\Support\Http\Api\TransactionFilter; use FireflyIII\Transformers\AccountTransformer; @@ -54,8 +53,6 @@ use League\Fractal\Serializer\JsonApiSerializer; class AccountController extends Controller { use AccountFilter, TransactionFilter; - /** @var CurrencyRepositoryInterface The currency repository */ - private $currencyRepository; /** @var AccountRepositoryInterface The account repository */ private $repository; @@ -73,9 +70,6 @@ class AccountController extends Controller $this->repository = app(AccountRepositoryInterface::class); $this->repository->setUser($user); - $this->currencyRepository = app(CurrencyRepositoryInterface::class); - $this->currencyRepository->setUser($user); - return $next($request); } ); @@ -210,12 +204,7 @@ class AccountController extends Controller */ public function store(AccountRequest $request): JsonResponse { - $data = $request->getAll(); - // if currency ID is 0, find the currency by the code: - if (0 === $data['currency_id']) { - $currency = $this->currencyRepository->findByCodeNull($data['currency_code']); - $data['currency_id'] = null === $currency ? 0 : $currency->id; - } + $data = $request->getAll(); $account = $this->repository->store($data); $manager = new Manager; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; @@ -244,10 +233,9 @@ class AccountController extends Controller $type = $request->get('type') ?? 'default'; $this->parameters->set('type', $type); - $types = $this->mapTransactionTypes($this->parameters->get('type')); - $manager = new Manager(); - $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; - + $types = $this->mapTransactionTypes($this->parameters->get('type')); + $manager = new Manager(); + $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); @@ -282,7 +270,7 @@ class AccountController extends Controller $transformer = app(TransactionTransformer::class); $transformer->setParameters($this->parameters); - $resource = new FractalCollection($transactions, $transformer, 'transactions'); + $resource = new FractalCollection($transactions, $transformer, 'transactions'); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); @@ -298,13 +286,7 @@ class AccountController extends Controller */ public function update(AccountRequest $request, Account $account): JsonResponse { - $data = $request->getAll(); - // if currency ID is 0, find the currency by the code: - if (0 === $data['currency_id']) { - $currency = $this->currencyRepository->findByCodeNull($data['currency_code']); - $data['currency_id'] = null === $currency ? 0 : $currency->id; - } - // set correct type: + $data = $request->getAll(); $data['type'] = config('firefly.shortNamesByFullName.' . $account->accountType->type); $this->repository->update($account, $data); $manager = new Manager; diff --git a/app/Api/V1/Controllers/AvailableBudgetController.php b/app/Api/V1/Controllers/AvailableBudgetController.php index 8a53bded82..dee987c5cb 100644 --- a/app/Api/V1/Controllers/AvailableBudgetController.php +++ b/app/Api/V1/Controllers/AvailableBudgetController.php @@ -25,7 +25,9 @@ namespace FireflyIII\Api\V1\Controllers; use FireflyIII\Api\V1\Requests\AvailableBudgetRequest; use FireflyIII\Exceptions\FireflyException; +use FireflyIII\Factory\TransactionCurrencyFactory; use FireflyIII\Models\AvailableBudget; +use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Transformers\AvailableBudgetTransformer; @@ -155,11 +157,11 @@ class AvailableBudgetController extends Controller */ public function store(AvailableBudgetRequest $request): JsonResponse { - $data = $request->getAll(); - $currency = $this->currencyRepository->findNull($data['currency_id']); - if (null === $currency) { - $currency = $this->currencyRepository->findByCodeNull($data['currency_code']); - } + $data = $request->getAll(); + /** @var TransactionCurrencyFactory $factory */ + $factory = app(TransactionCurrencyFactory::class); + $currency = $factory->find($data['currency_id'], $data['currency_code']); + if (null === $currency) { $currency = app('amount')->getDefaultCurrency(); } @@ -178,7 +180,6 @@ class AvailableBudgetController extends Controller } - /** * Update the specified resource in storage. * @@ -190,6 +191,22 @@ class AvailableBudgetController extends Controller public function update(AvailableBudgetRequest $request, AvailableBudget $availableBudget): JsonResponse { $data = $request->getAll(); + + /** @var TransactionCurrencyFactory $factory */ + $factory = app(TransactionCurrencyFactory::class); + /** @var TransactionCurrency $currency */ + $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null); + + if (null === $currency) { + // use default currency: + $currency = app('amount')->getDefaultCurrency(); + } + $currency->enabled = true; + $currency->save(); + unset($data['currency_code']); + $data['currency_id'] = $currency->id; + + $this->repository->updateAvailableBudget($availableBudget, $data); $manager = new Manager; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; diff --git a/app/Api/V1/Controllers/BillController.php b/app/Api/V1/Controllers/BillController.php index b656aca581..17e2840bd0 100644 --- a/app/Api/V1/Controllers/BillController.php +++ b/app/Api/V1/Controllers/BillController.php @@ -29,7 +29,6 @@ use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Collector\TransactionCollectorInterface; use FireflyIII\Models\Bill; use FireflyIII\Repositories\Bill\BillRepositoryInterface; -use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Support\Http\Api\TransactionFilter; use FireflyIII\Transformers\AttachmentTransformer; use FireflyIII\Transformers\BillTransformer; @@ -288,7 +287,7 @@ class BillController extends Controller $transformer = app(TransactionTransformer::class); $transformer->setParameters($this->parameters); - $resource = new FractalCollection($transactions, $transformer, 'transactions'); + $resource = new FractalCollection($transactions, $transformer, 'transactions'); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); diff --git a/app/Api/V1/Controllers/BudgetController.php b/app/Api/V1/Controllers/BudgetController.php index f49619cff9..6b21f09bfc 100644 --- a/app/Api/V1/Controllers/BudgetController.php +++ b/app/Api/V1/Controllers/BudgetController.php @@ -27,11 +27,8 @@ use FireflyIII\Api\V1\Requests\BudgetLimitRequest; use FireflyIII\Api\V1\Requests\BudgetRequest; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Collector\TransactionCollectorInterface; -use FireflyIII\Helpers\Filter\InternalTransferFilter; use FireflyIII\Models\Budget; -use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; -use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Support\Http\Api\TransactionFilter; use FireflyIII\Transformers\BudgetLimitTransformer; use FireflyIII\Transformers\BudgetTransformer; diff --git a/app/Api/V1/Controllers/BudgetLimitController.php b/app/Api/V1/Controllers/BudgetLimitController.php index 64691b18a1..3d71b885c3 100644 --- a/app/Api/V1/Controllers/BudgetLimitController.php +++ b/app/Api/V1/Controllers/BudgetLimitController.php @@ -219,7 +219,7 @@ class BudgetLimitController extends Controller $transformer = app(TransactionTransformer::class); $transformer->setParameters($this->parameters); - $resource = new FractalCollection($transactions, $transformer, 'transactions'); + $resource = new FractalCollection($transactions, $transformer, 'transactions'); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); diff --git a/app/Api/V1/Controllers/CategoryController.php b/app/Api/V1/Controllers/CategoryController.php index d9dddc1933..a6da367a86 100644 --- a/app/Api/V1/Controllers/CategoryController.php +++ b/app/Api/V1/Controllers/CategoryController.php @@ -30,7 +30,6 @@ use FireflyIII\Helpers\Filter\InternalTransferFilter; use FireflyIII\Models\Category; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Category\CategoryRepositoryInterface; -use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Support\Http\Api\TransactionFilter; use FireflyIII\Transformers\CategoryTransformer; use FireflyIII\Transformers\TransactionTransformer; diff --git a/app/Api/V1/Controllers/ImportController.php b/app/Api/V1/Controllers/ImportController.php index fd376ca199..f3dbc67542 100644 --- a/app/Api/V1/Controllers/ImportController.php +++ b/app/Api/V1/Controllers/ImportController.php @@ -28,7 +28,6 @@ use FireflyIII\Helpers\Filter\InternalTransferFilter; use FireflyIII\Models\ImportJob; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; -use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Support\Http\Api\TransactionFilter; use FireflyIII\Transformers\ImportJobTransformer; use FireflyIII\Transformers\TransactionTransformer; @@ -176,7 +175,7 @@ class ImportController extends Controller $transformer = app(TransactionTransformer::class); $transformer->setParameters($this->parameters); - $resource = new FractalCollection($transactions, $transformer, 'transactions'); + $resource = new FractalCollection($transactions, $transformer, 'transactions'); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); diff --git a/app/Api/V1/Controllers/RuleController.php b/app/Api/V1/Controllers/RuleController.php index af68938173..6990f21a78 100644 --- a/app/Api/V1/Controllers/RuleController.php +++ b/app/Api/V1/Controllers/RuleController.php @@ -30,7 +30,6 @@ use FireflyIII\Jobs\ExecuteRuleOnExistingTransactions; use FireflyIII\Models\AccountType; use FireflyIII\Models\Rule; use FireflyIII\Repositories\Account\AccountRepositoryInterface; -use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\Rule\RuleRepositoryInterface; use FireflyIII\TransactionRules\TransactionMatcher; use FireflyIII\Transformers\RuleTransformer; diff --git a/app/Api/V1/Controllers/TransactionController.php b/app/Api/V1/Controllers/TransactionController.php index ab6caed46c..ce74aa411d 100644 --- a/app/Api/V1/Controllers/TransactionController.php +++ b/app/Api/V1/Controllers/TransactionController.php @@ -179,7 +179,7 @@ class TransactionController extends Controller $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); - $events = $this->repository->getPiggyBankEventsByTr($transaction); + $events = $this->repository->getPiggyBankEventsByTr($transaction); /** @var PiggyBankEventTransformer $transformer */ $transformer = app(PiggyBankEventTransformer::class); @@ -225,7 +225,7 @@ class TransactionController extends Controller /** @var TransactionTransformer $transformer */ $transformer = app(TransactionTransformer::class); $transformer->setParameters($this->parameters); - $resource = new FractalCollection($transactions, $transformer, 'transactions'); + $resource = new FractalCollection($transactions, $transformer, 'transactions'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } @@ -274,7 +274,7 @@ class TransactionController extends Controller $transformer = app(TransactionTransformer::class); $transformer->setParameters($this->parameters); - $resource = new FractalCollection($transactions, $transformer, 'transactions'); + $resource = new FractalCollection($transactions, $transformer, 'transactions'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } @@ -323,7 +323,7 @@ class TransactionController extends Controller $transformer = app(TransactionTransformer::class); $transformer->setParameters($this->parameters); - $resource = new FractalCollection($transactions, $transformer, 'transactions'); + $resource = new FractalCollection($transactions, $transformer, 'transactions'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); diff --git a/app/Factory/AccountFactory.php b/app/Factory/AccountFactory.php index 5a57dddae1..13c5059c68 100644 --- a/app/Factory/AccountFactory.php +++ b/app/Factory/AccountFactory.php @@ -30,6 +30,7 @@ namespace FireflyIII\Factory; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; +use FireflyIII\Models\TransactionCurrency; use FireflyIII\Services\Internal\Support\AccountServiceTrait; use FireflyIII\User; use Log; @@ -47,7 +48,8 @@ class AccountFactory use AccountServiceTrait; /** - * Constructor. + * AccountFactory constructor. + * @codeCoverageIgnore */ public function __construct() { @@ -79,7 +81,6 @@ class AccountFactory // account may exist already: $return = $this->find($data['name'], $type->type); - if (null === $return) { // create it: $databaseData @@ -92,6 +93,21 @@ class AccountFactory 'iban' => $data['iban'], ]; + // find currency, or use default currency instead. + /** @var TransactionCurrencyFactory $factory */ + $factory = app(TransactionCurrencyFactory::class); + /** @var TransactionCurrency $currency */ + $currency = $factory->find((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null)); + + if (null === $currency) { + // use default currency: + $currency = app('amount')->getDefaultCurrencyByUser($this->user); + } + $currency->enabled =true; + $currency->save(); + + unset($data['currency_code']); + $data['currency_id'] = $currency->id; // remove virtual balance when not an asset account or a liability $canHaveVirtual = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD]; if (!\in_array($type->type, $canHaveVirtual, true)) { diff --git a/app/Factory/BillFactory.php b/app/Factory/BillFactory.php index 797d9a14cd..660113c4f9 100644 --- a/app/Factory/BillFactory.php +++ b/app/Factory/BillFactory.php @@ -61,7 +61,7 @@ class BillFactory /** @var TransactionCurrencyFactory $factory */ $factory = app(TransactionCurrencyFactory::class); /** @var TransactionCurrency $currency */ - $currency = $factory->find((int)$data['currency_id'], (string)$data['currency_code']); + $currency = $factory->find((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null)); if(null === $currency) { // use default currency: diff --git a/app/Factory/TransactionCurrencyFactory.php b/app/Factory/TransactionCurrencyFactory.php index 8e1de1d3be..02ca1a35a9 100644 --- a/app/Factory/TransactionCurrencyFactory.php +++ b/app/Factory/TransactionCurrencyFactory.php @@ -37,7 +37,8 @@ use Log; class TransactionCurrencyFactory { /** - * Constructor. + * TransactionCurrencyFactory constructor. + * @codeCoverageIgnore */ public function __construct() { diff --git a/app/Factory/TransactionFactory.php b/app/Factory/TransactionFactory.php index dfaea6e8ec..1da058eec9 100644 --- a/app/Factory/TransactionFactory.php +++ b/app/Factory/TransactionFactory.php @@ -72,7 +72,8 @@ class TransactionFactory 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 + $currency = app('amount')->getDefaultCurrencyByUser($data['account']->user); + $currencyId = $currency->id; } $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'])); diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index ed227af253..497986a41f 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -894,7 +894,7 @@ class BudgetRepository implements BudgetRepositoryInterface if (null !== $existing) { throw new FireflyException(sprintf('An entry already exists for these parameters: available budget object with ID #%d', $existing->id)); } - $availableBudget->transaction_currency_id = $data['transaction_currency_id']; + $availableBudget->transaction_currency_id = $data['currency_id']; $availableBudget->start_date = $data['start_date']; $availableBudget->end_date = $data['end_date']; $availableBudget->amount = $data['amount']; @@ -931,6 +931,8 @@ class BudgetRepository implements BudgetRepositoryInterface if (null === $currency) { $currency = app('amount')->getDefaultCurrencyByUser($this->user); } + $currency->enabled = true; + $currency->save(); $budgetLimit->transaction_currency_id = $currency->id; $budgetLimit->save(); diff --git a/app/Rules/IsValidAttachmentModel.php b/app/Rules/IsValidAttachmentModel.php index ce6828bfa6..6034a8c34c 100644 --- a/app/Rules/IsValidAttachmentModel.php +++ b/app/Rules/IsValidAttachmentModel.php @@ -33,6 +33,7 @@ use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\User; use Illuminate\Contracts\Validation\Rule; +use Log; /** * Class IsValidAttachmentModel @@ -112,7 +113,7 @@ class IsValidAttachmentModel implements Rule return null !== $transaction; } - if (TransactionJournal::class === $this->model) { + if (TransactionJournal::class === $model) { $repository = app(JournalRepositoryInterface::class); $user = auth()->user(); $repository->setUser($user); @@ -120,6 +121,7 @@ class IsValidAttachmentModel implements Rule return null !== $result; } + Log::error(sprintf('No model was recognized from string "%s"', $model)); return false; } diff --git a/app/Services/Internal/Support/AccountServiceTrait.php b/app/Services/Internal/Support/AccountServiceTrait.php index d58661fda4..06ba9869a1 100644 --- a/app/Services/Internal/Support/AccountServiceTrait.php +++ b/app/Services/Internal/Support/AccountServiceTrait.php @@ -332,14 +332,17 @@ trait AccountServiceTrait * @param string $note * * @return bool - * @throws \Exception */ public function updateNote(Account $account, string $note): bool { if ('' === $note) { $dbNote = $account->notes()->first(); if (null !== $dbNote) { - $dbNote->delete(); + try { + $dbNote->delete(); + } catch (\Exception $e) { + Log::debug($e->getMessage()); + } } return true; @@ -365,7 +368,7 @@ trait AccountServiceTrait public function validIBData(array $data): bool { $data['openingBalance'] = (string)($data['openingBalance'] ?? ''); - if (isset($data['openingBalance'], $data['openingBalanceDate']) && \strlen($data['openingBalance']) > 0) { + if ('' !== $data['openingBalance'] && isset($data['openingBalance'], $data['openingBalanceDate'])) { Log::debug('Array has valid opening balance data.'); return true; diff --git a/app/Services/Internal/Support/RecurringTransactionTrait.php b/app/Services/Internal/Support/RecurringTransactionTrait.php index 03e2487fe9..894636e5bd 100644 --- a/app/Services/Internal/Support/RecurringTransactionTrait.php +++ b/app/Services/Internal/Support/RecurringTransactionTrait.php @@ -96,8 +96,8 @@ trait RecurringTransactionTrait /** @var TransactionCurrencyFactory $factory */ $factory = app(TransactionCurrencyFactory::class); - $currency = $factory->find($array['currency_id'], $array['currency_code']); - $foreignCurrency = $factory->find($array['foreign_currency_id'], $array['foreign_currency_code']); + $currency = $factory->find($array['currency_id'] ?? null, $array['currency_code'] ?? null); + $foreignCurrency = $factory->find($array['foreign_currency_id'] ?? null, $array['foreign_currency_code'] ?? null); if (null === $currency) { $currency = app('amount')->getDefaultCurrencyByUser($recurrence->user); } diff --git a/app/Services/Internal/Update/AccountUpdateService.php b/app/Services/Internal/Update/AccountUpdateService.php index 86fedcf9a2..a7c0a26548 100644 --- a/app/Services/Internal/Update/AccountUpdateService.php +++ b/app/Services/Internal/Update/AccountUpdateService.php @@ -23,7 +23,9 @@ declare(strict_types=1); namespace FireflyIII\Services\Internal\Update; +use FireflyIII\Factory\TransactionCurrencyFactory; use FireflyIII\Models\Account; +use FireflyIII\Models\TransactionCurrency; use FireflyIII\Services\Internal\Support\AccountServiceTrait; use Log; @@ -33,6 +35,7 @@ use Log; class AccountUpdateService { use AccountServiceTrait; + /** * Constructor. */ @@ -66,6 +69,19 @@ class AccountUpdateService if (isset($data['currency_id']) && 0 === $data['currency_id']) { unset($data['currency_id']); } + // find currency, or use default currency instead. + /** @var TransactionCurrencyFactory $factory */ + $factory = app(TransactionCurrencyFactory::class); + /** @var TransactionCurrency $currency */ + $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null); + + if (null === $currency) { + // use default currency: + $currency = app('amount')->getDefaultCurrencyByUser($account->user); + } + $currency->enabled = true; + $currency->save(); + $data['currency_id'] = $currency->id; // update all meta data: $this->updateMetaData($account, $data); diff --git a/app/Services/Internal/Update/BillUpdateService.php b/app/Services/Internal/Update/BillUpdateService.php index cb7ebe8403..811f7a4832 100644 --- a/app/Services/Internal/Update/BillUpdateService.php +++ b/app/Services/Internal/Update/BillUpdateService.php @@ -57,7 +57,7 @@ class BillUpdateService /** @var TransactionCurrencyFactory $factory */ $factory = app(TransactionCurrencyFactory::class); /** @var TransactionCurrency $currency */ - $currency = $factory->find((int)$data['currency_id'], (string)$data['currency_code']); + $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null); if(null === $currency) { // use default currency: diff --git a/tests/Api/V1/Controllers/AccountControllerTest.php b/tests/Api/V1/Controllers/AccountControllerTest.php index ebd548256b..fb82b7ab20 100644 --- a/tests/Api/V1/Controllers/AccountControllerTest.php +++ b/tests/Api/V1/Controllers/AccountControllerTest.php @@ -63,12 +63,10 @@ class AccountControllerTest extends TestCase public function testDelete(): void { // mock stuff: - $repository = $this->mock(AccountRepositoryInterface::class); - $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $repository = $this->mock(AccountRepositoryInterface::class); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('destroy')->atLeast()->once()->andReturn(true); - $currencyRepos->shouldReceive('setUser')->once(); // get account: $account = $this->getRandomAsset(); @@ -90,9 +88,8 @@ class AccountControllerTest extends TestCase $accounts = factory(Account::class, 10)->create(); // mock stuff: - $repository = $this->mock(AccountRepositoryInterface::class); - $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); - $transformer = $this->mock(AccountTransformer::class); + $repository = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(AccountTransformer::class); // mock calls to transformer: $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); @@ -103,7 +100,6 @@ class AccountControllerTest extends TestCase // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('getAccountsByType')->withAnyArgs()->andReturn($accounts)->once(); // test API @@ -126,10 +122,9 @@ class AccountControllerTest extends TestCase public function testPiggyBanks(): void { // mock stuff: - $repository = $this->mock(AccountRepositoryInterface::class); - $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); - $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); - $transformer = $this->mock(PiggyBankTransformer::class); + $repository = $this->mock(AccountRepositoryInterface::class); + $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(PiggyBankTransformer::class); // mock calls to transformer: $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); @@ -144,7 +139,7 @@ class AccountControllerTest extends TestCase // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('setUser')->atLeast()->once(); + $repository->shouldReceive('getPiggyBanks')->andReturn($piggies)->atLeast()->once(); // test API @@ -169,10 +164,10 @@ class AccountControllerTest extends TestCase public function testShow(): void { // mock stuff: - $account = $this->getRandomAsset(); - $repository = $this->mock(AccountRepositoryInterface::class); - $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); - $transformer = $this->mock(AccountTransformer::class); + $account = $this->getRandomAsset(); + $repository = $this->mock(AccountRepositoryInterface::class); + + $transformer = $this->mock(AccountTransformer::class); // mock calls to transformer: $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); @@ -183,7 +178,7 @@ class AccountControllerTest extends TestCase // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('setUser')->atLeast()->once(); + // test API $response = $this->get(route('api.v1.accounts.show', [$account->id])); @@ -201,12 +196,12 @@ class AccountControllerTest extends TestCase public function testStoreInvalidBalance(): void { // mock repositories - $repository = $this->mock(AccountRepositoryInterface::class); - $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $repository = $this->mock(AccountRepositoryInterface::class); + // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('setUser')->atLeast()->once(); + // data to submit $data = [ @@ -239,16 +234,14 @@ class AccountControllerTest extends TestCase public function testStoreLiability(): void { // mock repositories - $repository = $this->mock(AccountRepositoryInterface::class); - $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); - $transformer = $this->mock(AccountTransformer::class); - $account = $this->getRandomAsset(); + $repository = $this->mock(AccountRepositoryInterface::class); + + $transformer = $this->mock(AccountTransformer::class); + $account = $this->getRandomAsset(); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('store')->atLeast()->once()->andReturn($account); - $currencyRepos->shouldReceive('findByCodeNull')->andReturn(null); // mock calls to transformer: $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); @@ -287,13 +280,13 @@ class AccountControllerTest extends TestCase public function testStoreNoCreditCardData(): void { // mock repositories - $repository = $this->mock(AccountRepositoryInterface::class); - $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); - $transformer = $this->mock(AccountTransformer::class); + $repository = $this->mock(AccountRepositoryInterface::class); + + $transformer = $this->mock(AccountTransformer::class); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('setUser')->atLeast()->once(); + // data to submit $data = [ @@ -327,9 +320,9 @@ class AccountControllerTest extends TestCase public function testStoreNoCurrencyInfo(): void { // mock repositories - $repository = $this->mock(AccountRepositoryInterface::class); - $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); - $transformer = $this->mock(AccountTransformer::class); + $repository = $this->mock(AccountRepositoryInterface::class); + + $transformer = $this->mock(AccountTransformer::class); // mock calls to transformer: $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); @@ -340,8 +333,6 @@ class AccountControllerTest extends TestCase // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('findByCodeNull')->atLeast()->once()->andReturnNull(); $repository->shouldReceive('store')->once()->andReturn(new Account); // data to submit @@ -367,13 +358,13 @@ class AccountControllerTest extends TestCase public function testStoreNotUnique(): void { // mock repositories - $repository = $this->mock(AccountRepositoryInterface::class); - $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); - $transformer = $this->mock(AccountTransformer::class); + $repository = $this->mock(AccountRepositoryInterface::class); + + $transformer = $this->mock(AccountTransformer::class); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('setUser')->atLeast()->once(); + $account = $this->getRandomAsset(); // data to submit @@ -409,15 +400,13 @@ class AccountControllerTest extends TestCase public function testStoreValid(): void { // mock repositories - $repository = $this->mock(AccountRepositoryInterface::class); - $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); - $transformer = $this->mock(AccountTransformer::class); - $account = $this->getRandomAsset(); + $repository = $this->mock(AccountRepositoryInterface::class); + + $transformer = $this->mock(AccountTransformer::class); + $account = $this->getRandomAsset(); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('findByCodeNull')->atLeast()->once()->andReturnNull(); $repository->shouldReceive('store')->atLeast()->once()->andReturn($account); // mock calls to transformer: @@ -450,16 +439,15 @@ class AccountControllerTest extends TestCase public function testStoreWithCurrencyCode(): void { // mock repositories - $repository = $this->mock(AccountRepositoryInterface::class); - $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); - $transformer = $this->mock(AccountTransformer::class); - $account = $this->getRandomAsset(); + $repository = $this->mock(AccountRepositoryInterface::class); + + $transformer = $this->mock(AccountTransformer::class); + $account = $this->getRandomAsset(); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('setUser')->atLeast()->once(); + $repository->shouldReceive('store')->atLeast()->once()->andReturn($account); - $currencyRepos->shouldReceive('findByCodeNull')->atLeast()->once()->andReturn(new TransactionCurrency()); // mock calls to transformer: $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); @@ -493,12 +481,10 @@ class AccountControllerTest extends TestCase $accountRepos = $this->mock(AccountRepositoryInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); - $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); $transformer = $this->mock(TransactionTransformer::class); // default mocks $accountRepos->shouldReceive('setUser')->atLeast()->once(); - $currencyRepository->shouldReceive('setUser')->atLeast()->once(); $accountRepos->shouldReceive('isAsset')->atLeast()->once()->andReturnTrue(); // mock collector: @@ -537,12 +523,10 @@ class AccountControllerTest extends TestCase $accountRepos = $this->mock(AccountRepositoryInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); - $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); $transformer = $this->mock(TransactionTransformer::class); // default mocks $accountRepos->shouldReceive('setUser')->atLeast()->once(); - $currencyRepository->shouldReceive('setUser')->atLeast()->once(); $accountRepos->shouldReceive('isAsset')->atLeast()->once()->andReturnFalse(); // mock collector: @@ -583,12 +567,10 @@ class AccountControllerTest extends TestCase $accountRepos = $this->mock(AccountRepositoryInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); - $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); $transformer = $this->mock(TransactionTransformer::class); // default mocks $accountRepos->shouldReceive('setUser')->atLeast()->once(); - $currencyRepository->shouldReceive('setUser')->atLeast()->once(); $accountRepos->shouldReceive('isAsset')->atLeast()->once()->andReturnTrue(); // mock collector: @@ -627,15 +609,14 @@ class AccountControllerTest extends TestCase public function testUpdate(): void { // mock repositories - $repository = $this->mock(AccountRepositoryInterface::class); - $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); - $transformer = $this->mock(AccountTransformer::class); + $repository = $this->mock(AccountRepositoryInterface::class); + + $transformer = $this->mock(AccountTransformer::class); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('setUser')->atLeast()->once(); + $repository->shouldReceive('update')->atLeast()->once(); - $currencyRepos->shouldReceive('findByCodeNull')->andReturn(null); // mock calls to transformer: $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); @@ -669,15 +650,14 @@ class AccountControllerTest extends TestCase public function testUpdateCurrencyCode(): void { // mock repositories - $repository = $this->mock(AccountRepositoryInterface::class); - $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); - $transformer = $this->mock(AccountTransformer::class); + $repository = $this->mock(AccountRepositoryInterface::class); + + $transformer = $this->mock(AccountTransformer::class); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); - $currencyRepos->shouldReceive('setUser')->atLeast()->once(); + $repository->shouldReceive('update')->atLeast()->once(); - $currencyRepos->shouldReceive('findByCodeNull')->andReturn(TransactionCurrency::find(1)); // mock calls to transformer: $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); diff --git a/tests/Api/V1/Controllers/AttachmentControllerTest.php b/tests/Api/V1/Controllers/AttachmentControllerTest.php index 6549c54ad9..5235fab1ea 100644 --- a/tests/Api/V1/Controllers/AttachmentControllerTest.php +++ b/tests/Api/V1/Controllers/AttachmentControllerTest.php @@ -260,26 +260,29 @@ class AttachmentControllerTest extends TestCase $transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]); // mock calls: + $journal = $this->getRandomWithdrawal(); $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('store')->once()->andReturn($attachment); $repository->shouldReceive('getNoteText')->andReturn('Hi There'); $journalRepos->shouldReceive('setUser')->once(); - $journalRepos->shouldReceive('findNull')->once()->andReturn($this->user()->transactionJournals()->find(1)); + + $journalRepos->shouldReceive('findNull')->once()->andReturn($journal ); // data to submit $data = [ 'filename' => 'Some new att', 'description' => sprintf('Attempt #%d', random_int(1, 10000)), - 'model' => TransactionJournal::class, - 'model_id' => 1, + 'model' => 'TransactionJournal', + 'model_id' => $journal->id, ]; // test API - $response = $this->post(route('api.v1.attachments.store'), $data); + $response = $this->post(route('api.v1.attachments.store'), $data, ['accept' => 'application/json']); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); + } /** @@ -314,7 +317,7 @@ class AttachmentControllerTest extends TestCase $data = [ 'filename' => $attachment->filename, 'description' => sprintf('Attempt #%d', random_int(1, 10000)), - 'model' => TransactionJournal::class, + 'model' => 'TransactionJournal', 'model_id' => 1, ]; diff --git a/tests/Api/V1/Controllers/AvailableBudgetControllerTest.php b/tests/Api/V1/Controllers/AvailableBudgetControllerTest.php index 84c9cdf2f5..81fd8ea195 100644 --- a/tests/Api/V1/Controllers/AvailableBudgetControllerTest.php +++ b/tests/Api/V1/Controllers/AvailableBudgetControllerTest.php @@ -24,6 +24,8 @@ declare(strict_types=1); namespace Tests\Api\V1\Controllers; +use Amount; +use FireflyIII\Factory\TransactionCurrencyFactory; use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; @@ -60,6 +62,7 @@ class AvailableBudgetControllerTest extends TestCase $repository = $this->mock(BudgetRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); $transformer = $this->mock(AvailableBudgetTransformer::class); + $factory = $this->mock(TransactionCurrencyFactory::class); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); @@ -85,6 +88,7 @@ class AvailableBudgetControllerTest extends TestCase $repository = $this->mock(BudgetRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); $transformer = $this->mock(AvailableBudgetTransformer::class); + $factory = $this->mock(TransactionCurrencyFactory::class); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); @@ -115,6 +119,7 @@ class AvailableBudgetControllerTest extends TestCase $repository = $this->mock(BudgetRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); $transformer = $this->mock(AvailableBudgetTransformer::class); + $factory = $this->mock(TransactionCurrencyFactory::class); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); @@ -143,6 +148,7 @@ class AvailableBudgetControllerTest extends TestCase $repository = $this->mock(BudgetRepositoryInterface::class); $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); $transformer = $this->mock(AvailableBudgetTransformer::class); + $factory = $this->mock(TransactionCurrencyFactory::class); $availableBudget = new AvailableBudget; // mock transformer @@ -151,15 +157,15 @@ class AvailableBudgetControllerTest extends TestCase $transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]); $transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]); $transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]); + $factory->shouldReceive('find')->withArgs([2, ''])->once()->andReturn(TransactionCurrency::find(2)); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('setAvailableBudget')->once()->andReturn($availableBudget); - $currencyRepository->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1)); // data to submit $data = [ - 'currency_id' => '1', + 'currency_id' => '2', 'amount' => '100', 'start' => '2018-01-01', 'end' => '2018-01-31', @@ -183,8 +189,9 @@ class AvailableBudgetControllerTest extends TestCase { // mock stuff: $repository = $this->mock(BudgetRepositoryInterface::class); - $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); $transformer = $this->mock(AvailableBudgetTransformer::class); + $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); + $factory = $this->mock(TransactionCurrencyFactory::class); $availableBudget = new AvailableBudget; // mock transformer @@ -193,20 +200,19 @@ class AvailableBudgetControllerTest extends TestCase $transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]); $transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]); $transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]); + $factory->shouldReceive('find')->withArgs([0, ''])->once()->andReturnNull(); + + Amount::shouldReceive('getDefaultCurrency')->once()->andReturn(TransactionCurrency::find(5)); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); - $currencyRepository->shouldReceive('findNull')->withArgs([1])->andReturn(null)->once(); - $currencyRepository->shouldReceive('findByCodeNull')->withArgs(['EUR'])->andReturn(null)->once(); $repository->shouldReceive('setAvailableBudget')->once()->andReturn($availableBudget); // data to submit $data = [ - 'currency_id' => '1', - 'currency_code' => 'EUR', - 'amount' => '100', - 'start' => '2018-01-01', - 'end' => '2018-01-31', + 'amount' => '100', + 'start' => '2018-01-01', + 'end' => '2018-01-31', ]; @@ -228,9 +234,10 @@ class AvailableBudgetControllerTest extends TestCase $availableBudget = $this->user()->availableBudgets()->first(); // mock stuff: - $repository = $this->mock(BudgetRepositoryInterface::class); + $repository = $this->mock(BudgetRepositoryInterface::class); + $transformer = $this->mock(AvailableBudgetTransformer::class); + $factory = $this->mock(TransactionCurrencyFactory::class); $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); - $transformer = $this->mock(AvailableBudgetTransformer::class); // mock transformer $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); @@ -239,15 +246,15 @@ class AvailableBudgetControllerTest extends TestCase $transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]); $transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]); + $factory->shouldReceive('find')->withArgs([0, 'EUR'])->once()->andReturnNull(); + Amount::shouldReceive('getDefaultCurrency')->once()->andReturn(TransactionCurrency::find(5)); + // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('setAvailableBudget')->once()->andReturn($availableBudget); - $currencyRepository->shouldReceive('findNull')->withArgs([1])->andReturn(null)->once(); - $currencyRepository->shouldReceive('findByCodeNull')->withArgs(['EUR'])->andReturn(TransactionCurrency::find(1))->once(); // data to submit $data = [ - 'currency_id' => '1', 'currency_code' => 'EUR', 'amount' => '100', 'start' => '2018-01-01', @@ -275,6 +282,7 @@ class AvailableBudgetControllerTest extends TestCase $repository = $this->mock(BudgetRepositoryInterface::class); $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); $transformer = $this->mock(AvailableBudgetTransformer::class); + $factory = $this->mock(TransactionCurrencyFactory::class); // mock transformer $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); @@ -283,6 +291,8 @@ class AvailableBudgetControllerTest extends TestCase $transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]); $transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]); + $factory->shouldReceive('find')->withArgs([1, ''])->once()->andReturnNull(); + /** @var AvailableBudget $availableBudget */ $availableBudget = $this->user()->availableBudgets()->first(); @@ -295,8 +305,8 @@ class AvailableBudgetControllerTest extends TestCase $data = [ 'currency_id' => '1', 'amount' => '100', - 'start' => '2018-01-01', - 'end' => '2018-01-31', + 'start' => '2018-01-01', + 'end' => '2018-01-31', ]; // test API diff --git a/tests/Unit/Factory/AccountFactoryTest.php b/tests/Unit/Factory/AccountFactoryTest.php index b014718e3c..35d7c07c59 100644 --- a/tests/Unit/Factory/AccountFactoryTest.php +++ b/tests/Unit/Factory/AccountFactoryTest.php @@ -30,6 +30,7 @@ use FireflyIII\Factory\AccountFactory; use FireflyIII\Models\Account; use FireflyIII\Models\AccountMeta; use FireflyIII\Models\AccountType; +use FireflyIII\Models\TransactionCurrency; use Log; use Tests\TestCase; @@ -67,6 +68,7 @@ class AccountFactoryTest extends TestCase 'accountRole' => 'defaultAsset', ]; + /** @var AccountFactory $factory */ $factory = app(AccountFactory::class); $factory->setUser($this->user()); @@ -84,6 +86,12 @@ class AccountFactoryTest extends TestCase $meta = $account->accountMeta()->where('name', 'accountRole')->first(); $this->assertNotNull($meta); $this->assertEquals('defaultAsset', $meta->data); + + // get the currency ID: + /** @var AccountMeta $meta */ + $currencyId = $account->accountMeta()->where('name', 'currency_id')->first(); + $this->assertNotNull($currencyId); + $this->assertEquals('1', $currencyId->data); } /** @@ -507,6 +515,100 @@ class AccountFactoryTest extends TestCase $this->assertEquals('Hello!', $note->text); } + /** + * Test minimal set of data to make factory work (asset account). + * + * @covers \FireflyIII\Factory\AccountFactory + * @covers \FireflyIII\Factory\AccountMetaFactory + * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait + */ + public function testCreateCurrencyCode(): void + { + $currency = TransactionCurrency::where('code', 'CAD')->first(); + $data = [ + 'account_type_id' => null, + 'accountType' => 'asset', + 'iban' => null, + 'currency_code' => $currency->code, + 'name' => 'Basic asset account #' . random_int(1, 10000), + 'virtualBalance' => null, + 'active' => true, + 'accountRole' => 'defaultAsset', + ]; + + + /** @var AccountFactory $factory */ + $factory = app(AccountFactory::class); + $factory->setUser($this->user()); + $account = $factory->create($data); + + // assert stuff about account: + $this->assertEquals($account->name, $data['name']); + $this->assertEquals(AccountType::ASSET, $account->accountType->type); + $this->assertEquals('', $account->iban); + $this->assertTrue($account->active); + $this->assertEquals('0', $account->virtual_balance); + + // get the role: + /** @var AccountMeta $meta */ + $meta = $account->accountMeta()->where('name', 'accountRole')->first(); + $this->assertNotNull($meta); + $this->assertEquals('defaultAsset', $meta->data); + + // get the currency ID: + /** @var AccountMeta $meta */ + $currencyId = $account->accountMeta()->where('name', 'currency_id')->first(); + $this->assertNotNull($currencyId); + $this->assertEquals($currency->id, $currencyId->data); + } + + /** + * Test minimal set of data to make factory work (asset account). + * + * @covers \FireflyIII\Factory\AccountFactory + * @covers \FireflyIII\Factory\AccountMetaFactory + * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait + */ + public function testCreateCurrencyId(): void + { + $currency = TransactionCurrency::where('code', 'USD')->first(); + $data = [ + 'account_type_id' => null, + 'accountType' => 'asset', + 'iban' => null, + 'currency_id' => $currency->id, + 'name' => 'Basic asset account #' . random_int(1, 10000), + 'virtualBalance' => null, + 'active' => true, + 'accountRole' => 'defaultAsset', + ]; + + + /** @var AccountFactory $factory */ + $factory = app(AccountFactory::class); + $factory->setUser($this->user()); + $account = $factory->create($data); + + // assert stuff about account: + $this->assertEquals($account->name, $data['name']); + $this->assertEquals(AccountType::ASSET, $account->accountType->type); + $this->assertEquals('', $account->iban); + $this->assertTrue($account->active); + $this->assertEquals('0', $account->virtual_balance); + + // get the role: + /** @var AccountMeta $meta */ + $meta = $account->accountMeta()->where('name', 'accountRole')->first(); + $this->assertNotNull($meta); + $this->assertEquals('defaultAsset', $meta->data); + + // get the currency ID: + /** @var AccountMeta $meta */ + $currencyId = $account->accountMeta()->where('name', 'currency_id')->first(); + $this->assertNotNull($currencyId); + $this->assertEquals($currency->id, $currencyId->data); + } + /** * Should return existing account. * diff --git a/tests/Unit/Factory/BillFactoryTest.php b/tests/Unit/Factory/BillFactoryTest.php index 808b54dc41..45f7f26a19 100644 --- a/tests/Unit/Factory/BillFactoryTest.php +++ b/tests/Unit/Factory/BillFactoryTest.php @@ -25,8 +25,11 @@ namespace Tests\Unit\Factory; use FireflyIII\Factory\BillFactory; +use FireflyIII\Factory\TransactionCurrencyFactory; +use FireflyIII\Models\TransactionCurrency; use Log; use Tests\TestCase; +use Amount; /** * Class BillFactoryTest @@ -52,7 +55,8 @@ class BillFactoryTest extends TestCase */ public function testCreateBasic(): void { - $data = [ + $currencyFactory = $this->mock(TransactionCurrencyFactory::class); + $data = [ 'name' => 'Some new bill #' . random_int(1, 10000), 'amount_min' => '5', 'currency_id' => 1, @@ -66,6 +70,10 @@ class BillFactoryTest extends TestCase 'notes' => 'Hello!', ]; + $currencyFactory->shouldReceive('find')->atLeast()->once() + ->withArgs([1, ''])->andReturn(TransactionCurrency::find(1)); + + /** @var BillFactory $factory */ $factory = app(BillFactory::class); $factory->setUser($this->user()); @@ -73,6 +81,88 @@ class BillFactoryTest extends TestCase $this->assertEquals($data['name'], $bill->name); $this->assertEquals($data['amount_min'], $bill->amount_min); + $this->assertEquals(1, $bill->transaction_currency_id); + $this->assertEquals($data['repeat_freq'], $bill->repeat_freq); + $note = $bill->notes()->first(); + $this->assertEquals($data['notes'], $note->text); + + } + + /** + * Create basic bill with minimum data. + * + * @covers \FireflyIII\Factory\BillFactory + * @covers \FireflyIII\Services\Internal\Support\BillServiceTrait + */ + public function testCreateDifferentCurrency(): void + { + $currencyFactory = $this->mock(TransactionCurrencyFactory::class); + $data = [ + 'name' => 'Some new bill #' . random_int(1, 10000), + 'amount_min' => '5', + 'currency_code' => 'USD', + 'amount_max' => '10', + 'date' => '2018-01-01', + 'repeat_freq' => 'monthly', + 'skip' => 0, + 'automatch' => true, + 'active' => true, + 'notes' => 'Hello!', + ]; + + $currencyFactory->shouldReceive('find')->atLeast()->once() + ->withArgs([0, 'USD'])->andReturn(TransactionCurrency::find(5)); + + + /** @var BillFactory $factory */ + $factory = app(BillFactory::class); + $factory->setUser($this->user()); + $bill = $factory->create($data); + + $this->assertEquals($data['name'], $bill->name); + $this->assertEquals($data['amount_min'], $bill->amount_min); + $this->assertEquals(5, $bill->transaction_currency_id); + $this->assertEquals($data['repeat_freq'], $bill->repeat_freq); + $note = $bill->notes()->first(); + $this->assertEquals($data['notes'], $note->text); + + } + + /** + * Create basic bill with minimum data. + * + * @covers \FireflyIII\Factory\BillFactory + * @covers \FireflyIII\Services\Internal\Support\BillServiceTrait + */ + public function testCreateNoCurrency(): void + { + $currencyFactory = $this->mock(TransactionCurrencyFactory::class); + $data = [ + 'name' => 'Some new bill #' . random_int(1, 10000), + 'amount_min' => '5', + 'amount_max' => '10', + 'date' => '2018-01-01', + 'repeat_freq' => 'monthly', + 'skip' => 0, + 'automatch' => true, + 'active' => true, + 'notes' => 'Hello!', + ]; + + $currencyFactory->shouldReceive('find')->atLeast()->once() + ->withArgs([0, ''])->andReturnNull(); + + Amount::shouldReceive('getDefaultCurrencyByUser')->atLeast()->once()->andReturn(TransactionCurrency::find(3)); + + + /** @var BillFactory $factory */ + $factory = app(BillFactory::class); + $factory->setUser($this->user()); + $bill = $factory->create($data); + + $this->assertEquals($data['name'], $bill->name); + $this->assertEquals($data['amount_min'], $bill->amount_min); + $this->assertEquals(3, $bill->transaction_currency_id); $this->assertEquals($data['repeat_freq'], $bill->repeat_freq); $note = $bill->notes()->first(); $this->assertEquals($data['notes'], $note->text); @@ -87,7 +177,8 @@ class BillFactoryTest extends TestCase */ public function testCreateEmptyNotes(): void { - $data = [ + $currencyFactory = $this->mock(TransactionCurrencyFactory::class); + $data = [ 'name' => 'Some new bill #' . random_int(1, 10000), 'amount_min' => '5', 'amount_max' => '10', @@ -101,12 +192,17 @@ class BillFactoryTest extends TestCase 'notes' => '', ]; + $currencyFactory->shouldReceive('find')->atLeast()->once() + ->withArgs([1, ''])->andReturn(TransactionCurrency::find(1)); + + /** @var BillFactory $factory */ $factory = app(BillFactory::class); $factory->setUser($this->user()); $bill = $factory->create($data); $this->assertEquals($data['name'], $bill->name); + $this->assertEquals(1, $bill->transaction_currency_id); $this->assertEquals($data['amount_min'], $bill->amount_min); $this->assertEquals($data['repeat_freq'], $bill->repeat_freq); $this->assertEquals(0, $bill->notes()->count()); @@ -121,7 +217,8 @@ class BillFactoryTest extends TestCase */ public function testFindById(): void { - $existing = $this->user()->piggyBanks()->first(); + $currencyFactory = $this->mock(TransactionCurrencyFactory::class); + $existing = $this->user()->piggyBanks()->first(); /** @var BillFactory $factory */ $factory = app(BillFactory::class); $factory->setUser($this->user()); @@ -137,7 +234,8 @@ class BillFactoryTest extends TestCase */ public function testFindByName(): void { - $existing = $this->user()->bills()->first(); + $currencyFactory = $this->mock(TransactionCurrencyFactory::class); + $existing = $this->user()->bills()->first(); /** @var BillFactory $factory */ $factory = app(BillFactory::class); $factory->setUser($this->user()); @@ -154,6 +252,7 @@ class BillFactoryTest extends TestCase */ public function testFindByUnknownName(): void { + $currencyFactory = $this->mock(TransactionCurrencyFactory::class); /** @var BillFactory $factory */ $factory = app(BillFactory::class); $factory->setUser($this->user()); @@ -170,6 +269,7 @@ class BillFactoryTest extends TestCase */ public function testFindNull(): void { + $currencyFactory = $this->mock(TransactionCurrencyFactory::class); /** @var BillFactory $factory */ $factory = app(BillFactory::class); $factory->setUser($this->user());