mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 02:26:58 +00:00
Improve transformers and tests
This commit is contained in:
@@ -73,8 +73,8 @@ class AttachmentTransformer extends AbstractTransformer
|
|||||||
'download_uri' => route('api.v1.attachments.download', [$attachment->id]),
|
'download_uri' => route('api.v1.attachments.download', [$attachment->id]),
|
||||||
'upload_uri' => route('api.v1.attachments.upload', [$attachment->id]),
|
'upload_uri' => route('api.v1.attachments.upload', [$attachment->id]),
|
||||||
'title' => $attachment->title,
|
'title' => $attachment->title,
|
||||||
'mime' => $attachment->mime,
|
|
||||||
'notes' => $this->repository->getNoteText($attachment),
|
'notes' => $this->repository->getNoteText($attachment),
|
||||||
|
'mime' => $attachment->mime,
|
||||||
'size' => (int)$attachment->size,
|
'size' => (int)$attachment->size,
|
||||||
'links' => [
|
'links' => [
|
||||||
[
|
[
|
||||||
|
@@ -25,9 +25,7 @@ namespace FireflyIII\Transformers;
|
|||||||
|
|
||||||
|
|
||||||
use FireflyIII\Models\AvailableBudget;
|
use FireflyIII\Models\AvailableBudget;
|
||||||
use League\Fractal\TransformerAbstract;
|
|
||||||
use Log;
|
use Log;
|
||||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class AvailableBudgetTransformer
|
* Class AvailableBudgetTransformer
|
||||||
@@ -64,9 +62,10 @@ class AvailableBudgetTransformer extends AbstractTransformer
|
|||||||
'currency_code' => $currency->code,
|
'currency_code' => $currency->code,
|
||||||
'currency_symbol' => $currency->symbol,
|
'currency_symbol' => $currency->symbol,
|
||||||
'currency_decimal_places' => $currency->decimal_places,
|
'currency_decimal_places' => $currency->decimal_places,
|
||||||
|
'amount' => round($availableBudget->amount, $currency->decimal_places),
|
||||||
'start' => $availableBudget->start_date->format('Y-m-d'),
|
'start' => $availableBudget->start_date->format('Y-m-d'),
|
||||||
'end' => $availableBudget->end_date->format('Y-m-d'),
|
'end' => $availableBudget->end_date->format('Y-m-d'),
|
||||||
'amount' => round($availableBudget->amount, $currency->decimal_places),
|
|
||||||
'links' => [
|
'links' => [
|
||||||
[
|
[
|
||||||
'rel' => 'self',
|
'rel' => 'self',
|
||||||
|
@@ -24,15 +24,10 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Transformers;
|
namespace FireflyIII\Transformers;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
|
|
||||||
use FireflyIII\Models\Bill;
|
use FireflyIII\Models\Bill;
|
||||||
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use League\Fractal\Resource\Collection as FractalCollection;
|
|
||||||
use League\Fractal\Resource\Item;
|
|
||||||
use League\Fractal\TransformerAbstract;
|
|
||||||
use Log;
|
use Log;
|
||||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BillTransformer
|
* Class BillTransformer
|
||||||
@@ -66,27 +61,29 @@ class BillTransformer extends AbstractTransformer
|
|||||||
{
|
{
|
||||||
$paidData = $this->paidData($bill);
|
$paidData = $this->paidData($bill);
|
||||||
$payDates = $this->payDates($bill);
|
$payDates = $this->payDates($bill);
|
||||||
|
$currency = $bill->transactionCurrency;
|
||||||
|
$notes = $this->repository->getNoteText($bill);
|
||||||
|
$notes = '' === $notes ? null : $notes;
|
||||||
$this->repository->setUser($bill->user);
|
$this->repository->setUser($bill->user);
|
||||||
$data = [
|
$data = [
|
||||||
'id' => (int)$bill->id,
|
'id' => (int)$bill->id,
|
||||||
'created_at' => $bill->created_at->toAtomString(),
|
'created_at' => $bill->created_at->toAtomString(),
|
||||||
'updated_at' => $bill->updated_at->toAtomString(),
|
'updated_at' => $bill->updated_at->toAtomString(),
|
||||||
'currency_id' => $bill->transaction_currency_id,
|
'currency_id' => $bill->transaction_currency_id,
|
||||||
'currency_code' => $bill->transactionCurrency->code,
|
'currency_code' => $currency->code,
|
||||||
'currency_symbol' => $bill->transactionCurrency->symbol,
|
'currency_symbol' => $currency->symbol,
|
||||||
'currency_decimal_places' => $bill->transactionCurrency->decimal_places,
|
'currency_decimal_places' => $currency->decimal_places,
|
||||||
'name' => $bill->name,
|
'name' => $bill->name,
|
||||||
'amount_min' => round((float)$bill->amount_min, 2),
|
'amount_min' => round((float)$bill->amount_min, $currency->decimal_places),
|
||||||
'amount_max' => round((float)$bill->amount_max, 2),
|
'amount_max' => round((float)$bill->amount_max, $currency->decimal_places),
|
||||||
'date' => $bill->date->format('Y-m-d'),
|
'date' => $bill->date->format('Y-m-d'),
|
||||||
'repeat_freq' => $bill->repeat_freq,
|
'repeat_freq' => $bill->repeat_freq,
|
||||||
'skip' => (int)$bill->skip,
|
'skip' => (int)$bill->skip,
|
||||||
'active' => $bill->active,
|
'active' => $bill->active,
|
||||||
'notes' => $this->repository->getNoteText($bill),
|
'notes' => $notes,
|
||||||
'next_expected_match' => $paidData['next_expected_match'],
|
'next_expected_match' => $paidData['next_expected_match'],
|
||||||
'pay_dates' => $payDates,
|
'pay_dates' => $payDates,
|
||||||
'paid_dates' => $paidData['paid_dates'],
|
'paid_dates' => $paidData['paid_dates'],
|
||||||
|
|
||||||
'links' => [
|
'links' => [
|
||||||
[
|
[
|
||||||
'rel' => 'self',
|
'rel' => 'self',
|
||||||
@@ -161,10 +158,7 @@ class BillTransformer extends AbstractTransformer
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var BillRepositoryInterface $repository */
|
$set = $this->repository->getPaidDatesInRange($bill, $this->parameters->get('start'), $this->parameters->get('end'));
|
||||||
$repository = app(BillRepositoryInterface::class);
|
|
||||||
$repository->setUser($bill->user);
|
|
||||||
$set = $repository->getPaidDatesInRange($bill, $this->parameters->get('start'), $this->parameters->get('end'));
|
|
||||||
Log::debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count()));
|
Log::debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count()));
|
||||||
$simple = $set->map(
|
$simple = $set->map(
|
||||||
function (Carbon $date) {
|
function (Carbon $date) {
|
||||||
@@ -179,7 +173,7 @@ class BillTransformer extends AbstractTransformer
|
|||||||
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
|
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
|
||||||
}
|
}
|
||||||
$end = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
|
$end = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
|
||||||
$journalCount = $repository->getPaidDatesInRange($bill, $nextMatch, $end)->count();
|
$journalCount = $this->repository->getPaidDatesInRange($bill, $nextMatch, $end)->count();
|
||||||
if ($journalCount > 0) {
|
if ($journalCount > 0) {
|
||||||
$nextMatch = clone $end;
|
$nextMatch = clone $end;
|
||||||
}
|
}
|
||||||
|
59
tests/Unit/Transformers/AvailableBudgetTransformerTest.php
Normal file
59
tests/Unit/Transformers/AvailableBudgetTransformerTest.php
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* AvailableBudgetTransformerTest.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\AvailableBudget;
|
||||||
|
use FireflyIII\Transformers\AvailableBudgetTransformer;
|
||||||
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class AvailableBudgetTransformerTest
|
||||||
|
*/
|
||||||
|
class AvailableBudgetTransformerTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Test basic transformer
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Transformers\AvailableBudgetTransformer
|
||||||
|
*/
|
||||||
|
public function testBasic(): void
|
||||||
|
{
|
||||||
|
/** @var AvailableBudget $availableBudget */
|
||||||
|
$availableBudget = AvailableBudget::first();
|
||||||
|
$currency = $availableBudget->transactionCurrency;
|
||||||
|
// make transformer
|
||||||
|
$transformer = app(AvailableBudgetTransformer::class);
|
||||||
|
$transformer->setParameters(new ParameterBag);
|
||||||
|
$result = $transformer->transform($availableBudget);
|
||||||
|
|
||||||
|
// test results
|
||||||
|
$this->assertEquals($availableBudget->id, $result['id']);
|
||||||
|
$this->assertEquals($currency->id, $result['currency_id']);
|
||||||
|
$this->assertEquals($availableBudget->start_date->format('Y-m-d'), $result['start']);
|
||||||
|
$this->assertEquals(round($availableBudget->amount, 2), $result['amount']);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -43,66 +43,65 @@ class BillTransformerTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testBasic(): void
|
public function testBasic(): void
|
||||||
{
|
{
|
||||||
|
$repository = $this->mock(BillRepositoryInterface::class);
|
||||||
|
$repository->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
$repository->shouldReceive('getNoteText')->atLeast()->once()->andReturn('');
|
||||||
|
|
||||||
$bill = Bill::create(
|
|
||||||
[
|
/** @var Bill $bill */
|
||||||
'user_id' => $this->user()->id,
|
$bill = Bill::first();
|
||||||
'name' => 'Some bill ' . random_int(1, 10000),
|
$transformer = app(BillTransformer::class);
|
||||||
'match' => 'word,' . random_int(1, 10000),
|
$transformer->setParameters(new ParameterBag);
|
||||||
'amount_min' => 12.34,
|
|
||||||
'amount_max' => 45.67,
|
|
||||||
'transaction_currency_id' => 1,
|
|
||||||
'date' => '2018-01-02',
|
|
||||||
'repeat_freq' => 'weekly',
|
|
||||||
'skip' => 0,
|
|
||||||
'active' => 1,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
$transformer = new BillTransformer(new ParameterBag);
|
|
||||||
$result = $transformer->transform($bill);
|
$result = $transformer->transform($bill);
|
||||||
|
|
||||||
|
// assert fields.
|
||||||
$this->assertEquals($bill->name, $result['name']);
|
$this->assertEquals($bill->name, $result['name']);
|
||||||
$this->assertTrue($result['active']);
|
$this->assertEquals($bill->transactionCurrency->decimal_places, $result['currency_decimal_places']);
|
||||||
|
$this->assertEquals($bill->active, $result['active']);
|
||||||
|
$this->assertNull($result['notes']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Coverage for dates.
|
* Basic coverage
|
||||||
*
|
*
|
||||||
* @covers \FireflyIII\Transformers\BillTransformer
|
* @covers \FireflyIII\Transformers\BillTransformer
|
||||||
*/
|
*/
|
||||||
public function testWithDates(): void
|
public function testWithDates(): void
|
||||||
{
|
{
|
||||||
// mock stuff
|
|
||||||
$repository = $this->mock(BillRepositoryInterface::class);
|
$repository = $this->mock(BillRepositoryInterface::class);
|
||||||
$repository->shouldReceive('setUser')->andReturnSelf();
|
$repository->shouldReceive('setUser')->atLeast()->once();
|
||||||
$repository->shouldReceive('getNoteText')->andReturn('Hi there');
|
$repository->shouldReceive('getNoteText')->atLeast()->once()->andReturn('');
|
||||||
$repository->shouldReceive('getPaidDatesInRange')->andReturn(new Collection([new Carbon('2018-01-02')]));
|
|
||||||
$bill = Bill::create(
|
// repos should also receive call for dates:
|
||||||
[
|
$list = new Collection(
|
||||||
'user_id' => $this->user()->id,
|
[new Carbon('2018-01-02'), new Carbon('2018-01-09'), new Carbon('2018-01-16'),
|
||||||
'name' => 'Some bill ' . random_int(1, 10000),
|
new Carbon('2018-01-21'), new Carbon('2018-01-30'),
|
||||||
'match' => 'word,' . random_int(1, 10000),
|
|
||||||
'amount_min' => 12.34,
|
|
||||||
'amount_max' => 45.67,
|
|
||||||
'date' => '2018-01-02',
|
|
||||||
'transaction_currency_id' => 1,
|
|
||||||
'repeat_freq' => 'monthly',
|
|
||||||
'skip' => 0,
|
|
||||||
'active' => 1,
|
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
$parameters = new ParameterBag();
|
$repository->shouldReceive('getPaidDatesInRange')->atLeast()->once()->andReturn($list);
|
||||||
|
|
||||||
|
$parameters = new ParameterBag;
|
||||||
$parameters->set('start', new Carbon('2018-01-01'));
|
$parameters->set('start', new Carbon('2018-01-01'));
|
||||||
$parameters->set('end', new Carbon('2018-01-31'));
|
$parameters->set('end', new Carbon('2018-01-31'));
|
||||||
$transformer = new BillTransformer($parameters);
|
|
||||||
|
/** @var Bill $bill */
|
||||||
|
$bill = Bill::first();
|
||||||
|
$transformer = app(BillTransformer::class);
|
||||||
|
$transformer->setParameters($parameters);
|
||||||
$result = $transformer->transform($bill);
|
$result = $transformer->transform($bill);
|
||||||
|
|
||||||
|
// assert fields.
|
||||||
$this->assertEquals($bill->name, $result['name']);
|
$this->assertEquals($bill->name, $result['name']);
|
||||||
$this->assertTrue($result['active']);
|
$this->assertEquals($bill->transactionCurrency->decimal_places, $result['currency_decimal_places']);
|
||||||
$this->assertCount(1, $result['pay_dates']);
|
$this->assertEquals($bill->active, $result['active']);
|
||||||
$this->assertEquals('2018-01-02', $result['pay_dates'][0]);
|
$this->assertNull($result['notes']);
|
||||||
$this->assertCount(1, $result['paid_dates']);
|
|
||||||
$this->assertEquals('2018-01-02', $result['paid_dates'][0]);
|
$this->assertEquals('2018-03-01', $result['next_expected_match']);
|
||||||
|
$this->assertEquals(['2018-01-01'], $result['pay_dates']);
|
||||||
|
$this->assertEquals(
|
||||||
|
['2018-01-02', '2018-01-09', '2018-01-16', '2018-01-21', '2018-01-30',]
|
||||||
|
, $result['paid_dates']
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user