1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Fixes for tests (#3262)

* Working on Refunds

* Refund tests

* fixes for tests
This commit is contained in:
David Bomba 2020-01-29 15:25:08 +11:00 committed by GitHub
parent 133e56dd0b
commit 956d4ba12e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 111 additions and 23 deletions

View File

@ -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;

View File

@ -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 true;
}

View File

@ -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;
}

View File

@ -120,12 +120,12 @@ trait Refundable
private function refundPaymentWithInvoices($data)
{
return $this;
}
private function refundPaymentWithInvoicesAndCredits($data)
{
return $this;
}
private function createCreditLineItems()

View File

@ -27,7 +27,7 @@ use Tests\TestCase;
/**
* @test
* @covers App\Models\InvoiceInvitation\InvoiceInvitationFactory
* @covers App\Models\InvoiceInvitation
*/
class InvitationTest extends TestCase

View File

@ -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
}

View File

@ -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;
@ -306,17 +397,14 @@ class RefundTest extends TestCase
])->post('/api/v1/payments/refund', $data);
}catch(ValidationException $e)
{
$message = json_decode($e->validator->getMessageBag(),1);
\Log::error($message);
}
$response->assertStatus(200);
if($response)
$response->assertStatus(302);
}
}

View File

@ -13,7 +13,7 @@ use Tests\TestCase;
/**
* @test
* @covers App\Utils\Traits\MakesReminder
* @covers App\Utils\Traits\MakesReminders
*/
class CheckRemindersTest extends TestCase
{