2017-12-17 18:23:10 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ReconcileControllerTest.php
|
2020-02-16 13:59:55 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2017-12-17 18:23:10 +01:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2017-12-17 18:23:10 +01:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-12-17 18:23:10 +01:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-12-17 18:23:10 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:45:03 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-12-17 18:23:10 +01:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-12-17 18:23:10 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Tests\Feature\Controllers\Account;
|
|
|
|
|
2018-12-12 20:30:25 +01:00
|
|
|
use Carbon\Carbon;
|
2019-06-23 05:53:01 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Factory\TransactionGroupFactory;
|
|
|
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
|
|
|
use FireflyIII\Helpers\Fiscal\FiscalHelperInterface;
|
2018-02-28 15:50:00 +01:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
2018-09-03 08:41:03 +02:00
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
2018-02-28 20:18:47 +01:00
|
|
|
use Log;
|
2018-03-28 19:38:20 +02:00
|
|
|
use Mockery;
|
2019-06-23 05:53:01 +02:00
|
|
|
use Preferences;
|
|
|
|
use Steam;
|
2018-08-06 19:14:30 +02:00
|
|
|
use Tests\TestCase;
|
2017-12-17 18:23:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ConfigurationControllerTest
|
2019-07-23 17:33:23 +02:00
|
|
|
*
|
2019-08-17 10:48:28 +02:00
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
|
2017-12-17 18:23:10 +01:00
|
|
|
*/
|
|
|
|
class ReconcileControllerTest extends TestCase
|
|
|
|
{
|
2018-02-28 20:18:47 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-07-14 11:45:05 +02:00
|
|
|
public function setUp(): void
|
2018-02-28 20:18:47 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2019-04-09 20:05:20 +02:00
|
|
|
Log::info(sprintf('Now in %s.', get_class($this)));
|
2018-02-28 20:18:47 +01:00
|
|
|
}
|
|
|
|
|
2017-12-17 18:23:10 +01:00
|
|
|
|
|
|
|
/**
|
2018-07-01 09:27:22 +02:00
|
|
|
* Test showing the reconciliation.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
|
2017-12-17 18:23:10 +01:00
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testReconcile(): void
|
2017-12-17 18:23:10 +01:00
|
|
|
{
|
2019-06-23 05:53:01 +02:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
|
|
|
$this->mock(CurrencyRepositoryInterface::class);
|
2018-09-03 18:52:46 +02:00
|
|
|
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
2018-12-12 20:30:25 +01:00
|
|
|
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
|
2019-06-23 05:53:01 +02:00
|
|
|
$this->mock(GroupCollectorInterface::class);
|
2019-06-23 11:13:36 +02:00
|
|
|
$euro = $this->getEuro();
|
|
|
|
$asset = $this->getRandomAsset();
|
|
|
|
$date = new Carbon;
|
2017-12-17 18:23:10 +01:00
|
|
|
|
|
|
|
|
2019-06-23 05:53:01 +02:00
|
|
|
$userRepos->shouldReceive('hasRole')->atLeast()->once()->withArgs([Mockery::any(), 'owner'])->andReturnTrue();
|
2019-04-09 15:32:48 +02:00
|
|
|
|
2018-12-12 20:30:25 +01:00
|
|
|
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
|
|
|
|
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
|
2019-06-23 05:53:01 +02:00
|
|
|
Steam::shouldReceive('balance')->atLeast()->once()->andReturn('100');
|
|
|
|
$accountRepos->shouldReceive('getAccountCurrency')->atLeast()->once()->andReturn($euro);
|
2018-02-28 15:50:00 +01:00
|
|
|
|
2019-06-23 11:13:36 +02:00
|
|
|
// mock default session stuff
|
|
|
|
$this->mockDefaultSession();
|
|
|
|
|
2017-12-17 18:23:10 +01:00
|
|
|
$this->be($this->user());
|
2019-06-23 05:53:01 +02:00
|
|
|
$response = $this->get(route('accounts.reconcile', [$asset->id, '20170101', '20170131']));
|
2017-12-17 18:23:10 +01:00
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
// has bread crumb
|
|
|
|
$response->assertSee('<ol class="breadcrumb">');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-23 05:53:01 +02:00
|
|
|
* Submit reconciliation.
|
2018-07-01 09:27:22 +02:00
|
|
|
*
|
2019-06-23 05:53:01 +02:00
|
|
|
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
|
|
|
|
* @covers \FireflyIII\Http\Requests\ReconciliationStoreRequest
|
2017-12-17 18:23:10 +01:00
|
|
|
*/
|
2019-06-23 05:53:01 +02:00
|
|
|
public function testSubmit(): void
|
2017-12-17 18:23:10 +01:00
|
|
|
{
|
2019-06-23 05:53:01 +02:00
|
|
|
$repository = $this->mock(AccountRepositoryInterface::class);
|
2018-12-12 20:30:25 +01:00
|
|
|
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
|
2019-06-23 11:13:36 +02:00
|
|
|
$journalRepos = $this->mockDefaultSession();
|
2019-06-23 05:53:01 +02:00
|
|
|
$asset = $this->getRandomAsset();
|
|
|
|
$euro = $this->getEuro();
|
2018-12-12 20:30:25 +01:00
|
|
|
$date = new Carbon;
|
2019-06-23 05:53:01 +02:00
|
|
|
$factory = $this->mock(TransactionGroupFactory::class);
|
|
|
|
$group = $this->getRandomWithdrawalGroup();
|
|
|
|
$this->mock(CurrencyRepositoryInterface::class);
|
|
|
|
$this->mock(GroupCollectorInterface::class);
|
2019-04-09 15:32:48 +02:00
|
|
|
|
2019-06-23 05:53:01 +02:00
|
|
|
Preferences::shouldReceive('mark')->atLeast()->once();
|
2019-04-09 15:32:48 +02:00
|
|
|
|
2019-06-23 11:13:36 +02:00
|
|
|
|
2018-12-12 20:30:25 +01:00
|
|
|
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
|
|
|
|
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
|
2019-06-23 05:53:01 +02:00
|
|
|
$journalRepos->shouldReceive('reconcileById')->times(3);
|
|
|
|
$repository->shouldReceive('getReconciliation')->atLeast()->once()->andReturn($asset);
|
|
|
|
$repository->shouldReceive('getAccountCurrency')->atLeast()->once()->andReturn($euro);
|
|
|
|
$factory->shouldReceive('setUser')->atLeast()->once();
|
|
|
|
$factory->shouldReceive('create')->andReturn($group);
|
2018-03-28 19:38:20 +02:00
|
|
|
|
2019-06-23 11:13:36 +02:00
|
|
|
|
2017-12-17 18:23:10 +01:00
|
|
|
$data = [
|
2019-06-23 05:53:01 +02:00
|
|
|
'journals' => [1, 2, 3],
|
|
|
|
'reconcile' => 'create',
|
|
|
|
'difference' => '5',
|
|
|
|
'start' => '20170101',
|
|
|
|
'end' => '20170131',
|
2017-12-17 18:23:10 +01:00
|
|
|
];
|
|
|
|
$this->be($this->user());
|
2019-06-23 05:53:01 +02:00
|
|
|
$response = $this->post(route('accounts.reconcile.submit', [$asset->id, '20170101', '20170131']), $data);
|
2017-12-17 18:23:10 +01:00
|
|
|
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertSessionHas('success');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-23 05:53:01 +02:00
|
|
|
* Submit reconciliation, but throw an error.
|
|
|
|
*
|
2018-07-01 09:27:22 +02:00
|
|
|
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
|
2019-06-23 05:53:01 +02:00
|
|
|
* @covers \FireflyIII\Http\Requests\ReconciliationStoreRequest
|
2017-12-17 18:23:10 +01:00
|
|
|
*/
|
2019-06-23 05:53:01 +02:00
|
|
|
public function testSubmitError(): void
|
2017-12-17 18:23:10 +01:00
|
|
|
{
|
2019-06-23 05:53:01 +02:00
|
|
|
$repository = $this->mock(AccountRepositoryInterface::class);
|
|
|
|
$fiscalHelper = $this->mock(FiscalHelperInterface::class);
|
2019-06-23 11:13:36 +02:00
|
|
|
$journalRepos = $this->mockDefaultSession();
|
2019-06-23 05:53:01 +02:00
|
|
|
$asset = $this->getRandomAsset();
|
|
|
|
$euro = $this->getEuro();
|
|
|
|
$date = new Carbon;
|
|
|
|
$factory = $this->mock(TransactionGroupFactory::class);
|
|
|
|
$this->mock(CurrencyRepositoryInterface::class);
|
|
|
|
$this->mock(GroupCollectorInterface::class);
|
2018-09-03 08:41:03 +02:00
|
|
|
|
2019-06-23 05:53:01 +02:00
|
|
|
Preferences::shouldReceive('mark')->atLeast()->once();
|
2018-09-03 18:52:46 +02:00
|
|
|
|
2019-06-23 05:53:01 +02:00
|
|
|
$fiscalHelper->shouldReceive('endOfFiscalYear')->atLeast()->once()->andReturn($date);
|
|
|
|
$fiscalHelper->shouldReceive('startOfFiscalYear')->atLeast()->once()->andReturn($date);
|
|
|
|
$journalRepos->shouldReceive('reconcileById')->times(3);
|
|
|
|
$repository->shouldReceive('getReconciliation')->atLeast()->once()->andReturn($asset);
|
|
|
|
$repository->shouldReceive('getAccountCurrency')->atLeast()->once()->andReturn($euro);
|
|
|
|
$factory->shouldReceive('setUser')->atLeast()->once();
|
|
|
|
$factory->shouldReceive('create')->andThrow(new FireflyException('Some error'));
|
2017-12-17 18:23:10 +01:00
|
|
|
|
2019-06-23 05:53:01 +02:00
|
|
|
$data = [
|
|
|
|
'journals' => [1, 2, 3],
|
|
|
|
'reconcile' => 'create',
|
|
|
|
'difference' => '5',
|
|
|
|
'start' => '20170101',
|
|
|
|
'end' => '20170131',
|
|
|
|
];
|
2017-12-17 18:23:10 +01:00
|
|
|
$this->be($this->user());
|
2019-06-23 05:53:01 +02:00
|
|
|
$response = $this->post(route('accounts.reconcile.submit', [$asset->id, '20170101', '20170131']), $data);
|
2019-04-09 15:32:48 +02:00
|
|
|
|
2017-12-17 18:23:10 +01:00
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertSessionHas('error');
|
|
|
|
}
|
2017-12-22 18:32:43 +01:00
|
|
|
}
|