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

191 lines
5.8 KiB
PHP
Raw Normal View History

2019-04-16 07:28:30 +02:00
<?php
namespace Tests\Feature;
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;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Session;
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;
public function setUp()
{
2019-04-17 02:58:23 +02:00
2019-04-16 07:28:30 +02:00
parent::setUp();
Session::start();
$this->faker = \Faker\Factory::create();
Model::reguard();
2019-04-17 02:58:23 +02:00
}
public function testInvoiceList()
{
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'email' => $this->faker->unique()->safeEmail,
'password' => 'ALongAndBrilliantPassword123',
'_token' => csrf_token(),
'privacy_policy' => 1,
'terms_of_service' => 1
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
])->post('/api/v1/signup', $data);
$acc = $response->json();
2019-04-18 13:57:22 +02:00
$account = Account::find($this->decodePrimaryKey($acc['data']['id']));
2019-04-17 02:58:23 +02:00
$company_token = $account->default_company->tokens()->first();
$token = $company_token->token;
$company = $company_token->company;
$user = $company_token->user;
$this->assertNotNull($company_token);
$this->assertNotNull($token);
$this->assertNotNull($user);
$this->assertNotNull($company);
$this->assertNotNull($user->tokens->first()->company);
factory(\App\Models\Client::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){
factory(\App\Models\ClientContact::class,1)->create([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id,
'is_primary' => 1
]);
factory(\App\Models\ClientContact::class,1)->create([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id
]);
});
$client = Client::all()->first();
factory(\App\Models\Invoice::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]);
2019-04-17 02:58:23 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->get('/api/v1/invoices');
$response->assertStatus(200);
2019-04-16 07:28:30 +02:00
}
public function testInvoiceRESTEndPoints()
{
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'email' => $this->faker->unique()->safeEmail,
'password' => 'ALongAndBrilliantPassword123',
'_token' => csrf_token(),
'privacy_policy' => 1,
'terms_of_service' => 1
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
])->post('/api/v1/signup', $data);
$acc = $response->json();
2019-04-18 13:57:22 +02:00
$account = Account::find($this->decodePrimaryKey($acc['data']['id']));
$company_token = $account->default_company->tokens()->first();
$token = $company_token->token;
$company = $company_token->company;
$user = $company_token->user;
$this->assertNotNull($company_token);
$this->assertNotNull($token);
$this->assertNotNull($user);
$this->assertNotNull($company);
$this->assertNotNull($user->tokens->first()->company);
factory(\App\Models\Client::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){
factory(\App\Models\ClientContact::class,1)->create([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id,
'is_primary' => 1
]);
factory(\App\Models\ClientContact::class,1)->create([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id
]);
});
$client = Client::all()->first();
factory(\App\Models\Invoice::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]);
$invoice = Invoice::where('user_id',$user->id)->first();
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->get('/api/v1/invoices/'.$this->encodePrimaryKey($invoice->id));
$response->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->get('/api/v1/invoices/'.$this->encodePrimaryKey($invoice->id).'/edit');
$response->assertStatus(200);
$invoice_update = [
'invoice_status_id' => Invoice::STATUS_PAID
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->put('/api/v1/invoices/'.$this->encodePrimaryKey($invoice->id), $invoice_update)
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->delete('/api/v1/invoices/'.$this->encodePrimaryKey($invoice->id));
$response->assertStatus(200);
}
2019-04-16 07:28:30 +02:00
}