1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/tests/Unit/CreditBalanceTest.php

119 lines
3.2 KiB
PHP
Raw Normal View History

2021-07-02 01:48:11 +02: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-07-02 01:48:11 +02:00
*/
2021-07-02 01:48:11 +02:00
namespace Tests\Unit;
use App\Models\Credit;
use App\Utils\Traits\AppSetup;
2021-07-02 02:06:26 +02:00
use Tests\MockUnitData;
2021-07-02 01:48:11 +02:00
use Tests\TestCase;
/**
* @test
*/
class CreditBalanceTest extends TestCase
{
2021-07-02 02:06:26 +02:00
use MockUnitData;
use AppSetup;
2021-07-02 02:06:26 +02:00
protected function setUp() :void
2021-07-02 01:48:11 +02:00
{
parent::setUp();
Credit::all()->each(function ($credit) {
2021-07-02 12:20:46 +02:00
$credit->forceDelete();
});
2021-07-02 02:06:26 +02:00
$this->makeTestData();
$this->buildCache(true);
2021-07-02 01:48:11 +02:00
}
public function testCreditBalance()
{
$credit = Credit::factory()->create([
2021-07-02 02:06:26 +02:00
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_id' => $this->client->id,
2021-07-02 01:48:11 +02:00
'balance' => 10,
'number' => 'testing-number-01',
'status_id' => Credit::STATUS_SENT,
]);
2021-07-02 02:06:26 +02:00
$this->assertEquals($this->client->service()->getCreditBalance(), 10);
2021-07-02 01:48:11 +02:00
}
public function testExpiredCreditBalance()
{
$credit = Credit::factory()->create([
2021-07-02 02:06:26 +02:00
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_id' => $this->client->id,
2021-07-02 01:48:11 +02:00
'balance' => 10,
'due_date' => now()->addDays(5),
'number' => 'testing-number-02',
'status_id' => Credit::STATUS_SENT,
]);
2021-07-02 02:06:26 +02:00
$this->assertEquals($this->client->service()->getCreditBalance(), 0);
2021-07-02 01:48:11 +02:00
}
2022-08-22 02:27:11 +02:00
public function testCreditDeleteCheckClientBalance()
{
$credit = Credit::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_id' => $this->client->id,
'balance' => 10,
'number' => 'testing-number-01',
'status_id' => Credit::STATUS_SENT,
]);
$credit->client->credit_balance = 10;
$credit->push();
2023-02-16 02:36:09 +01:00
//delete invoice
2022-08-22 02:27:11 +02:00
$data = [
'ids' => [$credit->hashed_id],
];
//restore invoice
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/credits/bulk?action=delete', $data)->assertStatus(200);
$client = $credit->client->fresh();
$this->assertEquals(0, $client->credit_balance);
2023-02-16 02:36:09 +01:00
//restore invoice
2022-08-22 02:27:11 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/credits/bulk?action=restore', $data)->assertStatus(200);
$client = $credit->client->fresh();
$this->assertEquals(10, $client->credit_balance);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/credits/bulk?action=archive', $data)->assertStatus(200);
$client = $credit->client->fresh();
$this->assertEquals(10, $client->credit_balance);
}
2021-07-02 01:48:11 +02:00
}