Skip to content

Commit

Permalink
Provide a SlurpPayload when the Load Aborted Event is triggered.
Browse files Browse the repository at this point in the history
  • Loading branch information
courtney-miles committed Mar 26, 2019
1 parent 57658e4 commit f0087e9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/Event/LoadAbortedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,29 @@

namespace MilesAsylum\Slurp\Event;

use MilesAsylum\Slurp\SlurpPayload;
use Symfony\Component\EventDispatcher\Event;

class LoadAbortedEvent extends Event
{
/**
* @var SlurpPayload|null
*/
private $payload;

public const NAME = 'slurp.load.aborted';

public function __construct(SlurpPayload $payload = null)
{
$this->payload = $payload;
}

/**
* The payload that caused the load to be aborted.
* @return SlurpPayload|null
*/
public function getPayload():? SlurpPayload
{
return $this->payload;
}
}
2 changes: 1 addition & 1 deletion src/InnerPipeline/LoadStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __invoke(SlurpPayload $payload): SlurpPayload
$this->loader->abort();
$this->loadAborted = true;
$payload->setLoadAborted($this->loadAborted);
$this->dispatch(LoadAbortedEvent::NAME, new LoadAbortedEvent());
$this->dispatch(LoadAbortedEvent::NAME, new LoadAbortedEvent($payload));
} else {
$this->loader->loadValues($payload->getRecord());
$this->dispatch(RecordLoadedEvent::NAME, new RecordLoadedEvent($payload));
Expand Down
30 changes: 30 additions & 0 deletions tests/Slurp/Event/LoadAbortedEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Author: Courtney Miles
* Date: 26/03/19
* Time: 5:18 PM
*/

namespace MilesAsylum\Slurp\Tests\Slurp\Event;

use MilesAsylum\Slurp\Event\LoadAbortedEvent;
use MilesAsylum\Slurp\SlurpPayload;
use PHPUnit\Framework\TestCase;

class LoadAbortedEventTest extends TestCase
{
public function testGetPayloadDefaultValue()
{
$event = new LoadAbortedEvent();

$this->assertNull($event->getPayload());
}

public function testGetPayload()
{
$mockPayload = \Mockery::mock(SlurpPayload::class);
$event = new LoadAbortedEvent($mockPayload);

$this->assertSame($mockPayload, $event->getPayload());
}
}
19 changes: 17 additions & 2 deletions tests/Slurp/InnerPipeline/LoadStageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,29 @@ public function testDispatchEventOnLoad()

public function testDispatchLoadAbortedEventOnInvalidRecord()
{
/** @var LoadAbortedEvent $event */
$event = null;
$mockPayload = $this->createMockPayload(['foo'], true);
$mockDispatcher = \Mockery::mock(EventDispatcherInterface::class);
$mockDispatcher->shouldReceive('dispatch')
->with(LoadAbortedEvent::NAME, \Mockery::type(LoadAbortedEvent::class))
->once();
->with(
LoadAbortedEvent::NAME,
\Mockery::on(
function ($arg) use (&$event) {
if (!$arg instanceof LoadAbortedEvent) {
return false;
}

$event = $arg;

return true;
}
)
)->once();
$this->stage->setEventDispatcher($mockDispatcher);

($this->stage)($mockPayload);
$this->assertSame($mockPayload, $event->getPayload());
}

/**
Expand Down

0 comments on commit f0087e9

Please sign in to comment.