From 8c328f31caf9ce7510d7c954c75845f7a855c659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Wed, 27 Oct 2021 11:04:42 +0300 Subject: [PATCH] added payment test case --- tests/Feature/PaymentTestCase.php | 170 ++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 tests/Feature/PaymentTestCase.php diff --git a/tests/Feature/PaymentTestCase.php b/tests/Feature/PaymentTestCase.php new file mode 100644 index 000000000..9239d6490 --- /dev/null +++ b/tests/Feature/PaymentTestCase.php @@ -0,0 +1,170 @@ +updateSetting(); + + $this->createInvoice(); + + $this->get($this->getShowUrl()) + ->assertOk() + ->assertSeeText($this->setting_request['name']); + + $this->post($this->getConfirmUrl(), $this->payment_request) + ->assertOk(); + + $this->assertPayment(); + } + + public function assertPaymentFromPortal() + { + $this->is_portal = true; + + $this->updateSetting(); + + $this->createCustomer(); + + $this->createInvoice(); + + $this->loginAs($this->customer_user) + ->get($this->getShowUrl()) + ->assertOk() + ->assertSeeText($this->setting_request['name']); + + $this->loginAs($this->customer_user) + ->post($this->getConfirmUrl(), $this->payment_request) + ->assertOk(); + + $this->assertPayment(); + } + + public function updateSetting() + { + if (in_array($this->invoice_currency, ['USD', 'EUR', 'GBP', 'TRY'])) { + if ($this->invoice_currency != 'USD') { + setting()->set('default.currency', $this->invoice_currency); + } + } elseif ($this->invoice_currency != null) { + $this->dispatch(new CreateCurrency([ + 'company_id' => company_id(), + 'name' => config('money.' . $this->invoice_currency . '.name'), + 'code' => $this->invoice_currency, + 'rate' => config(['money.' . $this->invoice_currency . '.rate' => 1]), + 'enabled' => 1, + 'symbol_first' => config('money.' . $this->invoice_currency . '.symbol_first'), + 'decimal_mark' => config('money.' . $this->invoice_currency . '.decimal_mark'), + 'thousands_separator' => config('money.' . $this->invoice_currency . '.thousands_separator'), + 'default_currency' => true, + ])); + } + + $module = module($this->alias); + + if (File::exists(base_path('modules/' . $module->getStudlyName() . '/Routes/admin.php'))) { + $this->loginAs() + ->post(route($this->alias . '.settings.update'), $this->setting_request) + ->assertOk(); + } else { + $this->loginAs() + ->patch(route('settings.module.update', $this->alias), $this->setting_request) + ->assertOk(); + } + + $this->assertFlashLevel('success'); + } + + public function getShowUrl() + { + return $this->is_portal + ? route('portal.invoices.show', $this->invoice->id) + : URL::signedRoute('signed.invoices.show', [$this->invoice->id]); + } + + public function getConfirmUrl() + { + return $this->is_portal + ? route('portal.' . $this->alias . '.invoices.confirm', $this->invoice->id) + : URL::signedRoute('signed.' . $this->alias . '.invoices.confirm', [$this->invoice->id]); + } + + public function assertPayment() + { + $this->assertFlashLevel('success'); + + $invoice = Document::where('document_number', $this->invoice->document_number)->first(); + + $this->assertEquals('paid', $invoice->status); + + $this->assertDatabaseHas('transactions', [ + 'document_id' => $invoice->id, + 'contact_id' => $invoice->contact_id, + 'amount' => $invoice->amount, + 'currency_code' => $invoice->currency_code, + 'currency_rate' => $invoice->currency_rate, + 'type' => 'income', + ]); + } + + public function createInvoice() + { + $this->invoice = $this->dispatch(new CreateDocument($this->getInvoiceRequest())); + } + + public function createCustomer() + { + $password = $this->faker->password; + + $request = Contact::factory()->customer()->enabled()->raw() + [ + 'create_user' => 'true', + 'locale' => 'en-GB', + 'password' => $password, + 'password_confirmation' => $password, + ]; + + $this->customer = $this->dispatch(new CreateContact($request)); + + $this->customer_user = User::where('email', $request['email'])->first(); + } + + public function getInvoiceRequest() + { + if ($this->is_portal) { + return Document::factory()->invoice()->draft()->items()->raw([ + 'contact_id' => $this->customer->id, + 'contact_name' => $this->customer->name, + ]); + } + + return Document::factory()->invoice()->draft()->items()->raw(); + } +}