From 956d4ba12e647f7eb36ba5e2d5726e23934a0f06 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 29 Jan 2020 15:25:08 +1100 Subject: [PATCH] Fixes for tests (#3262) * Working on Refunds * Refund tests * fixes for tests --- .../Payment/ValidRefundableRequest.php | 4 +- .../ValidRefundableInvoices.php | 8 +- app/Models/Invoice.php | 10 +- app/Utils/Traits/Payment/Refundable.php | 4 +- tests/Feature/InvitationTest.php | 2 +- tests/Feature/InvoiceEmailTest.php | 2 +- tests/Feature/RefundTest.php | 102 ++++++++++++++++-- tests/Integration/CheckRemindersTest.php | 2 +- 8 files changed, 111 insertions(+), 23 deletions(-) diff --git a/app/Http/ValidationRules/Payment/ValidRefundableRequest.php b/app/Http/ValidationRules/Payment/ValidRefundableRequest.php index 13791b49cd3a..72e90cd6d425 100644 --- a/app/Http/ValidationRules/Payment/ValidRefundableRequest.php +++ b/app/Http/ValidationRules/Payment/ValidRefundableRequest.php @@ -141,7 +141,7 @@ class ValidRefundableRequest implements Rule $refundable_amount = ($paymentable->pivot->amount - $paymentable->pivot->refunded); - if($request_invoice['amount'] > $refundable_amount){ + if($request_invoice['refunded'] > $refundable_amount){ $invoice = $paymentable->paymentable; @@ -176,7 +176,7 @@ class ValidRefundableRequest implements Rule $refundable_amount = ($paymentable->pivot->amount - $paymentable->pivot->refunded); - if($request_invoice['amount'] > $refundable_amount){ + if($request_credit['refunded'] > $refundable_amount){ $credit = $paymentable->paymentable; diff --git a/app/Http/ValidationRules/ValidRefundableInvoices.php b/app/Http/ValidationRules/ValidRefundableInvoices.php index d24525b46d20..940ed86dc329 100644 --- a/app/Http/ValidationRules/ValidRefundableInvoices.php +++ b/app/Http/ValidationRules/ValidRefundableInvoices.php @@ -60,17 +60,15 @@ class ValidRefundableInvoices implements Rule foreach ($value as $val) { if ($val['invoice_id'] == $invoice->id) { - if($val['refunded'] > ($invoice->amount - $invoice->balance)) + if($val['refunded'] > ($invoice->amount - $invoice->balance)){ $this->error_msg = "Attempting to refund more than is possible for an invoice"; - return false; - + return false; + } } } } - - return true; } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 22ea2d6a090d..7d2be3d07705 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -233,11 +233,13 @@ class Invoice extends BaseModel public function isRefundable() : bool { - if($this->is_deleted){ - return false; - } elseif ($this->balance <= 0) - return false; + // if($this->is_deleted){ + // return false; + // } elseif ($this->balance <= 0) + // return false; + if($this->is_deleted) + return false; return true; } diff --git a/app/Utils/Traits/Payment/Refundable.php b/app/Utils/Traits/Payment/Refundable.php index a6b624f22c8b..477f1819b795 100644 --- a/app/Utils/Traits/Payment/Refundable.php +++ b/app/Utils/Traits/Payment/Refundable.php @@ -120,12 +120,12 @@ trait Refundable private function refundPaymentWithInvoices($data) { - + return $this; } private function refundPaymentWithInvoicesAndCredits($data) { - + return $this; } private function createCreditLineItems() diff --git a/tests/Feature/InvitationTest.php b/tests/Feature/InvitationTest.php index bc54775b2ef6..657aa75c4587 100644 --- a/tests/Feature/InvitationTest.php +++ b/tests/Feature/InvitationTest.php @@ -27,7 +27,7 @@ use Tests\TestCase; /** * @test - * @covers App\Models\InvoiceInvitation\InvoiceInvitationFactory + * @covers App\Models\InvoiceInvitation */ class InvitationTest extends TestCase diff --git a/tests/Feature/InvoiceEmailTest.php b/tests/Feature/InvoiceEmailTest.php index a30f53f6576a..3f7851430cd0 100644 --- a/tests/Feature/InvoiceEmailTest.php +++ b/tests/Feature/InvoiceEmailTest.php @@ -79,7 +79,7 @@ class InvoiceEmailTest extends TestCase //fire any events - sleep(5);//here to cope with mailtrap time delays + //sleep(5);//here to cope with mailtrap time delays } diff --git a/tests/Feature/RefundTest.php b/tests/Feature/RefundTest.php index 3b3355454c81..c5a6d0c4007b 100644 --- a/tests/Feature/RefundTest.php +++ b/tests/Feature/RefundTest.php @@ -242,7 +242,7 @@ class RefundTest extends TestCase $this->invoice->status_id = Invoice::STATUS_SENT; $this->invoice->line_items = $this->buildLineItems(); - $this->invoice->uses_inclusive_Taxes = false; + $this->invoice->uses_inclusive_taxes = false; $this->invoice->save(); @@ -288,6 +288,50 @@ class RefundTest extends TestCase $data = [ 'id' => $this->encodePrimaryKey($payment->id), 'refunded' => 50, + 'invoices' => [ + [ + 'invoice_id' => $this->invoice->hashed_id, + 'refunded' => $this->invoice->amount + ], + ], + 'date' => '2020/12/12', + ]; + + $response = false; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/payments/refund', $data); + + $response->assertStatus(200); + + } + + + public function testRefundValidationWithInValidInvoiceProvided() + { + $client = ClientFactory::create($this->company->id, $this->user->id); + $client->save(); + + $this->invoice = InvoiceFactory::create($this->company->id,$this->user->id);//stub the company and user_id + $this->invoice->client_id = $client->id; + $this->invoice->status_id = Invoice::STATUS_SENT; + + $this->invoice->line_items = $this->buildLineItems(); + $this->invoice->uses_inclusive_taxes = false; + + $this->invoice->save(); + + $this->invoice_calc = new InvoiceSum($this->invoice); + $this->invoice_calc->build(); + + $this->invoice = $this->invoice_calc->getInvoice(); + $this->invoice->save(); + + $data = [ + 'amount' => 50, + 'client_id' => $client->hashed_id, 'invoices' => [ [ 'invoice_id' => $this->invoice->hashed_id, @@ -295,6 +339,53 @@ class RefundTest extends TestCase ], ], 'date' => '2020/12/12', + + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/payments', $data); + + + $arr = $response->json(); + $response->assertStatus(200); + + $payment_id = $arr['data']['id']; + + $this->assertEquals(50, $arr['data']['amount']); + + $payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first(); + + $this->assertNotNull($payment); + $this->assertNotNull($payment->invoices()); + $this->assertEquals(1, $payment->invoices()->count()); + + $this->invoice = InvoiceFactory::create($this->company->id,$this->user->id);//stub the company and user_id + $this->invoice->client_id = $client->id; + $this->invoice->status_id = Invoice::STATUS_SENT; + + $this->invoice->line_items = $this->buildLineItems(); + $this->invoice->uses_inclusive_taxes = false; + + $this->invoice->save(); + + $this->invoice_calc = new InvoiceSum($this->invoice); + $this->invoice_calc->build(); + + $this->invoice = $this->invoice_calc->getInvoice(); + $this->invoice->save(); + + $data = [ + 'id' => $this->encodePrimaryKey($payment->id), + 'refunded' => 50, + 'invoices' => [ + [ + 'invoice_id' => $this->invoice->hashed_id, + 'refunded' => $this->invoice->amount + ], + ], + 'date' => '2020/12/12', ]; $response = false; @@ -304,19 +395,16 @@ class RefundTest extends TestCase 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, ])->post('/api/v1/payments/refund', $data); - }catch( ValidationException $e) + }catch(ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(),1); \Log::error($message); } - $response->assertStatus(200); + if($response) + $response->assertStatus(302); } - - - } diff --git a/tests/Integration/CheckRemindersTest.php b/tests/Integration/CheckRemindersTest.php index ca3eaa270ed5..4a17d18bef61 100644 --- a/tests/Integration/CheckRemindersTest.php +++ b/tests/Integration/CheckRemindersTest.php @@ -13,7 +13,7 @@ use Tests\TestCase; /** * @test - * @covers App\Utils\Traits\MakesReminder + * @covers App\Utils\Traits\MakesReminders */ class CheckRemindersTest extends TestCase {