mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 02:26:58 +00:00
Test coverage for routine and prerequisites.
This commit is contained in:
38
tests/Object/FakeApiContext.php
Normal file
38
tests/Object/FakeApiContext.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* FakeApiContext.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\Object;
|
||||
|
||||
/**
|
||||
* Class FakeApiContext
|
||||
*/
|
||||
class FakeApiContext
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toJson(): string{
|
||||
return json_encode(['a' => 'b']);
|
||||
}
|
||||
|
||||
}
|
218
tests/Unit/Import/Prerequisites/BunqPrerequisitesTest.php
Normal file
218
tests/Unit/Import/Prerequisites/BunqPrerequisitesTest.php
Normal file
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
/**
|
||||
* BunqPrerequisitesTest.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\Import\Prerequisites;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Import\Prerequisites\BunqPrerequisites;
|
||||
use FireflyIII\Models\Preference;
|
||||
use FireflyIII\Services\Bunq\ApiContext;
|
||||
use FireflyIII\Services\IP\IPRetrievalInterface;
|
||||
use Mockery;
|
||||
use Preferences;
|
||||
use Tests\Object\FakeApiContext;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class BunqPrerequisitesTest
|
||||
*/
|
||||
class BunqPrerequisitesTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Has no API key, has no external IP.
|
||||
*
|
||||
* @covers \FireflyIII\Import\Prerequisites\BunqPrerequisites
|
||||
*/
|
||||
public function testGetViewParameters(): void
|
||||
{
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_api_key', null])->andReturnNull()->once();
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_external_ip', null])->andReturnNull()->twice();
|
||||
|
||||
$service = $this->mock(IPRetrievalInterface::class);
|
||||
$service->shouldReceive('getIP')->once()->andReturn('10.0.0.15');
|
||||
|
||||
$object = new BunqPrerequisites;
|
||||
$object->setUser($this->user());
|
||||
$parameters = $object->getViewParameters();
|
||||
$this->assertEquals(['api_key' => '', 'external_ip' => '10.0.0.15'], $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Has empty API key, has empty external IP.
|
||||
*
|
||||
* @covers \FireflyIII\Import\Prerequisites\BunqPrerequisites
|
||||
*/
|
||||
public function testGetViewParametersEmpty(): void
|
||||
{
|
||||
$pref = new Preference;
|
||||
$pref->name = 'dontmatter';
|
||||
$pref->data = '';
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_api_key', null])->andReturn($pref)->once();
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_external_ip', null])->andReturn($pref)->twice();
|
||||
|
||||
$service = $this->mock(IPRetrievalInterface::class);
|
||||
$service->shouldReceive('getIP')->once()->andReturn('10.0.0.15');
|
||||
|
||||
$object = new BunqPrerequisites;
|
||||
$object->setUser($this->user());
|
||||
$parameters = $object->getViewParameters();
|
||||
$this->assertEquals(['api_key' => '', 'external_ip' => '10.0.0.15'], $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Has API key, has external IP.
|
||||
*
|
||||
* @covers \FireflyIII\Import\Prerequisites\BunqPrerequisites
|
||||
*/
|
||||
public function testGetViewParametersFilled(): void
|
||||
{
|
||||
$pref = new Preference;
|
||||
$pref->name = 'dontmatter';
|
||||
$pref->data = 'data';
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_api_key', null])->andReturn($pref)->times(2);
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_external_ip', null])->andReturn($pref)->times(3);
|
||||
|
||||
$object = new BunqPrerequisites;
|
||||
$object->setUser($this->user());
|
||||
$parameters = $object->getViewParameters();
|
||||
$this->assertEquals(['api_key' => 'data', 'external_ip' => 'data'], $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* API context empty
|
||||
*
|
||||
* @covers \FireflyIII\Import\Prerequisites\BunqPrerequisites
|
||||
*/
|
||||
public function testIsCompleteEmpty(): void
|
||||
{
|
||||
$pref = new Preference;
|
||||
$pref->name = 'dontmatter';
|
||||
$pref->data = 'data';
|
||||
|
||||
$empty = new Preference;
|
||||
$empty->name = 'dontmatter';
|
||||
$empty->data = '';
|
||||
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_api_key', null])->andReturn($pref)->once();
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_external_ip', null])->andReturn($pref)->once();
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_api_context', null])->andReturn($empty)->once();
|
||||
$object = new BunqPrerequisites;
|
||||
$object->setUser($this->user());
|
||||
$this->assertFalse($object->isComplete());
|
||||
}
|
||||
|
||||
/**
|
||||
* API context filled
|
||||
*
|
||||
* @covers \FireflyIII\Import\Prerequisites\BunqPrerequisites
|
||||
*/
|
||||
public function testIsCompleteFilled(): void
|
||||
{
|
||||
$pref = new Preference;
|
||||
$pref->name = 'dontmatter';
|
||||
$pref->data = 'data';
|
||||
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_api_key', null])->andReturn($pref)->once();
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_external_ip', null])->andReturn($pref)->once();
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_api_context', null])->andReturn($pref)->once();
|
||||
$object = new BunqPrerequisites;
|
||||
$object->setUser($this->user());
|
||||
$this->assertTrue($object->isComplete());
|
||||
}
|
||||
|
||||
/**
|
||||
* API context null.
|
||||
*
|
||||
* @covers \FireflyIII\Import\Prerequisites\BunqPrerequisites
|
||||
*/
|
||||
public function testIsCompleteNull(): void
|
||||
{
|
||||
$pref = new Preference;
|
||||
$pref->name = 'dontmatter';
|
||||
$pref->data = 'data';
|
||||
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_api_key', null])->andReturn($pref)->once();
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_external_ip', null])->andReturn($pref)->once();
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'bunq_api_context', null])->andReturnNull()->once();
|
||||
$object = new BunqPrerequisites;
|
||||
$object->setUser($this->user());
|
||||
$this->assertFalse($object->isComplete());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test call to API.
|
||||
*
|
||||
* @covers \FireflyIII\Import\Prerequisites\BunqPrerequisites
|
||||
*/
|
||||
public function testStorePrerequisites(): void
|
||||
{
|
||||
$object = new BunqPrerequisites;
|
||||
$object->setUser($this->user());
|
||||
|
||||
$data = [
|
||||
'api_key' => 'Some API key',
|
||||
'external_ip' => '10.0.0.15',
|
||||
];
|
||||
|
||||
Preferences::shouldReceive('setForUser')->withArgs([Mockery::any(), 'bunq_api_key', $data['api_key']])->once();
|
||||
Preferences::shouldReceive('setForUser')->withArgs([Mockery::any(), 'bunq_external_ip', $data['external_ip']])->once();
|
||||
Preferences::shouldReceive('setForUser')->withArgs([Mockery::any(), 'bunq_api_context', '{"a":"b"}'])->once();
|
||||
|
||||
// create fake context
|
||||
$context = $this->mock(ApiContext::class);
|
||||
$context->shouldReceive('create')
|
||||
->withArgs([Mockery::any(), 'Some API key', 'Firefly III v' . config('firefly.version'), [$data['external_ip']]])
|
||||
->once()->andReturn(new FakeApiContext());
|
||||
$messages = $object->storePrerequisites($data);
|
||||
$this->assertEquals('', $messages->first());
|
||||
$this->assertCount(0, $messages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test call that throws error.
|
||||
*
|
||||
* @covers \FireflyIII\Import\Prerequisites\BunqPrerequisites
|
||||
*/
|
||||
public function testStorePrerequisitesExp(): void
|
||||
{
|
||||
$object = new BunqPrerequisites;
|
||||
$object->setUser($this->user());
|
||||
|
||||
$data = [
|
||||
'api_key' => 'Some API key',
|
||||
'external_ip' => '10.0.0.15',
|
||||
];
|
||||
|
||||
Preferences::shouldReceive('setForUser')->withArgs([Mockery::any(), 'bunq_api_key', $data['api_key']])->once();
|
||||
Preferences::shouldReceive('setForUser')->withArgs([Mockery::any(), 'bunq_external_ip', $data['external_ip']])->once();
|
||||
|
||||
// create fake context
|
||||
$context = $this->mock(ApiContext::class);
|
||||
$context->shouldReceive('create')
|
||||
->withArgs([Mockery::any(), 'Some API key', 'Firefly III v' . config('firefly.version'), [$data['external_ip']]])
|
||||
->once()->andThrow(new FireflyException('Some exception'));
|
||||
$messages = $object->storePrerequisites($data);
|
||||
$this->assertEquals('Some exception', $messages->first());
|
||||
$this->assertCount(1, $messages);
|
||||
}
|
||||
}
|
117
tests/Unit/Import/Routine/BunqRoutineTest.php
Normal file
117
tests/Unit/Import/Routine/BunqRoutineTest.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
/**
|
||||
* BunqRoutineTest.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\Import\Routine;
|
||||
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Import\Routine\BunqRoutine;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Support\Import\Routine\Bunq\StageImportDataHandler;
|
||||
use FireflyIII\Support\Import\Routine\Bunq\StageNewHandler;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class BunqRoutineTest
|
||||
*/
|
||||
class BunqRoutineTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \FireflyIII\Import\Routine\BunqRoutine
|
||||
*/
|
||||
public function testRunImport(): void
|
||||
{
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'br_' . random_int(1, 1000);
|
||||
$job->status = 'ready_to_run';
|
||||
$job->stage = 'go-for-import';
|
||||
$job->provider = 'bunq';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [];
|
||||
$job->save();
|
||||
|
||||
// mock stuff:
|
||||
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
||||
$handler = $this->mock(StageImportDataHandler::class);
|
||||
|
||||
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'running']);
|
||||
$handler->shouldReceive('setImportJob')->once();
|
||||
$handler->shouldReceive('run')->once();
|
||||
$handler->shouldReceive('getTransactions')->once()->andReturn(['a' => 'c']);
|
||||
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'provider_finished'])->once();
|
||||
$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'final'])->once();
|
||||
$repository->shouldReceive('setTransactions')->withArgs([Mockery::any(), ['a' => 'c']])->once();
|
||||
|
||||
$routine = new BunqRoutine;
|
||||
$routine->setImportJob($job);
|
||||
try {
|
||||
$routine->run();
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertFalse(true, $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Import\Routine\BunqRoutine
|
||||
*/
|
||||
public function testRunNew(): void
|
||||
{
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'br_' . random_int(1, 1000);
|
||||
$job->status = 'ready_to_run';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'bunq';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [];
|
||||
$job->save();
|
||||
|
||||
// mock stuff:
|
||||
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
||||
$handler = $this->mock(StageNewHandler::class);
|
||||
|
||||
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'running']);
|
||||
$handler->shouldReceive('setImportJob')->once();
|
||||
$handler->shouldReceive('run')->once();
|
||||
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'need_job_config'])->once();
|
||||
$repository->shouldReceive('setStage')->withArgs([Mockery::any(), 'choose-accounts'])->once();
|
||||
|
||||
$routine = new BunqRoutine;
|
||||
$routine->setImportJob($job);
|
||||
try {
|
||||
$routine->run();
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertFalse(true, $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user