1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/tests/Feature/EntityPaidToDateTest.php

146 lines
3.7 KiB
PHP
Raw Normal View History

2021-01-24 10:00:45 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
2021-01-24 10:00:45 +01:00
*/
2021-01-24 10:00:45 +01:00
namespace Tests\Feature;
use App\Factory\InvoiceItemFactory;
use App\Models\Client;
use App\Models\Invoice;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Facades\Session;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
*/
class EntityPaidToDateTest extends TestCase
{
use MakesHash;
use DatabaseTransactions;
use MockAccountData;
protected function setUp() :void
2021-01-24 10:00:45 +01:00
{
parent::setUp();
Session::start();
$this->faker = \Faker\Factory::create();
Model::reguard();
$this->makeTestData();
$this->withoutExceptionHandling();
$this->withoutMiddleware(
ThrottleRequests::class
);
}
public function testPaidToDateWithMarkPaidAction()
{
2021-01-24 10:08:57 +01:00
$invoice = $this->bootNewInvoice();
$this->assertEquals($invoice->balance, 0);
$this->assertEquals($invoice->paid_to_date, 0);
$invoice->service()->markSent()->save();
$this->assertEquals($invoice->balance, 20);
$invoice = $invoice->service()->markPaid()->save();
2021-01-24 10:08:57 +01:00
$this->assertEquals($invoice->paid_to_date, 20);
}
public function testPaidToDateWithInvoiceCancellation()
{
$invoice = $this->bootNewInvoice();
$invoice = $invoice->service()->markPaid()->save();
2021-01-24 10:08:57 +01:00
$this->assertEquals(20, $invoice->paid_to_date);
$invoice->service()->handleReversal()->save();
$this->assertEquals(0, $invoice->paid_to_date);
}
private function bootNewInvoice()
{
2021-01-24 10:00:45 +01:00
$data = [
'name' => 'A Nice Client',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/clients', $data);
2021-01-24 10:00:45 +01:00
$response->assertStatus(200);
$arr = $response->json();
$client_hash_id = $arr['data']['id'];
$client = Client::find($this->decodePrimaryKey($client_hash_id));
2021-01-24 10:00:45 +01:00
$this->assertEquals($client->balance, 0);
$this->assertEquals($client->paid_to_date, 0);
//create new invoice.
$line_items = [];
$item = InvoiceItemFactory::create();
$item->quantity = 1;
$item->cost = 10;
$line_items[] = (array) $item;
2021-01-24 10:00:45 +01:00
$item = InvoiceItemFactory::create();
$item->quantity = 1;
$item->cost = 10;
$line_items[] = (array) $item;
2021-01-24 10:00:45 +01:00
$invoice = [
'status_id' => 1,
'number' => '',
'discount' => 0,
'is_amount_discount' => 1,
'po_number' => '3434343',
'public_notes' => 'notes',
'is_deleted' => 0,
'custom_value1' => 0,
'custom_value2' => 0,
'custom_value3' => 0,
'custom_value4' => 0,
'client_id' => $client_hash_id,
'line_items' => (array) $line_items,
2021-01-24 10:00:45 +01:00
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/invoices/', $invoice)
2021-01-24 10:00:45 +01:00
->assertStatus(200);
$arr = $response->json();
2021-01-24 10:00:45 +01:00
$invoice_one_hashed_id = $arr['data']['id'];
2021-01-24 10:08:57 +01:00
return Invoice::find($this->decodePrimaryKey($invoice_one_hashed_id));
2021-01-24 10:00:45 +01:00
}
}