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

221 lines
6.8 KiB
PHP
Raw Normal View History

2019-04-16 07:28:30 +02:00
<?php
namespace Tests\Feature;
2019-04-24 12:01:40 +02:00
use App\DataMapper\ClientSettings;
use App\DataMapper\CompanySettings;
use App\Factory\InvoiceFactory;
2019-04-17 02:58:23 +02:00
use App\Models\Account;
use App\Models\Client;
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\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
2019-04-24 12:01:40 +02:00
use Illuminate\Support\Facades\Log;
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()
{
factory(\App\Models\Client::class, 1)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c){
factory(\App\Models\ClientContact::class, 1)->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
'is_primary' => 1
]);
factory(\App\Models\ClientContact::class, 1)->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();
factory(\App\Models\Invoice::class, 1)->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
//test that the same request should produce a validation error due
//to duplicate number being used.
$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 */
$invoice = factory(\App\Models\Invoice::class)->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);
info("inside update invoice validator");
info($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);
}
}