From de32d57b5b19c9d30617e9d164267bb1dd44518a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 6 Sep 2021 09:37:35 +1000 Subject: [PATCH] Working on testing scenarios --- app/Factory/InvoiceFactory.php | 2 +- app/Helpers/Invoice/InvoiceSum.php | 2 +- tests/Feature/MultiPaymentDeleteTest.php | 178 +++++++++++++++++++++++ 3 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 tests/Feature/MultiPaymentDeleteTest.php diff --git a/app/Factory/InvoiceFactory.php b/app/Factory/InvoiceFactory.php index 42956086773d..0d3a5e334a1e 100644 --- a/app/Factory/InvoiceFactory.php +++ b/app/Factory/InvoiceFactory.php @@ -49,7 +49,7 @@ class InvoiceFactory $invoice->user_id = $user_id; $invoice->company_id = $company_id; $invoice->recurring_id = null; - + return $invoice; } } diff --git a/app/Helpers/Invoice/InvoiceSum.php b/app/Helpers/Invoice/InvoiceSum.php index aa3940f8ea59..53012df09105 100644 --- a/app/Helpers/Invoice/InvoiceSum.php +++ b/app/Helpers/Invoice/InvoiceSum.php @@ -30,7 +30,7 @@ class InvoiceSum public $invoice_item; - public $total_taxes; + public $total_taxes = 0; private $total; diff --git a/tests/Feature/MultiPaymentDeleteTest.php b/tests/Feature/MultiPaymentDeleteTest.php new file mode 100644 index 000000000000..f9f98b688c78 --- /dev/null +++ b/tests/Feature/MultiPaymentDeleteTest.php @@ -0,0 +1,178 @@ +faker = \Faker\Factory::create(); + + } + + public function testComplexRefundDeleteScenario() + { + $account = Account::factory()->create(); + $company = Company::factory()->create([ + 'account_id' => $account->id, + ]); + + $account->default_company_id = $company->id; + $account->save(); + + $user = User::factory()->create([ + 'account_id' => $account->id, + 'confirmation_code' => '11', + ]); + + $cu = CompanyUserFactory::create($user->id, $company->id, $account->id); + $cu->is_owner = true; + $cu->is_admin = true; + $cu->save(); + + $token = new CompanyToken; + $token->user_id = $user->id; + $token->company_id = $company->id; + $token->account_id = $account->id; + $token->name = 'test token'; + $token->token = 'okeytokey'; + $token->is_system = true; + $token->save(); + + $client = Client::factory()->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + ]); + + ClientContact::factory()->create([ + 'user_id' => $user->id, + 'client_id' => $client->id, + 'company_id' => $company->id, + 'is_primary' => 1, + ]); + + ClientContact::factory()->create([ + 'user_id' => $user->id, + 'client_id' => $client->id, + 'company_id' => $company->id, + ]); + + $invoice = Invoice::factory()->create([ + 'user_id' => $user->id, + 'client_id' => $client->id, + 'company_id' => $company->id, + 'number' => (string)$this->faker->randomNumber(6), + ]); + + $invoice = InvoiceFactory::create($company->id,$user->id); + $invoice->client_id = $client->id; + $invoice->status_id = Invoice::STATUS_DRAFT; + + $line_items = []; + + $item = InvoiceItemFactory::create(); + $item->quantity = 1; + $item->cost = 325; + $item->type_id = 1; + + $line_items[] = $item; + + $invoice->line_items = $line_items; + + $invoice = $invoice->calc()->getInvoice(); + + $this->assertEquals(0, $client->balance); + $this->assertEquals(0, $invoice->balance); + + $invoice = $invoice->service()->markSent()->save(); + + $invoice->fresh(); + $invoice->client->fresh(); + + $this->assertEquals(325, $invoice->balance); + $this->assertEquals(325, $invoice->client->balance); + + $data = [ + 'amount' => 163.0, + 'client_id' => $this->encodePrimaryKey($client->id), + 'invoices' => [ + [ + 'invoice_id' => $this->encodePrimaryKey($invoice->id), + 'amount' => 163, + ], + ], + 'date' => '2019/12/12', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $token->token, + ])->post('/api/v1/payments/', $data); + + $arr = $response->json(); + $payment_id = $arr['data']['id']; + $payment_1 = Payment::whereId($this->decodePrimaryKey($payment_id))->first(); + + $this->assertEquals(162, $invoice->fresh()->balance); + $this->assertEquals(162, $invoice->client->fresh()->balance); + + + $data = [ + 'amount' => 162.0, + 'client_id' => $this->encodePrimaryKey($client->id), + 'invoices' => [ + [ + 'invoice_id' => $this->encodePrimaryKey($invoice->id), + 'amount' => 162, + ], + ], + 'date' => '2019/12/12', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $token->token, + ])->post('/api/v1/payments/', $data); + + $arr = $response->json(); + $payment_id = $arr['data']['id']; + $payment_2 = Payment::whereId($this->decodePrimaryKey($payment_id))->first(); + + $this->assertEquals(0, $invoice->fresh()->balance); + $this->assertEquals(0, $invoice->client->fresh()->balance); + } +}