Files
firefly-iii/tests/Unit/Transformers/PiggyBankTransformerTest.php

137 lines
5.1 KiB
PHP
Raw Normal View History

2018-02-17 10:47:06 +01:00
<?php
/**
* PiggyBankTransformerTest.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 Amount;
2018-02-17 10:47:06 +01:00
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\TransactionCurrency;
2018-12-20 05:46:05 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-02-17 10:47:06 +01:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Transformers\PiggyBankTransformer;
2018-12-20 20:50:05 +01:00
use Log;
2018-12-20 05:46:05 +01:00
use Mockery;
2018-02-17 10:47:06 +01:00
use Symfony\Component\HttpFoundation\ParameterBag;
use Tests\TestCase;
/**
* Class PiggyBankTransformerTest
2019-08-17 10:48:28 +02:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-02-17 10:47:06 +01:00
*/
class PiggyBankTransformerTest extends TestCase
{
2018-12-20 20:50:05 +01:00
/**
*
*/
public function setUp(): void
{
parent::setUp();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-12-20 20:50:05 +01:00
}
2018-02-17 10:47:06 +01:00
/**
* Test basic transformer.
*
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Transformers\PiggyBankTransformer
2018-02-17 10:47:06 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testBasic(): void
2018-02-17 10:47:06 +01:00
{
2018-12-20 05:46:05 +01:00
// mock repositories
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-02-17 10:47:06 +01:00
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-12-20 05:46:05 +01:00
// mock calls:
$piggyRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
// return a currency
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->atLeast()->once()->andReturn('1');
2019-08-03 04:46:47 +02:00
$currencyRepos->shouldReceive('findNull')->withArgs([1])->atLeast()->once()->andReturn($this->getEuro());
2018-12-20 05:46:05 +01:00
// get a note
$piggyRepos->shouldReceive('getNoteText')->atLeast()->once()->andReturn('I am a note.');
// get some amounts
$piggyRepos->shouldReceive('getCurrentAmount')->atLeast()->once()->andReturn('123.45');
$piggyRepos->shouldReceive('getSuggestedMonthlyAmount')->atLeast()->once()->andReturn('12.45');
/** @var PiggyBankTransformer $transformer */
$transformer = app(PiggyBankTransformer::class);
$transformer->setParameters(new ParameterBag());
$piggy = PiggyBank::first();
$result = $transformer->transform($piggy);
$this->assertEquals(12.45, $result['save_per_month']);
2018-02-17 10:47:06 +01:00
$this->assertEquals($piggy->name, $result['name']);
2018-12-20 05:46:05 +01:00
$this->assertEquals(1, $result['currency_id']);
2018-02-17 10:47:06 +01:00
}
/**
2018-12-20 05:46:05 +01:00
* Test basic transformer.
2018-02-17 10:47:06 +01:00
*
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Transformers\PiggyBankTransformer
2018-02-17 10:47:06 +01:00
*/
2018-12-20 05:46:05 +01:00
public function testNoCurrency(): void
2018-02-17 10:47:06 +01:00
{
2018-12-20 05:46:05 +01:00
// mock repositories
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-02-17 10:47:06 +01:00
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-12-20 05:46:05 +01:00
// mock calls:
$piggyRepos->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
// return a currency
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->atLeast()->once()->andReturn('1');
$currencyRepos->shouldReceive('findNull')->withArgs([1])->atLeast()->once()->andReturn(null);
2019-08-03 04:46:47 +02:00
Amount::shouldReceive('getDefaultCurrencyByUser')->atLeast()->once()->andReturn($this->getEuro());
2018-12-20 05:46:05 +01:00
// get a note
$piggyRepos->shouldReceive('getNoteText')->atLeast()->once()->andReturn('I am a note.');
// get some amounts
$piggyRepos->shouldReceive('getCurrentAmount')->atLeast()->once()->andReturn('123.45');
$piggyRepos->shouldReceive('getSuggestedMonthlyAmount')->atLeast()->once()->andReturn('12.45');
/** @var PiggyBankTransformer $transformer */
$transformer = app(PiggyBankTransformer::class);
$transformer->setParameters(new ParameterBag());
$piggy = PiggyBank::first();
$result = $transformer->transform($piggy);
$this->assertEquals(12.45, $result['save_per_month']);
2018-02-17 10:47:06 +01:00
$this->assertEquals($piggy->name, $result['name']);
2018-12-20 05:46:05 +01:00
$this->assertEquals(1, $result['currency_id']);
2018-02-17 10:47:06 +01:00
}
2018-12-20 05:46:05 +01:00
2018-03-04 15:14:29 +01:00
}