mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 21:22:58 +01:00
84 lines
2.1 KiB
PHP
84 lines
2.1 KiB
PHP
<?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;
|
|
|
|
use App\Models\Product;
|
|
use App\Utils\Traits\MakesHash;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
|
use Illuminate\Validation\ValidationException;
|
|
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
|
|
);
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
$this->makeTestData();
|
|
|
|
$this->withoutExceptionHandling();
|
|
}
|
|
|
|
public function testCsvRead()
|
|
{
|
|
$csv = file_get_contents(base_path().'/tests/Feature/Import/invoice.csv');
|
|
|
|
return $this->getCsvData($csv);
|
|
}
|
|
|
|
|
|
private function getCsvData($csvfile)
|
|
{
|
|
|
|
if (! ini_get('auto_detect_line_endings')) {
|
|
ini_set('auto_detect_line_endings', '1');
|
|
}
|
|
|
|
$csv = Reader::createFromString($csvfile);
|
|
//$csv->setHeaderOffset(0); //set the CSV header offset
|
|
$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
|
|
}
|
|
}
|
|
}
|
|
info(print_r($data,1));
|
|
return $data;
|
|
}
|
|
} |