Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Documents/ReceiptTest.php
Show All 18 Lines | class ReceiptTest extends TestCase | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->deleteTestUser('receipt-test@kolabnow.com'); | $this->deleteTestUser('receipt-test@kolabnow.com'); | ||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test receipt HTML output | * Test receipt HTML output (without VAT) | ||||
* | |||||
* @return void | |||||
*/ | */ | ||||
public function testHtmlOutput() | public function testHtmlOutput(): void | ||||
{ | { | ||||
$appName = \config('app.name'); | $appName = \config('app.name'); | ||||
$wallet = $this->getTestData(); | $wallet = $this->getTestData(); | ||||
$receipt = new Receipt($wallet, 2020, 5); | $receipt = new Receipt($wallet, 2020, 5); | ||||
$html = $receipt->htmlOutput(); | $html = $receipt->htmlOutput(); | ||||
$this->assertStringStartsWith('<!DOCTYPE html>', $html); | $this->assertStringStartsWith('<!DOCTYPE html>', $html); | ||||
Show All 19 Lines | public function testHtmlOutput(): void | ||||
$this->assertCount(3, $headerCells); | $this->assertCount(3, $headerCells); | ||||
$this->assertSame('Date', $this->getNodeContent($headerCells[0])); | $this->assertSame('Date', $this->getNodeContent($headerCells[0])); | ||||
$this->assertSame('Description', $this->getNodeContent($headerCells[1])); | $this->assertSame('Description', $this->getNodeContent($headerCells[1])); | ||||
$this->assertSame('Amount', $this->getNodeContent($headerCells[2])); | $this->assertSame('Amount', $this->getNodeContent($headerCells[2])); | ||||
$cells = $records[1]->getElementsByTagName('td'); | $cells = $records[1]->getElementsByTagName('td'); | ||||
$this->assertCount(3, $cells); | $this->assertCount(3, $cells); | ||||
$this->assertSame('2020-05-01', $this->getNodeContent($cells[0])); | $this->assertSame('2020-05-01', $this->getNodeContent($cells[0])); | ||||
$this->assertSame("$appName Services", $this->getNodeContent($cells[1])); | $this->assertSame("$appName Services", $this->getNodeContent($cells[1])); | ||||
$this->assertSame('12.34 CHF', $this->getNodeContent($cells[2])); | $this->assertSame('12,34 CHF', $this->getNodeContent($cells[2])); | ||||
$cells = $records[2]->getElementsByTagName('td'); | $cells = $records[2]->getElementsByTagName('td'); | ||||
$this->assertCount(3, $cells); | $this->assertCount(3, $cells); | ||||
$this->assertSame('2020-05-10', $this->getNodeContent($cells[0])); | $this->assertSame('2020-05-10', $this->getNodeContent($cells[0])); | ||||
$this->assertSame("$appName Services", $this->getNodeContent($cells[1])); | $this->assertSame("$appName Services", $this->getNodeContent($cells[1])); | ||||
$this->assertSame('0.01 CHF', $this->getNodeContent($cells[2])); | $this->assertSame('0,01 CHF', $this->getNodeContent($cells[2])); | ||||
$cells = $records[3]->getElementsByTagName('td'); | $cells = $records[3]->getElementsByTagName('td'); | ||||
$this->assertCount(3, $cells); | $this->assertCount(3, $cells); | ||||
$this->assertSame('2020-05-31', $this->getNodeContent($cells[0])); | $this->assertSame('2020-05-31', $this->getNodeContent($cells[0])); | ||||
$this->assertSame("$appName Services", $this->getNodeContent($cells[1])); | $this->assertSame("$appName Services", $this->getNodeContent($cells[1])); | ||||
$this->assertSame('1.00 CHF', $this->getNodeContent($cells[2])); | $this->assertSame('1,00 CHF', $this->getNodeContent($cells[2])); | ||||
$summaryCells = $records[4]->getElementsByTagName('td'); | $summaryCells = $records[4]->getElementsByTagName('td'); | ||||
$this->assertCount(2, $summaryCells); | $this->assertCount(2, $summaryCells); | ||||
$this->assertSame('Total', $this->getNodeContent($summaryCells[0])); | $this->assertSame('Total', $this->getNodeContent($summaryCells[0])); | ||||
$this->assertSame('13.35 CHF', $this->getNodeContent($summaryCells[1])); | $this->assertSame('13,35 CHF', $this->getNodeContent($summaryCells[1])); | ||||
// Customer data | // Customer data | ||||
$customer = $dom->getElementById('customer'); | $customer = $dom->getElementById('customer'); | ||||
$customerCells = $customer->getElementsByTagName('td'); | $customerCells = $customer->getElementsByTagName('td'); | ||||
$customerOutput = $this->getNodeContent($customerCells[0]); | $customerOutput = $this->getNodeContent($customerCells[0]); | ||||
$customerExpected = "Firstname Lastname\nTest Unicode Straße 150\n10115 Berlin"; | $customerExpected = "Firstname Lastname\nTest Unicode Straße 150\n10115 Berlin"; | ||||
$this->assertSame($customerExpected, $this->getNodeContent($customerCells[0])); | $this->assertSame($customerExpected, $this->getNodeContent($customerCells[0])); | ||||
$customerIdents = $this->getNodeContent($customerCells[1]); | $customerIdents = $this->getNodeContent($customerCells[1]); | ||||
$this->assertTrue(strpos($customerIdents, "Account ID {$wallet->id}") !== false); | $this->assertTrue(strpos($customerIdents, "Account ID {$wallet->id}") !== false); | ||||
$this->assertTrue(strpos($customerIdents, "Customer No. {$wallet->owner->id}") !== false); | $this->assertTrue(strpos($customerIdents, "Customer No. {$wallet->owner->id}") !== false); | ||||
// TODO: Company details in the footer | // Company details in the footer | ||||
$footer = $dom->getElementById('footer'); | $footer = $dom->getElementById('footer'); | ||||
$footerOutput = $footer->textContent; | $footerOutput = $footer->textContent; | ||||
$this->assertStringStartsWith(\config('app.company.details'), $footerOutput); | $this->assertStringStartsWith(\config('app.company.details'), $footerOutput); | ||||
$this->assertTrue(strpos($footerOutput, \config('app.company.email')) !== false); | $this->assertTrue(strpos($footerOutput, \config('app.company.email')) !== false); | ||||
} | } | ||||
/** | /** | ||||
* Test receipt HTML output (with VAT) | |||||
*/ | |||||
public function testHtmlOutputVat(): void | |||||
{ | |||||
\config(['app.vat.rate' => 7.7]); | |||||
\config(['app.vat.countries' => 'ch']); | |||||
$appName = \config('app.name'); | |||||
$wallet = $this->getTestData('CH'); | |||||
$receipt = new Receipt($wallet, 2020, 5); | |||||
$html = $receipt->htmlOutput(); | |||||
$this->assertStringStartsWith('<!DOCTYPE html>', $html); | |||||
$dom = new \DOMDocument('1.0', 'UTF-8'); | |||||
$dom->loadHTML($html); | |||||
// The main table content | |||||
$content = $dom->getElementById('content'); | |||||
$records = $content->getElementsByTagName('tr'); | |||||
$this->assertCount(7, $records); | |||||
$cells = $records[1]->getElementsByTagName('td'); | |||||
$this->assertCount(3, $cells); | |||||
$this->assertSame('2020-05-01', $this->getNodeContent($cells[0])); | |||||
$this->assertSame("$appName Services", $this->getNodeContent($cells[1])); | |||||
$this->assertSame('11,39 CHF', $this->getNodeContent($cells[2])); | |||||
$cells = $records[2]->getElementsByTagName('td'); | |||||
$this->assertCount(3, $cells); | |||||
$this->assertSame('2020-05-10', $this->getNodeContent($cells[0])); | |||||
$this->assertSame("$appName Services", $this->getNodeContent($cells[1])); | |||||
$this->assertSame('0,01 CHF', $this->getNodeContent($cells[2])); | |||||
$cells = $records[3]->getElementsByTagName('td'); | |||||
$this->assertCount(3, $cells); | |||||
$this->assertSame('2020-05-31', $this->getNodeContent($cells[0])); | |||||
$this->assertSame("$appName Services", $this->getNodeContent($cells[1])); | |||||
$this->assertSame('0,92 CHF', $this->getNodeContent($cells[2])); | |||||
$subtotalCells = $records[4]->getElementsByTagName('td'); | |||||
$this->assertCount(2, $subtotalCells); | |||||
$this->assertSame('Subtotal', $this->getNodeContent($subtotalCells[0])); | |||||
$this->assertSame('12,32 CHF', $this->getNodeContent($subtotalCells[1])); | |||||
$vatCells = $records[5]->getElementsByTagName('td'); | |||||
$this->assertCount(2, $vatCells); | |||||
$this->assertSame('VAT (7.7%)', $this->getNodeContent($vatCells[0])); | |||||
$this->assertSame('1,03 CHF', $this->getNodeContent($vatCells[1])); | |||||
$totalCells = $records[6]->getElementsByTagName('td'); | |||||
$this->assertCount(2, $totalCells); | |||||
$this->assertSame('Total', $this->getNodeContent($totalCells[0])); | |||||
$this->assertSame('13,35 CHF', $this->getNodeContent($totalCells[1])); | |||||
} | |||||
/** | |||||
* Test receipt PDF output | * Test receipt PDF output | ||||
* | |||||
* @return void | |||||
*/ | */ | ||||
public function testPdfOutput() | public function testPdfOutput(): void | ||||
{ | { | ||||
$wallet = $this->getTestData(); | $wallet = $this->getTestData(); | ||||
$receipt = new Receipt($wallet, 2020, 5); | $receipt = new Receipt($wallet, 2020, 5); | ||||
$pdf = $receipt->PdfOutput(); | $pdf = $receipt->PdfOutput(); | ||||
$this->assertStringStartsWith("%PDF-1.3\n", $pdf); | $this->assertStringStartsWith("%PDF-1.3\n", $pdf); | ||||
$this->assertTrue(strlen($pdf) > 5000); | $this->assertTrue(strlen($pdf) > 5000); | ||||
// TODO: Test the content somehow | // TODO: Test the content somehow | ||||
} | } | ||||
/** | /** | ||||
* Prepare data for a test | * Prepare data for a test | ||||
* | * | ||||
* @param string $country User country code | |||||
* | |||||
* @return \App\Wallet | * @return \App\Wallet | ||||
*/ | */ | ||||
protected function getTestData() | protected function getTestData(string $country = null): Wallet | ||||
{ | { | ||||
Bus::fake(); | Bus::fake(); | ||||
$user = $this->getTestUser('receipt-test@kolabnow.com'); | $user = $this->getTestUser('receipt-test@kolabnow.com'); | ||||
$user->setSettings([ | $user->setSettings([ | ||||
'first_name' => 'Firstname', | 'first_name' => 'Firstname', | ||||
'last_name' => 'Lastname', | 'last_name' => 'Lastname', | ||||
'billing_address' => "Test Unicode Straße 150\n10115 Berlin", | 'billing_address' => "Test Unicode Straße 150\n10115 Berlin", | ||||
'country' => $country | |||||
]); | ]); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
// Create two payments out of the 2020-05 period | // Create two payments out of the 2020-05 period | ||||
// and three in it, plus one in the period but unpaid, | // and three in it, plus one in the period but unpaid, | ||||
// and one with amount 0 | // and one with amount 0 | ||||
▲ Show 20 Lines • Show All 123 Lines • Show Last 20 Lines |