Wrote export tests.

This commit is contained in:
James Cole 2016-12-11 18:34:18 +01:00
parent 358d83dcfc
commit a67f10c99e
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
10 changed files with 217 additions and 180 deletions

View File

@ -30,7 +30,7 @@ use ZipArchive;
*
* @package FireflyIII\Export
*/
class Processor
class Processor implements ProcessorInterface
{
/** @var Collection */

View File

@ -0,0 +1,67 @@
<?php
/**
* ProcessorInterface.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Export;
use Illuminate\Support\Collection;
/**
* Interface ProcessorInterface
*
* @package FireflyIII\Export
*/
interface ProcessorInterface
{
/**
* Processor constructor.
*
* @param array $settings
*/
public function __construct(array $settings);
/**
* @return bool
*/
public function collectAttachments(): bool;
/**
* @return bool
*/
public function collectJournals(): bool;
/**
* @return bool
*/
public function collectOldUploads(): bool;
/**
* @return bool
*/
public function convertJournals(): bool;
/**
* @return bool
*/
public function createZipFile(): bool;
/**
* @return bool
*/
public function exportJournals(): bool;
/**
* @return Collection
*/
public function getFiles(): Collection;
}

View File

@ -1,44 +0,0 @@
<?php
/**
* BillChartGeneratorInterface.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Generator\Chart\Bill;
use FireflyIII\Models\Bill;
use Illuminate\Support\Collection;
/**
* Interface BillChartGeneratorInterface
*
* @package FireflyIII\Generator\Chart\Bill
*/
interface BillChartGeneratorInterface
{
/**
* @param string $paid
* @param string $unpaid
*
* @return array
*/
public function frontpage(string $paid, string $unpaid): array;
/**
* @param Bill $bill
* @param Collection $entries
*
* @return array
*/
public function single(Bill $bill, Collection $entries): array;
}

View File

@ -1,94 +0,0 @@
<?php
/**
* ChartJsBillChartGenerator.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Generator\Chart\Bill;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Transaction;
use FireflyIII\Support\ChartColour;
use Illuminate\Support\Collection;
/**
* Class ChartJsBillChartGenerator
*
* @package FireflyIII\Generator\Chart\Bill
*/
class ChartJsBillChartGenerator implements BillChartGeneratorInterface
{
/**
* @param string $paid
* @param string $unpaid
*
* @return array
*/
public function frontpage(string $paid, string $unpaid): array
{
$data = [
'datasets' => [
[
'data' => [round($unpaid, 2), round(bcmul($paid, '-1'), 2)],
'backgroundColor' => [ChartColour::getColour(0), ChartColour::getColour(1)],
],
],
'labels' => [strval(trans('firefly.unpaid')), strval(trans('firefly.paid'))],
];
return $data;
}
/**
* @param Bill $bill
* @param Collection $entries
*
* @return array
*/
public function single(Bill $bill, Collection $entries): array
{
$format = (string)trans('config.month');
$data = ['count' => 3, 'labels' => [], 'datasets' => [],];
$minAmount = [];
$maxAmount = [];
$actualAmount = [];
/** @var Transaction $entry */
foreach ($entries as $entry) {
$data['labels'][] = $entry->date->formatLocalized($format);
$minAmount[] = round($bill->amount_min, 2);
$maxAmount[] = round($bill->amount_max, 2);
// journalAmount has been collected in BillRepository::getJournals
$actualAmount[] = bcmul($entry->transaction_amount, '-1');
}
$data['datasets'][] = [
'type' => 'bar',
'label' => trans('firefly.minAmount'),
'data' => $minAmount,
];
$data['datasets'][] = [
'type' => 'line',
'label' => trans('firefly.billEntry'),
'data' => $actualAmount,
];
$data['datasets'][] = [
'type' => 'bar',
'label' => trans('firefly.maxAmount'),
'data' => $maxAmount,
];
$data['count'] = count($data['datasets']);
return $data;
}
}

View File

@ -15,7 +15,6 @@ namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Bill;
@ -33,7 +32,7 @@ use Response;
class BillController extends Controller
{
/** @var \FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface */
/** @var GeneratorInterface */
protected $generator;
/**
@ -42,8 +41,7 @@ class BillController extends Controller
public function __construct()
{
parent::__construct();
// create chart generator:
$this->generator = app(BillChartGeneratorInterface::class);
$this->generator = app(GeneratorInterface::class);
}
/**
@ -72,9 +70,7 @@ class BillController extends Controller
strval(trans('firefly.paid')) => $paid,
];
/** @var GeneratorInterface $generator */
$generator = app(GeneratorInterface::class);
$data = $generator->pieChart($chartData);
$data = $this->generator->pieChart($chartData);
$cache->store($data);
return Response::json($data);
@ -131,9 +127,7 @@ class BillController extends Controller
$chartData[2]['entries'][$date] = bcmul($entry->transaction_amount, '-1');
}
/** @var GeneratorInterface $generator */
$generator = app(GeneratorInterface::class);
$data = $generator->multiSet($chartData);
$data = $this->generator->multiSet($chartData);
$cache->store($data);
return Response::json($data);

View File

@ -18,6 +18,7 @@ use Carbon\Carbon;
use ExpandedForm;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Export\Processor;
use FireflyIII\Export\ProcessorInterface;
use FireflyIII\Http\Requests\ExportFormRequest;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\ExportJob;
@ -71,7 +72,6 @@ class ExportController extends Controller
throw new FireflyException('Against all expectations, zip file "' . $file . '" does not exist.');
}
$job->change('export_downloaded');
return response($disk->get($file), 200)
@ -133,7 +133,6 @@ class ExportController extends Controller
*/
public function postIndex(ExportFormRequest $request, AccountRepositoryInterface $repository, EJRI $jobs)
{
set_time_limit(0);
$job = $jobs->findByKey($request->get('job'));
$settings = [
'accounts' => $repository->getAccountsById($request->get('accounts')),
@ -146,7 +145,9 @@ class ExportController extends Controller
];
$job->change('export_status_make_exporter');
$processor = new Processor($settings);
/** @var ProcessorInterface $processor */
$processor = app(ProcessorInterface::class, [$settings]);
/*
* Collect journals:

View File

@ -97,6 +97,7 @@ class FireflyServiceProvider extends ServiceProvider
$this->app->bind('FireflyIII\Generator\Chart\Basic\GeneratorInterface', 'FireflyIII\Generator\Chart\Basic\ChartJsGenerator');
// other generators
$this->app->bind('FireflyIII\Export\ProcessorInterface', 'FireflyIII\Export\Processor');
$this->app->bind('FireflyIII\Repositories\User\UserRepositoryInterface', 'FireflyIII\Repositories\User\UserRepository');
$this->app->bind('FireflyIII\Helpers\Attachments\AttachmentHelperInterface', 'FireflyIII\Helpers\Attachments\AttachmentHelper');
$this->app->bind('FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface', 'FireflyIII\Generator\Chart\Bill\ChartJsBillChartGenerator');

View File

@ -264,6 +264,29 @@ class TestData
DB::table('transaction_currencies')->insert($insert);
}
/**
*
*/
private function createExportJobs()
{
$insert = [];
$disk = Storage::disk('export');
foreach ($this->data['export-jobs'] as $job) {
$insert[] = [
'created_at' => $this->time,
'updated_at' => $this->time,
'user_id' => $job['user_id'],
'key' => $job['key'],
'status' => $job['status'],
];
$disk->put($job['key'] . '.zip', 'Nonsense data for "ziP" file.');
}
DB::table('export_jobs')->insert($insert);
// store fake export file:
}
/**
*
*/
@ -863,6 +886,7 @@ class TestData
$this->createMultiTransfers();
$this->createImportJobs();
$this->createCurrencies();
$this->createExportJobs();
}
}

View File

@ -995,6 +995,89 @@
]
}
],
"import-jobs": [],
"import-jobs": [
{
"user_id": 1,
"key": "testImport",
"file_type": "csv",
"status": "settings_complete",
"extended_status": {
"steps_done": 0,
"total_steps": 0,
"errors": [],
"import_count": 0,
"importTag": 0
},
"configuration": {
"has-headers": false,
"date-format": "Ymd",
"delimiter": ",",
"import-account": 1,
"specifics": {
"RabobankDescription": 1
},
"column-count": 19,
"column-roles": [
"account-iban",
"currency-code",
"date-interest",
"rabo-debet-credit",
"amount",
"opposing-iban",
"opposing-name",
"date-book",
"description",
"_ignore",
"description",
"description",
"description",
"description",
"description",
"description",
"sepa-ct-id",
"sepa-ct-op",
"sepa-db"
],
"column-do-mapping": [
true,
true,
false,
false,
false,
true,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false
],
"column-roles-complete": false,
"column-mapping-config": {
"0": [],
"1": {
"EUR": 1
},
"5": [],
"6": []
},
"column-mapping-complete": false
}
}
],
"export-jobs": [
{
"user_id": 1,
"key": "testExport",
"status": "unknown"
}
],
"currencies": []
}

View File

@ -8,6 +8,7 @@
*
* See the LICENSE file for details.
*/
use FireflyIII\Export\Processor;
/**
@ -28,57 +29,61 @@ class ExportControllerTest extends TestCase
/**
* @covers \FireflyIII\Http\Controllers\ExportController::download
* Implement testDownload().
*/
public function testDownload()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$this->be($this->user());
$this->call('GET', route('export.download', ['testExport']));
$this->assertResponseStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\ExportController::getStatus
* Implement testGetStatus().
*/
public function testGetStatus()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$this->be($this->user());
$this->call('GET', route('export.status', ['testExport']));
$this->assertResponseStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\ExportController::index
* Implement testIndex().
*/
public function testIndex()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$this->be($this->user());
$this->call('GET', route('export.index'));
$this->assertResponseStatus(200);
// has bread crumb
$this->see('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\ExportController::postIndex
* Implement testPostIndex().
*/
public function testPostIndex()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$data = [
'export_start_range' => '2015-01-01',
'export_end_range' => '2015-01-21',
'exportFormat' => 'csv',
'accounts' => [1],
'job' => 'testExport',
];
$processor = $this->mock(Processor::class);
$processor->shouldReceive('collectJournals')->once();
$processor->shouldReceive('convertJournals')->once();
$processor->shouldReceive('exportJournals')->once();
$processor->shouldReceive('createZipFile')->once();
$this->be($this->user());
$this->call('post', route('export.export'), $data);
$this->assertResponseStatus(200);
$this->see('ok');
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
}