. */ declare(strict_types=1); namespace Tests\Unit\Import\Routine; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Import\Routine\FakeRoutine; use FireflyIII\Models\ImportJob; use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; use FireflyIII\Support\Import\Routine\Fake\StageAhoyHandler; use FireflyIII\Support\Import\Routine\Fake\StageFinalHandler; use FireflyIII\Support\Import\Routine\Fake\StageNewHandler; use Log; use Mockery; use Tests\TestCase; /** * Class FakeRoutineTest * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.TooManyPublicMethods) */ class FakeRoutineTest extends TestCase { /** * */ public function setUp(): void { parent::setUp(); Log::info(sprintf('Now in %s.', get_class($this))); } /** * @covers \FireflyIII\Import\Routine\FakeRoutine */ public function testRunAhoy(): void { $job = new ImportJob; $job->user_id = $this->user()->id; $job->key = 'a_route_' . $this->randomInt(); $job->status = 'ready_to_run'; $job->stage = 'ahoy'; $job->provider = 'fake'; $job->file_type = ''; $job->configuration = []; $job->save(); // mock $handler = $this->mock(StageAhoyHandler::class); $repository = $this->mock(ImportJobRepositoryInterface::class); // calls $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(), 'final'])->once(); $handler->shouldReceive('run')->once(); $routine = new FakeRoutine; $routine->setImportJob($job); try { $routine->run(); } catch (FireflyException $e) { $this->assertTrue(false, $e->getMessage()); } } /** * @covers \FireflyIII\Import\Routine\FakeRoutine */ public function testRunFinal(): void { $job = new ImportJob; $job->user_id = $this->user()->id; $job->key = 'a_route_' . $this->randomInt(); $job->status = 'ready_to_run'; $job->stage = 'final'; $job->provider = 'fake'; $job->file_type = ''; $job->configuration = []; $job->save(); // mock $handler = $this->mock(StageFinalHandler::class); $repository = $this->mock(ImportJobRepositoryInterface::class); // calls $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'provider_finished'])->once(); $repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'final'])->once(); $repository->shouldReceive('setTransactions')->withArgs([Mockery::any(), []])->once(); $handler->shouldReceive('getTransactions')->once()->andReturn([]); $repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'running'])->once(); $handler->shouldReceive('setImportJob')->once(); $routine = new FakeRoutine; $routine->setImportJob($job); try { $routine->run(); } catch (FireflyException $e) { $this->assertTrue(false, $e->getMessage()); } } /** * @covers \FireflyIII\Import\Routine\FakeRoutine */ public function testRunNew(): void { $job = new ImportJob; $job->user_id = $this->user()->id; $job->key = 'a_route_' . $this->randomInt(); $job->status = 'ready_to_run'; $job->stage = 'new'; $job->provider = 'fake'; $job->file_type = ''; $job->configuration = []; $job->save(); // mock $handler = $this->mock(StageNewHandler::class); $repository = $this->mock(ImportJobRepositoryInterface::class); // calls $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'ahoy'])->once(); $repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'ready_to_run'])->once(); $repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'running'])->once(); $handler->shouldReceive('run')->once(); $routine = new FakeRoutine; $routine->setImportJob($job); try { $routine->run(); } catch (FireflyException $e) { $this->assertTrue(false, $e->getMessage()); } } }