. */ declare(strict_types=1); namespace Tests\Unit\Import\Routine; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Import\Routine\SpectreRoutine; use FireflyIII\Models\ImportJob; use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; use FireflyIII\Support\Import\Routine\Spectre\StageImportDataHandler; use FireflyIII\Support\Import\Routine\Spectre\StageAuthenticatedHandler; use FireflyIII\Support\Import\Routine\Spectre\StageNewHandler; use Mockery; use Tests\TestCase; /** * Class SpectreRoutineTest */ class SpectreRoutineTest extends TestCase { /** * @covers \FireflyIII\Import\Routine\SpectreRoutine */ public function testRunDoAuthenticate(): void { $job = new ImportJob; $job->user_id = $this->user()->id; $job->key = 'SRA' . random_int(1, 1000); $job->status = 'ready_to_run'; $job->stage = 'do-authenticate'; $job->provider = 'spectre'; $job->file_type = ''; $job->configuration = []; $job->save(); // mock handler and repository $repository = $this->mock(ImportJobRepositoryInterface::class); // mock calls for repository $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'need_job_config'])->once(); $routine = new SpectreRoutine; $routine->setImportJob($job); try { $routine->run(); } catch (FireflyException $e) { $this->assertTrue(false, $e->getMessage()); } } /** * @covers \FireflyIII\Import\Routine\SpectreRoutine */ public function testRunAuthenticated(): void { $job = new ImportJob; $job->user_id = $this->user()->id; $job->key = 'SRA' . random_int(1, 1000); $job->status = 'ready_to_run'; $job->stage = 'authenticated'; $job->provider = 'spectre'; $job->file_type = ''; $job->configuration = []; $job->save(); // mock handler and repository $handler = $this->mock(StageAuthenticatedHandler::class); $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(), 'need_job_config'])->once(); $repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'choose-accounts'])->once(); // mock calls for handler $handler->shouldReceive('setImportJob')->once(); $handler->shouldReceive('run')->once(); $routine = new SpectreRoutine; $routine->setImportJob($job); try { $routine->run(); } catch (FireflyException $e) { $this->assertTrue(false, $e->getMessage()); } } /** * @covers \FireflyIII\Import\Routine\SpectreRoutine */ public function testRunGoImport(): void { $job = new ImportJob; $job->user_id = $this->user()->id; $job->key = 'SRA' . random_int(1, 1000); $job->status = 'ready_to_run'; $job->stage = 'go-for-import'; $job->provider = 'spectre'; $job->file_type = ''; $job->configuration = []; $job->save(); // mock handler and repository $handler = $this->mock(StageImportDataHandler::class); $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(), 'provider_finished'])->once(); $repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'do_import'])->once(); $repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'final'])->once(); // mock calls for handler $handler->shouldReceive('setImportJob')->once(); $handler->shouldReceive('run')->once(); $routine = new SpectreRoutine; $routine->setImportJob($job); try { $routine->run(); } catch (FireflyException $e) { $this->assertTrue(false, $e->getMessage()); } } /** * @covers \FireflyIII\Import\Routine\SpectreRoutine */ public function testRunNewOneLogin(): void { $job = new ImportJob; $job->user_id = $this->user()->id; $job->key = 'SRA' . random_int(1, 1000); $job->status = 'ready_to_run'; $job->stage = 'new'; $job->provider = 'spectre'; $job->file_type = ''; $job->configuration = []; $job->save(); // mock handler and repository $handler = $this->mock(StageNewHandler::class); $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(), 'need_job_config'])->once(); $repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'choose-login'])->once(); // mock calls for handler $handler->shouldReceive('setImportJob')->once(); $handler->shouldReceive('getCountLogins')->once()->andReturn(2); $handler->shouldReceive('run')->once(); $routine = new SpectreRoutine; $routine->setImportJob($job); try { $routine->run(); } catch (FireflyException $e) { $this->assertTrue(false, $e->getMessage()); } } /** * @covers \FireflyIII\Import\Routine\SpectreRoutine */ public function testRunNewZeroLogins(): void { $job = new ImportJob; $job->user_id = $this->user()->id; $job->key = 'SRA' . random_int(1, 1000); $job->status = 'ready_to_run'; $job->stage = 'new'; $job->provider = 'spectre'; $job->file_type = ''; $job->configuration = []; $job->save(); // mock handler and repository $handler = $this->mock(StageNewHandler::class); $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(); $repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'do-authenticate'])->once(); // mock calls for handler $handler->shouldReceive('setImportJob')->once(); $handler->shouldReceive('getCountLogins')->once()->andReturn(0); $handler->shouldReceive('run')->once(); $routine = new SpectreRoutine; $routine->setImportJob($job); try { $routine->run(); } catch (FireflyException $e) { $this->assertTrue(false, $e->getMessage()); } } }