1<?php 2 3/* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Symfony\Component\EventDispatcher\Tests; 13 14use PHPUnit\Framework\MockObject\MockObject; 15use PHPUnit\Framework\TestCase; 16use Symfony\Component\EventDispatcher\Event; 17use Symfony\Component\EventDispatcher\ImmutableEventDispatcher; 18 19/** 20 * @author Bernhard Schussek <bschussek@gmail.com> 21 */ 22class ImmutableEventDispatcherTest extends TestCase 23{ 24 /** 25 * @var MockObject 26 */ 27 private $innerDispatcher; 28 29 /** 30 * @var ImmutableEventDispatcher 31 */ 32 private $dispatcher; 33 34 protected function setUp() 35 { 36 $this->innerDispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); 37 $this->dispatcher = new ImmutableEventDispatcher($this->innerDispatcher); 38 } 39 40 public function testDispatchDelegates() 41 { 42 $event = new Event(); 43 $resultEvent = new Event(); 44 45 $this->innerDispatcher->expects($this->once()) 46 ->method('dispatch') 47 ->with('event', $event) 48 ->willReturn($resultEvent); 49 50 $this->assertSame($resultEvent, $this->dispatcher->dispatch('event', $event)); 51 } 52 53 public function testGetListenersDelegates() 54 { 55 $this->innerDispatcher->expects($this->once()) 56 ->method('getListeners') 57 ->with('event') 58 ->willReturn(['result']); 59 60 $this->assertSame(['result'], $this->dispatcher->getListeners('event')); 61 } 62 63 public function testHasListenersDelegates() 64 { 65 $this->innerDispatcher->expects($this->once()) 66 ->method('hasListeners') 67 ->with('event') 68 ->willReturn(true); 69 70 $this->assertTrue($this->dispatcher->hasListeners('event')); 71 } 72 73 public function testAddListenerDisallowed() 74 { 75 $this->expectException('\BadMethodCallException'); 76 $this->dispatcher->addListener('event', function () { return 'foo'; }); 77 } 78 79 public function testAddSubscriberDisallowed() 80 { 81 $this->expectException('\BadMethodCallException'); 82 $subscriber = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventSubscriberInterface')->getMock(); 83 84 $this->dispatcher->addSubscriber($subscriber); 85 } 86 87 public function testRemoveListenerDisallowed() 88 { 89 $this->expectException('\BadMethodCallException'); 90 $this->dispatcher->removeListener('event', function () { return 'foo'; }); 91 } 92 93 public function testRemoveSubscriberDisallowed() 94 { 95 $this->expectException('\BadMethodCallException'); 96 $subscriber = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventSubscriberInterface')->getMock(); 97 98 $this->dispatcher->removeSubscriber($subscriber); 99 } 100} 101