Skip to content

Commit

Permalink
Make sure transformers match API definition.
Browse files Browse the repository at this point in the history
  • Loading branch information
JC5 committed Dec 19, 2018
1 parent 03b4a50 commit 446ff81
Show file tree
Hide file tree
Showing 18 changed files with 485 additions and 221 deletions.
5 changes: 4 additions & 1 deletion app/Models/Budget.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace FireflyIII\Models;

use Carbon\Carbon;
use Crypt;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -43,6 +44,8 @@
* @property bool encrypted
* @property Collection budgetlimits
* @property int $order
* @property Carbon created_at
* @property Carbon updated_at
*/
class Budget extends Model
{
Expand All @@ -62,7 +65,7 @@ class Budget extends Model
'encrypted' => 'boolean',
];
/** @var array Fields that can be filled */
protected $fillable = ['user_id', 'name', 'active','order'];
protected $fillable = ['user_id', 'name', 'active', 'order'];
/** @var array Hidden from view */
protected $hidden = ['encrypted'];

Expand Down
9 changes: 5 additions & 4 deletions app/Repositories/Budget/BudgetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -747,10 +747,11 @@ public function spentInPeriodMc(Collection $budgets, Collection $accounts, Carbo
/** @var TransactionCurrency $currency */
$currency = $currencies[$code];
$return[] = [
'currency_code' => $code,
'currency_symbol' => $currency->symbol,
'currency_dp' => $currency->decimal_places,
'amount' => round($spent, $currency->decimal_places),
'currency_id' => $currency->id,
'currency_code' => $code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'amount' => round($spent, $currency->decimal_places),
];
}

Expand Down
5 changes: 1 addition & 4 deletions app/Support/Http/Api/AccountFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function mapAccountTypes(string $type): array
$types = [
'all' => [AccountType::DEFAULT, AccountType::CASH, AccountType::ASSET, AccountType::EXPENSE, AccountType::REVENUE,
AccountType::INITIAL_BALANCE, AccountType::BENEFICIARY, AccountType::IMPORT, AccountType::RECONCILIATION,
AccountType::LOAN,AccountType::DEBT, AccountType::MORTGAGE],
AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE],
'asset' => [AccountType::DEFAULT, AccountType::ASSET,],
'cash' => [AccountType::CASH,],
'expense' => [AccountType::EXPENSE, AccountType::BENEFICIARY,],
Expand All @@ -51,9 +51,6 @@ protected function mapAccountTypes(string $type): array
'hidden' => [AccountType::INITIAL_BALANCE, AccountType::IMPORT, AccountType::RECONCILIATION],
'liability' => [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD],
'liabilities' => [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD],
// 'cc' => [AccountType::CREDITCARD],
// 'creditcard' => [AccountType::CREDITCARD],
// 'credit_card' => [AccountType::CREDITCARD],
AccountType::DEFAULT => [AccountType::DEFAULT],
AccountType::CASH => [AccountType::CASH],
AccountType::ASSET => [AccountType::ASSET],
Expand Down
6 changes: 2 additions & 4 deletions app/Transformers/BudgetLimitTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
namespace FireflyIII\Transformers;

use FireflyIII\Models\BudgetLimit;
use League\Fractal\TransformerAbstract;
use Log;
use Symfony\Component\HttpFoundation\ParameterBag;

/**
* Class BudgetLimitTransformer
Expand Down Expand Up @@ -71,8 +69,8 @@ public function transform(BudgetLimit $budgetLimit): array
'id' => (int)$budgetLimit->id,
'created_at' => $budgetLimit->created_at->toAtomString(),
'updated_at' => $budgetLimit->updated_at->toAtomString(),
'start_date' => $budgetLimit->start_date->format('Y-m-d'),
'end_date' => $budgetLimit->end_date->format('Y-m-d'),
'start' => $budgetLimit->start_date->format('Y-m-d'),
'end' => $budgetLimit->end_date->format('Y-m-d'),
'budget_id' => $budgetLimit->budget_id,
'currency_id' => $currencyId,
'currency_code' => $currencyCode,
Expand Down
12 changes: 6 additions & 6 deletions app/Transformers/BudgetTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@
*/
class BudgetTransformer extends AbstractTransformer
{
private $repository;

/**
* BudgetTransformer constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
$this->repository = app(BudgetRepositoryInterface::class);
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
}
Expand All @@ -55,22 +58,19 @@ public function __construct()
*/
public function transform(Budget $budget): array
{
$this->repository->setUser($budget->user);
$start = $this->parameters->get('start');
$end = $this->parameters->get('end');
$spent = [];
if (null !== $start && null !== $end) {
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$repository->setUser($budget->user);
$spent = $repository->spentInPeriodMc(new Collection([$budget]), new Collection, $start, $end);
$spent = $this->repository->spentInPeriodMc(new Collection([$budget]), new Collection, $start, $end);
}


$data = [
'id' => (int)$budget->id,
'created_at' => $budget->created_at->toAtomString(),
'updated_at' => $budget->updated_at->toAtomString(),
'active' => 1 === (int)$budget->active,
'active' => $budget->active,
'name' => $budget->name,
'spent' => $spent,
'links' => [
Expand Down
35 changes: 17 additions & 18 deletions app/Transformers/CategoryTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use Illuminate\Support\Collection;
use League\Fractal\TransformerAbstract;
use Log;
use Symfony\Component\HttpFoundation\ParameterBag;

/**
* Class CategoryTransformer
*/
class CategoryTransformer extends AbstractTransformer
{
/** @var CategoryRepositoryInterface */
private $repository;

/**
* CategoryTransformer constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
$this->repository = app(CategoryRepositoryInterface::class);
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
}
Expand All @@ -60,6 +62,7 @@ public function __construct()
*/
public function transform(Category $category): array
{
$this->repository->setUser($category->user);
$spent = [];
$earned = [];
$start = $this->parameters->get('start');
Expand Down Expand Up @@ -95,10 +98,7 @@ public function transform(Category $category): array
*/
private function getEarnedInformation(Category $category, Carbon $start, Carbon $end): array
{
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$repository->setUser($category->user);
$collection = $repository->earnedInPeriodCollection(new Collection([$category]), new Collection, $start, $end);
$collection = $this->repository->earnedInPeriodCollection(new Collection([$category]), new Collection, $start, $end);
$return = [];
$total = [];
$currencies = [];
Expand All @@ -114,10 +114,11 @@ private function getEarnedInformation(Category $category, Carbon $start, Carbon
/** @var TransactionCurrency $currency */
$currency = $currencies[$code];
$return[] = [
'currency_code' => $code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'amount' => round($earned, $currency->decimal_places),
'currency_id' => $currency->id,
'currency_code' => $code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'amount' => round($earned, $currency->decimal_places),
];
}

Expand All @@ -133,10 +134,7 @@ private function getEarnedInformation(Category $category, Carbon $start, Carbon
*/
private function getSpentInformation(Category $category, Carbon $start, Carbon $end): array
{
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$repository->setUser($category->user);
$collection = $repository->spentInPeriodCollection(new Collection([$category]), new Collection, $start, $end);
$collection = $this->repository->spentInPeriodCollection(new Collection([$category]), new Collection, $start, $end);
$return = [];
$total = [];
$currencies = [];
Expand All @@ -152,10 +150,11 @@ private function getSpentInformation(Category $category, Carbon $start, Carbon $
/** @var TransactionCurrency $currency */
$currency = $currencies[$code];
$return[] = [
'currency_code' => $code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'amount' => round($spent, $currency->decimal_places),
'currency_id' => $currency->id,
'currency_code' => $code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'amount' => round($spent, $currency->decimal_places),
];
}

Expand Down
36 changes: 17 additions & 19 deletions app/Transformers/CurrencyExchangeRateTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@


use FireflyIII\Models\CurrencyExchangeRate;
use League\Fractal\TransformerAbstract;
use Log;
use Symfony\Component\HttpFoundation\ParameterBag;

/**
* Class CurrencyExchangeRateTransformer
Expand Down Expand Up @@ -57,23 +55,23 @@ public function transform(CurrencyExchangeRate $rate): array
$result = round((float)$rate->rate * (float)$this->parameters->get('amount'), $rate->toCurrency->decimal_places);
$result = 0.0 === $result ? null : $result;
$data = [
'id' => (int)$rate->id,
'created_at' => $rate->created_at->toAtomString(),
'updated_at' => $rate->updated_at->toAtomString(),
'from_currency_id' => $rate->fromCurrency->id,
'from_currency_name' => $rate->fromCurrency->name,
'from_currency_code' => $rate->fromCurrency->code,
'from_currency_symbol' => $rate->fromCurrency->symbol,
'from_currency_decimal_places' => $rate->fromCurrency->decimal_places,
'to_currency_id' => $rate->toCurrency->id,
'to_currency_name' => $rate->toCurrency->name,
'to_currency_code' => $rate->toCurrency->code,
'to_currency_symbol' => $rate->toCurrency->symbol,
'to_currency_decimal_places' => $rate->toCurrency->decimal_places,
'date' => $rate->date->format('Y-m-d'),
'rate' => (float)$rate->rate,
'amount' => $result,
'links' => [
'id' => (int)$rate->id,
'created_at' => $rate->created_at->toAtomString(),
'updated_at' => $rate->updated_at->toAtomString(),
'from_currency_id' => $rate->fromCurrency->id,
'from_currency_name' => $rate->fromCurrency->name,
'from_currency_code' => $rate->fromCurrency->code,
'from_currency_symbol' => $rate->fromCurrency->symbol,
'from_currency_decimal_places' => $rate->fromCurrency->decimal_places,
'to_currency_id' => $rate->toCurrency->id,
'to_currency_name' => $rate->toCurrency->name,
'to_currency_code' => $rate->toCurrency->code,
'to_currency_symbol' => $rate->toCurrency->symbol,
'to_currency_decimal_places' => $rate->toCurrency->decimal_places,
'date' => $rate->date->format('Y-m-d'),
'rate' => (float)$rate->rate,
'amount' => $result,
'links' => [
[
'rel' => 'self',
'uri' => '/currency_exchange_rates/' . $rate->id,
Expand Down
85 changes: 0 additions & 85 deletions app/Transformers/JournalMetaTransformer.php

This file was deleted.

10 changes: 10 additions & 0 deletions tests/Unit/Transformers/AccountTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Transformers\AccountTransformer;
use Log;
use Mockery;
use Steam;
use Symfony\Component\HttpFoundation\ParameterBag;
Expand All @@ -37,6 +38,15 @@
*/
class AccountTransformerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', \get_class($this)));
}

/**
* Check balance on a different date.
*
Expand Down

0 comments on commit 446ff81

Please sign in to comment.