Skip to content

Commit

Permalink
Add new tests for transformers.
Browse files Browse the repository at this point in the history
  • Loading branch information
JC5 committed Dec 20, 2018
1 parent c1ae0ab commit 6f54f41
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 421 deletions.
4 changes: 4 additions & 0 deletions app/Models/PiggyBankEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
* @property int $id
* @property Carbon date
* @property TransactionJournal transactionJournal
* @property string $amount
* @property Carbon created_at
* @property Carbon updated_at
*
*/
class PiggyBankEvent extends Model
{
Expand Down
43 changes: 43 additions & 0 deletions app/Repositories/PiggyBank/PiggyBankRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,24 @@ public function getMaxOrder(): int
return (int)$this->user->piggyBanks()->max('order');
}

/**
* Return note for piggy bank.
*
* @param PiggyBank $piggyBank
*
* @return string
*/
public function getNoteText(PiggyBank $piggyBank): string
{
/** @var Note $note */
$note = $piggyBank->notes()->first();
if (null === $note) {
return '';
}

return $note->text;
}

/**
* @return Collection
*/
Expand Down Expand Up @@ -381,6 +399,31 @@ public function getSuggestedMonthlyAmount(PiggyBank $piggyBank): string
return $savePerMonth;
}

/**
* @param PiggyBankEvent $event
*
* @return int|null
*/
public function getTransactionWithEvent(PiggyBankEvent $event): ?int
{
$journal = $event->transactionJournal;
if (null === $journal) {
return null;
}
if ((float)$event->amount < 0) {
$transaction = $journal->transactions()->where('amount', '<', 0)->first();

return $transaction->id ?? null;
}
if ((float)$event->amount > 0) {
$transaction = $journal->transactions()->where('amount', '>', 0)->first();

return $transaction->id ?? null;
}

return null;
}

/**
* Get for piggy account what is left to put in piggies.
*
Expand Down
17 changes: 17 additions & 0 deletions app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/
interface PiggyBankRepositoryInterface
{

/**
* @param PiggyBank $piggyBank
* @param string $amount
Expand Down Expand Up @@ -152,6 +153,15 @@ public function getExactAmount(PiggyBank $piggyBank, PiggyBankRepetition $repeti
*/
public function getMaxOrder(): int;

/**
* Return note for piggy bank.
*
* @param PiggyBank $piggyBank
*
* @return string
*/
public function getNoteText(PiggyBank $piggyBank): string;

/**
* Return all piggy banks.
*
Expand Down Expand Up @@ -182,6 +192,13 @@ public function getRepetition(PiggyBank $piggyBank): ?PiggyBankRepetition;
*/
public function getSuggestedMonthlyAmount(PiggyBank $piggyBank): string;

/**
* @param PiggyBankEvent $event
*
* @return int|null
*/
public function getTransactionWithEvent(PiggyBankEvent $event): ?int;

/**
* Get for piggy account what is left to put in piggies.
*
Expand Down
96 changes: 0 additions & 96 deletions app/Transformers/NoteTransformer.php

This file was deleted.

67 changes: 36 additions & 31 deletions app/Transformers/PiggyBankEventTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,31 @@
use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use League\Fractal\TransformerAbstract;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use Log;
use Symfony\Component\HttpFoundation\ParameterBag;

/**
* Class PiggyBankEventTransformer
*/
class PiggyBankEventTransformer extends AbstractTransformer
{
/** @var CurrencyRepositoryInterface */
private $currencyRepos;
/** @var PiggyBankRepositoryInterface */
private $piggyRepos;
/** @var AccountRepositoryInterface */
private $repository;

/**
* PiggyBankEventTransformer constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
$this->repository = app(AccountRepositoryInterface::class);
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
$this->piggyRepos = app(PiggyBankRepositoryInterface::class);
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
}
Expand All @@ -57,41 +66,37 @@ public function __construct()
*/
public function transform(PiggyBankEvent $event): array
{
// get account linked to piggy bank
$account = $event->piggyBank->account;
/** @var AccountRepositoryInterface $accountRepos */
$accountRepos = app(AccountRepositoryInterface::class);
$accountRepos->setUser($account->user);

$currencyId = (int)$accountRepos->getMetaValue($account, 'currency_id');
$journal = $event->transactionJournal;
$transactionId = null;
$decimalPlaces = 2;
if ($currencyId > 0) {
/** @var CurrencyRepositoryInterface $repository */
$repository = app(CurrencyRepositoryInterface::class);
$repository->setUser($account->user);
$currency = $repository->findNull($currencyId);
/** @noinspection NullPointerExceptionInspection */
$decimalPlaces = $currency->decimal_places;
}
if (0 === $currencyId) {
// set up repositories.
$this->repository->setUser($account->user);
$this->currencyRepos->setUser($account->user);
$this->piggyRepos->setUser($account->user);

// get associated currency or fall back to the default:
$currencyId = (int)$this->repository->getMetaValue($account, 'currency_id');
$currency = $this->currencyRepos->findNull($currencyId);
if (null === $currency) {
$currency = app('amount')->getDefaultCurrencyByUser($account->user);
}
if (null !== $journal) {
$transactionId = $journal->transactions()->first()->id;
}

// get associated journal and transaction, if any:
$journalId = $event->transaction_journal_id;
$transactionId = $this->piggyRepos->getTransactionWithEvent($event);

$data = [
'id' => (int)$event->id,
'created_at' => $event->created_at->toAtomString(),
'updated_at' => $event->updated_at->toAtomString(),
'amount' => round($event->amount, $decimalPlaces),
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'transaction_id' => $transactionId,
'links' => [
'id' => (int)$event->id,
'created_at' => $event->created_at->toAtomString(),
'updated_at' => $event->updated_at->toAtomString(),
'amount' => round($event->amount, $currency->decimal_places),
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'journal_id' => $journalId,
'transaction_id' => $transactionId,
'links' => [
[
'rel' => 'self',
'uri' => '/piggy_bank_events/' . $event->id,
Expand Down

0 comments on commit 6f54f41

Please sign in to comment.