Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Documents/ReceiptTest.php
<?php | <?php | ||||
namespace Tests\Feature\Documents; | namespace Tests\Feature\Documents; | ||||
use App\Documents\Receipt; | use App\Documents\Receipt; | ||||
use App\Payment; | use App\Payment; | ||||
use App\Providers\PaymentProvider; | use App\Providers\PaymentProvider; | ||||
use App\User; | use App\User; | ||||
use App\Wallet; | use App\Wallet; | ||||
use App\VatRate; | |||||
use Carbon\Carbon; | use Carbon\Carbon; | ||||
use Illuminate\Support\Facades\Bus; | use Illuminate\Support\Facades\Bus; | ||||
use Tests\TestCase; | use Tests\TestCase; | ||||
class ReceiptTest extends TestCase | class ReceiptTest extends TestCase | ||||
{ | { | ||||
private $paymentIDs = ['AAA1', 'AAA2', 'AAA3', 'AAA4', 'AAA5', 'AAA6', 'AAA7']; | |||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
Payment::whereIn('id', $this->paymentIDs)->delete(); | Payment::query()->delete(); | ||||
VatRate::query()->delete(); | |||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->deleteTestUser('receipt-test@kolabnow.com'); | $this->deleteTestUser('receipt-test@kolabnow.com'); | ||||
Payment::whereIn('id', $this->paymentIDs)->delete(); | Payment::query()->delete(); | ||||
VatRate::query()->delete(); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test receipt HTML output (without VAT) | * Test receipt HTML output (without VAT) | ||||
*/ | */ | ||||
public function testHtmlOutput(): void | public function testHtmlOutput(): void | ||||
▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | public function testHtmlOutput(): void | ||||
$this->assertTrue(strpos($footerOutput, \config('app.company.email')) !== false); | $this->assertTrue(strpos($footerOutput, \config('app.company.email')) !== false); | ||||
} | } | ||||
/** | /** | ||||
* Test receipt HTML output (with VAT) | * Test receipt HTML output (with VAT) | ||||
*/ | */ | ||||
public function testHtmlOutputVat(): void | public function testHtmlOutputVat(): void | ||||
{ | { | ||||
\config(['app.vat.rate' => 7.7]); | |||||
\config(['app.vat.countries' => 'ch']); | |||||
$appName = \config('app.name'); | $appName = \config('app.name'); | ||||
$wallet = $this->getTestData('CH'); | $wallet = $this->getTestData('CH'); | ||||
$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); | ||||
$dom = new \DOMDocument('1.0', 'UTF-8'); | $dom = new \DOMDocument('1.0', 'UTF-8'); | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | protected function getTestData(string $country = null): Wallet | ||||
'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 | 'country' => $country | ||||
]); | ]); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
$vat = null; | |||||
if ($country) { | |||||
$vat = VatRate::create([ | |||||
'country' => $country, | |||||
'rate' => 7.7, | |||||
'start' => now(), | |||||
])->id; | |||||
} | |||||
// 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 an extra refund and chanrgeback | // and one with amount 0, and an extra refund and chanrgeback | ||||
$payment = Payment::create([ | $payment = Payment::create([ | ||||
'id' => 'AAA1', | 'id' => 'AAA1', | ||||
'status' => PaymentProvider::STATUS_PAID, | 'status' => PaymentProvider::STATUS_PAID, | ||||
'type' => PaymentProvider::TYPE_ONEOFF, | 'type' => PaymentProvider::TYPE_ONEOFF, | ||||
'description' => 'Paid in April', | 'description' => 'Paid in April', | ||||
'wallet_id' => $wallet->id, | 'wallet_id' => $wallet->id, | ||||
'provider' => 'stripe', | 'provider' => 'stripe', | ||||
'amount' => 1111, | 'amount' => 1111, | ||||
'credit_amount' => 1111, | |||||
'vat_rate_id' => $vat, | |||||
'currency' => 'CHF', | 'currency' => 'CHF', | ||||
'currency_amount' => 1111, | 'currency_amount' => 1111, | ||||
]); | ]); | ||||
$payment->updated_at = Carbon::create(2020, 4, 30, 12, 0, 0); | $payment->updated_at = Carbon::create(2020, 4, 30, 12, 0, 0); | ||||
$payment->save(); | $payment->save(); | ||||
$payment = Payment::create([ | $payment = Payment::create([ | ||||
'id' => 'AAA2', | 'id' => 'AAA2', | ||||
'status' => PaymentProvider::STATUS_PAID, | 'status' => PaymentProvider::STATUS_PAID, | ||||
'type' => PaymentProvider::TYPE_ONEOFF, | 'type' => PaymentProvider::TYPE_ONEOFF, | ||||
'description' => 'Paid in June', | 'description' => 'Paid in June', | ||||
'wallet_id' => $wallet->id, | 'wallet_id' => $wallet->id, | ||||
'provider' => 'stripe', | 'provider' => 'stripe', | ||||
'amount' => 2222, | 'amount' => 2222, | ||||
'credit_amount' => 2222, | |||||
'vat_rate_id' => $vat, | |||||
'currency' => 'CHF', | 'currency' => 'CHF', | ||||
'currency_amount' => 2222, | 'currency_amount' => 2222, | ||||
]); | ]); | ||||
$payment->updated_at = Carbon::create(2020, 6, 1, 0, 0, 0); | $payment->updated_at = Carbon::create(2020, 6, 1, 0, 0, 0); | ||||
$payment->save(); | $payment->save(); | ||||
$payment = Payment::create([ | $payment = Payment::create([ | ||||
'id' => 'AAA3', | 'id' => 'AAA3', | ||||
'status' => PaymentProvider::STATUS_PAID, | 'status' => PaymentProvider::STATUS_PAID, | ||||
'type' => PaymentProvider::TYPE_ONEOFF, | 'type' => PaymentProvider::TYPE_ONEOFF, | ||||
'description' => 'Auto-Payment Setup', | 'description' => 'Auto-Payment Setup', | ||||
'wallet_id' => $wallet->id, | 'wallet_id' => $wallet->id, | ||||
'provider' => 'stripe', | 'provider' => 'stripe', | ||||
'amount' => 0, | 'amount' => 0, | ||||
'credit_amount' => 0, | |||||
'vat_rate_id' => $vat, | |||||
'currency' => 'CHF', | 'currency' => 'CHF', | ||||
'currency_amount' => 0, | 'currency_amount' => 0, | ||||
]); | ]); | ||||
$payment->updated_at = Carbon::create(2020, 5, 1, 0, 0, 0); | $payment->updated_at = Carbon::create(2020, 5, 1, 0, 0, 0); | ||||
$payment->save(); | $payment->save(); | ||||
$payment = Payment::create([ | $payment = Payment::create([ | ||||
'id' => 'AAA4', | 'id' => 'AAA4', | ||||
'status' => PaymentProvider::STATUS_OPEN, | 'status' => PaymentProvider::STATUS_OPEN, | ||||
'type' => PaymentProvider::TYPE_ONEOFF, | 'type' => PaymentProvider::TYPE_ONEOFF, | ||||
'description' => 'Payment not yet paid', | 'description' => 'Payment not yet paid', | ||||
'wallet_id' => $wallet->id, | 'wallet_id' => $wallet->id, | ||||
'provider' => 'stripe', | 'provider' => 'stripe', | ||||
'amount' => 990, | 'amount' => 990, | ||||
'credit_amount' => 990, | |||||
'vat_rate_id' => $vat, | |||||
'currency' => 'CHF', | 'currency' => 'CHF', | ||||
'currency_amount' => 990, | 'currency_amount' => 990, | ||||
]); | ]); | ||||
$payment->updated_at = Carbon::create(2020, 5, 1, 0, 0, 0); | $payment->updated_at = Carbon::create(2020, 5, 1, 0, 0, 0); | ||||
$payment->save(); | $payment->save(); | ||||
// ... so we expect the five three on the receipt | // ... so we expect the five three on the receipt | ||||
$payment = Payment::create([ | $payment = Payment::create([ | ||||
'id' => 'AAA5', | 'id' => 'AAA5', | ||||
'status' => PaymentProvider::STATUS_PAID, | 'status' => PaymentProvider::STATUS_PAID, | ||||
'type' => PaymentProvider::TYPE_ONEOFF, | 'type' => PaymentProvider::TYPE_ONEOFF, | ||||
'description' => 'Payment OK', | 'description' => 'Payment OK', | ||||
'wallet_id' => $wallet->id, | 'wallet_id' => $wallet->id, | ||||
'provider' => 'stripe', | 'provider' => 'stripe', | ||||
'amount' => 1234, | 'amount' => 1234, | ||||
'credit_amount' => 1234, | |||||
'vat_rate_id' => $vat, | |||||
'currency' => 'CHF', | 'currency' => 'CHF', | ||||
'currency_amount' => 1234, | 'currency_amount' => 1234, | ||||
]); | ]); | ||||
$payment->updated_at = Carbon::create(2020, 5, 1, 0, 0, 0); | $payment->updated_at = Carbon::create(2020, 5, 1, 0, 0, 0); | ||||
$payment->save(); | $payment->save(); | ||||
$payment = Payment::create([ | $payment = Payment::create([ | ||||
'id' => 'AAA6', | 'id' => 'AAA6', | ||||
'status' => PaymentProvider::STATUS_PAID, | 'status' => PaymentProvider::STATUS_PAID, | ||||
'type' => PaymentProvider::TYPE_ONEOFF, | 'type' => PaymentProvider::TYPE_ONEOFF, | ||||
'description' => 'Payment OK', | 'description' => 'Payment OK', | ||||
'wallet_id' => $wallet->id, | 'wallet_id' => $wallet->id, | ||||
'provider' => 'stripe', | 'provider' => 'stripe', | ||||
'amount' => 1, | 'amount' => 1, | ||||
'credit_amount' => 1, | |||||
'vat_rate_id' => $vat, | |||||
'currency' => 'CHF', | 'currency' => 'CHF', | ||||
'currency_amount' => 1, | 'currency_amount' => 1, | ||||
]); | ]); | ||||
$payment->updated_at = Carbon::create(2020, 5, 10, 0, 0, 0); | $payment->updated_at = Carbon::create(2020, 5, 10, 0, 0, 0); | ||||
$payment->save(); | $payment->save(); | ||||
$payment = Payment::create([ | $payment = Payment::create([ | ||||
'id' => 'AAA7', | 'id' => 'AAA7', | ||||
'status' => PaymentProvider::STATUS_PAID, | 'status' => PaymentProvider::STATUS_PAID, | ||||
'type' => PaymentProvider::TYPE_RECURRING, | 'type' => PaymentProvider::TYPE_RECURRING, | ||||
'description' => 'Payment OK', | 'description' => 'Payment OK', | ||||
'wallet_id' => $wallet->id, | 'wallet_id' => $wallet->id, | ||||
'provider' => 'stripe', | 'provider' => 'stripe', | ||||
'amount' => 100, | 'amount' => 100, | ||||
'credit_amount' => 100, | |||||
'vat_rate_id' => $vat, | |||||
'currency' => 'CHF', | 'currency' => 'CHF', | ||||
'currency_amount' => 100, | 'currency_amount' => 100, | ||||
]); | ]); | ||||
$payment->updated_at = Carbon::create(2020, 5, 21, 23, 59, 0); | $payment->updated_at = Carbon::create(2020, 5, 21, 23, 59, 0); | ||||
$payment->save(); | $payment->save(); | ||||
$payment = Payment::create([ | $payment = Payment::create([ | ||||
'id' => 'ref1', | 'id' => 'ref1', | ||||
'status' => PaymentProvider::STATUS_PAID, | 'status' => PaymentProvider::STATUS_PAID, | ||||
'type' => PaymentProvider::TYPE_REFUND, | 'type' => PaymentProvider::TYPE_REFUND, | ||||
'description' => 'refund desc', | 'description' => 'refund desc', | ||||
'wallet_id' => $wallet->id, | 'wallet_id' => $wallet->id, | ||||
'provider' => 'stripe', | 'provider' => 'stripe', | ||||
'amount' => -100, | 'amount' => -100, | ||||
'credit_amount' => -100, | |||||
'vat_rate_id' => $vat, | |||||
'currency' => 'CHF', | 'currency' => 'CHF', | ||||
'currency_amount' => -100, | 'currency_amount' => -100, | ||||
]); | ]); | ||||
$payment->updated_at = Carbon::create(2020, 5, 30, 23, 59, 0); | $payment->updated_at = Carbon::create(2020, 5, 30, 23, 59, 0); | ||||
$payment->save(); | $payment->save(); | ||||
$payment = Payment::create([ | $payment = Payment::create([ | ||||
'id' => 'chback1', | 'id' => 'chback1', | ||||
'status' => PaymentProvider::STATUS_PAID, | 'status' => PaymentProvider::STATUS_PAID, | ||||
'type' => PaymentProvider::TYPE_CHARGEBACK, | 'type' => PaymentProvider::TYPE_CHARGEBACK, | ||||
'description' => '', | 'description' => '', | ||||
'wallet_id' => $wallet->id, | 'wallet_id' => $wallet->id, | ||||
'provider' => 'stripe', | 'provider' => 'stripe', | ||||
'amount' => -10, | 'amount' => -10, | ||||
'credit_amount' => -10, | |||||
'vat_rate_id' => $vat, | |||||
'currency' => 'CHF', | 'currency' => 'CHF', | ||||
'currency_amount' => -10, | 'currency_amount' => -10, | ||||
]); | ]); | ||||
$payment->updated_at = Carbon::create(2020, 5, 31, 23, 59, 0); | $payment->updated_at = Carbon::create(2020, 5, 31, 23, 59, 0); | ||||
$payment->save(); | $payment->save(); | ||||
// Make sure some config is set so we can test it's put into the receipt | // Make sure some config is set so we can test it's put into the receipt | ||||
if (empty(\config('app.company.name'))) { | if (empty(\config('app.company.name'))) { | ||||
Show All 36 Lines |