mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 03:04:05 +00:00
144 - Testing the Event Listener
This commit is contained in:
26
app/Jobs/ProcessPosterImage.php
Normal file
26
app/Jobs/ProcessPosterImage.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
|
||||
class ProcessPosterImage implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public $concert;
|
||||
|
||||
public function __construct($concert)
|
||||
{
|
||||
$this->concert = $concert;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
18
app/Listeners/SchedulePosterImageProcessing.php
Normal file
18
app/Listeners/SchedulePosterImageProcessing.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\ConcertAdded;
|
||||
use App\Jobs\ProcessPosterImage;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class SchedulePosterImageProcessing
|
||||
{
|
||||
public function handle(ConcertAdded $event)
|
||||
{
|
||||
if ($event->concert->hasPoster()) {
|
||||
ProcessPosterImage::dispatch($event->concert);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,8 @@ class EventServiceProvider extends ServiceProvider
|
||||
* @var array
|
||||
*/
|
||||
protected $listen = [
|
||||
'App\Events\SomeEvent' => [
|
||||
'App\Listeners\EventListener',
|
||||
'App\Events\ConcertAdded' => [
|
||||
'App\Listeners\SchedulePosterImageProcessing',
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
44
tests/Unit/Listeners/SchedulePosterImageProcessingTest.php
Normal file
44
tests/Unit/Listeners/SchedulePosterImageProcessingTest.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Listeners;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Events\ConcertAdded;
|
||||
use App\Jobs\ProcessPosterImage;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class SchedulePosterImageProcessingTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/** @test */
|
||||
function it_queues_a_job_to_process_a_poster_image_if_a_poster_image_is_present()
|
||||
{
|
||||
Queue::fake();
|
||||
|
||||
$concert = \ConcertFactory::createUnpublished([
|
||||
'poster_image_path' => 'posters/example-poster.png',
|
||||
]);
|
||||
|
||||
ConcertAdded::dispatch($concert);
|
||||
|
||||
Queue::assertPushed(ProcessPosterImage::class, function ($job) use ($concert) {
|
||||
return $job->concert->is($concert);
|
||||
});
|
||||
}
|
||||
|
||||
/** @test */
|
||||
function a_job_is_not_queued_if_a_poster_is_not_present()
|
||||
{
|
||||
Queue::fake();
|
||||
|
||||
$concert = \ConcertFactory::createUnpublished([
|
||||
'poster_image_path' => null,
|
||||
]);
|
||||
|
||||
ConcertAdded::dispatch($concert);
|
||||
|
||||
Queue::assertNotPushed(ProcessPosterImage::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user