diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php index a881a38585..ecec9f9c09 100644 --- a/app/Transformers/AccountTransformer.php +++ b/app/Transformers/AccountTransformer.php @@ -132,6 +132,8 @@ class AccountTransformer extends TransformerAbstract } /** + * Transform the account. + * * @param Account $account * * @return array diff --git a/app/Transformers/BillTransformer.php b/app/Transformers/BillTransformer.php index fd366ed877..3975cdd60c 100644 --- a/app/Transformers/BillTransformer.php +++ b/app/Transformers/BillTransformer.php @@ -32,6 +32,7 @@ use Illuminate\Support\Collection; use League\Fractal\Resource\Collection as FractalCollection; use League\Fractal\Resource\Item; use League\Fractal\TransformerAbstract; +use Log; use Symfony\Component\HttpFoundation\ParameterBag; /** @@ -212,13 +213,18 @@ class BillTransformer extends TransformerAbstract } /** + * Get the data the bill was paid and predict the next expected match. + * * @param Bill $bill * * @return array */ protected function paidData(Bill $bill): array { + Log::debug(sprintf('Now in paidData for bill #%d', $bill->id)); if (is_null($this->parameters->get('start')) || is_null($this->parameters->get('end'))) { + Log::debug('parameters are NULL, return empty array'); + return [ 'paid_dates' => [], 'next_expected_match' => null, @@ -228,7 +234,8 @@ class BillTransformer extends TransformerAbstract /** @var BillRepositoryInterface $repository */ $repository = app(BillRepositoryInterface::class); $repository->setUser($bill->user); - $set = $repository->getPaidDatesInRange($bill, $this->parameters->get('start'), $this->parameters->get('end')); + $set = $repository->getPaidDatesInRange($bill, $this->parameters->get('start'), $this->parameters->get('end')); + Log::debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count())); $simple = $set->map( function (Carbon $date) { return $date->format('Y-m-d'); diff --git a/app/Transformers/BudgetTransformer.php b/app/Transformers/BudgetTransformer.php index fb3cf05b4e..baf60542e4 100644 --- a/app/Transformers/BudgetTransformer.php +++ b/app/Transformers/BudgetTransformer.php @@ -24,7 +24,11 @@ declare(strict_types=1); namespace FireflyIII\Transformers; +use FireflyIII\Helpers\Collector\JournalCollector; use FireflyIII\Models\Budget; +use Illuminate\Support\Collection; +use League\Fractal\Resource\Collection as FractalCollection; +use League\Fractal\Resource\Item; use League\Fractal\TransformerAbstract; use Symfony\Component\HttpFoundation\ParameterBag; @@ -50,7 +54,9 @@ class BudgetTransformer extends TransformerAbstract protected $parameters; /** - * BillTransformer constructor. + * BudgetTransformer constructor. + * + * @codeCoverageIgnore * * @param ParameterBag $parameters */ @@ -60,6 +66,48 @@ class BudgetTransformer extends TransformerAbstract } /** + * Include any transactions. + * + * @param Budget $budget + * + * @codeCoverageIgnore + * @return FractalCollection + */ + public function includeTransactions(Budget $budget): FractalCollection + { + $pageSize = intval(app('preferences')->getForUser($budget->user, 'listPageSize', 50)->data); + + // journals always use collector and limited using URL parameters. + $collector = new JournalCollector; + $collector->setUser($budget->user); + $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation(); + $collector->setAllAssetAccounts(); + $collector->setBudgets(new Collection([$budget])); + if (!is_null($this->parameters->get('start')) && !is_null($this->parameters->get('end'))) { + $collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); + } + $collector->setLimit($pageSize)->setPage($this->parameters->get('page')); + $journals = $collector->getJournals(); + + return $this->collection($journals, new TransactionTransformer($this->parameters), 'transactions'); + } + + /** + * Include the user. + * + * @param Budget $budget + * + * @codeCoverageIgnore + * @return Item + */ + public function includeUser(Budget $budget): Item + { + return $this->item($budget->user, new UserTransformer($this->parameters), 'users'); + } + + /** + * Transform a budget. + * * @param Budget $budget * * @return array @@ -70,6 +118,7 @@ class BudgetTransformer extends TransformerAbstract 'id' => (int)$budget->id, 'updated_at' => $budget->updated_at->toAtomString(), 'created_at' => $budget->created_at->toAtomString(), + 'active' => intval($budget->active) === 1, 'name' => $budget->name, 'links' => [ [ diff --git a/app/Transformers/CategoryTransformer.php b/app/Transformers/CategoryTransformer.php index 3d78fbfaf0..ac31d4ade5 100644 --- a/app/Transformers/CategoryTransformer.php +++ b/app/Transformers/CategoryTransformer.php @@ -24,7 +24,11 @@ declare(strict_types=1); namespace FireflyIII\Transformers; +use FireflyIII\Helpers\Collector\JournalCollector; use FireflyIII\Models\Category; +use Illuminate\Support\Collection; +use League\Fractal\Resource\Collection as FractalCollection; +use League\Fractal\Resource\Item; use League\Fractal\TransformerAbstract; use Symfony\Component\HttpFoundation\ParameterBag; @@ -38,7 +42,7 @@ class CategoryTransformer extends TransformerAbstract * * @var array */ - protected $availableIncludes = ['user','transactions']; + protected $availableIncludes = ['user', 'transactions']; /** * List of resources to automatically include * @@ -50,7 +54,9 @@ class CategoryTransformer extends TransformerAbstract protected $parameters; /** - * BillTransformer constructor. + * CategoryTransformer constructor. + * + * @codeCoverageIgnore * * @param ParameterBag $parameters */ @@ -60,6 +66,48 @@ class CategoryTransformer extends TransformerAbstract } /** + * Include any transactions. + * + * @param Category $category + * + * @codeCoverageIgnore + * @return FractalCollection + */ + public function includeTransactions(Category $category): FractalCollection + { + $pageSize = intval(app('preferences')->getForUser($category->user, 'listPageSize', 50)->data); + + // journals always use collector and limited using URL parameters. + $collector = new JournalCollector; + $collector->setUser($category->user); + $collector->withOpposingAccount()->withCategoryInformation()->withCategoryInformation(); + $collector->setAllAssetAccounts(); + $collector->setCategories(new Collection([$category])); + if (!is_null($this->parameters->get('start')) && !is_null($this->parameters->get('end'))) { + $collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); + } + $collector->setLimit($pageSize)->setPage($this->parameters->get('page')); + $journals = $collector->getJournals(); + + return $this->collection($journals, new TransactionTransformer($this->parameters), 'transactions'); + } + + /** + * Include the user. + * + * @param Category $category + * + * @codeCoverageIgnore + * @return Item + */ + public function includeUser(Category $category): Item + { + return $this->item($category->user, new UserTransformer($this->parameters), 'users'); + } + + /** + * Convert category. + * * @param Category $category * * @return array diff --git a/app/Transformers/JournalMetaTransformer.php b/app/Transformers/JournalMetaTransformer.php index eb766523a6..3a23582d68 100644 --- a/app/Transformers/JournalMetaTransformer.php +++ b/app/Transformers/JournalMetaTransformer.php @@ -24,7 +24,10 @@ declare(strict_types=1); namespace FireflyIII\Transformers; +use FireflyIII\Helpers\Collector\JournalCollector; use FireflyIII\Models\TransactionJournalMeta; +use Illuminate\Support\Collection; +use League\Fractal\Resource\Collection as FractalCollection; use League\Fractal\TransformerAbstract; use Symfony\Component\HttpFoundation\ParameterBag; @@ -38,7 +41,7 @@ class JournalMetaTransformer extends TransformerAbstract * * @var array */ - protected $availableIncludes = ['journal']; + protected $availableIncludes = ['transactions']; /** * List of resources to automatically include * @@ -50,7 +53,9 @@ class JournalMetaTransformer extends TransformerAbstract protected $parameters; /** - * BillTransformer constructor. + * JournalMetaTransformer constructor. + * + * @codeCoverageIgnore * * @param ParameterBag $parameters */ @@ -60,6 +65,36 @@ class JournalMetaTransformer extends TransformerAbstract } /** + * Include any transactions. + * + * @param TransactionJournalMeta $meta + * + * @codeCoverageIgnore + * @return FractalCollection + */ + public function includeTransactions(TransactionJournalMeta $meta): FractalCollection + { + $journal = $meta->transactionJournal; + $pageSize = intval(app('preferences')->getForUser($journal->user, 'listPageSize', 50)->data); + + // journals always use collector and limited using URL parameters. + $collector = new JournalCollector; + $collector->setUser($journal->user); + $collector->withOpposingAccount()->withCategoryInformation()->withCategoryInformation(); + $collector->setAllAssetAccounts(); + $collector->setJournals(new Collection([$journal])); + if (!is_null($this->parameters->get('start')) && !is_null($this->parameters->get('end'))) { + $collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); + } + $collector->setLimit($pageSize)->setPage($this->parameters->get('page')); + $journals = $collector->getJournals(); + + return $this->collection($journals, new TransactionTransformer($this->parameters), 'transactions'); + } + + /** + * Convert meta object. + * * @param TransactionJournalMeta $meta * * @return array diff --git a/app/Transformers/PiggyBankEventTransformer.php b/app/Transformers/PiggyBankEventTransformer.php index f00afae74d..d83383d18e 100644 --- a/app/Transformers/PiggyBankEventTransformer.php +++ b/app/Transformers/PiggyBankEventTransformer.php @@ -24,8 +24,11 @@ declare(strict_types=1); namespace FireflyIII\Transformers; +use FireflyIII\Helpers\Collector\JournalCollector; use FireflyIII\Models\PiggyBankEvent; -use FireflyIII\Models\TransactionCurrency; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use Illuminate\Support\Collection; +use League\Fractal\Resource\Item; use League\Fractal\TransformerAbstract; use Symfony\Component\HttpFoundation\ParameterBag; @@ -39,7 +42,7 @@ class PiggyBankEventTransformer extends TransformerAbstract * * @var array */ - protected $availableIncludes = ['piggy_bank', 'transactions']; + protected $availableIncludes = ['piggy_bank', 'transaction']; /** * List of resources to automatically include * @@ -51,7 +54,9 @@ class PiggyBankEventTransformer extends TransformerAbstract protected $parameters; /** - * BillTransformer constructor. + * PiggyBankEventTransformer constructor. + * + * @codeCoverageIgnore * * @param ParameterBag $parameters */ @@ -61,6 +66,51 @@ class PiggyBankEventTransformer extends TransformerAbstract } /** + * Include piggy bank into end result. + * + * @codeCoverageIgnore + * + * @param PiggyBankEvent $event + * + * @return Item + */ + public function includePiggyBank(PiggyBankEvent $event): Item + { + return $this->item($event->piggyBank, new PiggyBankTransformer($this->parameters), 'piggy_banks'); + } + + /** + * Include transaction into end result. + * + * @codeCoverageIgnore + * + * @param PiggyBankEvent $event + * + * @return Item + */ + public function includeTransaction(PiggyBankEvent $event): Item + { + $journal = $event->transactionJournal; + $pageSize = intval(app('preferences')->getForUser($journal->user, 'listPageSize', 50)->data); + + // journals always use collector and limited using URL parameters. + $collector = new JournalCollector; + $collector->setUser($journal->user); + $collector->withOpposingAccount()->withCategoryInformation()->withCategoryInformation(); + $collector->setAllAssetAccounts(); + $collector->setJournals(new Collection([$journal])); + if (!is_null($this->parameters->get('start')) && !is_null($this->parameters->get('end'))) { + $collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); + } + $collector->setLimit($pageSize)->setPage($this->parameters->get('page')); + $journals = $collector->getJournals(); + + return $this->item($journals->first(), new TransactionTransformer($this->parameters), 'transactions'); + } + + /** + * Convert piggy bank event. + * * @param PiggyBankEvent $event * * @return array @@ -71,7 +121,10 @@ class PiggyBankEventTransformer extends TransformerAbstract $currencyId = intval($account->getMeta('currency_id')); $decimalPlaces = 2; if ($currencyId > 0) { - $currency = TransactionCurrency::find($currencyId); + /** @var CurrencyRepositoryInterface $repository */ + $repository = app(CurrencyRepositoryInterface::class); + $repository->setUser($account->user); + $currency = $repository->findNull($currencyId); $decimalPlaces = $currency->decimal_places; } diff --git a/app/Transformers/PiggyBankTransformer.php b/app/Transformers/PiggyBankTransformer.php index 817549d1f8..55ba083cf6 100644 --- a/app/Transformers/PiggyBankTransformer.php +++ b/app/Transformers/PiggyBankTransformer.php @@ -26,6 +26,10 @@ namespace FireflyIII\Transformers; use FireflyIII\Models\Note; use FireflyIII\Models\PiggyBank; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; +use League\Fractal\Resource\Collection as FractalCollection; +use League\Fractal\Resource\Item; use League\Fractal\TransformerAbstract; use Symfony\Component\HttpFoundation\ParameterBag; @@ -51,7 +55,9 @@ class PiggyBankTransformer extends TransformerAbstract protected $parameters; /** - * BillTransformer constructor. + * PiggyBankTransformer constructor. + * + * @codeCoverageIgnore * * @param ParameterBag $parameters */ @@ -61,20 +67,88 @@ class PiggyBankTransformer extends TransformerAbstract } /** + * Include account. + * + * @codeCoverageIgnore + * + * @param PiggyBank $piggyBank + * + * @return Item + */ + public function includeAccount(PiggyBank $piggyBank): Item + { + return $this->item($piggyBank->account, new AccountTransformer($this->parameters), 'accounts'); + } + + /** + * Include events. + * + * @codeCoverageIgnore + * + * @param PiggyBank $piggyBank + * + * @return FractalCollection + */ + public function includePiggyBankEvents(PiggyBank $piggyBank): FractalCollection + { + return $this->collection($piggyBank->piggyBankEvents, new PiggyBankEventTransformer($this->parameters), 'piggy_bank_events'); + } + + /** + * Include the user. + * + * @param PiggyBank $piggyBank + * + * @codeCoverageIgnore + * @return Item + */ + public function includeUser(PiggyBank $piggyBank): Item + { + return $this->item($piggyBank->account->user, new UserTransformer($this->parameters), 'users'); + } + + /** + * Transform the piggy bank. + * * @param PiggyBank $piggyBank * * @return array */ public function transform(PiggyBank $piggyBank): array { + $account = $piggyBank->account; + $currencyId = intval($account->getMeta('currency_id')); + $decimalPlaces = 2; + if ($currencyId > 0) { + /** @var CurrencyRepositoryInterface $repository */ + $repository = app(CurrencyRepositoryInterface::class); + $repository->setUser($account->user); + $currency = $repository->findNull($currencyId); + $decimalPlaces = $currency->decimal_places; + } - $data = [ - 'id' => (int)$piggyBank->id, - 'updated_at' => $piggyBank->updated_at->toAtomString(), - 'created_at' => $piggyBank->created_at->toAtomString(), - 'name' => $piggyBank->name, - 'notes' => null, - 'links' => [ + // get currently saved amount: + /** @var PiggyBankRepositoryInterface $piggyRepos */ + $piggyRepos = app(PiggyBankRepositoryInterface::class); + $piggyRepos->setUser($account->user); + $currentAmount = round($piggyRepos->getCurrentAmount($piggyBank), $decimalPlaces); + + $startDate = is_null($piggyBank->startdate) ? null : $piggyBank->startdate->format('Y-m-d'); + $targetDate = is_null($piggyBank->targetdate) ? null : $piggyBank->targetdate->format('Y-m-d'); + $targetAmount = round($piggyBank->targetamount, $decimalPlaces); + $data = [ + 'id' => (int)$piggyBank->id, + 'updated_at' => $piggyBank->updated_at->toAtomString(), + 'created_at' => $piggyBank->created_at->toAtomString(), + 'name' => $piggyBank->name, + 'target_amount' => $targetAmount, + 'current_amount' => $currentAmount, + 'startdate' => $startDate, + 'targetdate' => $targetDate, + 'order' => (int)$piggyBank->order, + 'active' => intval($piggyBank->active) === 1, + 'notes' => null, + 'links' => [ [ 'rel' => 'self', 'uri' => '/piggy_banks/' . $piggyBank->id, diff --git a/app/Transformers/TagTransformer.php b/app/Transformers/TagTransformer.php index 11f74c904c..32f64efe06 100644 --- a/app/Transformers/TagTransformer.php +++ b/app/Transformers/TagTransformer.php @@ -24,7 +24,10 @@ declare(strict_types=1); namespace FireflyIII\Transformers; +use FireflyIII\Helpers\Collector\JournalCollector; use FireflyIII\Models\Tag; +use League\Fractal\Resource\Collection as FractalCollection; +use League\Fractal\Resource\Item; use League\Fractal\TransformerAbstract; use Symfony\Component\HttpFoundation\ParameterBag; @@ -38,7 +41,7 @@ class TagTransformer extends TransformerAbstract * * @var array */ - protected $availableIncludes = ['user']; + protected $availableIncludes = ['user', 'transactions']; /** * List of resources to automatically include * @@ -50,7 +53,9 @@ class TagTransformer extends TransformerAbstract protected $parameters; /** - * BillTransformer constructor. + * TagTransformer constructor. + * + * @codeCoverageIgnore * * @param ParameterBag $parameters */ @@ -60,18 +65,67 @@ class TagTransformer extends TransformerAbstract } /** + * Include any transactions. + * + * @param Tag $tag + * + * @codeCoverageIgnore + * @return FractalCollection + */ + public function includeTransactions(Tag $tag): FractalCollection + { + $pageSize = intval(app('preferences')->getForUser($tag->user, 'listPageSize', 50)->data); + + // journals always use collector and limited using URL parameters. + $collector = new JournalCollector; + $collector->setUser($tag->user); + $collector->withOpposingAccount()->withCategoryInformation()->withCategoryInformation(); + $collector->setAllAssetAccounts(); + $collector->setTag($tag); + if (!is_null($this->parameters->get('start')) && !is_null($this->parameters->get('end'))) { + $collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); + } + $collector->setLimit($pageSize)->setPage($this->parameters->get('page')); + $journals = $collector->getJournals(); + + return $this->collection($journals, new TransactionTransformer($this->parameters), 'transactions'); + } + + /** + * Include the user. + * + * @param Tag $tag + * + * @codeCoverageIgnore + * @return Item + */ + public function includeUser(Tag $tag): Item + { + return $this->item($tag->user, new UserTransformer($this->parameters), 'users'); + } + + /** + * Transform a tag. + * * @param Tag $tag * * @return array */ public function transform(Tag $tag): array { + $date = is_null($tag->date) ? null : $tag->date->format('Y-m-d'); $data = [ - 'id' => (int)$tag->id, - 'updated_at' => $tag->updated_at->toAtomString(), - 'created_at' => $tag->created_at->toAtomString(), - 'tag' => $tag->tag, - 'links' => [ + 'id' => (int)$tag->id, + 'updated_at' => $tag->updated_at->toAtomString(), + 'created_at' => $tag->created_at->toAtomString(), + 'tag' => $tag->tag, + 'tag_mode' => $tag->tagMode, + 'date' => $date, + 'description' => $tag->description === '' ? null : $tag->description, + 'latitude' => (float)$tag->latitude, + 'longitude' => (float)$tag->longitude, + 'zoom_level' => (int)$tag->zoomLevel, + 'links' => [ [ 'rel' => 'self', 'uri' => '/tags/' . $tag->id, diff --git a/app/Transformers/TransactionTransformer.php b/app/Transformers/TransactionTransformer.php index 7cc92d00cb..ad91f99cc6 100644 --- a/app/Transformers/TransactionTransformer.php +++ b/app/Transformers/TransactionTransformer.php @@ -54,7 +54,9 @@ class TransactionTransformer extends TransformerAbstract protected $parameters; /** - * BillTransformer constructor. + * TransactionTransformer constructor. + * + * @codeCoverageIgnore * * @param ParameterBag $parameters */ @@ -64,6 +66,10 @@ class TransactionTransformer extends TransformerAbstract } /** + * Include attachments. + * + * @codeCoverageIgnore + * * @param Transaction $transaction * * @return FractalCollection @@ -74,6 +80,10 @@ class TransactionTransformer extends TransformerAbstract } /** + * Include meta data + * + * @codeCoverageIgnore + * * @param Transaction $transaction * * @return FractalCollection @@ -86,6 +96,10 @@ class TransactionTransformer extends TransformerAbstract } /** + * Include piggy bank events + * + * @codeCoverageIgnore + * * @param Transaction $transaction * * @return FractalCollection @@ -98,6 +112,10 @@ class TransactionTransformer extends TransformerAbstract } /** + * Include tags + * + * @codeCoverageIgnore + * * @param Transaction $transaction * * @return FractalCollection @@ -110,6 +128,10 @@ class TransactionTransformer extends TransformerAbstract } /** + * Include the user. + * + * @codeCoverageIgnore + * * @param Transaction $transaction * * @return \League\Fractal\Resource\Item @@ -121,6 +143,8 @@ class TransactionTransformer extends TransformerAbstract /** + * Transform the journal. + * * @param Transaction $transaction * * @return array @@ -128,6 +152,21 @@ class TransactionTransformer extends TransformerAbstract */ public function transform(Transaction $transaction): array { + $categoryId = null; + $categoryName = null; + $budgetId = null; + $budgetName = null; + $categoryId = is_null($transaction->transaction_category_id) ? $transaction->transaction_journal_category_id + : $transaction->transaction_category_id; + $categoryName = is_null($transaction->transaction_category_name) ? $transaction->transaction_journal_category_name + : $transaction->transaction_category_name; + + if ($transaction->transaction_type_type === TransactionType::WITHDRAWAL) { + $budgetId = is_null($transaction->transaction_budget_id) ? $transaction->transaction_journal_budget_id + : $transaction->transaction_budget_id; + $budgetName = is_null($transaction->transaction_budget_name) ? $transaction->transaction_journal_budget_name + : $transaction->transaction_budget_name; + } $data = [ 'id' => (int)$transaction->id, 'updated_at' => $transaction->updated_at->toAtomString(), @@ -138,7 +177,7 @@ class TransactionTransformer extends TransformerAbstract 'identifier' => $transaction->identifier, 'journal_id' => (int)$transaction->journal_id, 'reconciled' => (bool)$transaction->reconciled, - 'amount' => round($transaction->transaction_amount, $transaction->transaction_currency_dp), + 'amount' => round($transaction->transaction_amount, intval($transaction->transaction_currency_dp)), 'currency_id' => $transaction->transaction_currency_id, 'currency_code' => $transaction->transaction_currency_code, 'currency_dp' => $transaction->transaction_currency_dp, @@ -148,14 +187,10 @@ class TransactionTransformer extends TransformerAbstract 'foreign_currency_dp' => $transaction->foreign_currency_dp, 'bill_id' => $transaction->bill_id, 'bill_name' => $transaction->bill_name, - 'category_id' => is_null($transaction->transaction_category_id) ? $transaction->transaction_journal_category_id - : $transaction->transaction_category_id, - 'category_name' => is_null($transaction->transaction_category_name) ? $transaction->transaction_journal_category_name - : $transaction->transaction_category_name, - 'budget_id' => is_null($transaction->transaction_budget_id) ? $transaction->transaction_journal_budget_id - : $transaction->transaction_budget_id, - 'budget_name' => is_null($transaction->transaction_budget_name) ? $transaction->transaction_journal_budget_name - : $transaction->transaction_budget_name, + 'category_id' => $categoryId, + 'category_name' => $categoryName, + 'budget_id' => $budgetId, + 'budget_name' => $budgetName, 'links' => [ [ 'rel' => 'self', @@ -166,12 +201,12 @@ class TransactionTransformer extends TransformerAbstract // expand foreign amount: if (!is_null($transaction->transaction_foreign_amount)) { - $data['foreign_amount'] = round($transaction->transaction_foreign_amount, $transaction->foreign_currency_dp); + $data['foreign_amount'] = round($transaction->transaction_foreign_amount, intval($transaction->foreign_currency_dp)); } // switch on type for consistency - switch (true) { - case TransactionType::WITHDRAWAL === $transaction->transaction_type_type: + switch ($transaction->transaction_type_type) { + case TransactionType::WITHDRAWAL: $data['source_id'] = $transaction->account_id; $data['source_name'] = $transaction->account_name; $data['source_iban'] = $transaction->account_iban; @@ -181,7 +216,10 @@ class TransactionTransformer extends TransformerAbstract $data['destination_iban'] = $transaction->opposing_account_iban; $data['destination_type'] = $transaction->opposing_account_type; break; - case TransactionType::DEPOSIT === $transaction->transaction_type_type: + case TransactionType::DEPOSIT: + case TransactionType::TRANSFER: + case TransactionType::OPENING_BALANCE: + case TransactionType::RECONCILIATION: $data['source_id'] = $transaction->opposing_account_id; $data['source_name'] = $transaction->opposing_account_name; $data['source_iban'] = $transaction->opposing_account_iban; @@ -191,30 +229,12 @@ class TransactionTransformer extends TransformerAbstract $data['destination_iban'] = $transaction->account_iban; $data['destination_type'] = $transaction->account_type; break; - case TransactionType::TRANSFER === $transaction->transaction_type_type && bccomp($transaction->transaction_amount, '0') > 0: - $data['source_id'] = $transaction->opposing_account_id; - $data['source_name'] = $transaction->opposing_account_name; - $data['source_iban'] = $transaction->opposing_account_iban; - $data['source_type'] = $transaction->opposing_account_type; - $data['destination_id'] = $transaction->account_id; - $data['destination_name'] = $transaction->account_name; - $data['destination_iban'] = $transaction->account_iban; - $data['destination_type'] = $transaction->account_type; - break; - case TransactionType::TRANSFER === $transaction->transaction_type_type && bccomp($transaction->transaction_amount, '0') < 0: - $data['source_id'] = $transaction->account_id; - $data['source_name'] = $transaction->account_name; - $data['source_iban'] = $transaction->account_iban; - $data['source_type'] = $transaction->account_type; - $data['destination_id'] = $transaction->opposing_account_id; - $data['destination_name'] = $transaction->opposing_account_name; - $data['destination_iban'] = $transaction->opposing_account_iban; - $data['destination_type'] = $transaction->opposing_account_type; - $data['amount'] = $data['amount'] * -1; - $data['foreign_amount'] = is_null($data['foreign_amount']) ? null : $data['foreign_amount'] * -1; - break; default: - throw new FireflyException(sprintf('Cannot handle % s!', $transaction->transaction_type_type)); + // @codeCoverageIgnoreStart + throw new FireflyException( + sprintf('Transaction transformer cannot handle transactions of type "%s"!', $transaction->transaction_type_type) + ); + // @codeCoverageIgnoreEnd } diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php index ff48d36386..e07fb51908 100644 --- a/app/Transformers/UserTransformer.php +++ b/app/Transformers/UserTransformer.php @@ -38,7 +38,9 @@ class UserTransformer extends TransformerAbstract protected $parameters; /** - * BillTransformer constructor. + * UserTransformer constructor. + * + * @codeCoverageIgnore * * @param ParameterBag $parameters */ @@ -48,6 +50,8 @@ class UserTransformer extends TransformerAbstract } /** + * Transform user. + * * @param User $user * * @return array diff --git a/tests/Unit/Transformers/BillTransformerTest.php b/tests/Unit/Transformers/BillTransformerTest.php index 165f6fe5c5..4a6863f2d8 100644 --- a/tests/Unit/Transformers/BillTransformerTest.php +++ b/tests/Unit/Transformers/BillTransformerTest.php @@ -75,7 +75,7 @@ class BillTransformerTest extends TestCase public function testNote() { - $bill = Bill::create( + $bill = Bill::create( [ 'user_id' => $this->user()->id, 'name' => 'Some bill ' . rand(1, 10000), @@ -88,8 +88,8 @@ class BillTransformerTest extends TestCase 'active' => 1, ] ); - $noteText = 'I are a note ' . rand(1, 10000); - $note = Note::create( + $noteText = 'I are a note ' . rand(1, 10000); + Note::create( [ 'noteable_id' => $bill->id, 'noteable_type' => Bill::class, @@ -118,7 +118,7 @@ class BillTransformerTest extends TestCase // mock stuff $repository = $this->mock(BillRepositoryInterface::class); $repository->shouldReceive('setUser')->andReturnSelf(); - $repository->shouldReceive('getPaidDatesInRange')->andReturn(new Collection([new Carbon])); + $repository->shouldReceive('getPaidDatesInRange')->andReturn(new Collection([new Carbon('2018-01-02')])); $bill = Bill::create( [ 'user_id' => $this->user()->id, @@ -127,7 +127,7 @@ class BillTransformerTest extends TestCase 'amount_min' => 12.34, 'amount_max' => 45.67, 'date' => '2018-01-02', - 'repeat_freq' => 'weekly', + 'repeat_freq' => 'monthly', 'skip' => 0, 'active' => 1, ] @@ -140,6 +140,10 @@ class BillTransformerTest extends TestCase $this->assertEquals($bill->name, $result['name']); $this->assertTrue($result['active']); + $this->assertCount(1, $result['pay_dates']); + $this->assertEquals('2018-01-02', $result['pay_dates'][0]); + $this->assertCount(1, $result['paid_dates']); + $this->assertEquals('2018-01-02', $result['paid_dates'][0]); } } \ No newline at end of file diff --git a/tests/Unit/Transformers/BudgetTransformerTest.php b/tests/Unit/Transformers/BudgetTransformerTest.php new file mode 100644 index 0000000000..9c8eee637b --- /dev/null +++ b/tests/Unit/Transformers/BudgetTransformerTest.php @@ -0,0 +1,58 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Transformers; + +use FireflyIII\Models\Budget; +use FireflyIII\Transformers\BudgetTransformer; +use Symfony\Component\HttpFoundation\ParameterBag; +use Tests\TestCase; + + +/** + * Class BudgetTransformerTest + */ +class BudgetTransformerTest extends TestCase +{ + /** + * Basic coverage + * + * @covers \FireflyIII\Transformers\BudgetTransformer::transform + */ + public function testBasic() + { + + $budget = Budget::create( + [ + 'user_id' => $this->user()->id, + 'name' => 'Some budget ' . rand(1, 10000), + 'active' => 1, + ] + ); + $transformer = new BudgetTransformer(new ParameterBag); + $result = $transformer->transform($budget); + + $this->assertEquals($budget->name, $result['name']); + $this->assertTrue($result['active']); + } +} \ No newline at end of file diff --git a/tests/Unit/Transformers/CategoryTransformerTest.php b/tests/Unit/Transformers/CategoryTransformerTest.php new file mode 100644 index 0000000000..5616d654c7 --- /dev/null +++ b/tests/Unit/Transformers/CategoryTransformerTest.php @@ -0,0 +1,57 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Transformers; + +use FireflyIII\Models\Category; +use FireflyIII\Transformers\CategoryTransformer; +use Symfony\Component\HttpFoundation\ParameterBag; +use Tests\TestCase; + + +/** + * Class CategoryTransformerTest + */ +class CategoryTransformerTest extends TestCase +{ + /** + * Basic coverage + * + * @covers \FireflyIII\Transformers\CategoryTransformer::transform + */ + public function testBasic() + { + + $category = Category::create( + [ + 'user_id' => $this->user()->id, + 'name' => 'Some budget ' . rand(1, 10000), + 'active' => 1, + ] + ); + $transformer = new CategoryTransformer(new ParameterBag); + $result = $transformer->transform($category); + + $this->assertEquals($category->name, $result['name']); + } +} \ No newline at end of file diff --git a/tests/Unit/Transformers/JournalMetaTransformerTest.php b/tests/Unit/Transformers/JournalMetaTransformerTest.php new file mode 100644 index 0000000000..59681efbbc --- /dev/null +++ b/tests/Unit/Transformers/JournalMetaTransformerTest.php @@ -0,0 +1,60 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Transformers; + +use FireflyIII\Models\TransactionJournalMeta; +use FireflyIII\Transformers\JournalMetaTransformer; +use Symfony\Component\HttpFoundation\ParameterBag; +use Tests\TestCase; + +/** + * Class JournalMetaTransformerTest + */ +class JournalMetaTransformerTest extends TestCase +{ + /** + * Basic coverage + * + * @covers \FireflyIII\Transformers\JournalMetaTransformer::transform + */ + public function testBasic() + { + $data = 'Lots of data'; + $hash = hash('sha256', json_encode($data)); + $meta = TransactionJournalMeta::create( + [ + 'transaction_journal_id' => 1, + 'name' => 'someField', + 'data' => $data, + ] + ); + + $transformer = new JournalMetaTransformer(new ParameterBag); + $result = $transformer->transform($meta); + + $this->assertEquals($meta->name, $result['name']); + $this->assertEquals($hash, $result['hash']); + } + +} \ No newline at end of file diff --git a/tests/Unit/Transformers/PiggyBankEventTransformerTest.php b/tests/Unit/Transformers/PiggyBankEventTransformerTest.php new file mode 100644 index 0000000000..aa9688b0c3 --- /dev/null +++ b/tests/Unit/Transformers/PiggyBankEventTransformerTest.php @@ -0,0 +1,144 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Transformers; + +use FireflyIII\Models\Account; +use FireflyIII\Models\AccountMeta; +use FireflyIII\Models\PiggyBank; +use FireflyIII\Models\PiggyBankEvent; +use FireflyIII\Models\TransactionCurrency; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use FireflyIII\Transformers\PiggyBankEventTransformer; +use Symfony\Component\HttpFoundation\ParameterBag; +use Tests\TestCase; + +/** + * Class PiggyBankEventTransformerTest + */ +class PiggyBankEventTransformerTest extends TestCase +{ + /** + * Basic test with no meta data. + * + * @covers \FireflyIII\Transformers\PiggyBankEventTransformer::transform + */ + public function testBasic() + { + // make new account: + $account = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + $piggy = PiggyBank::create( + [ + 'account_id' => $account->id, + 'name' => 'Some random piggy #' . rand(1, 10000), + 'targetamount' => '1000', + 'startdate' => '2018-01-01', + 'targetdate' => '2018-01-31', + 'order' => 1, + 'active' => 1, + ] + ); + $event = PiggyBankEvent::create( + [ + 'piggy_bank_id' => $piggy->id, + 'date' => '2018-01-01', + 'amount' => '123.45', + ] + ); + + $transformer = new PiggyBankEventTransformer(new ParameterBag); + $result = $transformer->transform($event); + $this->assertEquals($event->id, $result['id']); + $this->assertEquals(123.45, $result['amount']); + } + + /** + * Basic test with currency meta data. + * + * @covers \FireflyIII\Transformers\PiggyBankEventTransformer::transform + */ + public function testBasicCurrency() + { + // mock repository. + $repository = $this->mock(CurrencyRepositoryInterface::class); + $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1))->once(); + + // make new account: + $account = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // meta + $accountMeta = AccountMeta::create( + [ + 'account_id' => $account->id, + 'name' => 'currency_id', + 'data' => 1, + ] + ); + + $piggy = PiggyBank::create( + [ + 'account_id' => $account->id, + 'name' => 'Some random piggy #' . rand(1, 10000), + 'targetamount' => '1000', + 'startdate' => '2018-01-01', + 'targetdate' => '2018-01-31', + 'order' => 1, + 'active' => 1, + ] + ); + $event = PiggyBankEvent::create( + [ + 'piggy_bank_id' => $piggy->id, + 'date' => '2018-01-01', + 'amount' => '123.45', + ] + ); + + $transformer = new PiggyBankEventTransformer(new ParameterBag); + $result = $transformer->transform($event); + $this->assertEquals($event->id, $result['id']); + $this->assertEquals(123.45, $result['amount']); + } + +} \ No newline at end of file diff --git a/tests/Unit/Transformers/PiggyBankTransformerTest.php b/tests/Unit/Transformers/PiggyBankTransformerTest.php new file mode 100644 index 0000000000..a830c0fccf --- /dev/null +++ b/tests/Unit/Transformers/PiggyBankTransformerTest.php @@ -0,0 +1,208 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Transformers; + +use FireflyIII\Models\Account; +use FireflyIII\Models\AccountMeta; +use FireflyIII\Models\Note; +use FireflyIII\Models\PiggyBank; +use FireflyIII\Models\TransactionCurrency; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; +use FireflyIII\Transformers\PiggyBankTransformer; +use Symfony\Component\HttpFoundation\ParameterBag; +use Tests\TestCase; + +/** + * Class PiggyBankTransformerTest + */ +class PiggyBankTransformerTest extends TestCase +{ + /** + * Test basic transformer. + * + * @covers \FireflyIII\Transformers\PiggyBankTransformer::transform() + */ + public function testBasic() + { + // mock repository: + $repository = $this->mock(PiggyBankRepositoryInterface::class); + $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('getCurrentAmount')->andReturn('12.34')->once(); + + // make new account and piggy + $account = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + $piggy = PiggyBank::create( + [ + 'account_id' => $account->id, + 'name' => 'Some random piggy #' . rand(1, 10000), + 'targetamount' => '1000', + 'startdate' => '2018-01-01', + 'targetdate' => '2018-01-31', + 'order' => 1, + 'active' => 1, + ] + ); + $transformer = new PiggyBankTransformer(new ParameterBag); + $result = $transformer->transform($piggy); + $this->assertTrue($result['active']); + $this->assertEquals(12.34, $result['current_amount']); + $this->assertEquals($piggy->name, $result['name']); + $this->assertEquals('', $result['notes']); + } + + /** + * Test basic transformer with currency preference + * + * @covers \FireflyIII\Transformers\PiggyBankTransformer::transform() + */ + public function testBasicWithCurrency() + { + // mock repository. + $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $currencyRepos->shouldReceive('setUser')->once(); + $currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1))->once(); + + // mock repository: + $repository = $this->mock(PiggyBankRepositoryInterface::class); + $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('getCurrentAmount')->andReturn('12.34')->once(); + + // make new account and piggy + $account = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + // meta + $accountMeta = AccountMeta::create( + [ + 'account_id' => $account->id, + 'name' => 'currency_id', + 'data' => 1, + ] + ); + + $piggy = PiggyBank::create( + [ + 'account_id' => $account->id, + 'name' => 'Some random piggy #' . rand(1, 10000), + 'targetamount' => '1000', + 'startdate' => '2018-01-01', + 'targetdate' => '2018-01-31', + 'order' => 1, + 'active' => 1, + ] + ); + $transformer = new PiggyBankTransformer(new ParameterBag); + $result = $transformer->transform($piggy); + $this->assertTrue($result['active']); + $this->assertEquals(12.34, $result['current_amount']); + $this->assertEquals($piggy->name, $result['name']); + $this->assertEquals('', $result['notes']); + } + + /** + * Test basic transformer with currency preference and a note + * + * @covers \FireflyIII\Transformers\PiggyBankTransformer::transform() + */ + public function testBasicWithCurrencyAndNote() + { + // mock repository. + $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $currencyRepos->shouldReceive('setUser')->once(); + $currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1))->once(); + + // mock repository: + $repository = $this->mock(PiggyBankRepositoryInterface::class); + $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('getCurrentAmount')->andReturn('12.34')->once(); + + // make new account and piggy + $account = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + // meta + $accountMeta = AccountMeta::create( + [ + 'account_id' => $account->id, + 'name' => 'currency_id', + 'data' => 1, + ] + ); + + $piggy = PiggyBank::create( + [ + 'account_id' => $account->id, + 'name' => 'Some random piggy #' . rand(1, 10000), + 'targetamount' => '1000', + 'startdate' => '2018-01-01', + 'targetdate' => '2018-01-31', + 'order' => 1, + 'active' => 1, + ] + ); + + // note: + Note::create( + [ + 'noteable_id' => $piggy->id, + 'noteable_type' => PiggyBank::class, + 'title' => null, + 'text' => 'I am a note.', + ] + ); + $transformer = new PiggyBankTransformer(new ParameterBag); + $result = $transformer->transform($piggy); + $this->assertTrue($result['active']); + $this->assertEquals(12.34, $result['current_amount']); + $this->assertEquals($piggy->name, $result['name']); + $this->assertEquals('I am a note.', $result['notes']); + } +} \ No newline at end of file diff --git a/tests/Unit/Transformers/TagTransformerTest.php b/tests/Unit/Transformers/TagTransformerTest.php new file mode 100644 index 0000000000..0d8d89ecb7 --- /dev/null +++ b/tests/Unit/Transformers/TagTransformerTest.php @@ -0,0 +1,64 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Transformers; + + +use FireflyIII\Models\Tag; +use FireflyIII\Transformers\TagTransformer; +use Symfony\Component\HttpFoundation\ParameterBag; +use Tests\TestCase; + +/** + * Class TagTransformerTest + */ +class TagTransformerTest extends TestCase +{ + /** + * Test basic tag transformer + * + * @covers \FireflyIII\Transformers\TagTransformer::transform + */ + public function testBasic() + { + $tag = Tag::create( + [ + 'user_id' => $this->user()->id, + 'tag' => 'Some tag ' . rand(1, 1000), + 'tagMode' => 'nothing', + 'date' => '2018-01-01', + 'description' => 'Some tag', + 'latitude' => 5.5, + 'longitude' => '6.6', + 'zoomLevel' => 3, + ] + ); + $transformer = new TagTransformer(new ParameterBag); + $result = $transformer->transform($tag); + $this->assertEquals('nothing', $result['tag_mode']); + $this->assertEquals($tag->tag, $result['tag']); + $this->assertEquals(5.5, $result['latitude']); + $this->assertEquals(6.6, $result['longitude']); + } + +} \ No newline at end of file diff --git a/tests/Unit/Transformers/TransactionTransformerTest.php b/tests/Unit/Transformers/TransactionTransformerTest.php new file mode 100644 index 0000000000..6aeab4d66b --- /dev/null +++ b/tests/Unit/Transformers/TransactionTransformerTest.php @@ -0,0 +1,1403 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Transformers; + + +use FireflyIII\Helpers\Collector\JournalCollector; +use FireflyIII\Helpers\Filter\NegativeAmountFilter; +use FireflyIII\Helpers\Filter\PositiveAmountFilter; +use FireflyIII\Models\Account; +use FireflyIII\Models\Budget; +use FireflyIII\Models\Category; +use FireflyIII\Models\Transaction; +use FireflyIII\Models\TransactionCurrency; +use FireflyIII\Models\TransactionJournal; +use FireflyIII\Models\TransactionType; +use FireflyIII\Transformers\TransactionTransformer; +use Illuminate\Support\Collection; +use Symfony\Component\HttpFoundation\ParameterBag; +use Tests\TestCase; + +/** + * todo: test foreign amount. + * todo: test transfer left, test transfer right. + * todo: test transaction description + * Class TransactionTransformerTest + */ +class TransactionTransformerTest extends TestCase +{ + /** + * Basic journal (withdrawal) + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testBasic() + { + // make new asset account: + $asset = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new expense account: + $expense = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 4, // expense account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create withdrawal + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 1, // withdrawal + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + // basic transactions + Transaction::create( + ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] + ); + Transaction::create( + ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] + ); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Withdrawal', $result['type']); + $this->assertEquals($journal->description, $result['description']); + + // budget and category: + $this->assertNull($result['budget_id']); + $this->assertNull($result['budget_name']); + $this->assertNull($result['category_id']); + $this->assertNull($result['category_name']); + + // source: + $this->assertEquals($asset->name, $result['source_name']); + $this->assertEquals($asset->iban, $result['source_iban']); + $this->assertEquals($asset->id, $result['source_id']); + $this->assertEquals('Asset account', $result['source_type']); + + // destination: + $this->assertEquals($expense->name, $result['destination_name']); + $this->assertEquals($expense->iban, $result['destination_iban']); + $this->assertEquals($expense->id, $result['destination_id']); + $this->assertEquals('Expense account', $result['destination_type']); + } + + /** + * Basic journal (withdrawal) + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testDeposit() + { + // make new asset account: + $asset = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new revenue account: + $revenue = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 5, // revenue account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create deposit + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 2, // deposit + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + // basic transactions + Transaction::create( + ['account_id' => $revenue->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] + ); + Transaction::create( + ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] + ); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Deposit', $result['type']); + + // budget and category: + $this->assertNull($result['budget_id']); + $this->assertNull($result['budget_name']); + $this->assertNull($result['category_id']); + $this->assertNull($result['category_name']); + + // source: + $this->assertEquals($revenue->name, $result['source_name']); + $this->assertEquals($revenue->iban, $result['source_iban']); + $this->assertEquals($revenue->id, $result['source_id']); + $this->assertEquals('Revenue account', $result['source_type']); + + // destination: + $this->assertEquals($asset->name, $result['destination_name']); + $this->assertEquals($asset->iban, $result['destination_iban']); + $this->assertEquals($asset->id, $result['destination_id']); + $this->assertEquals('Asset account', $result['destination_type']); + + } + + /** + * Deposit cannot have a budget + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testDepositBudget() + { + // make new asset account: + $asset = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new revenue account: + $revenue = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 5, // revenue account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create deposit + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 2, // deposit + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + // basic transactions + Transaction::create( + ['account_id' => $revenue->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] + ); + Transaction::create( + ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] + ); + + // create budget: + $budget = Budget::create( + [ + 'user_id' => $this->user()->id, + 'name' => 'Random budget #' . rand(1, 1000), + 'active' => 1, + ] + ); + $journal->budgets()->save($budget); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Deposit', $result['type']); + + // budget and category: + $this->assertNull($result['budget_id']); + $this->assertNull($result['budget_name']); + $this->assertNull($result['category_id']); + $this->assertNull($result['category_name']); + + // source: + $this->assertEquals($revenue->name, $result['source_name']); + $this->assertEquals($revenue->iban, $result['source_iban']); + $this->assertEquals($revenue->id, $result['source_id']); + $this->assertEquals('Revenue account', $result['source_type']); + + // destination: + $this->assertEquals($asset->name, $result['destination_name']); + $this->assertEquals($asset->iban, $result['destination_iban']); + $this->assertEquals($asset->id, $result['destination_id']); + $this->assertEquals('Asset account', $result['destination_type']); + } + + /** + * Basic journal (withdrawal) with a foreign amount. + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testForeignAmount() + { + // make new asset account: + $asset = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new expense account: + $expense = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 4, // expense account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create withdrawal + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 1, // withdrawal + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + // basic transactions + Transaction::create( + ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0, + 'foreign_amount' => -100, 'foreign_currency_id' => 2, + ] + ); + Transaction::create( + ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0, + 'foreign_amount' => 100, 'foreign_currency_id' => 2, + ] + ); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Withdrawal', $result['type']); + + // budget and category: + $this->assertNull($result['budget_id']); + $this->assertNull($result['budget_name']); + $this->assertNull($result['category_id']); + $this->assertNull($result['category_name']); + + // source: + $this->assertEquals($asset->name, $result['source_name']); + $this->assertEquals($asset->iban, $result['source_iban']); + $this->assertEquals($asset->id, $result['source_id']); + $this->assertEquals('Asset account', $result['source_type']); + + // destination: + $this->assertEquals($expense->name, $result['destination_name']); + $this->assertEquals($expense->iban, $result['destination_iban']); + $this->assertEquals($expense->id, $result['destination_id']); + $this->assertEquals('Expense account', $result['destination_type']); + + // foreign info: + $currency = TransactionCurrency::find(2); + $this->assertEquals(-100, $result['foreign_amount']); + $this->assertEquals($currency->code, $result['foreign_currency_code']); + } + + /** + * Basic journal (withdrawal) with a budget + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testJournalBudget() + { + // make new asset account: + $asset = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new expense account: + $expense = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 4, // expense account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create withdrawal + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 1, // withdrawal + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + + // create budget: + $budget = Budget::create( + [ + 'user_id' => $this->user()->id, + 'name' => 'Random budget #' . rand(1, 1000), + 'active' => 1, + ] + ); + $journal->budgets()->save($budget); + + // basic transactions + Transaction::create( + ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] + ); + Transaction::create( + ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] + ); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Withdrawal', $result['type']); + + // budget and category: + $this->assertNotNull($result['budget_id']); + $this->assertNotNull($result['budget_name']); + $this->assertNull($result['category_id']); + $this->assertNull($result['category_name']); + + // source: + $this->assertEquals($asset->name, $result['source_name']); + $this->assertEquals($asset->iban, $result['source_iban']); + $this->assertEquals($asset->id, $result['source_id']); + $this->assertEquals('Asset account', $result['source_type']); + + // destination: + $this->assertEquals($expense->name, $result['destination_name']); + $this->assertEquals($expense->iban, $result['destination_iban']); + $this->assertEquals($expense->id, $result['destination_id']); + $this->assertEquals('Expense account', $result['destination_type']); + + // budget ID and name: + $this->assertEquals($budget->id, $result['budget_id']); + $this->assertEquals($budget->name, $result['budget_name']); + } + + /** + * Basic journal (withdrawal) with a category + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testJournalCategory() + { + // make new asset account: + $asset = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new expense account: + $expense = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 4, // expense account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create withdrawal + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 1, // withdrawal + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + + // create category: + $category = Category::create( + [ + 'user_id' => $this->user()->id, + 'name' => 'Random category #' . rand(1, 1000), + 'active' => 1, + ] + ); + $journal->categories()->save($category); + + // basic transactions + Transaction::create( + ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] + ); + Transaction::create( + ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] + ); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Withdrawal', $result['type']); + + // budget and category: + $this->assertNull($result['budget_id']); + $this->assertNull($result['budget_name']); + $this->assertNotNull($result['category_id']); + $this->assertNotNull($result['category_name']); + + // source: + $this->assertEquals($asset->name, $result['source_name']); + $this->assertEquals($asset->iban, $result['source_iban']); + $this->assertEquals($asset->id, $result['source_id']); + $this->assertEquals('Asset account', $result['source_type']); + + // destination: + $this->assertEquals($expense->name, $result['destination_name']); + $this->assertEquals($expense->iban, $result['destination_iban']); + $this->assertEquals($expense->id, $result['destination_id']); + $this->assertEquals('Expense account', $result['destination_type']); + + // budget ID and name: + $this->assertEquals($category->id, $result['category_id']); + $this->assertEquals($category->name, $result['category_name']); + } + + /** + * Basic journal (opening balance) + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testOpeningBalanceNeg() + { + // make new asset account: + $asset = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new initial balance account: + $initial = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 6, // initial balance account + 'name' => 'Initial balance for ' . $asset->name, + 'virtual_balance' => 0, + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create withdrawal + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 4, // opening balance + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + // basic transactions (negative opening balance). + Transaction::create( + ['account_id' => $initial->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 100, 'identifier' => 0,] + ); + + Transaction::create( + ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -100, 'identifier' => 0,] + ); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Opening balance', $result['type']); + $this->assertEquals($journal->description, $result['description']); + + // budget and category: + $this->assertNull($result['budget_id']); + $this->assertNull($result['budget_name']); + $this->assertNull($result['category_id']); + $this->assertNull($result['category_name']); + + // source: + $this->assertEquals($asset->name, $result['source_name']); + $this->assertEquals($asset->iban, $result['source_iban']); + $this->assertEquals($asset->id, $result['source_id']); + $this->assertEquals('Asset account', $result['source_type']); + + // destination: + $this->assertEquals($initial->name, $result['destination_name']); + $this->assertEquals($initial->iban, $result['destination_iban']); + $this->assertEquals($initial->id, $result['destination_id']); + $this->assertEquals('Initial balance account', $result['destination_type']); + + } + + /** + * Basic journal (opening balance) + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testOpeningBalancePos() + { + // make new asset account: + $asset = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new initial balance account: + $initial = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 6, // initial balance account + 'name' => 'Initial balance for ' . $asset->name, + 'virtual_balance' => 0, + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create withdrawal + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 4, // opening balance + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + // basic transactions (positive opening balance). + + Transaction::create( + ['account_id' => $initial->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -100, 'identifier' => 0,] + ); + + Transaction::create( + ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 100, 'identifier' => 0,] + ); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Opening balance', $result['type']); + $this->assertEquals($journal->description, $result['description']); + + // budget and category: + $this->assertNull($result['budget_id']); + $this->assertNull($result['budget_name']); + $this->assertNull($result['category_id']); + $this->assertNull($result['category_name']); + + // source: + $this->assertEquals($initial->name, $result['source_name']); + $this->assertEquals($initial->iban, $result['source_iban']); + $this->assertEquals($initial->id, $result['source_id']); + $this->assertEquals('Initial balance account', $result['source_type']); + + // destination: + $this->assertEquals($asset->name, $result['destination_name']); + $this->assertEquals($asset->iban, $result['destination_iban']); + $this->assertEquals($asset->id, $result['destination_id']); + $this->assertEquals('Asset account', $result['destination_type']); + } + + /** + * Basic journal (reconciliation) + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testReconciliationNeg() + { + // make new asset account: + $asset = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new initial balance account: + $recon = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 10, // Reconciliation account + 'name' => 'Reconciliation for something', + 'virtual_balance' => 0, + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create withdrawal + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 5, // reconciliation + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + // basic transactions (negative reconciliation). + + Transaction::create( + ['account_id' => $recon->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 100, 'identifier' => 0,] + ); + + Transaction::create( + ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -100, 'identifier' => 0,] + ); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Reconciliation', $result['type']); + $this->assertEquals($journal->description, $result['description']); + + // budget and category: + $this->assertNull($result['budget_id']); + $this->assertNull($result['budget_name']); + $this->assertNull($result['category_id']); + $this->assertNull($result['category_name']); + + // source: + $this->assertEquals($asset->name, $result['source_name']); + $this->assertEquals($asset->iban, $result['source_iban']); + $this->assertEquals($asset->id, $result['source_id']); + $this->assertEquals('Asset account', $result['source_type']); + + // destination: + $this->assertEquals($recon->name, $result['destination_name']); + $this->assertEquals($recon->iban, $result['destination_iban']); + $this->assertEquals($recon->id, $result['destination_id']); + $this->assertEquals('Reconciliation account', $result['destination_type']); + } + + /** + * Basic journal (reconciliation) + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testReconciliationPos() + { + // make new asset account: + $asset = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new initial balance account: + $recon = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 10, // Reconciliation account + 'name' => 'Reconciliation for something', + 'virtual_balance' => 0, + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create withdrawal + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 5, // reconciliation + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + // basic transactions (positive reconciliation). + + Transaction::create( + ['account_id' => $recon->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -100, 'identifier' => 0,] + ); + + Transaction::create( + ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 100, 'identifier' => 0,] + ); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Reconciliation', $result['type']); + $this->assertEquals($journal->description, $result['description']); + + // budget and category: + $this->assertNull($result['budget_id']); + $this->assertNull($result['budget_name']); + $this->assertNull($result['category_id']); + $this->assertNull($result['category_name']); + + // source: + $this->assertEquals($recon->name, $result['source_name']); + $this->assertEquals($recon->iban, $result['source_iban']); + $this->assertEquals($recon->id, $result['source_id']); + $this->assertEquals('Reconciliation account', $result['source_type']); + + // destination: + $this->assertEquals($asset->name, $result['destination_name']); + $this->assertEquals($asset->iban, $result['destination_iban']); + $this->assertEquals($asset->id, $result['destination_id']); + $this->assertEquals('Asset account', $result['destination_type']); + } + + /** + * Basic journal (withdrawal) with budget on the transactions. + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testTransactionBudget() + { + // make new asset account: + $asset = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new expense account: + $expense = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 4, // expense account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create withdrawal + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 1, // withdrawal + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + + // create budget: + $budget = Budget::create( + [ + 'user_id' => $this->user()->id, + 'name' => 'Random budget #' . rand(1, 1000), + 'active' => 1, + ] + ); + + + // basic transactions + $one = Transaction::create( + ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] + ); + $two = Transaction::create( + ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] + ); + $one->budgets()->save($budget); + $two->budgets()->save($budget); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Withdrawal', $result['type']); + + // budget and category: + $this->assertNotNull($result['budget_id']); + $this->assertNotNull($result['budget_name']); + $this->assertNull($result['category_id']); + $this->assertNull($result['category_name']); + + // source: + $this->assertEquals($asset->name, $result['source_name']); + $this->assertEquals($asset->iban, $result['source_iban']); + $this->assertEquals($asset->id, $result['source_id']); + $this->assertEquals('Asset account', $result['source_type']); + + // destination: + $this->assertEquals($expense->name, $result['destination_name']); + $this->assertEquals($expense->iban, $result['destination_iban']); + $this->assertEquals($expense->id, $result['destination_id']); + $this->assertEquals('Expense account', $result['destination_type']); + + // budget ID and name: + $this->assertEquals($budget->id, $result['budget_id']); + $this->assertEquals($budget->name, $result['budget_name']); + } + + /** + * Basic journal (withdrawal) with a category on the transactions + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testTransactionCategory() + { + // make new asset account: + $asset = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new expense account: + $expense = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 4, // expense account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create withdrawal + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 1, // withdrawal + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + + // create category: + $category = Category::create( + [ + 'user_id' => $this->user()->id, + 'name' => 'Random category #' . rand(1, 1000), + 'active' => 1, + ] + ); + + // basic transactions + $one = Transaction::create( + ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] + ); + $two = Transaction::create( + ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] + ); + $one->categories()->save($category); + $two->categories()->save($category); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Withdrawal', $result['type']); + + // budget and category: + $this->assertNull($result['budget_id']); + $this->assertNull($result['budget_name']); + $this->assertNotNull($result['category_id']); + $this->assertNotNull($result['category_name']); + + // source: + $this->assertEquals($asset->name, $result['source_name']); + $this->assertEquals($asset->iban, $result['source_iban']); + $this->assertEquals($asset->id, $result['source_id']); + $this->assertEquals('Asset account', $result['source_type']); + + // destination: + $this->assertEquals($expense->name, $result['destination_name']); + $this->assertEquals($expense->iban, $result['destination_iban']); + $this->assertEquals($expense->id, $result['destination_id']); + $this->assertEquals('Expense account', $result['destination_type']); + + // budget ID and name: + $this->assertEquals($category->id, $result['category_id']); + $this->assertEquals($category->name, $result['category_name']); + } + + /** + * Basic journal (withdrawal) with a description for transactions. + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testTransactionDescription() + { + // make new asset account: + $asset = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new expense account: + $expense = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 4, // expense account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create withdrawal + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 1, // withdrawal + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + // basic transactions + Transaction::create( + ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0, 'description' => 'Hello'] + ); + Transaction::create( + ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0, 'description' => 'Hello'] + ); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Withdrawal', $result['type']); + $this->assertEquals('Hello (' . $journal->description . ')', $result['description']); + + // budget and category: + $this->assertNull($result['budget_id']); + $this->assertNull($result['budget_name']); + $this->assertNull($result['category_id']); + $this->assertNull($result['category_name']); + + // source: + $this->assertEquals($asset->name, $result['source_name']); + $this->assertEquals($asset->iban, $result['source_iban']); + $this->assertEquals($asset->id, $result['source_id']); + $this->assertEquals('Asset account', $result['source_type']); + + // destination: + $this->assertEquals($expense->name, $result['destination_name']); + $this->assertEquals($expense->iban, $result['destination_iban']); + $this->assertEquals($expense->id, $result['destination_id']); + $this->assertEquals('Expense account', $result['destination_type']); + } + + /** + * Basic journal (transfer) + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testTransferOne() + { + // make new asset account: + $left = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new asset account: + $right = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create withdrawal + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 3, // transfer + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + // basic transactions + Transaction::create( + ['account_id' => $left->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] + ); + Transaction::create( + ['account_id' => $right->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] + ); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Transfer', $result['type']); + $this->assertEquals($journal->description, $result['description']); + + // budget and category: + $this->assertNull($result['budget_id']); + $this->assertNull($result['budget_name']); + $this->assertNull($result['category_id']); + $this->assertNull($result['category_name']); + + // source: + $this->assertEquals($left->name, $result['source_name']); + $this->assertEquals($left->iban, $result['source_iban']); + $this->assertEquals($left->id, $result['source_id']); + $this->assertEquals('Asset account', $result['source_type']); + + // destination: + $this->assertEquals($right->name, $result['destination_name']); + $this->assertEquals($right->iban, $result['destination_iban']); + $this->assertEquals($right->id, $result['destination_id']); + $this->assertEquals('Asset account', $result['destination_type']); + } + + /** + * Basic journal (transfer) + * + * @covers \FireflyIII\Transformers\TransactionTransformer::transform + * @throws \FireflyIII\Exceptions\FireflyException + */ + public function testTransferTwo() + { + // make new asset account: + $left = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // make new asset account: + $right = Account::create( + [ + 'user_id' => $this->user()->id, + 'account_type_id' => 3, // asset account + 'name' => 'Random name #' . rand(1, 10000), + 'virtual_balance' => 12.34, + 'iban' => 'NL85ABNA0466812694', + 'active' => 1, + 'encrypted' => 0, + ] + ); + + // create withdrawal + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user()->id, + 'transaction_type_id' => 3, // transfer + 'transaction_currency_id' => 1, // EUR + 'description' => 'Some journal', + 'date' => '2018-01-01', + 'completed' => 1, + ] + ); + // basic transactions + + Transaction::create( + ['account_id' => $left->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] + ); + + Transaction::create( + ['account_id' => $right->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] + ); + + // use collector to get it: + $transaction = $this->getTransaction($journal); + $transformer = new TransactionTransformer(new ParameterBag); + $result = $transformer->transform($transaction); + // basic fields: + $this->assertEquals($journal->id, $result['journal_id']); + $this->assertEquals('Transfer', $result['type']); + $this->assertEquals($journal->description, $result['description']); + + // budget and category: + $this->assertNull($result['budget_id']); + $this->assertNull($result['budget_name']); + $this->assertNull($result['category_id']); + $this->assertNull($result['category_name']); + + // source: + $this->assertEquals($right->name, $result['source_name']); + $this->assertEquals($right->iban, $result['source_iban']); + $this->assertEquals($right->id, $result['source_id']); + $this->assertEquals('Asset account', $result['source_type']); + + // destination: + $this->assertEquals($left->name, $result['destination_name']); + $this->assertEquals($left->iban, $result['destination_iban']); + $this->assertEquals($left->id, $result['destination_id']); + $this->assertEquals('Asset account', $result['destination_type']); + } + + /** + * @param TransactionJournal $journal + * + * @return Transaction + */ + protected function getTransaction(TransactionJournal $journal): Transaction + { + $collector = new JournalCollector; + $collector->setUser($this->user()); + $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation(); + $collector->setJournals(new Collection([$journal])); + + // add filter to remove transactions: + $transactionType = $journal->transactionType->type; + if ($transactionType === TransactionType::WITHDRAWAL) { + $collector->addFilter(PositiveAmountFilter::class); + } + if (!($transactionType === TransactionType::WITHDRAWAL)) { + $collector->addFilter(NegativeAmountFilter::class); + } + $journals = $collector->getJournals(); + + return $journals->first(); + } + + +} \ No newline at end of file diff --git a/tests/Unit/Transformers/UserTransformerTest.php b/tests/Unit/Transformers/UserTransformerTest.php new file mode 100644 index 0000000000..848351e774 --- /dev/null +++ b/tests/Unit/Transformers/UserTransformerTest.php @@ -0,0 +1,67 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Transformers; + + +use FireflyIII\Transformers\UserTransformer; +use Symfony\Component\HttpFoundation\ParameterBag; +use Tests\TestCase; + +/** + * Class UserTransformerTest + */ +class UserTransformerTest extends TestCase +{ + + /** + * Test basic transformer. + * + * @covers \FireflyIII\Transformers\UserTransformer::transform + */ + public function testBasic() + { + $user = $this->user(); + $transformer = new UserTransformer(new ParameterBag()); + $result = $transformer->transform($user); + + $this->assertEquals($user->email, $result['email']); + $this->assertEquals('owner', $result['role']); + } + + /** + * Test basic transformer. + * + * @covers \FireflyIII\Transformers\UserTransformer::transform + */ + public function testEmptyUser() + { + $user = $this->emptyUser(); + $transformer = new UserTransformer(new ParameterBag()); + $result = $transformer->transform($user); + + $this->assertEquals($user->email, $result['email']); + $this->assertNull($result['role']); + } + +} \ No newline at end of file