mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Add new tests for transformers.
This commit is contained in:
parent
c1ae0ab57d
commit
6f54f41946
@ -35,6 +35,10 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
* @property int $id
|
||||
* @property Carbon date
|
||||
* @property TransactionJournal transactionJournal
|
||||
* @property string $amount
|
||||
* @property Carbon created_at
|
||||
* @property Carbon updated_at
|
||||
*
|
||||
*/
|
||||
class PiggyBankEvent extends Model
|
||||
{
|
||||
|
@ -308,6 +308,24 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
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
|
||||
*/
|
||||
@ -381,6 +399,31 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
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.
|
||||
*
|
||||
|
@ -35,6 +35,7 @@ use Illuminate\Support\Collection;
|
||||
*/
|
||||
interface PiggyBankRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
@ -152,6 +153,15 @@ interface PiggyBankRepositoryInterface
|
||||
*/
|
||||
public function getMaxOrder(): int;
|
||||
|
||||
/**
|
||||
* Return note for piggy bank.
|
||||
*
|
||||
* @param PiggyBank $piggyBank
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNoteText(PiggyBank $piggyBank): string;
|
||||
|
||||
/**
|
||||
* Return all piggy banks.
|
||||
*
|
||||
@ -182,6 +192,13 @@ interface PiggyBankRepositoryInterface
|
||||
*/
|
||||
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.
|
||||
*
|
||||
|
@ -1,96 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* NoteTransformer.php
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Models\Note;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use Log;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
/**
|
||||
* Class NoteTransformer
|
||||
*/
|
||||
class NoteTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* List of resources possible to include
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = [];
|
||||
/**
|
||||
* List of resources to automatically include
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultIncludes = [];
|
||||
|
||||
/** @var ParameterBag */
|
||||
protected $parameters;
|
||||
|
||||
/**
|
||||
* CurrencyTransformer constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param ParameterBag $parameters
|
||||
*/
|
||||
public function __construct(ParameterBag $parameters)
|
||||
{
|
||||
$this->parameters = $parameters;
|
||||
if ('testing' === config('app.env')) {
|
||||
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the note.
|
||||
*
|
||||
* @param Note $note
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Note $note): array
|
||||
{
|
||||
$converter = new CommonMarkConverter;
|
||||
$data = [
|
||||
'id' => (int)$note->id,
|
||||
'created_at' => $note->created_at->toAtomString(),
|
||||
'updated_at' => $note->updated_at->toAtomString(),
|
||||
'title' => $note->title,
|
||||
'text' => $note->text,
|
||||
'markdown' => $converter->convertToHtml($note->text),
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/notes/' . $note->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
@ -27,15 +27,21 @@ namespace FireflyIII\Transformers;
|
||||
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.
|
||||
*
|
||||
@ -43,6 +49,9 @@ class PiggyBankEventTransformer extends AbstractTransformer
|
||||
*/
|
||||
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)));
|
||||
}
|
||||
@ -57,41 +66,37 @@ class PiggyBankEventTransformer extends AbstractTransformer
|
||||
*/
|
||||
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,
|
||||
|
@ -25,7 +25,6 @@ namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
@ -37,6 +36,13 @@ use Log;
|
||||
*/
|
||||
class PiggyBankTransformer extends AbstractTransformer
|
||||
{
|
||||
/** @var AccountRepositoryInterface */
|
||||
private $accountRepos;
|
||||
/** @var CurrencyRepositoryInterface */
|
||||
private $currencyRepos;
|
||||
/** @var PiggyBankRepositoryInterface */
|
||||
private $piggyRepos;
|
||||
|
||||
/**
|
||||
* PiggyBankTransformer constructor.
|
||||
*
|
||||
@ -44,6 +50,9 @@ class PiggyBankTransformer extends AbstractTransformer
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->accountRepos = 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)));
|
||||
}
|
||||
@ -61,70 +70,62 @@ class PiggyBankTransformer extends AbstractTransformer
|
||||
{
|
||||
/** @var Account $account */
|
||||
$account = $piggyBank->account;
|
||||
/** @var AccountRepositoryInterface $accountRepos */
|
||||
$accountRepos = app(AccountRepositoryInterface::class);
|
||||
$accountRepos->setUser($account->user);
|
||||
$currencyId = (int)$accountRepos->getMetaValue($account, 'currency_id');
|
||||
$currency = null;
|
||||
if (0 === $currencyId) {
|
||||
|
||||
// set up repositories
|
||||
$this->accountRepos->setUser($account->user);
|
||||
$this->currencyRepos->setUser($account->user);
|
||||
$this->piggyRepos->setUser($account->user);
|
||||
|
||||
// get currency from account, or use default.
|
||||
$currencyId = (int)$this->accountRepos->getMetaValue($account, 'currency_id');
|
||||
$currency = $this->currencyRepos->findNull($currencyId);
|
||||
if (null === $currency) {
|
||||
$currency = app('amount')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
|
||||
if ($currencyId > 0) {
|
||||
/** @var CurrencyRepositoryInterface $repository */
|
||||
$repository = app(CurrencyRepositoryInterface::class);
|
||||
$repository->setUser($account->user);
|
||||
$currency = $repository->findNull($currencyId);
|
||||
}
|
||||
|
||||
$decimalPlaces = $currency->decimal_places;
|
||||
// note
|
||||
$notes = $this->piggyRepos->getNoteText($piggyBank);
|
||||
$notes = '' === $notes ? null : $notes;
|
||||
|
||||
// get currently saved amount:
|
||||
/** @var PiggyBankRepositoryInterface $piggyRepos */
|
||||
$piggyRepos = app(PiggyBankRepositoryInterface::class);
|
||||
$piggyRepos->setUser($account->user);
|
||||
$currentAmountStr = $this->piggyRepos->getCurrentAmount($piggyBank);
|
||||
$currentAmount = round($currentAmountStr, $currency->decimal_places);
|
||||
|
||||
// current amount in piggy bank:
|
||||
$currentAmountStr = $piggyRepos->getCurrentAmount($piggyBank);
|
||||
$currentAmount = round($currentAmountStr, $decimalPlaces);
|
||||
// left to save:
|
||||
$leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr);
|
||||
$startDate = null === $piggyBank->startdate ? null : $piggyBank->startdate->format('Y-m-d');
|
||||
$targetDate = null === $piggyBank->targetdate ? null : $piggyBank->targetdate->format('Y-m-d');
|
||||
|
||||
// left to save to target:
|
||||
$leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr);
|
||||
$startDate = null === $piggyBank->startdate ? null : $piggyBank->startdate->format('Y-m-d');
|
||||
$targetDate = null === $piggyBank->targetdate ? null : $piggyBank->targetdate->format('Y-m-d');
|
||||
$targetAmount = round($piggyBank->targetamount, $decimalPlaces);
|
||||
// target and percentage:
|
||||
$targetAmount = round($piggyBank->targetamount, $currency->decimal_places);
|
||||
$percentage = (int)(0 !== bccomp('0', $currentAmountStr) ? $currentAmount / $targetAmount * 100 : 0);
|
||||
$data = [
|
||||
'id' => (int)$piggyBank->id,
|
||||
'created_at' => $piggyBank->created_at->toAtomString(),
|
||||
'updated_at' => $piggyBank->updated_at->toAtomString(),
|
||||
'name' => $piggyBank->name,
|
||||
'currency_id' => $currency->id,
|
||||
'currency_code' => $currency->code,
|
||||
'currency_symbol' => $currency->symbol,
|
||||
'currency_decimal_places' => $currency->decimal_places,
|
||||
'target_amount' => $targetAmount,
|
||||
'percentage' => $percentage,
|
||||
'current_amount' => $currentAmount,
|
||||
'left_to_save' => round($leftToSave, $decimalPlaces),
|
||||
'save_per_month' => round($piggyRepos->getSuggestedMonthlyAmount($piggyBank), $decimalPlaces),
|
||||
'start_date' => $startDate,
|
||||
'target_date' => $targetDate,
|
||||
'order' => (int)$piggyBank->order,
|
||||
'active' => true,
|
||||
'notes' => null,
|
||||
'links' => [
|
||||
'id' => (int)$piggyBank->id,
|
||||
'created_at' => $piggyBank->created_at->toAtomString(),
|
||||
'updated_at' => $piggyBank->updated_at->toAtomString(),
|
||||
'account_id' => $piggyBank->account_id,
|
||||
'name' => $piggyBank->name,
|
||||
'currency_id' => $currency->id,
|
||||
'currency_code' => $currency->code,
|
||||
'currency_symbol' => $currency->symbol,
|
||||
'currency_decimal_places' => $currency->decimal_places,
|
||||
'target_amount' => $targetAmount,
|
||||
'percentage' => $percentage,
|
||||
'current_amount' => $currentAmount,
|
||||
'left_to_save' => round($leftToSave, $currency->decimal_places),
|
||||
'save_per_month' => round($this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places),
|
||||
'start_date' => $startDate,
|
||||
'target_date' => $targetDate,
|
||||
'order' => (int)$piggyBank->order,
|
||||
'active' => true,
|
||||
'notes' => $notes,
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/piggy_banks/' . $piggyBank->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
/** @var Note $note */
|
||||
$note = $piggyBank->notes()->first();
|
||||
if (null !== $note) {
|
||||
$data['notes'] = $note->text;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
@ -23,13 +23,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Transformers;
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountMeta;
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
use Amount;
|
||||
use FireflyIII\Models\PiggyBankEvent;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
||||
use FireflyIII\Transformers\PiggyBankEventTransformer;
|
||||
use Mockery;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
@ -45,100 +46,62 @@ class PiggyBankEventTransformerTest extends TestCase
|
||||
*/
|
||||
public function testBasic(): void
|
||||
{
|
||||
// make new account:
|
||||
$account = Account::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'account_type_id' => 3, // asset account
|
||||
'name' => 'Random name #' . random_int(1, 10000),
|
||||
'virtual_balance' => 12.34,
|
||||
'iban' => 'NL85ABNA0466812694',
|
||||
'active' => 1,
|
||||
'encrypted' => 0,
|
||||
]
|
||||
);
|
||||
$piggy = PiggyBank::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'Some random piggy #' . random_int(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',
|
||||
]
|
||||
);
|
||||
// repositories
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
|
||||
$transformer = new PiggyBankEventTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($event);
|
||||
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
||||
$piggyRepos->shouldReceive('setUser')->atLeast()->once();
|
||||
$accountRepos->shouldReceive('setUser')->atLeast()->once();
|
||||
|
||||
// mock calls:
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->atLeast()->once()->andReturn(1);
|
||||
$currencyRepos->shouldReceive('findNull')->withArgs([1])->atLeast()->once()->andReturn(TransactionCurrency::find(1));
|
||||
$piggyRepos->shouldReceive('getTransactionWithEvent')->atLeast()->once()->andReturn(123);
|
||||
|
||||
$event = PiggyBankEvent::first();
|
||||
$transformer = app(PiggyBankEventTransformer::class);
|
||||
$transformer->setParameters(new ParameterBag);
|
||||
|
||||
$result = $transformer->transform($event);
|
||||
$this->assertEquals($event->id, $result['id']);
|
||||
$this->assertEquals(123.45, $result['amount']);
|
||||
$this->assertEquals(245, $result['amount']);
|
||||
$this->assertEquals(123, $result['transaction_id']);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic test with currency meta data.
|
||||
* Basic test with no currency info.
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\PiggyBankEventTransformer
|
||||
*/
|
||||
public function testBasicCurrency(): void
|
||||
public function testNoCurrency(): void
|
||||
{
|
||||
// mock repository.
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1))->once();
|
||||
// repositories
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
|
||||
// make new account:
|
||||
$account = Account::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'account_type_id' => 3, // asset account
|
||||
'name' => 'Random name #' . random_int(1, 10000),
|
||||
'virtual_balance' => 12.34,
|
||||
'iban' => 'NL85ABNA0466812694',
|
||||
'active' => 1,
|
||||
'encrypted' => 0,
|
||||
]
|
||||
);
|
||||
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
||||
$piggyRepos->shouldReceive('setUser')->atLeast()->once();
|
||||
$accountRepos->shouldReceive('setUser')->atLeast()->once();
|
||||
|
||||
// meta
|
||||
$accountMeta = AccountMeta::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'currency_id',
|
||||
'data' => 1,
|
||||
]
|
||||
);
|
||||
// mock calls:
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->atLeast()->once()->andReturn(1);
|
||||
$currencyRepos->shouldReceive('findNull')->withArgs([1])->atLeast()->once()->andReturn(null);
|
||||
$piggyRepos->shouldReceive('getTransactionWithEvent')->atLeast()->once()->andReturn(123);
|
||||
|
||||
$piggy = PiggyBank::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'Some random piggy #' . random_int(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',
|
||||
]
|
||||
);
|
||||
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn(TransactionCurrency::find(1))->atLeast()->once();
|
||||
|
||||
$transformer = new PiggyBankEventTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($event);
|
||||
$event = PiggyBankEvent::first();
|
||||
$transformer = app(PiggyBankEventTransformer::class);
|
||||
$transformer->setParameters(new ParameterBag);
|
||||
|
||||
$result = $transformer->transform($event);
|
||||
$this->assertEquals($event->id, $result['id']);
|
||||
$this->assertEquals(123.45, $result['amount']);
|
||||
}
|
||||
$this->assertEquals(245, $result['amount']);
|
||||
$this->assertEquals(123, $result['transaction_id']);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -23,16 +23,16 @@ 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\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
||||
use FireflyIII\Transformers\PiggyBankTransformer;
|
||||
use Mockery;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
use Amount;
|
||||
|
||||
/**
|
||||
* Class PiggyBankTransformerTest
|
||||
@ -46,168 +46,78 @@ class PiggyBankTransformerTest extends TestCase
|
||||
*/
|
||||
public function testBasic(): void
|
||||
{
|
||||
// mock repository:
|
||||
$repository = $this->mock(PiggyBankRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('getCurrentAmount')->andReturn('12.34')->once();
|
||||
$repository->shouldReceive('getSuggestedMonthlyAmount')->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 #' . random_int(1, 10000),
|
||||
'virtual_balance' => 12.34,
|
||||
'iban' => 'NL85ABNA0466812694',
|
||||
'active' => 1,
|
||||
'encrypted' => 0,
|
||||
]
|
||||
);
|
||||
$piggy = PiggyBank::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'Some random piggy #' . random_int(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()
|
||||
*/
|
||||
public function testBasicWithCurrency(): void
|
||||
{
|
||||
// mock repository.
|
||||
// mock repositories
|
||||
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$currencyRepos->shouldReceive('setUser')->once();
|
||||
$currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1))->once();
|
||||
|
||||
// mock calls:
|
||||
$piggyRepos->shouldReceive('setUser')->atLeast()->once();
|
||||
$accountRepos->shouldReceive('setUser')->atLeast()->once();
|
||||
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
||||
|
||||
// mock repository:
|
||||
$repository = $this->mock(PiggyBankRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('getCurrentAmount')->andReturn('12.34')->once();
|
||||
$repository->shouldReceive('getSuggestedMonthlyAmount')->andReturn('12.34')->once();
|
||||
// return a currency
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->atLeast()->once()->andReturn('1');
|
||||
$currencyRepos->shouldReceive('findNull')->withArgs([1])->atLeast()->once()->andReturn(TransactionCurrency::find(1));
|
||||
|
||||
// make new account and piggy
|
||||
$account = Account::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'account_type_id' => 3, // asset account
|
||||
'name' => 'Random name #' . random_int(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,
|
||||
]
|
||||
);
|
||||
// get a note
|
||||
$piggyRepos->shouldReceive('getNoteText')->atLeast()->once()->andReturn('I am a note.');
|
||||
|
||||
$piggy = PiggyBank::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'Some random piggy #' . random_int(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']);
|
||||
// get some amounts
|
||||
$piggyRepos->shouldReceive('getCurrentAmount')->atLeast()->once()->andReturn('123.45');
|
||||
$piggyRepos->shouldReceive('getSuggestedMonthlyAmount')->atLeast()->once()->andReturn('12.45');
|
||||
|
||||
/** @var PiggyBankTransformer $transformer */
|
||||
$transformer = app(PiggyBankTransformer::class);
|
||||
$transformer->setParameters(new ParameterBag());
|
||||
|
||||
$piggy = PiggyBank::first();
|
||||
$result = $transformer->transform($piggy);
|
||||
|
||||
$this->assertEquals(12.45, $result['save_per_month']);
|
||||
$this->assertEquals($piggy->name, $result['name']);
|
||||
$this->assertEquals('', $result['notes']);
|
||||
$this->assertEquals(1, $result['currency_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test basic transformer with currency preference and a note
|
||||
* Test basic transformer.
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\PiggyBankTransformer
|
||||
*/
|
||||
public function testBasicWithCurrencyAndNote(): void
|
||||
public function testNoCurrency(): void
|
||||
{
|
||||
// mock repository.
|
||||
// mock repositories
|
||||
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$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();
|
||||
$repository->shouldReceive('getSuggestedMonthlyAmount')->andReturn('12.34')->once();
|
||||
// mock calls:
|
||||
$piggyRepos->shouldReceive('setUser')->atLeast()->once();
|
||||
$accountRepos->shouldReceive('setUser')->atLeast()->once();
|
||||
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
||||
|
||||
// make new account and piggy
|
||||
$account = Account::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'account_type_id' => 3, // asset account
|
||||
'name' => 'Random name #' . random_int(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,
|
||||
]
|
||||
);
|
||||
// return a currency
|
||||
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->atLeast()->once()->andReturn('1');
|
||||
$currencyRepos->shouldReceive('findNull')->withArgs([1])->atLeast()->once()->andReturn(null);
|
||||
Amount::shouldReceive('getDefaultCurrencyByUser')->atLeast()->once()->andReturn(TransactionCurrency::find(1));
|
||||
|
||||
$piggy = PiggyBank::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'Some random piggy #' . random_int(1, 10000),
|
||||
'targetamount' => '1000',
|
||||
'startdate' => '2018-01-01',
|
||||
'targetdate' => '2018-01-31',
|
||||
'order' => 1,
|
||||
'active' => 1,
|
||||
]
|
||||
);
|
||||
// get a note
|
||||
$piggyRepos->shouldReceive('getNoteText')->atLeast()->once()->andReturn('I am a note.');
|
||||
|
||||
// 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']);
|
||||
// get some amounts
|
||||
$piggyRepos->shouldReceive('getCurrentAmount')->atLeast()->once()->andReturn('123.45');
|
||||
$piggyRepos->shouldReceive('getSuggestedMonthlyAmount')->atLeast()->once()->andReturn('12.45');
|
||||
|
||||
/** @var PiggyBankTransformer $transformer */
|
||||
$transformer = app(PiggyBankTransformer::class);
|
||||
$transformer->setParameters(new ParameterBag());
|
||||
|
||||
$piggy = PiggyBank::first();
|
||||
$result = $transformer->transform($piggy);
|
||||
|
||||
$this->assertEquals(12.45, $result['save_per_month']);
|
||||
$this->assertEquals($piggy->name, $result['name']);
|
||||
$this->assertEquals('I am a note.', $result['notes']);
|
||||
$this->assertEquals(1, $result['currency_id']);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user