1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-18 00:53:10 +01:00
invoiceninja/tests/Feature/InvoiceTest.php

224 lines
6.9 KiB
PHP
Raw Normal View History

2019-04-16 07:28:30 +02:00
<?php
2020-09-14 13:11:46 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-09-14 13:11:46 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
2019-04-16 07:28:30 +02:00
namespace Tests\Feature;
2019-04-17 02:58:23 +02:00
use App\Models\Client;
2020-10-01 13:34:05 +02:00
use App\Models\ClientContact;
use App\Models\Invoice;
2019-04-16 07:28:30 +02:00
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
2019-04-24 12:01:40 +02:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2019-04-16 07:28:30 +02:00
use Illuminate\Support\Facades\Session;
use Tests\MockAccountData;
2019-04-16 07:28:30 +02:00
use Tests\TestCase;
2019-04-20 01:02:49 +02:00
/**
* @test
* @covers App\Http\Controllers\InvoiceController
*/
2019-04-16 07:28:30 +02:00
class InvoiceTest extends TestCase
{
use MakesHash;
2019-04-24 12:01:40 +02:00
use DatabaseTransactions;
use MockAccountData;
2019-04-16 07:28:30 +02:00
2019-04-24 12:01:40 +02:00
public function setUp() :void
2019-04-16 07:28:30 +02:00
{
parent::setUp();
Session::start();
$this->faker = \Faker\Factory::create();
Model::reguard();
$this->makeTestData();
2019-04-17 02:58:23 +02:00
}
public function testInvoiceList()
{
2020-10-01 13:34:05 +02:00
Client::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) {
ClientContact::factory()->create([
'user_id' => $this->user->id,
2019-04-17 02:58:23 +02:00
'client_id' => $c->id,
'company_id' => $this->company->id,
'is_primary' => 1,
2019-04-17 02:58:23 +02:00
]);
2020-10-01 13:34:05 +02:00
ClientContact::factory()->create([
'user_id' => $this->user->id,
2019-04-17 02:58:23 +02:00
'client_id' => $c->id,
'company_id' => $this->company->id,
2019-04-17 02:58:23 +02:00
]);
});
2019-04-17 02:58:23 +02:00
$client = Client::all()->first();
2020-10-01 13:34:05 +02:00
Invoice::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]);
2019-04-17 02:58:23 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2019-04-17 02:58:23 +02:00
])->get('/api/v1/invoices');
$response->assertStatus(200);
2019-04-16 07:28:30 +02:00
}
public function testInvoiceRESTEndPoints()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id));
$response->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id).'/edit');
$response->assertStatus(200);
$invoice_update = [
'tax_name1' => 'dippy',
];
$this->assertNotNull($this->invoice);
2019-10-07 11:39:22 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id), $invoice_update)
->assertStatus(200);
}
public function testPostNewInvoice()
{
$invoice = [
'status_id' => 1,
'number' => 'dfdfd',
'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,
'status' => 1,
'client_id' => $this->encodePrimaryKey($this->client->id),
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/invoices/', $invoice)
->assertStatus(200);
2020-07-25 01:02:32 +02:00
$arr = $response->json();
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/invoices/'.$arr['data']['id'], $invoice)
->assertStatus(200);
2020-07-25 01:02:32 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/invoices/', $invoice)
->assertStatus(302);
}
public function testDeleteInvoice()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->delete('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id));
$response->assertStatus(200);
}
public function testUniqueNumberValidation()
{
/* stub a invoice in the DB that we will use to test against later */
2020-10-01 13:34:05 +02:00
$invoice = Invoice::factory()->create([
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'company_id' => $this->company->id,
'number' => 'test',
]);
/* Test fire new invoice */
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'dude',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/invoices/', $data)
->assertStatus(200);
$arr = $response->json();
$this->assertEquals('dude', $arr['data']['number']);
/*test validation fires*/
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'test',
];
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/invoices/'.$arr['data']['id'], $data)
->assertStatus(302);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
2021-01-14 05:31:45 +01:00
// nlog('inside update invoice validator');
// nlog($message);
$this->assertNotNull($message);
}
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'style',
];
/* test number passed validation*/
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/invoices/'.$arr['data']['id'], $data)
->assertStatus(200);
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'style',
];
/* Make sure we can UPDATE using the same number*/
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/invoices/'.$arr['data']['id'], $data)
->assertStatus(200);
}
}