mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-12-31 19:27:51 -06:00
240 lines
8.6 KiB
PHP
240 lines
8.6 KiB
PHP
<?php
|
|
/**
|
|
* CsvControllerTest.php
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
*
|
|
* This software may be modified and distributed under the terms
|
|
* of the MIT license. See the LICENSE file for details.
|
|
*/
|
|
use Illuminate\Support\Collection;
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
|
|
|
|
|
/**
|
|
* Generated by PHPUnit_SkeletonGenerator on 2016-01-19 at 15:39:28.
|
|
*/
|
|
class CsvControllerTest extends TestCase
|
|
{
|
|
/**
|
|
* @covers FireflyIII\Http\Controllers\CsvController::columnRoles
|
|
* @covers FireflyIII\Http\Controllers\CsvController::__construct
|
|
*/
|
|
public function testColumnRoles()
|
|
{
|
|
$this->be($this->user());
|
|
|
|
// mock wizard
|
|
$fields = ['csv-file', 'csv-date-format', 'csv-has-headers', 'csv-import-account', 'csv-specifix', 'csv-delimiter'];
|
|
$wizard = $this->mock('FireflyIII\Helpers\Csv\WizardInterface');
|
|
$wizard->shouldReceive('sessionHasValues')->once()->with($fields)->andReturn(true);
|
|
|
|
// mock Data and Reader
|
|
$reader = $this->mock('League\Csv\Reader');
|
|
$data = $this->mock('FireflyIII\Helpers\Csv\Data');
|
|
$data->shouldReceive('getReader')->times(2)->andReturn($reader);
|
|
$reader->shouldReceive('fetchOne')->withNoArgs()->once()->andReturn([1, 2, 3, 4]);
|
|
$reader->shouldReceive('fetchOne')->with(1)->once()->andReturn([1, 2, 3, 4]);
|
|
|
|
$data->shouldReceive('getRoles')->once()->andReturn([]);
|
|
$data->shouldReceive('getMap')->once()->andReturn([]);
|
|
$data->shouldReceive('hasHeaders')->once()->andReturn(false);
|
|
|
|
$this->call('GET', '/csv/column_roles');
|
|
|
|
$this->assertResponseStatus(200);
|
|
}
|
|
|
|
/**
|
|
* @covers FireflyIII\Http\Controllers\CsvController::downloadConfig
|
|
*/
|
|
public function testDownloadConfig()
|
|
{
|
|
$this->be($this->user());
|
|
|
|
$fields = ['csv-date-format', 'csv-has-headers', 'csv-delimiter'];
|
|
$wizard = $this->mock('FireflyIII\Helpers\Csv\WizardInterface');
|
|
$wizard->shouldReceive('sessionHasValues')->once()->with($fields)->andReturn(true);
|
|
|
|
$this->call('GET', '/csv/download-config');
|
|
$this->assertResponseStatus(200);
|
|
}
|
|
|
|
/**
|
|
* @covers FireflyIII\Http\Controllers\CsvController::downloadConfigPage
|
|
*/
|
|
public function testDownloadConfigPage()
|
|
{
|
|
$this->be($this->user());
|
|
|
|
$fields = ['csv-date-format', 'csv-has-headers', 'csv-delimiter'];
|
|
$wizard = $this->mock('FireflyIII\Helpers\Csv\WizardInterface');
|
|
$wizard->shouldReceive('sessionHasValues')->once()->with($fields)->andReturn(true);
|
|
|
|
$this->call('GET', '/csv/download');
|
|
$this->assertResponseStatus(200);
|
|
}
|
|
|
|
/**
|
|
* @covers FireflyIII\Http\Controllers\CsvController::index
|
|
*/
|
|
public function testIndex()
|
|
{
|
|
|
|
$this->be($this->user());
|
|
$this->call('GET', '/csv');
|
|
$this->assertResponseStatus(200);
|
|
}
|
|
|
|
/**
|
|
* @covers FireflyIII\Http\Controllers\CsvController::initialParse
|
|
*/
|
|
public function testInitialParse()
|
|
{
|
|
$this->be($this->user());
|
|
|
|
$fields = ['csv-file', 'csv-date-format', 'csv-has-headers', 'csv-delimiter'];
|
|
$wizard = $this->mock('FireflyIII\Helpers\Csv\WizardInterface');
|
|
$wizard->shouldReceive('sessionHasValues')->once()->with($fields)->andReturn(true);
|
|
|
|
$wizard->shouldReceive('processSelectedRoles')->once()->with([])->andReturn([1, 2, 3]);
|
|
$wizard->shouldReceive('processSelectedMapping')->once()->with([1, 2, 3], [])->andReturn([1, 2, 3]);
|
|
|
|
$this->call('POST', '/csv/initial_parse');
|
|
// should be redirect
|
|
$this->assertResponseStatus(302);
|
|
|
|
// should be redirected to mapping:
|
|
$this->assertRedirectedToRoute('csv.map');
|
|
}
|
|
|
|
/**
|
|
* @covers FireflyIII\Http\Controllers\CsvController::initialParse
|
|
*/
|
|
public function testInitialParseNoMap()
|
|
{
|
|
$this->be($this->user());
|
|
|
|
$fields = ['csv-file', 'csv-date-format', 'csv-has-headers', 'csv-delimiter'];
|
|
$wizard = $this->mock('FireflyIII\Helpers\Csv\WizardInterface');
|
|
$wizard->shouldReceive('sessionHasValues')->once()->with($fields)->andReturn(true);
|
|
|
|
$wizard->shouldReceive('processSelectedRoles')->once()->with([])->andReturn([1, 2, 3]);
|
|
$wizard->shouldReceive('processSelectedMapping')->once()->with([1, 2, 3], [])->andReturn([]);
|
|
|
|
$this->call('POST', '/csv/initial_parse');
|
|
// should be redirect
|
|
$this->assertResponseStatus(302);
|
|
|
|
// should be redirected to download config:
|
|
$this->assertRedirectedToRoute('csv.download-config-page');
|
|
}
|
|
|
|
/**
|
|
* @covers FireflyIII\Http\Controllers\CsvController::map
|
|
*/
|
|
public function testMap()
|
|
{
|
|
$this->be($this->user());
|
|
|
|
$fields = ['csv-file', 'csv-date-format', 'csv-has-headers', 'csv-map', 'csv-roles', 'csv-delimiter'];
|
|
$wizard = $this->mock('FireflyIII\Helpers\Csv\WizardInterface');
|
|
$wizard->shouldReceive('sessionHasValues')->once()->with($fields)->andReturn(true);
|
|
|
|
$wizard->shouldReceive('showOptions')->once()->with([])->andReturn([]);
|
|
|
|
// mock Data and Reader
|
|
$reader = $this->mock('League\Csv\Reader');
|
|
$data = $this->mock('FireflyIII\Helpers\Csv\Data');
|
|
$data->shouldReceive('getReader')->once()->andReturn($reader);
|
|
$data->shouldReceive('getMap')->times(3)->andReturn([]);
|
|
$data->shouldReceive('hasHeaders')->once()->andReturn(true);
|
|
|
|
$wizard->shouldReceive('getMappableValues')->with($reader, [], true)->andReturn([]);
|
|
|
|
$data->shouldReceive('getMapped')->once()->andReturn([]);
|
|
|
|
|
|
$this->call('GET', '/csv/map');
|
|
$this->assertResponseStatus(200);
|
|
|
|
}
|
|
|
|
/**
|
|
* @covers FireflyIII\Http\Controllers\CsvController::process
|
|
*/
|
|
public function testProcess()
|
|
{
|
|
$this->be($this->user());
|
|
|
|
$fields = ['csv-file', 'csv-date-format', 'csv-has-headers', 'csv-map', 'csv-roles', 'csv-mapped', 'csv-delimiter'];
|
|
$wizard = $this->mock('FireflyIII\Helpers\Csv\WizardInterface');
|
|
$wizard->shouldReceive('sessionHasValues')->once()->with($fields)->andReturn(true);
|
|
|
|
// mock
|
|
$data = $this->mock('FireflyIII\Helpers\Csv\Data');
|
|
|
|
// mock
|
|
$importer = $this->mock('FireflyIII\Helpers\Csv\Importer');
|
|
$importer->shouldReceive('setData')->once()->with($data);
|
|
$importer->shouldReceive('run')->once()->withNoArgs();
|
|
|
|
$importer->shouldReceive('getRows')->once()->withNoArgs()->andReturn(0);
|
|
$importer->shouldReceive('getErrors')->once()->withNoArgs()->andReturn([]);
|
|
$importer->shouldReceive('getImported')->once()->withNoArgs()->andReturn(0);
|
|
$importer->shouldReceive('getJournals')->once()->withNoArgs()->andReturn(new Collection);
|
|
|
|
$this->call('GET', '/csv/process');
|
|
$this->assertResponseStatus(200);
|
|
}
|
|
|
|
|
|
/**
|
|
* @covers FireflyIII\Http\Controllers\CsvController::saveMapping
|
|
*/
|
|
public function testSaveMapping()
|
|
{
|
|
$this->be($this->user());
|
|
|
|
$fields = ['csv-file', 'csv-date-format', 'csv-has-headers', 'csv-map', 'csv-roles', 'csv-delimiter'];
|
|
$wizard = $this->mock('FireflyIII\Helpers\Csv\WizardInterface');
|
|
$wizard->shouldReceive('sessionHasValues')->once()->with($fields)->andReturn(true);
|
|
|
|
$this->call('POST', '/csv/save_mapping', ['mapping' => []]);
|
|
|
|
$this->assertResponseStatus(302);
|
|
$this->assertRedirectedToRoute('csv.download-config-page');
|
|
|
|
}
|
|
|
|
/**
|
|
* @covers FireflyIII\Http\Controllers\CsvController::upload
|
|
*/
|
|
public function testUpload()
|
|
{
|
|
$this->be($this->user());
|
|
|
|
$wizard = $this->mock('FireflyIII\Helpers\Csv\WizardInterface');
|
|
$wizard->shouldReceive('storeCsvFile')->andReturn('');
|
|
|
|
|
|
// mock Data and Reader
|
|
$data = $this->mock('FireflyIII\Helpers\Csv\Data');
|
|
$data->shouldReceive('setCsvFileLocation')->once()->with('');
|
|
$data->shouldReceive('setDateFormat')->once()->with('Ymd');
|
|
$data->shouldReceive('setHasHeaders')->once()->with('');
|
|
$data->shouldReceive('setMap')->once()->with([]);
|
|
$data->shouldReceive('setMapped')->once()->with([]);
|
|
$data->shouldReceive('setRoles')->once()->with([]);
|
|
$data->shouldReceive('setSpecifix')->once()->with([]);
|
|
$data->shouldReceive('setImportAccount')->once()->with(0);
|
|
$data->shouldReceive('setDelimiter')->once()->with(',');
|
|
|
|
|
|
$file = new UploadedFile(storage_path('build/test-upload.csv'), 'test-file.csv', 'text/plain', 446);
|
|
$this->call('POST', '/csv/upload', ['date_format' => 'Ymd'], [], ['csv' => $file]);
|
|
|
|
// csv data set:
|
|
$this->assertResponseStatus(302);
|
|
}
|
|
}
|