mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Make sure transformers match API definition.
This commit is contained in:
parent
03b4a50317
commit
446ff81335
@ -22,6 +22,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Crypt;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@ -43,6 +44,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
* @property bool encrypted
|
||||
* @property Collection budgetlimits
|
||||
* @property int $order
|
||||
* @property Carbon created_at
|
||||
* @property Carbon updated_at
|
||||
*/
|
||||
class Budget extends Model
|
||||
{
|
||||
@ -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'];
|
||||
|
||||
|
@ -747,10 +747,11 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
/** @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),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,7 @@ trait AccountFilter
|
||||
$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,],
|
||||
@ -51,9 +51,6 @@ trait AccountFilter
|
||||
'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],
|
||||
|
@ -24,9 +24,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Transformers;
|
||||
|
||||
use FireflyIII\Models\BudgetLimit;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use Log;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
/**
|
||||
* Class BudgetLimitTransformer
|
||||
@ -71,8 +69,8 @@ class BudgetLimitTransformer extends AbstractTransformer
|
||||
'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,
|
||||
|
@ -34,6 +34,8 @@ use Log;
|
||||
*/
|
||||
class BudgetTransformer extends AbstractTransformer
|
||||
{
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* BudgetTransformer constructor.
|
||||
*
|
||||
@ -41,6 +43,7 @@ class BudgetTransformer extends AbstractTransformer
|
||||
*/
|
||||
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)));
|
||||
}
|
||||
@ -55,22 +58,19 @@ class BudgetTransformer extends AbstractTransformer
|
||||
*/
|
||||
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' => [
|
||||
|
@ -30,15 +30,16 @@ use FireflyIII\Models\Transaction;
|
||||
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.
|
||||
*
|
||||
@ -46,6 +47,7 @@ class CategoryTransformer extends AbstractTransformer
|
||||
*/
|
||||
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)));
|
||||
}
|
||||
@ -60,6 +62,7 @@ class CategoryTransformer extends AbstractTransformer
|
||||
*/
|
||||
public function transform(Category $category): array
|
||||
{
|
||||
$this->repository->setUser($category->user);
|
||||
$spent = [];
|
||||
$earned = [];
|
||||
$start = $this->parameters->get('start');
|
||||
@ -95,10 +98,7 @@ class CategoryTransformer extends AbstractTransformer
|
||||
*/
|
||||
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 = [];
|
||||
@ -114,10 +114,11 @@ class CategoryTransformer extends AbstractTransformer
|
||||
/** @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),
|
||||
];
|
||||
}
|
||||
|
||||
@ -133,10 +134,7 @@ class CategoryTransformer extends AbstractTransformer
|
||||
*/
|
||||
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 = [];
|
||||
@ -152,10 +150,11 @@ class CategoryTransformer extends AbstractTransformer
|
||||
/** @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),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -25,9 +25,7 @@ namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Models\CurrencyExchangeRate;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use Log;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
/**
|
||||
* Class CurrencyExchangeRateTransformer
|
||||
@ -57,23 +55,23 @@ class CurrencyExchangeRateTransformer extends AbstractTransformer
|
||||
$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,
|
||||
|
@ -1,85 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* JournalMetaTransformer.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\Helpers\Collector\TransactionCollectorInterface;
|
||||
use FireflyIII\Models\TransactionJournalMeta;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use Log;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
/**
|
||||
* Class JournalMetaTransformer
|
||||
*/
|
||||
class JournalMetaTransformer extends TransformerAbstract
|
||||
{
|
||||
/** @var ParameterBag */
|
||||
protected $parameters;
|
||||
|
||||
/**
|
||||
* JournalMetaTransformer 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)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert meta object.
|
||||
*
|
||||
* @param TransactionJournalMeta $meta
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(TransactionJournalMeta $meta): array
|
||||
{
|
||||
$data = [
|
||||
'id' => (int)$meta->id,
|
||||
'created_at' => $meta->created_at->toAtomString(),
|
||||
'updated_at' => $meta->updated_at->toAtomString(),
|
||||
'name' => $meta->name,
|
||||
'data' => $meta->data,
|
||||
'hash' => $meta->hash,
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/journal_meta/' . $meta->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
@ -27,6 +27,7 @@ use Carbon\Carbon;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Transformers\AccountTransformer;
|
||||
use Log;
|
||||
use Mockery;
|
||||
use Steam;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
@ -37,6 +38,15 @@ use Tests\TestCase;
|
||||
*/
|
||||
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.
|
||||
*
|
||||
|
@ -27,6 +27,7 @@ use FireflyIII\Models\Attachment;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
|
||||
use FireflyIII\Transformers\AttachmentTransformer;
|
||||
use Log;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
@ -35,6 +36,15 @@ use Tests\TestCase;
|
||||
*/
|
||||
class AttachmentTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Log::info(sprintf('Now in %s.', \get_class($this)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test basic transformer
|
||||
*
|
||||
|
@ -25,6 +25,7 @@ namespace Tests\Unit\Transformers;
|
||||
|
||||
use FireflyIII\Models\AvailableBudget;
|
||||
use FireflyIII\Transformers\AvailableBudgetTransformer;
|
||||
use Log;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
@ -33,6 +34,15 @@ use Tests\TestCase;
|
||||
*/
|
||||
class AvailableBudgetTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Log::info(sprintf('Now in %s.', \get_class($this)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test basic transformer
|
||||
*
|
||||
|
@ -28,6 +28,7 @@ use FireflyIII\Models\Bill;
|
||||
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||
use FireflyIII\Transformers\BillTransformer;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
@ -36,6 +37,16 @@ use Tests\TestCase;
|
||||
*/
|
||||
class BillTransformerTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Log::info(sprintf('Now in %s.', \get_class($this)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic coverage
|
||||
*
|
||||
|
65
tests/Unit/Transformers/BudgetLimitTransformerTest.php
Normal file
65
tests/Unit/Transformers/BudgetLimitTransformerTest.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* BudgetLimitTransformerTest.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 Tests\Unit\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Models\BudgetLimit;
|
||||
use FireflyIII\Transformers\BudgetLimitTransformer;
|
||||
use Log;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class BudgetLimitTransformerTest
|
||||
*/
|
||||
class BudgetLimitTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Log::info(sprintf('Now in %s.', \get_class($this)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Transformers\BudgetLimitTransformer
|
||||
*/
|
||||
public function testBasic(): void
|
||||
{
|
||||
/** @var BudgetLimit $budgetLimit */
|
||||
$budgetLimit = BudgetLimit::first();
|
||||
|
||||
/** @var BudgetLimitTransformer $transformer */
|
||||
$transformer = app(BudgetLimitTransformer::class);
|
||||
$result = $transformer->transform($budgetLimit);
|
||||
|
||||
// compare results:
|
||||
$this->assertEquals($budgetLimit->id, $result['id']);
|
||||
$this->assertEquals($budgetLimit->start_date->format('Y-m-d'), $result['start']);
|
||||
$this->assertGreaterThanOrEqual(0, $result['amount']);
|
||||
}
|
||||
|
||||
}
|
@ -23,8 +23,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Transformers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use FireflyIII\Transformers\BudgetTransformer;
|
||||
use Log;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
@ -34,6 +37,14 @@ use Tests\TestCase;
|
||||
*/
|
||||
class BudgetTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Log::info(sprintf('Now in %s.', \get_class($this)));
|
||||
}
|
||||
/**
|
||||
* Basic coverage
|
||||
*
|
||||
@ -41,18 +52,66 @@ class BudgetTransformerTest extends TestCase
|
||||
*/
|
||||
public function testBasic(): void
|
||||
{
|
||||
// mocks and prep:
|
||||
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||
$parameters = new ParameterBag;
|
||||
$budget = Budget::first();
|
||||
$transformer = app(BudgetTransformer::class);
|
||||
$transformer->setParameters($parameters);
|
||||
|
||||
$budget = Budget::create(
|
||||
// mocks
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
|
||||
// action
|
||||
$result = $transformer->transform($budget);
|
||||
|
||||
|
||||
$this->assertEquals($budget->id, $result['id']);
|
||||
$this->assertEquals((bool)$budget->active, $result['active']);
|
||||
$this->assertEquals([], $result['spent']);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic coverage
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\BudgetTransformer
|
||||
*/
|
||||
public function testSpentArray(): void
|
||||
{
|
||||
// mocks and prep:
|
||||
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||
$parameters = new ParameterBag;
|
||||
|
||||
// set parameters
|
||||
$parameters->set('start', new Carbon('2018-01-01'));
|
||||
$parameters->set('end', new Carbon('2018-01-31'));
|
||||
|
||||
$budget = Budget::first();
|
||||
$transformer = app(BudgetTransformer::class);
|
||||
$transformer->setParameters($parameters);
|
||||
|
||||
// spent data
|
||||
$spent = [
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'name' => 'Some budget ' . random_int(1, 10000),
|
||||
'active' => 1,
|
||||
]
|
||||
);
|
||||
$transformer = new BudgetTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($budget);
|
||||
'currency_id' => 1,
|
||||
'currency_code' => 'AKC',
|
||||
'currency_symbol' => 'x',
|
||||
'currency_decimal_places' => 2,
|
||||
'amount' => 1000,
|
||||
],
|
||||
];
|
||||
|
||||
// mocks
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('spentInPeriodMc')->atLeast()->once()->andReturn($spent);
|
||||
|
||||
// action
|
||||
$result = $transformer->transform($budget);
|
||||
|
||||
$this->assertEquals($budget->id, $result['id']);
|
||||
$this->assertEquals((bool)$budget->active, $result['active']);
|
||||
$this->assertEquals($spent, $result['spent']);
|
||||
|
||||
$this->assertEquals($budget->name, $result['name']);
|
||||
$this->assertTrue($result['active']);
|
||||
}
|
||||
}
|
||||
|
@ -23,8 +23,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Transformers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
||||
use FireflyIII\Transformers\CategoryTransformer;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
@ -34,6 +40,15 @@ use Tests\TestCase;
|
||||
*/
|
||||
class CategoryTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Log::info(sprintf('Now in %s.', \get_class($this)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic coverage
|
||||
*
|
||||
@ -41,17 +56,79 @@ class CategoryTransformerTest extends TestCase
|
||||
*/
|
||||
public function testBasic(): void
|
||||
{
|
||||
$repository = $this->mock(CategoryRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
|
||||
$category = Category::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'name' => 'Some budget ' . random_int(1, 10000),
|
||||
'active' => 1,
|
||||
]
|
||||
);
|
||||
$transformer = new CategoryTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($category);
|
||||
/** @var Category $category */
|
||||
$category = Category::first();
|
||||
$transformer = app(CategoryTransformer::class);
|
||||
$transformer->setParameters(new ParameterBag);
|
||||
$result = $transformer->transform($category);
|
||||
|
||||
$this->assertEquals($category->name, $result['name']);
|
||||
$this->assertEquals([], $result['spent']);
|
||||
$this->assertEquals([], $result['earned']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic coverage
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\CategoryTransformer
|
||||
*/
|
||||
public function testWithDates(): void
|
||||
{
|
||||
$repository = $this->mock(CategoryRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
|
||||
$parameters = new ParameterBag;
|
||||
$parameters->set('start', new Carbon('2018-01-01'));
|
||||
$parameters->set('end', new Carbon('2018-01-31'));
|
||||
|
||||
// mock some objects for the spent/earned lists.
|
||||
$expense = new Transaction;
|
||||
$expense->transaction_currency_code = 'EUR';
|
||||
$expense->transactionCurrency = TransactionCurrency::find(1);
|
||||
$expense->transaction_amount = '-100';
|
||||
$income = new Transaction;
|
||||
$income->transaction_currency_code = 'EUR';
|
||||
$income->transactionCurrency = TransactionCurrency::find(1);
|
||||
$income->transaction_amount = '100';
|
||||
|
||||
|
||||
$incomeCollection = new Collection([$income]);
|
||||
$expenseCollection = new Collection([$expense]);
|
||||
|
||||
$repository->shouldReceive('spentInPeriodCollection')->atLeast()->once()->andReturn($expenseCollection);
|
||||
$repository->shouldReceive('earnedInPeriodCollection')->atLeast()->once()->andReturn($incomeCollection);
|
||||
|
||||
/** @var Category $category */
|
||||
$category = Category::first();
|
||||
$transformer = app(CategoryTransformer::class);
|
||||
$transformer->setParameters($parameters);
|
||||
$result = $transformer->transform($category);
|
||||
|
||||
$this->assertEquals($category->name, $result['name']);
|
||||
$this->assertEquals(
|
||||
[
|
||||
[
|
||||
'currency_id' => 1,
|
||||
'currency_code' => 'EUR',
|
||||
'currency_symbol' => '€',
|
||||
'currency_decimal_places' => 2,
|
||||
'amount' => -100,
|
||||
],
|
||||
], $result['spent']
|
||||
);
|
||||
$this->assertEquals(
|
||||
[
|
||||
[
|
||||
'currency_id' => 1,
|
||||
'currency_code' => 'EUR',
|
||||
'currency_symbol' => '€',
|
||||
'currency_decimal_places' => 2,
|
||||
'amount' => 100,
|
||||
],
|
||||
], $result['earned']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* CurrencyExchangeRateTransformerTest.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 Tests\Unit\Transformers;
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Models\CurrencyExchangeRate;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Transformers\CurrencyExchangeRateTransformer;
|
||||
use Log;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class CurrencyExchangeRateTransformerTest
|
||||
*/
|
||||
class CurrencyExchangeRateTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Log::info(sprintf('Now in %s.', \get_class($this)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Transformers\CurrencyExchangeRateTransformer
|
||||
*/
|
||||
public function testBasic()
|
||||
{
|
||||
$date = new Carbon;
|
||||
$eur = TransactionCurrency::whereCode('EUR')->first();
|
||||
$usd = TransactionCurrency::whereCode('USD')->first();
|
||||
$cer = new CurrencyExchangeRate;
|
||||
$cer->from_currency_id = $eur->id;
|
||||
$cer->to_currency_id = $usd->id;
|
||||
$cer->created_at = new Carbon;
|
||||
$cer->updated_at = new Carbon;
|
||||
$cer->rate = 1.5;
|
||||
$cer->date = $date;
|
||||
|
||||
$parameters = new ParameterBag;
|
||||
$parameters->set('amount', '100');
|
||||
|
||||
$transformer = app(CurrencyExchangeRateTransformer::class);
|
||||
$transformer->setParameters($parameters);
|
||||
$result = $transformer->transform($cer);
|
||||
|
||||
$this->assertEquals($cer->from_currency_id, $result['from_currency_id']);
|
||||
$this->assertEquals(150, $result['amount']);
|
||||
|
||||
|
||||
}
|
||||
}
|
93
tests/Unit/Transformers/CurrencyTransformerTest.php
Normal file
93
tests/Unit/Transformers/CurrencyTransformerTest.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
/**
|
||||
* CurrencyTransformerTest.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 Tests\Unit\Transformers;
|
||||
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Transformers\CurrencyTransformer;
|
||||
use Log;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
|
||||
/**
|
||||
* Class CurrencyTransformerTest
|
||||
*/
|
||||
class CurrencyTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Log::info(sprintf('Now in %s.', \get_class($this)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic coverage
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\CurrencyTransformer
|
||||
*/
|
||||
public function testBasic(): void
|
||||
{
|
||||
// mocks and prep:
|
||||
$parameters = new ParameterBag;
|
||||
$currency = TransactionCurrency::first();
|
||||
$transformer = app(CurrencyTransformer::class);
|
||||
$transformer->setParameters($parameters);
|
||||
|
||||
// action
|
||||
$result = $transformer->transform($currency);
|
||||
|
||||
|
||||
$this->assertEquals($currency->code, $result['code']);
|
||||
$this->assertFalse($result['default']);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic coverage with default currency
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\CurrencyTransformer
|
||||
*/
|
||||
public function testDefaultCurrency(): void
|
||||
{
|
||||
// mocks and prep:
|
||||
$parameters = new ParameterBag;
|
||||
$currency = TransactionCurrency::first();
|
||||
$parameters->set('defaultCurrency', $currency);
|
||||
$transformer = app(CurrencyTransformer::class);
|
||||
$transformer->setParameters($parameters);
|
||||
|
||||
|
||||
// action
|
||||
$result = $transformer->transform($currency);
|
||||
|
||||
|
||||
$this->assertEquals($currency->code, $result['code']);
|
||||
$this->assertTrue($result['default']);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* JournalMetaTransformerTest.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 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
|
||||
*/
|
||||
public function testBasic(): void
|
||||
{
|
||||
$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']);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user