2018-09-30 11:57:51 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* YnabRoutineTest.php
|
2019-10-02 06:45:03 +02:00
|
|
|
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
2018-09-30 11:57:51 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-09-30 11:57:51 +02: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.
|
2018-09-30 11:57:51 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-09-30 11:57:51 +02: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.
|
2018-09-30 11:57:51 +02: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/>.
|
2018-09-30 11:57:51 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace tests\Unit\Import\Routine;
|
|
|
|
|
|
|
|
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Import\Routine\YnabRoutine;
|
|
|
|
use FireflyIII\Models\ImportJob;
|
|
|
|
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
|
|
|
use FireflyIII\Support\Import\Routine\Ynab\GetAccountsHandler;
|
|
|
|
use FireflyIII\Support\Import\Routine\Ynab\ImportDataHandler;
|
|
|
|
use FireflyIII\Support\Import\Routine\Ynab\StageGetAccessHandler;
|
|
|
|
use FireflyIII\Support\Import\Routine\Ynab\StageGetBudgetsHandler;
|
|
|
|
use Log;
|
|
|
|
use Mockery;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class YnabRoutineTest
|
2019-08-17 10:48:28 +02:00
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
|
2018-09-30 11:57:51 +02:00
|
|
|
*/
|
|
|
|
class YnabRoutineTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2019-04-09 20:05:20 +02:00
|
|
|
Log::info(sprintf('Now in %s.', get_class($this)));
|
2018-09-30 11:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Import\Routine\YnabRoutine
|
|
|
|
*/
|
2019-04-09 15:32:48 +02:00
|
|
|
public function testRunBadStatus(): void
|
2018-09-30 11:57:51 +02:00
|
|
|
{
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2019-07-26 17:48:24 +02:00
|
|
|
$job->key = 'ynab_r_7_' . $this->randomInt();
|
2019-04-09 15:32:48 +02:00
|
|
|
$job->status = 'not_ready_to_run';
|
|
|
|
$job->stage = 'bad_state';
|
2018-09-30 11:57:51 +02:00
|
|
|
$job->provider = 'ynab';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->configuration = [];
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock handler and repository
|
2019-04-09 15:32:48 +02:00
|
|
|
$handler = $this->mock(ImportDataHandler::class);
|
2018-09-30 11:57:51 +02:00
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
|
|
|
|
// mock calls for repository
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
|
|
|
|
$routine = new YnabRoutine;
|
|
|
|
$routine->setImportJob($job);
|
|
|
|
try {
|
|
|
|
$routine->run();
|
|
|
|
} catch (FireflyException $e) {
|
2019-04-09 15:32:48 +02:00
|
|
|
$this->assertEquals('YNAB import routine cannot handle stage "bad_state"', $e->getMessage());
|
2018-09-30 11:57:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Import\Routine\YnabRoutine
|
|
|
|
*/
|
2019-04-09 15:32:48 +02:00
|
|
|
public function testRunException(): void
|
2018-09-30 11:57:51 +02:00
|
|
|
{
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2019-07-26 17:48:24 +02:00
|
|
|
$job->key = 'ynab_r_6_' . $this->randomInt();
|
2018-09-30 11:57:51 +02:00
|
|
|
$job->status = 'ready_to_run';
|
2019-04-09 15:32:48 +02:00
|
|
|
$job->stage = 'bad_state';
|
2018-09-30 11:57:51 +02:00
|
|
|
$job->provider = 'ynab';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->configuration = [];
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock handler and repository
|
2019-04-09 15:32:48 +02:00
|
|
|
$handler = $this->mock(ImportDataHandler::class);
|
2018-09-30 11:57:51 +02:00
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
|
|
|
|
// mock calls for repository
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
|
|
|
|
$routine = new YnabRoutine;
|
|
|
|
$routine->setImportJob($job);
|
|
|
|
try {
|
|
|
|
$routine->run();
|
|
|
|
} catch (FireflyException $e) {
|
2019-04-09 15:32:48 +02:00
|
|
|
$this->assertEquals('YNAB import routine cannot handle stage "bad_state"', $e->getMessage());
|
2018-09-30 11:57:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Import\Routine\YnabRoutine
|
|
|
|
*/
|
2019-04-09 15:32:48 +02:00
|
|
|
public function testRunGetAccessToken(): void
|
2018-09-30 11:57:51 +02:00
|
|
|
{
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2019-07-26 17:48:24 +02:00
|
|
|
$job->key = 'ynab_r_1_' . $this->randomInt();
|
2018-09-30 11:57:51 +02:00
|
|
|
$job->status = 'ready_to_run';
|
2019-04-09 15:32:48 +02:00
|
|
|
$job->stage = 'get_access_token';
|
2018-09-30 11:57:51 +02:00
|
|
|
$job->provider = 'ynab';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->configuration = [];
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock handler and repository
|
2019-04-09 15:32:48 +02:00
|
|
|
$handler = $this->mock(StageGetAccessHandler::class);
|
2018-09-30 11:57:51 +02:00
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
|
|
|
|
// mock calls for repository
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'running'])->once();
|
|
|
|
|
|
|
|
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'ready_to_run'])->once();
|
2019-04-09 15:32:48 +02:00
|
|
|
$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'get_budgets'])->once();
|
2018-09-30 11:57:51 +02:00
|
|
|
|
|
|
|
// mock calls for handler
|
|
|
|
$handler->shouldReceive('setImportJob')->once();
|
|
|
|
$handler->shouldReceive('run')->once();
|
|
|
|
|
|
|
|
$routine = new YnabRoutine;
|
|
|
|
$routine->setImportJob($job);
|
|
|
|
try {
|
|
|
|
$routine->run();
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
$this->assertTrue(false, $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Import\Routine\YnabRoutine
|
|
|
|
*/
|
|
|
|
public function testRunGetAccounts(): void
|
|
|
|
{
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2019-07-26 17:48:24 +02:00
|
|
|
$job->key = 'ynab_r_4_' . $this->randomInt();
|
2018-09-30 11:57:51 +02:00
|
|
|
$job->status = 'ready_to_run';
|
|
|
|
$job->stage = 'get_accounts';
|
|
|
|
$job->provider = 'ynab';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->configuration = [];
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock handler and repository
|
|
|
|
$handler = $this->mock(GetAccountsHandler::class);
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
|
|
|
|
// mock calls for repository
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'running'])->once();
|
|
|
|
|
|
|
|
$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'select_accounts'])->once();
|
|
|
|
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'need_job_config'])->once();
|
|
|
|
|
|
|
|
|
|
|
|
// mock calls for handler
|
|
|
|
$handler->shouldReceive('setImportJob')->once();
|
|
|
|
$handler->shouldReceive('run')->once();
|
|
|
|
|
|
|
|
$routine = new YnabRoutine;
|
|
|
|
$routine->setImportJob($job);
|
|
|
|
try {
|
|
|
|
$routine->run();
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
$this->assertTrue(false, $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Import\Routine\YnabRoutine
|
|
|
|
*/
|
|
|
|
public function testRunGoForImport(): void
|
|
|
|
{
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2019-07-26 17:48:24 +02:00
|
|
|
$job->key = 'ynab_r_5_' . $this->randomInt();
|
2018-09-30 11:57:51 +02:00
|
|
|
$job->status = 'ready_to_run';
|
|
|
|
$job->stage = 'go-for-import';
|
|
|
|
$job->provider = 'ynab';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->configuration = [];
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock handler and repository
|
|
|
|
$handler = $this->mock(ImportDataHandler::class);
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
|
|
|
|
// mock calls for repository
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'running'])->once();
|
|
|
|
$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'do_import'])->once();
|
|
|
|
|
|
|
|
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'provider_finished'])->once();
|
|
|
|
$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'final'])->once();
|
|
|
|
|
|
|
|
|
|
|
|
// mock calls for handler
|
|
|
|
$handler->shouldReceive('setImportJob')->once();
|
|
|
|
$handler->shouldReceive('run')->once();
|
|
|
|
|
|
|
|
$routine = new YnabRoutine;
|
|
|
|
$routine->setImportJob($job);
|
|
|
|
try {
|
|
|
|
$routine->run();
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
$this->assertTrue(false, $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Import\Routine\YnabRoutine
|
|
|
|
*/
|
2019-04-09 15:32:48 +02:00
|
|
|
public function testRunMultiBudgets(): void
|
2018-09-30 11:57:51 +02:00
|
|
|
{
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2019-07-26 17:48:24 +02:00
|
|
|
$job->key = 'ynab_r_2_' . $this->randomInt();
|
2018-09-30 11:57:51 +02:00
|
|
|
$job->status = 'ready_to_run';
|
2019-04-09 15:32:48 +02:00
|
|
|
$job->stage = 'get_budgets';
|
2018-09-30 11:57:51 +02:00
|
|
|
$job->provider = 'ynab';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->configuration = [];
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock handler and repository
|
2019-04-09 15:32:48 +02:00
|
|
|
$handler = $this->mock(StageGetBudgetsHandler::class);
|
2018-09-30 11:57:51 +02:00
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
|
2019-04-09 15:32:48 +02:00
|
|
|
$config = ['budgets' => [1, 2, 3]];
|
|
|
|
|
2018-09-30 11:57:51 +02:00
|
|
|
// mock calls for repository
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
2019-04-09 15:32:48 +02:00
|
|
|
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'running'])->once();
|
|
|
|
$repository->shouldReceive('getConfiguration')->once()->andReturn($config);
|
|
|
|
|
|
|
|
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'need_job_config'])->once();
|
|
|
|
$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'select_budgets'])->once();
|
|
|
|
|
|
|
|
// mock calls for handler
|
|
|
|
$handler->shouldReceive('setImportJob')->once();
|
|
|
|
$handler->shouldReceive('run')->once();
|
2018-09-30 11:57:51 +02:00
|
|
|
|
|
|
|
$routine = new YnabRoutine;
|
|
|
|
$routine->setImportJob($job);
|
|
|
|
try {
|
|
|
|
$routine->run();
|
|
|
|
} catch (FireflyException $e) {
|
2019-04-09 15:32:48 +02:00
|
|
|
$this->assertTrue(false, $e->getMessage());
|
2018-09-30 11:57:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Import\Routine\YnabRoutine
|
|
|
|
*/
|
2019-04-09 15:32:48 +02:00
|
|
|
public function testRunSingleBudget(): void
|
2018-09-30 11:57:51 +02:00
|
|
|
{
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2019-07-26 17:48:24 +02:00
|
|
|
$job->key = 'ynab_r_3_' . $this->randomInt();
|
2019-04-09 15:32:48 +02:00
|
|
|
$job->status = 'ready_to_run';
|
|
|
|
$job->stage = 'get_budgets';
|
2018-09-30 11:57:51 +02:00
|
|
|
$job->provider = 'ynab';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->configuration = [];
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock handler and repository
|
2019-04-09 15:32:48 +02:00
|
|
|
$handler = $this->mock(StageGetBudgetsHandler::class);
|
2018-09-30 11:57:51 +02:00
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
|
2019-04-09 15:32:48 +02:00
|
|
|
$config = ['budgets' => [1]];
|
|
|
|
|
2018-09-30 11:57:51 +02:00
|
|
|
// mock calls for repository
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
2019-04-09 15:32:48 +02:00
|
|
|
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'running'])->once();
|
|
|
|
$repository->shouldReceive('getConfiguration')->once()->andReturn($config);
|
|
|
|
|
|
|
|
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'ready_to_run'])->once();
|
|
|
|
$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'get_accounts'])->once();
|
|
|
|
|
|
|
|
// mock calls for handler
|
|
|
|
$handler->shouldReceive('setImportJob')->once();
|
|
|
|
$handler->shouldReceive('run')->once();
|
2018-09-30 11:57:51 +02:00
|
|
|
|
|
|
|
$routine = new YnabRoutine;
|
|
|
|
$routine->setImportJob($job);
|
|
|
|
try {
|
|
|
|
$routine->run();
|
|
|
|
} catch (FireflyException $e) {
|
2019-04-09 15:32:48 +02:00
|
|
|
$this->assertTrue(false, $e->getMessage());
|
2018-09-30 11:57:51 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-31 07:48:23 +01:00
|
|
|
}
|