mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 08:35:00 +00:00
Cover all transformers.
This commit is contained in:
@@ -98,6 +98,9 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
* @property string transaction_journal_budget_encrypted
|
||||
* @property string type
|
||||
* @property string name
|
||||
* @property Carbon created_at
|
||||
* @property Carbon updated_at
|
||||
* @property string foreign_currency_code
|
||||
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
|
||||
*/
|
||||
class Transaction extends Model
|
||||
|
@@ -31,9 +31,11 @@ use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
|
||||
use FireflyIII\Helpers\Filter\InternalTransferFilter;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\PiggyBankEvent;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionJournalLink;
|
||||
use FireflyIII\Models\TransactionJournalMeta;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;
|
||||
@@ -455,6 +457,23 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
return $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournalLink $link
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLinkNoteText(TransactionJournalLink $link): string
|
||||
{
|
||||
$notes = null;
|
||||
/** @var Note $note */
|
||||
$note = $link->notes()->first();
|
||||
if (null !== $note) {
|
||||
return $note->text ?? '';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return Carbon value of a meta field (or NULL).
|
||||
*
|
||||
|
@@ -27,6 +27,7 @@ use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionJournalLink;
|
||||
use FireflyIII\Models\TransactionJournalMeta;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\User;
|
||||
@@ -38,6 +39,14 @@ use Illuminate\Support\MessageBag;
|
||||
*/
|
||||
interface JournalRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param TransactionJournalLink $link
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLinkNoteText(TransactionJournalLink $link): string;
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param TransactionType $type
|
||||
|
@@ -217,6 +217,22 @@ class UserRepository implements UserRepositoryInterface
|
||||
return Role::where('name', $role)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRoleByUser(User $user): ?string
|
||||
{
|
||||
/** @var Role $role */
|
||||
$role = $user->roles()->first();
|
||||
if (null !== $role) {
|
||||
return $role->name;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return basic user information.
|
||||
*
|
||||
|
@@ -31,6 +31,13 @@ use Illuminate\Support\Collection;
|
||||
*/
|
||||
interface UserRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRoleByUser(User $user): ?string;
|
||||
/**
|
||||
* Returns a collection of all users.
|
||||
*
|
||||
|
@@ -24,11 +24,9 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\TransactionJournalLink;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use Log;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -36,6 +34,8 @@ use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
*/
|
||||
class TransactionLinkTransformer extends AbstractTransformer
|
||||
{
|
||||
/** @var JournalRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -44,6 +44,8 @@ class TransactionLinkTransformer extends AbstractTransformer
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->repository = app(JournalRepositoryInterface::class);
|
||||
|
||||
if ('testing' === config('app.env')) {
|
||||
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
|
||||
}
|
||||
@@ -56,14 +58,8 @@ class TransactionLinkTransformer extends AbstractTransformer
|
||||
*/
|
||||
public function transform(TransactionJournalLink $link): array
|
||||
{
|
||||
$notes = null;
|
||||
/** @var Note $note */
|
||||
$note = $link->notes()->first();
|
||||
if (null !== $note) {
|
||||
$notes = $note->text;
|
||||
}
|
||||
|
||||
$data = [
|
||||
$notes = $this->repository->getLinkNoteText($link);
|
||||
$data = [
|
||||
'id' => (int)$link->id,
|
||||
'created_at' => $link->created_at->toAtomString(),
|
||||
'updated_at' => $link->updated_at->toAtomString(),
|
||||
|
@@ -61,27 +61,21 @@ class TransactionTransformer extends AbstractTransformer
|
||||
*/
|
||||
public function transform(Transaction $transaction): array
|
||||
{
|
||||
$categoryId = null;
|
||||
$categoryName = null;
|
||||
$budgetId = null;
|
||||
$budgetName = null;
|
||||
$categoryId = $transaction->transaction_category_id ?? $transaction->transaction_journal_category_id;
|
||||
$categoryName = $transaction->transaction_category_name ?? $transaction->transaction_journal_category_name;
|
||||
$journal = $transaction->transactionJournal;
|
||||
$notes = $this->repository->getNoteText($journal);
|
||||
if ($transaction->transaction_type_type === TransactionType::WITHDRAWAL) {
|
||||
$budgetId = $transaction->transaction_budget_id ?? $transaction->transaction_journal_budget_id;
|
||||
$budgetName = $transaction->transaction_budget_name ?? $transaction->transaction_journal_budget_name;
|
||||
}
|
||||
// get tags:
|
||||
$tags = implode(',', $journal->tags->pluck('tag')->toArray());
|
||||
$journal = $transaction->transactionJournal;
|
||||
$category = $this->getCategory($transaction);
|
||||
$budget = $this->getBudget($transaction);
|
||||
|
||||
$this->repository->setUser($journal->user);
|
||||
|
||||
$notes = $this->repository->getNoteText($journal);
|
||||
$tags = implode(',', $this->repository->getTags($journal));
|
||||
|
||||
$data = [
|
||||
'id' => (int)$transaction->id,
|
||||
'created_at' => $transaction->created_at->toAtomString(),
|
||||
'updated_at' => $transaction->updated_at->toAtomString(),
|
||||
'description' => $transaction->description,
|
||||
'journal_description' => $transaction->description,
|
||||
'transaction_description' => $transaction->transaction_description,
|
||||
'date' => $transaction->date->format('Y-m-d'),
|
||||
'type' => $transaction->transaction_type_type,
|
||||
@@ -100,10 +94,10 @@ class TransactionTransformer extends AbstractTransformer
|
||||
'foreign_currency_decimal_places' => $transaction->foreign_currency_dp,
|
||||
'bill_id' => $transaction->bill_id,
|
||||
'bill_name' => $transaction->bill_name,
|
||||
'category_id' => $categoryId,
|
||||
'category_name' => $categoryName,
|
||||
'budget_id' => $budgetId,
|
||||
'budget_name' => $budgetName,
|
||||
'category_id' => $category['category_id'],
|
||||
'category_name' => $category['category_name'],
|
||||
'budget_id' => $budget['budget_id'],
|
||||
'budget_name' => $budget['budget_name'],
|
||||
'notes' => $notes,
|
||||
'sepa_cc' => $this->repository->getMetaField($journal, 'sepa-cc'),
|
||||
'sepa_ct_op' => $this->repository->getMetaField($journal, 'sepa-ct-op'),
|
||||
@@ -138,6 +132,7 @@ class TransactionTransformer extends AbstractTransformer
|
||||
if (null !== $transaction->transaction_foreign_amount) {
|
||||
$data['foreign_amount'] = round($transaction->transaction_foreign_amount, (int)$transaction->foreign_currency_dp);
|
||||
}
|
||||
|
||||
// switch on type for consistency
|
||||
switch ($transaction->transaction_type_type) {
|
||||
case TransactionType::WITHDRAWAL:
|
||||
@@ -176,11 +171,43 @@ class TransactionTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
// expand description.
|
||||
if (\strlen((string)$transaction->transaction_description) > 0) {
|
||||
if ('' !== (string)$transaction->transaction_description) {
|
||||
$data['description'] = $transaction->transaction_description . ' (' . $transaction->description . ')';
|
||||
}
|
||||
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getBudget(Transaction $transaction): array
|
||||
{
|
||||
if ($transaction->transaction_type_type !== TransactionType::WITHDRAWAL) {
|
||||
return [
|
||||
'budget_id' => null,
|
||||
'budget_name' => null,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'budget_id' => $transaction->transaction_budget_id ?? $transaction->transaction_journal_budget_id,
|
||||
'budget_name' => $transaction->transaction_budget_name ?? $transaction->transaction_journal_budget_name,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getCategory(Transaction $transaction): array
|
||||
{
|
||||
return [
|
||||
'category_id' => $transaction->transaction_category_id ?? $transaction->transaction_journal_category_id,
|
||||
'category_name' => $transaction->transaction_category_name ?? $transaction->transaction_journal_category_name,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@@ -25,6 +25,7 @@ namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Models\Role;
|
||||
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Log;
|
||||
|
||||
@@ -33,6 +34,8 @@ use Log;
|
||||
*/
|
||||
class UserTransformer extends AbstractTransformer
|
||||
{
|
||||
/** @var UserRepositoryInterface */
|
||||
private $repository;
|
||||
/**
|
||||
* UserTransformer constructor.
|
||||
*
|
||||
@@ -40,6 +43,7 @@ class UserTransformer extends AbstractTransformer
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->repository = app(UserRepositoryInterface::class);
|
||||
if ('testing' === config('app.env')) {
|
||||
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
|
||||
}
|
||||
@@ -54,12 +58,6 @@ class UserTransformer extends AbstractTransformer
|
||||
*/
|
||||
public function transform(User $user): array
|
||||
{
|
||||
/** @var Role $role */
|
||||
$role = $user->roles()->first();
|
||||
if (null !== $role) {
|
||||
$role = $role->name;
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => (int)$user->id,
|
||||
'created_at' => $user->created_at->toAtomString(),
|
||||
@@ -67,7 +65,7 @@ class UserTransformer extends AbstractTransformer
|
||||
'email' => $user->email,
|
||||
'blocked' => 1 === (int)$user->blocked,
|
||||
'blocked_code' => '' === $user->blocked_code ? null : $user->blocked_code,
|
||||
'role' => $role,
|
||||
'role' => $this->repository->getRoleByUser($user),
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
|
@@ -53,8 +53,10 @@ class TagTransformerTest extends TestCase
|
||||
'zoomLevel' => 3,
|
||||
]
|
||||
);
|
||||
$transformer = new TagTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($tag);
|
||||
$transformer = app(TagTransformer::class);
|
||||
$transformer->setParameters(new ParameterBag);
|
||||
$result = $transformer->transform($tag);
|
||||
|
||||
$this->assertEquals($tag->tag, $result['tag']);
|
||||
$this->assertEquals(5.5, $result['latitude']);
|
||||
$this->assertEquals(6.6, $result['longitude']);
|
||||
|
66
tests/Unit/Transformers/TransactionLinkTransformerTest.php
Normal file
66
tests/Unit/Transformers/TransactionLinkTransformerTest.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/**
|
||||
* TransactionLinkTransformerTest.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\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionJournalLink;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\Transformers\TransactionLinkTransformer;
|
||||
use FireflyIII\Transformers\TransactionTransformer;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class TransactionLinkTransformerTest
|
||||
*/
|
||||
class TransactionLinkTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Test basic tag transformer
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\TransactionLinkTransformer
|
||||
*/
|
||||
public function testBasic(): void
|
||||
{
|
||||
$repository = $this->mock(JournalRepositoryInterface::class);
|
||||
|
||||
$repository->shouldReceive('getLinkNoteText')->atLeast()->once()->andReturn('abc');
|
||||
|
||||
/** @var TransactionJournalLink $link */
|
||||
$link = TransactionJournalLink::first();
|
||||
|
||||
$transformer = app(TransactionLinkTransformer::class);
|
||||
$transformer->setParameters(new ParameterBag);
|
||||
|
||||
$result = $transformer->transform($link);
|
||||
|
||||
$this->assertEquals($link->source_id, $result['inward_id']);
|
||||
$this->assertEquals('abc', $result['notes']);
|
||||
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace Tests\Unit\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||
use FireflyIII\Transformers\UserTransformer;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
@@ -41,27 +42,15 @@ class UserTransformerTest extends TestCase
|
||||
*/
|
||||
public function testBasic(): void
|
||||
{
|
||||
$user = $this->user();
|
||||
$transformer = new UserTransformer(new ParameterBag());
|
||||
$result = $transformer->transform($user);
|
||||
$repository = $this->mock(UserRepositoryInterface::class);
|
||||
$repository->shouldReceive('getRoleByUser')->atLeast()->once()->andReturn('owner');
|
||||
$user = $this->user();
|
||||
|
||||
$transformer = app(UserTransformer::class);
|
||||
$transformer->setParameters(new ParameterBag);
|
||||
$result = $transformer->transform($user);
|
||||
|
||||
$this->assertEquals($user->email, $result['email']);
|
||||
$this->assertEquals('owner', $result['role']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test basic transformer.
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\UserTransformer
|
||||
*/
|
||||
public function testEmptyUser(): void
|
||||
{
|
||||
$user = $this->emptyUser();
|
||||
$transformer = new UserTransformer(new ParameterBag());
|
||||
$result = $transformer->transform($user);
|
||||
|
||||
$this->assertEquals($user->email, $result['email']);
|
||||
$this->assertNull($result['role']);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user