Event Tests

This commit is contained in:
David Bomba 2020-11-03 23:35:05 +11:00
parent 5c13e1681b
commit 14fd4a5af4
7 changed files with 160 additions and 4 deletions

View File

@ -11,6 +11,7 @@
namespace App\Events\Credit; namespace App\Events\Credit;
use App\Models\Company;
use App\Models\Credit; use App\Models\Credit;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;

View File

@ -27,6 +27,7 @@ class CloneQuoteToInvoiceFactory
unset($quote_array['hashed_id']); unset($quote_array['hashed_id']);
unset($quote_array['invoice_id']); unset($quote_array['invoice_id']);
unset($quote_array['id']); unset($quote_array['id']);
unset($quote_array['invitations']);
foreach ($quote_array as $key => $value) { foreach ($quote_array as $key => $value) {
$invoice->{$key} = $value; $invoice->{$key} = $value;

View File

@ -109,8 +109,8 @@ class QuoteController extends Controller
if ($process) { if ($process) {
foreach ($quotes as $quote) { foreach ($quotes as $quote) {
$quote->service()->approve()->save(); $quote->service()->approve(auth()->user())->save();
event(new QuoteWasApproved(auth()->user(), $quote, $quote->company, Ninja::eventVars())); event(new QuoteWasApproved($quote, $quote->company, Ninja::eventVars()));
} }
return redirect() return redirect()

View File

@ -51,7 +51,7 @@ class TaskRepository extends BaseRepository
$task->fill($data); $task->fill($data);
$task->save(); $task->save();
$task->number = empty($task->number) ? $this->getNextTaskNumber($task) : $data['number']; $task->number = empty($task->number) || !array_key_exists('number', $data) ? $this->getNextTaskNumber($task) : $data['number'];
if (isset($data['description'])) { if (isset($data['description'])) {
$task->description = trim($data['description']); $task->description = trim($data['description']);

View File

@ -34,6 +34,7 @@ class ConvertQuote
public function run($quote) public function run($quote)
{ {
$invoice = CloneQuoteToInvoiceFactory::create($quote, $quote->user_id); $invoice = CloneQuoteToInvoiceFactory::create($quote, $quote->user_id);
$invoice = $this->invoice_repo->save([], $invoice); $invoice = $this->invoice_repo->save([], $invoice);
$invoice->fresh(); $invoice->fresh();

View File

@ -11,12 +11,14 @@
namespace App\Services\Quote; namespace App\Services\Quote;
use App\Events\Quote\QuoteWasApproved;
use App\Factory\CloneQuoteToInvoiceFactory; use App\Factory\CloneQuoteToInvoiceFactory;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Quote; use App\Models\Quote;
use App\Repositories\QuoteRepository; use App\Repositories\QuoteRepository;
use App\Services\Quote\CreateInvitations; use App\Services\Quote\CreateInvitations;
use App\Services\Quote\GetQuotePdf; use App\Services\Quote\GetQuotePdf;
use App\Utils\Ninja;
class QuoteService class QuoteService
{ {
@ -108,10 +110,15 @@ class QuoteService
return $this; return $this;
} }
public function approve() :self public function approve($contact = null) :self
{ {
$this->setStatus(Quote::STATUS_APPROVED)->save(); $this->setStatus(Quote::STATUS_APPROVED)->save();
if(!$contact)
$contact = $this->quote->invitations->first()->contact;
event(new QuoteWasApproved($contact, $this->quote, $this->quote->company, Ninja::eventVars()));
$invoice = null; $invoice = null;
if ($this->quote->client->getSetting('auto_convert_quote')) { if ($this->quote->client->getSetting('auto_convert_quote')) {

View File

@ -17,6 +17,11 @@ use App\Events\Client\ClientWasCreated;
use App\Events\Client\ClientWasDeleted; use App\Events\Client\ClientWasDeleted;
use App\Events\Client\ClientWasRestored; use App\Events\Client\ClientWasRestored;
use App\Events\Client\ClientWasUpdated; use App\Events\Client\ClientWasUpdated;
use App\Events\Credit\CreditWasArchived;
use App\Events\Credit\CreditWasCreated;
use App\Events\Credit\CreditWasDeleted;
use App\Events\Credit\CreditWasRestored;
use App\Events\Credit\CreditWasUpdated;
use App\Events\Invoice\InvoiceWasArchived; use App\Events\Invoice\InvoiceWasArchived;
use App\Events\Invoice\InvoiceWasCreated; use App\Events\Invoice\InvoiceWasCreated;
use App\Events\Invoice\InvoiceWasDeleted; use App\Events\Invoice\InvoiceWasDeleted;
@ -27,11 +32,17 @@ use App\Events\Payment\PaymentWasCreated;
use App\Events\Payment\PaymentWasDeleted; use App\Events\Payment\PaymentWasDeleted;
use App\Events\Payment\PaymentWasRestored; use App\Events\Payment\PaymentWasRestored;
use App\Events\Payment\PaymentWasUpdated; use App\Events\Payment\PaymentWasUpdated;
use App\Events\Quote\QuoteWasApproved;
use App\Events\Quote\QuoteWasArchived; use App\Events\Quote\QuoteWasArchived;
use App\Events\Quote\QuoteWasCreated; use App\Events\Quote\QuoteWasCreated;
use App\Events\Quote\QuoteWasDeleted; use App\Events\Quote\QuoteWasDeleted;
use App\Events\Quote\QuoteWasRestored; use App\Events\Quote\QuoteWasRestored;
use App\Events\Quote\QuoteWasUpdated; use App\Events\Quote\QuoteWasUpdated;
use App\Events\Task\TaskWasArchived;
use App\Events\Task\TaskWasCreated;
use App\Events\Task\TaskWasDeleted;
use App\Events\Task\TaskWasRestored;
use App\Events\Task\TaskWasUpdated;
use App\Models\Credit; use App\Models\Credit;
use App\Models\Design; use App\Models\Design;
use App\Models\Invoice; use App\Models\Invoice;
@ -64,6 +75,130 @@ class EventTest extends TestCase
$this->makeTestData(); $this->makeTestData();
} }
public function testTaskEvents()
{
/* Test fire new invoice */
$data = [
'client_id' => $this->client->hashed_id,
'description' => 'dude',
];
$this->expectsEvents([
TaskWasCreated::class,
TaskWasUpdated::class,
TaskWasArchived::class,
TaskWasRestored::class,
TaskWasDeleted::class,
]);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/tasks/', $data)
->assertStatus(200);
$arr = $response->json();
$data = [
'client_id' => $this->client->hashed_id,
'description' => 'dude2',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/tasks/' . $arr['data']['id'], $data)
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/tasks/bulk?action=archive', $data)
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/tasks/bulk?action=restore', $data)
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/tasks/bulk?action=delete', $data)
->assertStatus(200);
}
public function testCreditEvents()
{
/* Test fire new invoice */
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'dude',
];
$this->expectsEvents([
CreditWasCreated::class,
CreditWasUpdated::class,
CreditWasArchived::class,
CreditWasRestored::class,
CreditWasDeleted::class,
]);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/credits/', $data)
->assertStatus(200);
$arr = $response->json();
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'dude2',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/credits/' . $arr['data']['id'], $data)
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
$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);
$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);
$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);
}
public function testQuoteEvents() public function testQuoteEvents()
{ {
@ -79,6 +214,7 @@ class EventTest extends TestCase
QuoteWasArchived::class, QuoteWasArchived::class,
QuoteWasRestored::class, QuoteWasRestored::class,
QuoteWasDeleted::class, QuoteWasDeleted::class,
QuoteWasApproved::class,
]); ]);
$response = $this->withHeaders([ $response = $this->withHeaders([
@ -106,6 +242,10 @@ class EventTest extends TestCase
'ids' => [$arr['data']['id']], 'ids' => [$arr['data']['id']],
]; ];
$quote = Quote::find($this->decodePrimaryKey($arr['data']['id']));
$quote->status_id = Quote::STATUS_SENT;
$quote->save();
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'), 'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token, 'X-API-TOKEN' => $this->token,
@ -118,6 +258,12 @@ class EventTest extends TestCase
])->post('/api/v1/quotes/bulk?action=restore', $data) ])->post('/api/v1/quotes/bulk?action=restore', $data)
->assertStatus(200); ->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/quotes/bulk?action=approve', $data)
->assertStatus(200);
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'), 'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token, 'X-API-TOKEN' => $this->token,