Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Test coverage for routine and prerequisites.
James Coleff45c9410624
diff --git a/tests/Object/FakeApiContext.php b/tests/Object/FakeApiContext.phpnew file mode 100644index 0000000000..cf5c585e85--- /dev/null+++ b/tests/Object/FakeApiContext.php@@ -0,0 +1,38 @@+<?php+/**+ * FakeApiContext.php+ * Copyright (c) 2018 [email protected]+ *+ * 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']);+ }++}\ No newline at end of filediff --git a/tests/Unit/Import/Prerequisites/BunqPrerequisitesTest.php b/tests/Unit/Import/Prerequisites/BunqPrerequisitesTest.phpnew file mode 100644index 0000000000..49fa193fce--- /dev/null+++ b/tests/Unit/Import/Prerequisites/BunqPrerequisitesTest.php@@ -0,0 +1,218 @@+<?php+/**+ * BunqPrerequisitesTest.php+ * Copyright (c) 2018 [email protected]+ *+ * 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);+ }+}\ No newline at end of filediff --git a/tests/Unit/Import/Routine/BunqRoutineTest.php b/tests/Unit/Import/Routine/BunqRoutineTest.phpnew file mode 100644index 0000000000..e16021c001--- /dev/null+++ b/tests/Unit/Import/Routine/BunqRoutineTest.php@@ -0,0 +1,117 @@+<?php+/**+ * BunqRoutineTest.php+ * Copyright (c) 2018 [email protected]+ *+ * 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());+ }++ }++}\ No newline at end of file