Skip to content

Firefly III: a personal finances manager

Make sure transformers match API definition.

James Cole446ff81335ea
diff --git a/app/Models/Budget.php b/app/Models/Budget.php
index 5efdbe2b58..d353074508 100644
--- a/app/Models/Budget.php
+++ b/app/Models/Budget.php
@@ -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'];
diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php
index 4437bb2875..ed227af253 100644
--- a/app/Repositories/Budget/BudgetRepository.php
+++ b/app/Repositories/Budget/BudgetRepository.php
@@ -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),
];
}
diff --git a/app/Support/Http/Api/AccountFilter.php b/app/Support/Http/Api/AccountFilter.php
index 831c28f946..866cee14ae 100644
--- a/app/Support/Http/Api/AccountFilter.php
+++ b/app/Support/Http/Api/AccountFilter.php
@@ -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],
diff --git a/app/Transformers/BudgetLimitTransformer.php b/app/Transformers/BudgetLimitTransformer.php
index bbe1ace88c..414f201a48 100644
--- a/app/Transformers/BudgetLimitTransformer.php
+++ b/app/Transformers/BudgetLimitTransformer.php
@@ -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,
diff --git a/app/Transformers/BudgetTransformer.php b/app/Transformers/BudgetTransformer.php
index 431d4b91fc..c6273ef5ca 100644
--- a/app/Transformers/BudgetTransformer.php
+++ b/app/Transformers/BudgetTransformer.php
@@ -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' => [
diff --git a/app/Transformers/CategoryTransformer.php b/app/Transformers/CategoryTransformer.php
index 4e6e7e4283..1b33b44414 100644
--- a/app/Transformers/CategoryTransformer.php
+++ b/app/Transformers/CategoryTransformer.php
@@ -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),
];
}
diff --git a/app/Transformers/CurrencyExchangeRateTransformer.php b/app/Transformers/CurrencyExchangeRateTransformer.php
index 143cb24c41..20f52caaa2 100644
--- a/app/Transformers/CurrencyExchangeRateTransformer.php
+++ b/app/Transformers/CurrencyExchangeRateTransformer.php
@@ -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,
diff --git a/app/Transformers/JournalMetaTransformer.php b/app/Transformers/JournalMetaTransformer.php
deleted file mode 100644
index d0efd43c9f..0000000000
--- a/app/Transformers/JournalMetaTransformer.php
+++ /dev/null
@@ -1,85 +0,0 @@
-<?php
-/**
- * JournalMetaTransformer.php
- * Copyright (c) 2018 [email protected]
- *
- * 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;
- }
-
-}
diff --git a/tests/Unit/Transformers/AccountTransformerTest.php b/tests/Unit/Transformers/AccountTransformerTest.php
index 91bb62add2..d17747cfd9 100644
--- a/tests/Unit/Transformers/AccountTransformerTest.php
+++ b/tests/Unit/Transformers/AccountTransformerTest.php
@@ -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.
*
diff --git a/tests/Unit/Transformers/AttachmentTransformerTest.php b/tests/Unit/Transformers/AttachmentTransformerTest.php
index 27832588f6..d07bc1818f 100644
--- a/tests/Unit/Transformers/AttachmentTransformerTest.php
+++ b/tests/Unit/Transformers/AttachmentTransformerTest.php
@@ -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
*
diff --git a/tests/Unit/Transformers/AvailableBudgetTransformerTest.php b/tests/Unit/Transformers/AvailableBudgetTransformerTest.php
index d25643820e..77091c944d 100644
--- a/tests/Unit/Transformers/AvailableBudgetTransformerTest.php
+++ b/tests/Unit/Transformers/AvailableBudgetTransformerTest.php
@@ -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
*
diff --git a/tests/Unit/Transformers/BillTransformerTest.php b/tests/Unit/Transformers/BillTransformerTest.php
index 4367edccff..34fbd17075 100644
--- a/tests/Unit/Transformers/BillTransformerTest.php
+++ b/tests/Unit/Transformers/BillTransformerTest.php
@@ -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
*
diff --git a/tests/Unit/Transformers/BudgetLimitTransformerTest.php b/tests/Unit/Transformers/BudgetLimitTransformerTest.php
new file mode 100644
index 0000000000..5713373c7d
--- /dev/null
+++ b/tests/Unit/Transformers/BudgetLimitTransformerTest.php
@@ -0,0 +1,65 @@
+<?php
+/**
+ * BudgetLimitTransformerTest.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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']);
+ }
+
+}
\ No newline at end of file
diff --git a/tests/Unit/Transformers/BudgetTransformerTest.php b/tests/Unit/Transformers/BudgetTransformerTest.php
index 63644ad8e3..0270fc75d9 100644
--- a/tests/Unit/Transformers/BudgetTransformerTest.php
+++ b/tests/Unit/Transformers/BudgetTransformerTest.php
@@ -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);
+
+ // 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;
- $budget = Budget::create(
+ // 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);
-
- $this->assertEquals($budget->name, $result['name']);
- $this->assertTrue($result['active']);
+ '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']);
+
}
}
diff --git a/tests/Unit/Transformers/CategoryTransformerTest.php b/tests/Unit/Transformers/CategoryTransformerTest.php
index c0276f1403..2d5ef88119 100644
--- a/tests/Unit/Transformers/CategoryTransformerTest.php
+++ b/tests/Unit/Transformers/CategoryTransformerTest.php
@@ -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();
+
+ /** @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';
- $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);
+
+ $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']
+ );
}
}
diff --git a/tests/Unit/Transformers/CurrencyExchangeRateTransformerTest.php b/tests/Unit/Transformers/CurrencyExchangeRateTransformerTest.php
new file mode 100644
index 0000000000..4cb503790a
--- /dev/null
+++ b/tests/Unit/Transformers/CurrencyExchangeRateTransformerTest.php
@@ -0,0 +1,78 @@
+<?php
+/**
+ * CurrencyExchangeRateTransformerTest.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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']);
+
+
+ }
+}
\ No newline at end of file
diff --git a/tests/Unit/Transformers/CurrencyTransformerTest.php b/tests/Unit/Transformers/CurrencyTransformerTest.php
new file mode 100644
index 0000000000..0cf0809f35
--- /dev/null
+++ b/tests/Unit/Transformers/CurrencyTransformerTest.php
@@ -0,0 +1,93 @@
+<?php
+/**
+ * CurrencyTransformerTest.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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']);
+
+ }
+
+}
diff --git a/tests/Unit/Transformers/JournalMetaTransformerTest.php b/tests/Unit/Transformers/JournalMetaTransformerTest.php
deleted file mode 100644
index 97d7528ecf..0000000000
--- a/tests/Unit/Transformers/JournalMetaTransformerTest.php
+++ /dev/null
@@ -1,60 +0,0 @@
-<?php
-/**
- * JournalMetaTransformerTest.php
- * Copyright (c) 2018 [email protected]
- *
- * 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']);
- }
-
-}