2018-02-17 03:47:06 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CategoryTransformerTest.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;
|
|
|
|
|
2018-12-19 12:02:16 -06:00
|
|
|
use Carbon\Carbon;
|
2018-02-17 03:47:06 -06:00
|
|
|
use FireflyIII\Models\Category;
|
2018-12-19 12:02:16 -06:00
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
2018-02-17 03:47:06 -06:00
|
|
|
use FireflyIII\Transformers\CategoryTransformer;
|
2018-12-19 12:02:16 -06:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Log;
|
2018-02-17 03:47:06 -06:00
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CategoryTransformerTest
|
|
|
|
*/
|
|
|
|
class CategoryTransformerTest extends TestCase
|
|
|
|
{
|
2018-12-19 12:02:16 -06:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2019-04-09 13:05:20 -05:00
|
|
|
Log::info(sprintf('Now in %s.', get_class($this)));
|
2018-12-19 12:02:16 -06:00
|
|
|
}
|
|
|
|
|
2018-02-17 03:47:06 -06:00
|
|
|
/**
|
|
|
|
* Basic coverage
|
|
|
|
*
|
2018-08-09 13:17:15 -05:00
|
|
|
* @covers \FireflyIII\Transformers\CategoryTransformer
|
2018-02-17 03:47:06 -06:00
|
|
|
*/
|
2018-05-11 12:58:10 -05:00
|
|
|
public function testBasic(): void
|
2018-02-17 03:47:06 -06:00
|
|
|
{
|
2018-12-19 12:02:16 -06:00
|
|
|
$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';
|
2019-08-02 21:46:47 -05:00
|
|
|
$expense->transactionCurrency = $this->getEuro();
|
2018-12-19 12:02:16 -06:00
|
|
|
$expense->transaction_amount = '-100';
|
|
|
|
$income = new Transaction;
|
|
|
|
$income->transaction_currency_code = 'EUR';
|
2019-08-02 21:46:47 -05:00
|
|
|
$income->transactionCurrency = $this->getEuro();
|
2018-12-19 12:02:16 -06:00
|
|
|
$income->transaction_amount = '100';
|
2018-02-17 03:47:06 -06:00
|
|
|
|
2018-12-19 12:02:16 -06:00
|
|
|
|
2019-08-02 21:46:47 -05:00
|
|
|
$incomeCollection = [$income];
|
|
|
|
$expenseCollection = [$expense];
|
2018-12-19 12:02:16 -06:00
|
|
|
|
|
|
|
$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);
|
2018-02-17 03:47:06 -06:00
|
|
|
|
|
|
|
$this->assertEquals($category->name, $result['name']);
|
2018-12-19 12:02:16 -06:00
|
|
|
$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']
|
|
|
|
);
|
2018-02-17 03:47:06 -06:00
|
|
|
}
|
2018-03-04 08:14:29 -06:00
|
|
|
}
|