Skip to content

Commit

Permalink
event dispatcher test
Browse files Browse the repository at this point in the history
  • Loading branch information
tattali committed Dec 15, 2019
1 parent 10178f3 commit c2310a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Controller/CalendarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function loadAction(Request $request): Response
return $response;
}

public function dispatchWithBC($event, string $eventName)
public function dispatchWithBC($event, ?string $eventName = null)
{
if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface) {
return $this->eventDispatcher->dispatch($event, $eventName);
Expand Down
42 changes: 13 additions & 29 deletions tests/Controller/CalendarControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;

class CalendarControllerTest extends TestCase
{
Expand Down Expand Up @@ -48,18 +47,7 @@ public function testItProvidesAnEventsFeedForACalendar()

$this->calendarEvent->getEvents()->willReturn([$this->event]);

$dispatcher = $this->getEventDispatcherMock();
if (Kernel::VERSION_ID >= 40300) {
$this->eventDispatcher
->dispatch(Argument::type(CalendarEvent::class), CalendarEvents::SET_DATA)
->willReturn($this->calendarEvent)
;
} else {
$this->eventDispatcher
->dispatch(CalendarEvents::SET_DATA, Argument::type(CalendarEvent::class))
->willReturn($this->calendarEvent)
;
}
$this->eventDispatcherWithBC(Argument::type(CalendarEvent::class), CalendarEvents::SET_DATA);

$data = json_encode([
[
Expand Down Expand Up @@ -95,18 +83,7 @@ public function testItNotFindAnyEvents()
$this->request->get('filters', '{}')->willReturn('{}');

$this->calendarEvent->getEvents()->willReturn([$this->event]);
$dispatcher = $this->getEventDispatcherMock();
if ($dispatcher instanceof ContractsEventDispatcherInterface) {
$this->eventDispatcher
->dispatch(Argument::type(CalendarEvent::class), CalendarEvents::SET_DATA)
->willReturn($this->calendarEvent)
;
} else {
$this->eventDispatcher
->dispatch(CalendarEvents::SET_DATA, Argument::type(CalendarEvent::class))
->willReturn($this->calendarEvent)
;
}
$this->eventDispatcherWithBC(Argument::type(CalendarEvent::class), CalendarEvents::SET_DATA);

$data = '';

Expand All @@ -123,10 +100,17 @@ public function testItNotFindAnyEvents()
$this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode());
}

private function getEventDispatcherMock()
private function eventDispatcherWithBC($event, ?string $eventName = null)
{
return $this->getMockBuilder(EventDispatcherInterface::class)
->disableOriginalConstructor()
->getMock();
if (Kernel::VERSION_ID >= 40300) {
$this->eventDispatcher
->dispatch($event, $eventName)
->willReturn($this->calendarEvent)
;
}
$this->eventDispatcher
->dispatch($eventName, $event)
->willReturn($this->calendarEvent)
;
}
}

0 comments on commit c2310a9

Please sign in to comment.