mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Invoice API tests scaffolding
This commit is contained in:
parent
cb7eea0e7b
commit
82d573d759
@ -318,6 +318,12 @@ class InvoiceCalc
|
||||
$this->balance = $value;
|
||||
}
|
||||
|
||||
public function getInvoice()
|
||||
{
|
||||
//todo build invoice values here and return Invoice
|
||||
|
||||
return $this->invoice;
|
||||
}
|
||||
/*
|
||||
private function setDiscount($amount, $discount, $is_amount_discount)
|
||||
{
|
||||
|
@ -101,8 +101,7 @@ class ClientController extends BaseController
|
||||
*/
|
||||
public function update(UpdateClientRequest $request, Client $client)
|
||||
{
|
||||
|
||||
$client = UpdateClient::dispatchNow($request, $client);
|
||||
$client = $this->clientRepo->save($request, $client);
|
||||
|
||||
return $this->itemResponse($client);
|
||||
|
||||
@ -129,8 +128,9 @@ class ClientController extends BaseController
|
||||
*/
|
||||
public function store(StoreClientRequest $request)
|
||||
{
|
||||
|
||||
$client = $this->clientRepo->save($request, ClientFactory::create(auth()->user()->company()->id, auth()->user()->id));
|
||||
|
||||
$client = StoreClient::dispatchNow($request, ClientFactory::create(auth()->user()->company()->id, auth()->user()->id));
|
||||
$client->load('contacts', 'primary_contact');
|
||||
|
||||
return $this->itemResponse($client);
|
||||
@ -145,6 +145,7 @@ class ClientController extends BaseController
|
||||
*/
|
||||
public function destroy(DestroyClientRequest $request, Client $client)
|
||||
{
|
||||
//may not need these destroy routes as we are using actions to 'archive/delete'
|
||||
$client->delete();
|
||||
|
||||
return response()->json([], 200);
|
||||
|
@ -78,7 +78,8 @@ class InvoiceController extends BaseController
|
||||
public function store(StoreInvoiceRequest $request)
|
||||
{
|
||||
|
||||
$invoice = $this->invoice_repo->save();
|
||||
$invoice = $this->invoice_repo->save($request, InvoiceFactory::create(auth()->user()->company()->id, auth()->user()->id));
|
||||
|
||||
return $this->itemResponse($invoice);
|
||||
|
||||
}
|
||||
@ -89,9 +90,11 @@ class InvoiceController extends BaseController
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show(ShowInvoiceRequest $request)
|
||||
public function show(ShowInvoiceRequest $request, Invoice $invoice)
|
||||
{
|
||||
//
|
||||
|
||||
return $this->itemResponse($invoice);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -100,9 +103,11 @@ class InvoiceController extends BaseController
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit(EditInvoiceRequest)
|
||||
public function edit(EditInvoiceRequest, Invoice $invoice)
|
||||
{
|
||||
//
|
||||
|
||||
return $this->itemResponse($invoice);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,9 +117,13 @@ class InvoiceController extends BaseController
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(UpdateInvoiceRequest $request)
|
||||
public function update(UpdateInvoiceRequest $request, Invoice $invoice)
|
||||
{
|
||||
//
|
||||
|
||||
$invoice = $this->invoice_repo->save($request, $invoice);
|
||||
|
||||
return $this->itemResponse($invoice);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -125,6 +134,10 @@ class InvoiceController extends BaseController
|
||||
*/
|
||||
public function destroy(DestroyInvoiceRequest $request, Invoice $invoice)
|
||||
{
|
||||
//
|
||||
|
||||
$invoice->delete();
|
||||
|
||||
return response()->json([], 200);
|
||||
|
||||
}
|
||||
}
|
||||
|
21
app/Http/Requests/Client/DestroyClientRequest.php
Normal file
21
app/Http/Requests/Client/DestroyClientRequest.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Client;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
use App\Models\Client;
|
||||
|
||||
class DestroyClientRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function authorize() : bool
|
||||
{
|
||||
return auth()->user()->can('edit', $this->client);
|
||||
}
|
||||
|
||||
}
|
@ -14,10 +14,9 @@ class UpdateClientRequest extends Request
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function authorize()
|
||||
public function authorize() : bool
|
||||
{
|
||||
return true;
|
||||
// return ! auth()->user(); //todo permissions
|
||||
return auth()->user()->can('edit', $this->client);
|
||||
}
|
||||
|
||||
public function rules()
|
||||
|
21
app/Http/Requests/Invoice/DestroyInvoiceRequest.php
Normal file
21
app/Http/Requests/Invoice/DestroyInvoiceRequest.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Invoice;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
use App\Models\Invoice;
|
||||
|
||||
class DestroyInvoiceRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function authorize() : bool
|
||||
{
|
||||
return auth()->user()->can('edit', $this->invoice);
|
||||
}
|
||||
|
||||
}
|
@ -24,6 +24,11 @@ class StoreInvoiceRequest extends Request
|
||||
|
||||
}
|
||||
|
||||
public function sanitize()
|
||||
{
|
||||
//do post processing of invoice request here, ie. invoice_items
|
||||
}
|
||||
|
||||
public function messages()
|
||||
{
|
||||
|
||||
|
@ -14,14 +14,31 @@ class UpdateInvoiceRequest extends Request
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function authorize()
|
||||
public function authorize() : bool
|
||||
{
|
||||
return true;
|
||||
// return ! auth()->user(); //todo permissions
|
||||
return auth()->user()->can('edit', $this->invoice);
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
if (! $this->entity()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$invoiceId = $this->entity()->id;
|
||||
|
||||
$rules = [
|
||||
'client' => 'required',
|
||||
'discount' => 'positive',
|
||||
'invoice_date' => 'required',
|
||||
];
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
public function sanitize()
|
||||
{
|
||||
//do post processing of invoice request here, ie. invoice_items
|
||||
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,22 @@ use Illuminate\Support\Facades\Log;
|
||||
class ClientRepository extends BaseRepository
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ClientContactRepository
|
||||
*/
|
||||
protected $contactRepo;
|
||||
|
||||
/**
|
||||
* ClientController constructor.
|
||||
* @param ClientContactRepository $contactRepo
|
||||
*/
|
||||
public function __construct(ClientContactRepository $contactRepo)
|
||||
{
|
||||
|
||||
$this->contactRepo = $contactRepo;
|
||||
|
||||
}
|
||||
|
||||
public function getClassName()
|
||||
{
|
||||
return Client::class;
|
||||
@ -23,6 +39,8 @@ class ClientRepository extends BaseRepository
|
||||
$client->fill($request->input());
|
||||
$client->save();
|
||||
|
||||
$contacts = $this->contactRepo->save($request->input('contacts'), $client);
|
||||
|
||||
return $client;
|
||||
}
|
||||
|
||||
|
@ -21,10 +21,17 @@ class InvoiceRepository extends BaseRepository
|
||||
public function save(Request $request, Invoice $invoice) : ?Invoice
|
||||
{
|
||||
$invoice->fill($request->input());
|
||||
|
||||
$invoice->save();
|
||||
|
||||
|
||||
$invoice_calc = new InvoiceCalc($invoice, $invoice->settings);
|
||||
$invoice_calc = new InvoiceCalc($invoice);
|
||||
|
||||
$invoice = $invoice_calc->build()->getInvoice();
|
||||
|
||||
//fire events here that cascading from the saving of an invoice
|
||||
//ie. client balance update...
|
||||
|
||||
return $invoice;
|
||||
}
|
||||
|
||||
|
@ -111,7 +111,7 @@ class ClientTest extends TestCase
|
||||
$this->assertNotNull($company);
|
||||
$this->assertNotNull($user->tokens->first()->company);
|
||||
|
||||
factory(\App\Models\Client::class, 5)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){
|
||||
factory(\App\Models\Client::class, 3)->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,
|
||||
|
30
tests/Feature/InvoiceTest.php
Normal file
30
tests/Feature/InvoiceTest.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
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;
|
||||
|
||||
class InvoiceTest extends TestCase
|
||||
{
|
||||
|
||||
use MakesHash;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
|
||||
parent::setUp();
|
||||
|
||||
Session::start();
|
||||
|
||||
$this->faker = \Faker\Factory::create();
|
||||
|
||||
Model::reguard();
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user