mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Make sure transformers match API definition.
This commit is contained in:
@@ -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']);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user