1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/tests/Feature/Import/ImportCsvTest.php

194 lines
5.2 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) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace Tests\Feature\Import;
2020-12-16 11:06:20 +01:00
use App\Jobs\Import\CSVImport;
2020-12-18 05:34:40 +01:00
use App\Models\Client;
2020-12-20 02:21:40 +01:00
use App\Models\Invoice;
2020-12-18 05:34:40 +01:00
use App\Models\Product;
2020-12-12 11:01:53 +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;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers App\Http\Controllers\ImportController
*/
class ImportCsvTest extends TestCase
{
use MakesHash;
use MockAccountData;
public function setUp() :void
{
parent::setUp();
$this->withoutMiddleware(
ThrottleRequests::class
);
2020-12-12 11:35:42 +01:00
// $this->faker = \Faker\Factory::create();
2020-12-12 11:01:53 +01:00
2020-12-16 11:06:20 +01:00
$this->makeTestData();
2020-12-12 11:01:53 +01:00
$this->withoutExceptionHandling();
}
public function testCsvRead()
{
$csv = file_get_contents(base_path().'/tests/Feature/Import/invoice.csv');
2020-12-12 11:35:42 +01:00
$this->assertTrue(is_array($this->getCsvData($csv)));
2020-12-12 11:01:53 +01:00
}
2020-12-20 02:21:40 +01:00
public function testInvoiceCsvImport()
{
$csv = file_get_contents(base_path().'/tests/Feature/Import/invoice.csv');
$hash = Str::random(32);
$column_map = [
0 => 'invoice.number',
4 => 'invoice.client_id',
7 => 'invoice.date',
8 => 'invoice.due_date',
9 => 'invoice.terms',
10 => 'invoice.public_notes',
11 => 'invoice.is_sent',
12 => 'invoice.private_notes',
13 => 'invoice.uses_inclusive_taxes',
14 => 'invoice.tax_name1',
15 => 'invoice.tax_rate1',
16 => 'invoice.tax_name2',
17 => 'invoice.tax_rate2',
18 => 'invoice.tax_name3',
19 => 'invoice.tax_rate3',
21 => 'invoice.footer',
22 => 'invoice.partial',
23 => 'invoice.partial_due_date',
33 => 'payment.date',
34 => 'payment.amount',
35 => 'payment.transaction_reference',
36 => 'item.quantity',
37 => 'item.cost',
38 => 'item.product_key',
39 => 'item.notes',
40 => 'item.discount',
41 => 'item.is_amount_discount',
];
$data = [
'hash' => $hash,
'column_map' => $column_map,
'skip_header' => true,
'entity_type'=> 'invoice',
];
$pre_import = Invoice::count();
Cache::put($hash, base64_encode($csv), 360);
CSVImport::dispatchNow($data, $this->company);
$this->assertGreaterThan($pre_import, Client::count());
}
2020-12-12 11:01:53 +01:00
2020-12-16 11:06:20 +01:00
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' => $column_map,
'skip_header' => true,
'entity_type'=> 'client',
];
2020-12-18 05:34:40 +01:00
$pre_import = Client::count();
2020-12-16 11:06:20 +01:00
Cache::put($hash, base64_encode($csv), 360);
2020-12-18 05:34:40 +01:00
CSVImport::dispatchNow($data, $this->company);
$this->assertGreaterThan($pre_import, Client::count());
2020-12-16 11:06:20 +01:00
}
2020-12-18 01:31:27 +01:00
public function testProductCsvImport()
{
$csv = file_get_contents(base_path().'/tests/Feature/Import/products.csv');
$hash = Str::random(32);
2020-12-18 05:34:40 +01:00
2020-12-18 01:31:27 +01:00
$column_map = [
2 => 'product.notes',
3 => 'product.cost',
];
$data = [
'hash' => $hash,
'column_map' => $column_map,
'skip_header' => true,
'entity_type'=> 'product',
];
2020-12-18 05:34:40 +01:00
$pre_import = Product::count();
2020-12-18 01:31:27 +01:00
Cache::put($hash, base64_encode($csv), 360);
CSVImport::dispatchNow($data, $this->company);
2020-12-18 05:34:40 +01:00
$this->assertGreaterThan($pre_import, Product::count());
2020-12-18 01:31:27 +01:00
}
2020-12-12 11:01:53 +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
}
}
}
2020-12-12 11:35:42 +01:00
2020-12-12 11:01:53 +01:00
return $data;
}
}