Skip to content

Firefly III: a personal finances manager

Expand test code for create export routine.

James Cole5c4d7734ac82
diff --git a/app/Console/Commands/CreateExport.php b/app/Console/Commands/CreateExport.php
index 2a8076e883..d83520a2ad 100644
--- a/app/Console/Commands/CreateExport.php
+++ b/app/Console/Commands/CreateExport.php
@@ -60,17 +60,6 @@ class CreateExport extends Command
{--with_uploads : Include user\'s uploads?}';
/**
- * Create a new command instance.
- */
- public function __construct()
- {
- parent::__construct();
- }
-
- /**
- * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's five its fine.
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- *
* Execute the console command.
*
* @return mixed
@@ -80,7 +69,7 @@ class CreateExport extends Command
if (!$this->verifyAccessToken()) {
$this->error('Invalid access token.');
- return;
+ return 1;
}
$this->line('Full export is running...');
// make repositories
@@ -94,7 +83,7 @@ class CreateExport extends Command
$journalRepository = app(JournalRepositoryInterface::class);
// set user
- $user = $userRepository->find(intval($this->option('user')));
+ $user = $userRepository->findNull((int)$this->option('user'));
$jobRepository->setUser($user);
$journalRepository->setUser($user);
$accountRepository->setUser($user);
@@ -140,7 +129,5 @@ class CreateExport extends Command
$this->line('The export has finished! You can find the ZIP file in this location:');
$this->line(storage_path(sprintf('export/%s', $fileName)));
-
- return;
}
}
diff --git a/app/Console/Commands/VerifiesAccessToken.php b/app/Console/Commands/VerifiesAccessToken.php
index 6871ab82d4..da3225a045 100644
--- a/app/Console/Commands/VerifiesAccessToken.php
+++ b/app/Console/Commands/VerifiesAccessToken.php
@@ -50,10 +50,10 @@ trait VerifiesAccessToken
protected function verifyAccessToken(): bool
{
$userId = (int)$this->option('user');
- $token = (int)$this->option('token');
+ $token = (string)$this->option('token');
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
- $user = $repository->find($userId);
+ $user = $repository->findNull($userId);
if (null === $user) {
Log::error(sprintf('verifyAccessToken(): no such user for input "%d"', $userId));
diff --git a/app/Repositories/User/UserRepository.php b/app/Repositories/User/UserRepository.php
index 4f664f4570..4a3d865606 100644
--- a/app/Repositories/User/UserRepository.php
+++ b/app/Repositories/User/UserRepository.php
@@ -158,6 +158,7 @@ class UserRepository implements UserRepositoryInterface
/**
* @param int $userId
*
+ * @deprecated
* @return User
*/
public function find(int $userId): User
@@ -180,6 +181,16 @@ class UserRepository implements UserRepositoryInterface
return User::where('email', $email)->first();
}
+ /**
+ * @param int $userId
+ *
+ * @return User|null
+ */
+ public function findNull(int $userId): ?User
+ {
+ return User::find($userId);
+ }
+
/**
* Returns the first user in the DB. Generally only works when there is just one.
*
diff --git a/app/Repositories/User/UserRepositoryInterface.php b/app/Repositories/User/UserRepositoryInterface.php
index 46d261d006..3582dc509e 100644
--- a/app/Repositories/User/UserRepositoryInterface.php
+++ b/app/Repositories/User/UserRepositoryInterface.php
@@ -103,11 +103,17 @@ interface UserRepositoryInterface
/**
* @param int $userId
- *
+ * @deprecated
* @return User
*/
public function find(int $userId): User;
+ /**
+ * @param int $userId
+ * @return User|null
+ */
+ public function findNull(int $userId): ?User;
+
/**
* @param string $email
*
diff --git a/tests/Unit/Console/Commands/CreateExportTest.php b/tests/Unit/Console/Commands/CreateExportTest.php
new file mode 100644
index 0000000000..bc07ceb780
--- /dev/null
+++ b/tests/Unit/Console/Commands/CreateExportTest.php
@@ -0,0 +1,184 @@
+<?php
+/**
+ * CreateExportTest.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\Console\Commands;
+
+use FireflyIII\Export\ProcessorInterface;
+use FireflyIII\Models\ExportJob;
+use FireflyIII\Models\Preference;
+use FireflyIII\Repositories\Account\AccountRepositoryInterface;
+use FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface;
+use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
+use FireflyIII\Repositories\User\UserRepositoryInterface;
+use Illuminate\Support\Collection;
+use Mockery;
+use Preferences;
+use Storage;
+use Tests\TestCase;
+
+/**
+ * Class CreateExportTest
+ */
+class CreateExportTest extends TestCase
+{
+
+ /**
+ * @covers \FireflyIII\Console\Commands\CreateExport
+ * @covers \FireflyIII\Console\Commands\VerifiesAccessToken
+ */
+ public function testBasic()
+ {
+ $journal = $this->user()->transactionJournals()->inRandomOrder()->first();
+ $preference = new Preference;
+ $preference->data = 'token';
+ $job = new ExportJob;
+ $job->key = 'export-' . random_int(1, 1000);
+ $journalRepository = $this->mock(JournalRepositoryInterface::class);
+ $userRepository = $this->mock(UserRepositoryInterface::class);
+ $jobRepository = $this->mock(ExportJobRepositoryInterface::class);
+ $accountRepository = $this->mock(AccountRepositoryInterface::class);
+ $processor = $this->mock(ProcessorInterface::class);
+
+ $journalRepository->shouldReceive('setUser')->once();
+ $journalRepository->shouldReceive('first')->once()->andReturn($journal);
+
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'access_token', null])->andReturn($preference)->once();
+
+ $accountRepository->shouldReceive('setUser')->once();
+ $accountRepository->shouldReceive('getAccountsByType')->andReturn(new Collection)->once();
+
+ $jobRepository->shouldReceive('setUser')->once();
+ $jobRepository->shouldReceive('create')->once()->andReturn($job);
+
+ $userRepository->shouldReceive('findNull')->andReturn($this->user())->twice();
+
+ $processor->shouldReceive('setSettings')->once();
+ $processor->shouldReceive('collectJournals')->once();
+ $processor->shouldReceive('exportJournals')->once();
+ $processor->shouldReceive('convertJournals')->once();
+ $processor->shouldReceive('collectAttachments')->once();
+ $processor->shouldReceive('collectOldUploads')->once();
+ $processor->shouldReceive('createZipFile')->once();
+
+ $fakeNews = 'I am a zipfile';
+ Storage::disk('export')->put(sprintf('%s.zip', $job->key), $fakeNews);
+
+ $output = $this->artisan(
+ 'firefly:create-export',
+ [
+ '--user' => 1,
+ '--token' => 'token',
+ '--with_attachments' => true,
+ '--with_uploads' => true,
+
+ ]
+ );
+
+ Storage::disk('export')->delete(sprintf('%s.zip', $job->key));
+ $this->assertEquals(0, $output);
+ }
+
+ /**
+ * @covers \FireflyIII\Console\Commands\CreateExport
+ * @covers \FireflyIII\Console\Commands\VerifiesAccessToken
+ */
+ public function testInvalidToken()
+ {
+ $preference = new Preference;
+ $preference->data = 'token';
+ $job = new ExportJob;
+ $job->key = 'export-' . random_int(1, 1000);
+ $userRepository = $this->mock(UserRepositoryInterface::class);
+
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'access_token', null])->andReturn($preference)->once();
+ $userRepository->shouldReceive('findNull')->andReturn($this->user())->once();
+
+ $output = $this->artisan(
+ 'firefly:create-export',
+ [
+ '--user' => 1,
+ '--token' => 'wrong_token',
+ '--with_attachments' => true,
+ '--with_uploads' => true,
+
+ ]
+ );
+ $this->assertEquals(1, $output);
+ }
+
+ /**
+ * @covers \FireflyIII\Console\Commands\CreateExport
+ * @covers \FireflyIII\Console\Commands\VerifiesAccessToken
+ */
+ public function testNoSuchUser()
+ {
+ $preference = new Preference;
+ $preference->data = 'token';
+ $job = new ExportJob;
+ $job->key = 'export-' . random_int(1, 1000);
+ $userRepository = $this->mock(UserRepositoryInterface::class);
+
+ $userRepository->shouldReceive('findNull')->andReturn(null)->once();
+
+ $output = $this->artisan(
+ 'firefly:create-export',
+ [
+ '--user' => 0,
+ '--token' => 'wrong_token',
+ '--with_attachments' => true,
+ '--with_uploads' => true,
+
+ ]
+ );
+ $this->assertEquals(1, $output);
+ }
+
+ /**
+ * @covers \FireflyIII\Console\Commands\CreateExport
+ * @covers \FireflyIII\Console\Commands\VerifiesAccessToken
+ */
+ public function testNoToken()
+ {
+ $preference = new Preference;
+ $preference->data = 'token';
+ $job = new ExportJob;
+ $job->key = 'export-' . random_int(1, 1000);
+ $userRepository = $this->mock(UserRepositoryInterface::class);
+
+ Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'access_token', null])->andReturn(null)->once();
+ $userRepository->shouldReceive('findNull')->andReturn($this->user())->once();
+
+ $output = $this->artisan(
+ 'firefly:create-export',
+ [
+ '--user' => 1,
+ '--token' => 'wrong_token',
+ '--with_attachments' => true,
+ '--with_uploads' => true,
+
+ ]
+ );
+ $this->assertEquals(1, $output);
+ }
+
+}
\ No newline at end of file