Expand test code for create export routine.

This commit is contained in:
James Cole 2018-03-30 16:44:33 +02:00
parent 15f8cd49d3
commit 5c4d7734ac
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
5 changed files with 206 additions and 18 deletions

View File

@ -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;
}
}

View File

@ -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));

View File

@ -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.
*

View File

@ -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
*

View File

@ -0,0 +1,184 @@
<?php
/**
* CreateExportTest.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\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);
}
}