2020-12-14 11:43:07 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-12-14 11:43:07 +01:00
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-12-14 11:43:07 +01:00
|
|
|
*/
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-12-14 11:43:07 +01:00
|
|
|
namespace Tests\Feature\Export;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use League\Csv\Writer;
|
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
class ExportCsvTest extends TestCase
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use MockAccountData;
|
|
|
|
|
2022-06-21 12:00:57 +02:00
|
|
|
protected function setUp() :void
|
2020-12-14 11:43:07 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->makeTestData();
|
|
|
|
|
|
|
|
$this->withoutExceptionHandling();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExportCsv()
|
|
|
|
{
|
|
|
|
$csv = Writer::createFromFileObject(new \SplTempFileObject());
|
|
|
|
|
|
|
|
$header_invoice = Invoice::take(10)->get()->toArray();
|
|
|
|
$header_item = $header_invoice[0]['line_items'][0];
|
|
|
|
unset($header_invoice[0]['line_items']);
|
|
|
|
|
|
|
|
$header_invoice_keys = array_keys($header_invoice[0]);
|
2022-06-21 11:57:17 +02:00
|
|
|
$header_item_keys = array_keys((array) $header_item);
|
2020-12-14 11:43:07 +01:00
|
|
|
|
|
|
|
$header_invoice_values = array_values($header_invoice[0]);
|
2022-06-21 11:57:17 +02:00
|
|
|
$header_item_values = array_values((array) $header_item);
|
2020-12-14 11:43:07 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$merged_values = array_merge($header_invoice_values, (array) $header_item_values);
|
|
|
|
$merged_keys = array_merge($header_invoice_keys, (array) $header_item_keys);
|
2020-12-14 11:43:07 +01:00
|
|
|
|
2021-01-14 10:31:27 +01:00
|
|
|
// nlog(print_r($merged_keys, 1));
|
|
|
|
// nlog(print_r($merged_values, 1));
|
2020-12-14 11:43:07 +01:00
|
|
|
|
2020-12-15 11:45:04 +01:00
|
|
|
foreach ($merged_keys as &$key) {
|
2020-12-14 11:43:07 +01:00
|
|
|
$key = ctrans('texts.'.$key);
|
|
|
|
}
|
|
|
|
|
|
|
|
$csv->insertOne($merged_keys);
|
|
|
|
|
2020-12-15 11:45:04 +01:00
|
|
|
foreach (Invoice::take(10)->get() as $invoice) {
|
|
|
|
foreach ($invoice->line_items as $item) {
|
2020-12-14 11:43:07 +01:00
|
|
|
unset($invoice->line_items);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$csv->insertOne(array_merge($invoice->toArray(), (array) $item));
|
2020-12-14 11:43:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 07:17:45 +02:00
|
|
|
// Storage::put('invy.csv', $csv->getContent());
|
2020-12-17 13:44:44 +01:00
|
|
|
|
|
|
|
$this->markTestSkipped();
|
2020-12-14 11:43:07 +01:00
|
|
|
}
|
2020-12-15 11:45:04 +01:00
|
|
|
}
|