Tests for events

This commit is contained in:
David Bomba 2020-11-03 21:19:32 +11:00
parent 6eb9f0f6a6
commit 5c13e1681b
6 changed files with 93 additions and 2 deletions

View File

@ -12,6 +12,7 @@
namespace App\Events\Quote;
use App\Models\Company;
use App\Models\Quote;
use Illuminate\Queue\SerializesModels;
class QuoteWasArchived

View File

@ -12,6 +12,7 @@
namespace App\Events\Quote;
use App\Models\Company;
use App\Models\Quote;
use Illuminate\Queue\SerializesModels;
/**

View File

@ -12,6 +12,7 @@
namespace App\Events\Quote;
use App\Models\Company;
use App\Models\Quote;
use Illuminate\Queue\SerializesModels;
/**

View File

@ -12,6 +12,7 @@
namespace App\Events\Quote;
use App\Models\Company;
use App\Models\Quote;
use Illuminate\Queue\SerializesModels;
/**

View File

@ -11,6 +11,8 @@
namespace App\Http\Controllers;
use App\Events\Quote\QuoteWasCreated;
use App\Events\Quote\QuoteWasUpdated;
use App\Factory\CloneInvoiceFactory;
use App\Factory\CloneInvoiceToQuoteFactory;
use App\Factory\CloneQuoteFactory;
@ -31,6 +33,7 @@ use App\Models\Quote;
use App\Repositories\QuoteRepository;
use App\Transformers\InvoiceTransformer;
use App\Transformers\QuoteTransformer;
use App\Utils\Ninja;
use App\Utils\TempFile;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
@ -204,6 +207,8 @@ class QuoteController extends BaseController
$quote = $this->quote_repo->save($request->all(), QuoteFactory::create(auth()->user()->company()->id, auth()->user()->id));
event(new QuoteWasCreated($quote, $quote->company, Ninja::eventVars()));
return $this->itemResponse($quote);
}
@ -378,6 +383,8 @@ class QuoteController extends BaseController
$quote = $this->quote_repo->save($request->all(), $quote);
event(new QuoteWasUpdated($quote, $quote->company, Ninja::eventVars()));
return $this->itemResponse($quote);
}
@ -658,15 +665,26 @@ class QuoteController extends BaseController
}, basename($quote->pdf_file_path()));
//return response()->download(TempFile::path($quote->pdf_file_path()), basename($quote->pdf_file_path()));
break;
case 'restore':
$this->quote_repo->restore($quote);
if (!$bulk)
return $this->listResponse($quote);
break;
case 'archive':
$this->quote_repo->archive($quote);
return $this->listResponse($quote);
if (!$bulk)
return $this->listResponse($quote);
break;
case 'delete':
$this->quote_repo->delete($quote);
return $this->listResponse($quote);
if (!$bulk)
return $this->listResponse($quote);
break;
case 'email':
$quote->service()->sendEmail();
@ -679,6 +697,7 @@ class QuoteController extends BaseController
if (! $bulk) {
return $this->itemResponse($quote);
}
break;
// no break
default:
return response()->json(['message' => "The requested action `{$action}` is not available."], 400);

View File

@ -27,6 +27,11 @@ use App\Events\Payment\PaymentWasCreated;
use App\Events\Payment\PaymentWasDeleted;
use App\Events\Payment\PaymentWasRestored;
use App\Events\Payment\PaymentWasUpdated;
use App\Events\Quote\QuoteWasArchived;
use App\Events\Quote\QuoteWasCreated;
use App\Events\Quote\QuoteWasDeleted;
use App\Events\Quote\QuoteWasRestored;
use App\Events\Quote\QuoteWasUpdated;
use App\Models\Credit;
use App\Models\Design;
use App\Models\Invoice;
@ -59,6 +64,69 @@ class EventTest extends TestCase
$this->makeTestData();
}
public function testQuoteEvents()
{
/* Test fire new invoice */
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'dude',
];
$this->expectsEvents([
QuoteWasCreated::class,
QuoteWasUpdated::class,
QuoteWasArchived::class,
QuoteWasRestored::class,
QuoteWasDeleted::class,
]);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/quotes/', $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/quotes/' . $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/quotes/bulk?action=archive', $data)
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/quotes/bulk?action=restore', $data)
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/quotes/bulk?action=delete', $data)
->assertStatus(200);
}
//@TODO paymentwasvoided
//@TODO paymentwasrefunded