1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00

Tests for report generation

This commit is contained in:
David Bomba 2023-07-16 20:05:30 +10:00
parent c858dcf40d
commit 323064b2ba

View File

@ -26,6 +26,7 @@ use App\Utils\Traits\MakesHash;
use App\DataMapper\CompanySettings;
use App\Factory\CompanyUserFactory;
use App\Factory\InvoiceItemFactory;
use App\Models\Expense;
use App\Services\Report\ARDetailReport;
use Illuminate\Routing\Middleware\ThrottleRequests;
@ -227,6 +228,17 @@ class ReportCsvGenerationTest extends TestCase
}
private function getFirstValueByColumn($csv, $column)
{
$reader = Reader::createFromString($csv);
$reader->setHeaderOffset(0);
$res = $reader->fetchColumnByName($column);
$res = iterator_to_array($res, true);
return $res[1];
}
public function testCreditCsvGeneration()
{
@ -256,6 +268,125 @@ class ReportCsvGenerationTest extends TestCase
])->post('/api/v1/reports/credits', $data);
$response->assertStatus(200);
$csv = $response->streamedContent();
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Amount'));
$this->assertEquals('50', $this->getFirstValueByColumn($csv, 'Balance'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Discount'));
$this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'PO Number'));
$this->assertEquals('Public', $this->getFirstValueByColumn($csv, 'Public Notes'));
$this->assertEquals('Private', $this->getFirstValueByColumn($csv, 'Private Notes'));
$this->assertEquals('Terms', $this->getFirstValueByColumn($csv, 'Terms'));
}
public function testInvoiceCsvGeneration()
{
Invoice::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_id' => $this->client->id,
'amount' => 100,
'balance' => 50,
'status_id' => 2,
'discount' => 10,
'po_number' => '1234',
'public_notes' => 'Public',
'private_notes' => 'Private',
'terms' => 'Terms',
'date' => '2020-01-01',
'due_date' => '2021-01-02',
'partial_due_date' => '2021-01-03',
'partial' => 10,
'discount' => 10,
'custom_value1' => 'Custom 1',
'custom_value2' => 'Custom 2',
'custom_value3' => 'Custom 3',
'custom_value4' => 'Custom 4',
'footer' => 'Footer',
'tax_name1' => 'Tax 1',
'tax_rate1' => 10,
'tax_name2' => 'Tax 2',
'tax_rate2' => 20,
'tax_name3' => 'Tax 3',
'tax_rate3' => 30,
]);
$data = [
'date_range' => 'all',
'report_keys' => [],
'send_email' => false,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/reports/invoices', $data);
$response->assertStatus(200);
$csv = $response->streamedContent();
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Amount'));
$this->assertEquals('50', $this->getFirstValueByColumn($csv, 'Balance'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Discount'));
$this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'PO Number'));
$this->assertEquals('Public', $this->getFirstValueByColumn($csv, 'Public Notes'));
$this->assertEquals('Private', $this->getFirstValueByColumn($csv, 'Private Notes'));
$this->assertEquals('Terms', $this->getFirstValueByColumn($csv, 'Terms'));
$this->assertEquals('2020-01-01', $this->getFirstValueByColumn($csv, 'Date'));
$this->assertEquals('2021-01-02', $this->getFirstValueByColumn($csv, 'Due Date'));
$this->assertEquals('2021-01-03', $this->getFirstValueByColumn($csv, 'Partial Due Date'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Partial/Deposit'));
$this->assertEquals('Custom 1', $this->getFirstValueByColumn($csv, 'Custom Value 1'));
$this->assertEquals('Custom 2', $this->getFirstValueByColumn($csv, 'Custom Value 2'));
$this->assertEquals('Custom 3', $this->getFirstValueByColumn($csv, 'Custom Value 3'));
$this->assertEquals('Custom 4', $this->getFirstValueByColumn($csv, 'Custom Value 4'));
$this->assertEquals('Footer', $this->getFirstValueByColumn($csv, 'Footer'));
$this->assertEquals('Tax 1', $this->getFirstValueByColumn($csv, 'Tax Name 1'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Tax Rate 1'));
$this->assertEquals('Tax 2', $this->getFirstValueByColumn($csv, 'Tax Name 2'));
$this->assertEquals('20', $this->getFirstValueByColumn($csv, 'Tax Rate 2'));
$this->assertEquals('Tax 3', $this->getFirstValueByColumn($csv, 'Tax Name 3'));
$this->assertEquals('30', $this->getFirstValueByColumn($csv, 'Tax Rate 3'));
$this->assertEquals('Sent', $this->getFirstValueByColumn($csv, 'Status'));
}
public function testExpenseCsvGeneration()
{
Expense::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_id' => $this->client->id,
'amount' => 100,
'public_notes' => 'Public',
'private_notes' => 'Private',
]);
$data = [
'date_range' => 'all',
'report_keys' => [],
'send_email' => false,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/reports/expenses', $data);
$response->assertStatus(200);
$csv = $response->streamedContent();
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Amount'));
$this->assertEquals('Public', $this->getFirstValueByColumn($csv, 'Public Notes'));
$this->assertEquals('Private', $this->getFirstValueByColumn($csv, 'Private Notes'));
$this->assertEquals($this->user->present()->name(), $this->getFirstValueByColumn($csv, 'User'));
}
}