1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/tests/Feature/Import/ImportCsvTest.php

359 lines
10 KiB
PHP
Raw Normal View History

2020-12-12 11:01:53 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-12-12 11:01:53 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
2020-12-12 11:01:53 +01:00
*/
2021-02-18 21:20:23 +01:00
namespace Tests\Feature\Import;
2020-12-12 11:01:53 +01:00
2021-02-18 21:20:23 +01:00
use App\Jobs\Import\CSVImport;
2020-12-18 05:34:40 +01:00
use App\Models\Client;
2021-02-18 21:20:23 +01:00
use App\Models\Expense;
2020-12-20 02:21:40 +01:00
use App\Models\Invoice;
2021-02-18 21:20:23 +01:00
use App\Models\Payment;
2020-12-18 05:34:40 +01:00
use App\Models\Product;
use App\Models\Vendor;
2021-02-18 21:20:23 +01:00
use App\Utils\Traits\MakesHash;
use Illuminate\Routing\Middleware\ThrottleRequests;
2020-12-16 11:06:20 +01:00
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
2020-12-12 11:01:53 +01:00
use League\Csv\Reader;
use League\Csv\Statement;
2021-02-18 21:20:23 +01:00
use Tests\MockAccountData;
use Tests\TestCase;
2020-12-12 11:01:53 +01:00
2021-02-18 21:20:23 +01:00
/**
* @test
* @covers App\Http\Controllers\ImportController
*/
class ImportCsvTest extends TestCase
{
use MakesHash;
use MockAccountData;
protected function setUp() :void
2021-02-18 21:20:23 +01:00
{
parent::setUp();
$this->withoutMiddleware(
ThrottleRequests::class
);
config(['database.default' => config('ninja.db.default')]);
2021-02-18 21:20:23 +01:00
// $this->faker = \Faker\Factory::create();
$this->makeTestData();
2021-07-03 06:45:40 +02:00
$this->markTestSkipped('Skipping CSV Import to improve test speed');
2021-02-18 21:20:23 +01:00
$this->withoutExceptionHandling();
}
public function testCsvRead()
{
$csv = file_get_contents(base_path().'/tests/Feature/Import/invoice.csv');
$this->assertTrue(is_array($this->getCsvData($csv)));
}
public function testClientCsvImport()
{
$csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv');
$hash = Str::random(32);
$column_map = [
1 => 'client.balance',
2 => 'client.paid_to_date',
0 => 'client.name',
19 => 'client.currency_id',
20 => 'client.public_notes',
21 => 'client.private_notes',
22 => 'contact.first_name',
23 => 'contact.last_name',
];
$data = [
'hash' => $hash,
'column_map' => ['client' => ['mapping' => $column_map]],
'skip_header' => true,
'import_type' => 'csv',
];
$pre_import = Client::count();
Cache::put($hash.'-client', base64_encode($csv), 360);
CSVImport::dispatchNow($data, $this->company);
$this->assertGreaterThan($pre_import, Client::count());
}
2021-02-18 21:20:23 +01:00
public function testInvoiceCsvImport()
{
/*Need to import clients first*/
$csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv');
$hash = Str::random(32);
$column_map = [
1 => 'client.balance',
2 => 'client.paid_to_date',
0 => 'client.name',
19 => 'client.currency_id',
20 => 'client.public_notes',
21 => 'client.private_notes',
22 => 'contact.first_name',
23 => 'contact.last_name',
];
$data = [
'hash' => $hash,
'column_map' => ['client' => ['mapping' => $column_map]],
'skip_header' => true,
'import_type' => 'csv',
];
Cache::put($hash.'-client', base64_encode($csv), 360);
CSVImport::dispatchNow($data, $this->company);
/*Now import invoices*/
2021-02-18 21:20:23 +01:00
$csv = file_get_contents(base_path().'/tests/Feature/Import/invoice.csv');
$hash = Str::random(32);
$column_map = [
1 => 'client.email',
3 => 'payment.amount',
5 => 'invoice.po_number',
8 => 'invoice.due_date',
9 => 'item.discount',
11 => 'invoice.partial_due_date',
12 => 'invoice.public_notes',
13 => 'invoice.private_notes',
0 => 'client.name',
2 => 'invoice.number',
7 => 'invoice.date',
14 => 'item.product_key',
15 => 'item.notes',
16 => 'item.cost',
17 => 'item.quantity',
];
2020-12-20 02:21:40 +01:00
$data = [
'hash' => $hash,
'column_map' => ['invoice' => ['mapping' => $column_map]],
'skip_header' => true,
'import_type' => 'csv',
];
2020-12-20 02:21:40 +01:00
$pre_import = Invoice::count();
2020-12-20 02:21:40 +01:00
Cache::put($hash.'-invoice', base64_encode($csv), 360);
2021-02-18 04:39:29 +01:00
CSVImport::dispatchNow($data, $this->company);
2021-02-18 04:39:29 +01:00
$this->assertGreaterThan($pre_import, Invoice::count());
}
2020-12-20 02:21:40 +01:00
public function testVendorCsvImport()
{
$csv = file_get_contents(base_path().'/tests/Feature/Import/vendors.csv');
$hash = Str::random(32);
$column_map = [
0 => 'vendor.name',
19 => 'vendor.currency_id',
20 => 'vendor.public_notes',
21 => 'vendor.private_notes',
22 => 'vendor.first_name',
23 => 'vendor.last_name',
];
2020-12-16 11:06:20 +01:00
$data = [
'hash' => $hash,
'column_map' => ['vendor' => ['mapping' => $column_map]],
'skip_header' => true,
'import_type' => 'csv',
];
2020-12-16 11:06:20 +01:00
$pre_import = Vendor::count();
2020-12-18 05:34:40 +01:00
Cache::put($hash.'-vendor', base64_encode($csv), 360);
2020-12-16 11:06:20 +01:00
CSVImport::dispatchNow($data, $this->company);
2020-12-18 05:34:40 +01:00
$this->assertGreaterThan($pre_import, Vendor::count());
}
2020-12-16 11:06:20 +01:00
public function testProductCsvImport()
{
$csv = file_get_contents(base_path().'/tests/Feature/Import/products.csv');
$hash = Str::random(32);
2020-12-18 01:31:27 +01:00
$column_map = [
2 => 'product.notes',
3 => 'product.cost',
];
2021-02-18 04:39:29 +01:00
$data = [
'hash' => $hash,
'column_map' => ['product' => ['mapping' => $column_map]],
'skip_header' => true,
'import_type' => 'csv',
];
2020-12-18 05:34:40 +01:00
$pre_import = Product::count();
2020-12-18 01:31:27 +01:00
Cache::put($hash.'-product', base64_encode($csv), 360);
2020-12-18 01:31:27 +01:00
CSVImport::dispatchNow($data, $this->company);
2020-12-18 01:31:27 +01:00
$this->assertGreaterThan($pre_import, Product::count());
}
2020-12-18 05:34:40 +01:00
public function testExpenseCsvImport()
{
$csv = file_get_contents(base_path().'/tests/Feature/Import/expenses.csv');
$hash = Str::random(32);
2021-02-18 04:39:29 +01:00
$column_map = [
2 => 'expense.public_notes',
3 => 'expense.amount',
];
2020-12-18 05:34:40 +01:00
$data = [
'hash' => $hash,
'column_map' => ['expense' => ['mapping' => $column_map]],
'skip_header' => true,
'import_type' => 'csv',
];
$pre_import = Expense::count();
Cache::put($hash.'-expense', base64_encode($csv), 360);
CSVImport::dispatchNow($data, $this->company);
2021-02-18 04:39:29 +01:00
$this->assertGreaterThan($pre_import, Expense::count());
}
public function testPaymentCsvImport()
{
2021-02-18 21:20:23 +01:00
/*Need to import clients first*/
$csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv');
$hash = Str::random(32);
$column_map = [
1 => 'client.balance',
2 => 'client.paid_to_date',
0 => 'client.name',
19 => 'client.currency_id',
20 => 'client.public_notes',
21 => 'client.private_notes',
22 => 'contact.first_name',
23 => 'contact.last_name',
];
$data = [
'hash' => $hash,
'column_map' => ['client' => ['mapping' => $column_map]],
'skip_header' => true,
'import_type' => 'csv',
];
Cache::put($hash.'-client', base64_encode($csv), 360);
CSVImport::dispatchNow($data, $this->company);
/*Now import invoices*/
2021-02-18 21:20:23 +01:00
$csv = file_get_contents(base_path().'/tests/Feature/Import/invoice.csv');
$hash = Str::random(32);
$column_map = [
1 => 'client.email',
3 => 'payment.amount',
5 => 'invoice.po_number',
8 => 'invoice.due_date',
9 => 'item.discount',
11 => 'invoice.partial_due_date',
12 => 'invoice.public_notes',
13 => 'invoice.private_notes',
0 => 'client.name',
2 => 'invoice.number',
7 => 'invoice.date',
14 => 'item.product_key',
15 => 'item.notes',
16 => 'item.cost',
17 => 'item.quantity',
];
$data = [
'hash' => $hash,
'column_map' => ['invoice' => ['mapping' => $column_map]],
'skip_header' => true,
'import_type' => 'csv',
];
$pre_import = Invoice::count();
Cache::put($hash.'-invoice', base64_encode($csv), 360);
CSVImport::dispatchNow($data, $this->company);
2020-12-18 01:31:27 +01:00
/* Test Now import payments*/
2021-02-18 04:39:29 +01:00
$csv = file_get_contents(base_path().'/tests/Feature/Import/payments.csv');
$hash = Str::random(32);
2021-02-18 04:39:29 +01:00
$column_map = [
0 => 'payment.client_id',
1 => 'payment.invoice_number',
2 => 'payment.amount',
3 => 'payment.date',
];
2021-02-18 04:39:29 +01:00
$data = [
'hash' => $hash,
'column_map' => ['payment' => ['mapping' => $column_map]],
'skip_header' => true,
'import_type' => 'csv',
];
2021-02-18 04:39:29 +01:00
$pre_import = Payment::count();
2021-02-18 04:39:29 +01:00
Cache::put($hash.'-payment', base64_encode($csv), 360);
2021-02-18 04:39:29 +01:00
CSVImport::dispatchNow($data, $this->company);
2021-02-18 04:39:29 +01:00
$this->assertGreaterThan($pre_import, Payment::count());
}
2021-02-18 04:39:29 +01:00
2021-02-18 21:20:23 +01:00
private function getCsvData($csvfile)
{
if (! ini_get('auto_detect_line_endings')) {
ini_set('auto_detect_line_endings', '1');
}
$csv = Reader::createFromString($csvfile);
$stmt = new Statement();
$data = iterator_to_array($stmt->process($csv));
if (count($data) > 0) {
$headers = $data[0];
// Remove Invoice Ninja headers
if (count($headers) && count($data) > 4) {
$firstCell = $headers[0];
if (strstr($firstCell, config('ninja.app_name'))) {
array_shift($data); // Invoice Ninja...
array_shift($data); // <blank line>
array_shift($data); // Enitty Type Header
}
}
}
return $data;
}
2021-02-20 05:03:55 +01:00
}