Overview

Namespaces

  • Composer
    • Autoload
  • Guzzle
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
        • Header
      • QueryAggregator
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Mock
    • Stream
  • Mockery
    • Adapter
      • Phpunit
    • CountValidator
    • Exception
    • Generator
      • StringManipulation
        • Pass
    • Loader
    • Matcher
  • None
  • Omnipay
    • Common
      • Exception
      • Message
    • Dummy
      • Message
    • Fatzebra
      • Message
  • PHP
  • Symfony
    • Component
      • EventDispatcher
        • Debug
        • DependencyInjection
        • Tests
          • Debug
          • DependencyInjection
      • HttpFoundation
        • File
          • Exception
          • MimeType
        • Session
          • Attribute
          • Flash
          • Storage
            • Handler
            • Proxy
        • Tests
          • File
            • MimeType
          • Session
            • Attribute
            • Flash
            • Storage
              • Handler
              • Proxy
      • Yaml
        • Exception
        • Tests

Classes

  • Symfony\Component\Yaml\Tests\A
  • Symfony\Component\Yaml\Tests\B
  • Symfony\Component\Yaml\Tests\DumperTest
  • Symfony\Component\Yaml\Tests\InlineTest
  • Symfony\Component\Yaml\Tests\ParseExceptionTest
  • Symfony\Component\Yaml\Tests\ParserTest
  • Symfony\Component\Yaml\Tests\YamlTest
  • Overview
  • Namespace
  • Function
  • Tree
  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: 
 12: namespace Symfony\Component\EventDispatcher\Tests\Debug;
 13: 
 14: use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
 15: use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 16: use Symfony\Component\EventDispatcher\EventDispatcher;
 17: use Symfony\Component\EventDispatcher\Event;
 18: use Symfony\Component\Stopwatch\Stopwatch;
 19: 
 20: class TraceableEventDispatcherTest extends \PHPUnit_Framework_TestCase
 21: {
 22:     public function testAddRemoveListener()
 23:     {
 24:         $dispatcher = new EventDispatcher();
 25:         $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
 26: 
 27:         $tdispatcher->addListener('foo', $listener = function () {; });
 28:         $listeners = $dispatcher->getListeners('foo');
 29:         $this->assertCount(1, $listeners);
 30:         $this->assertSame($listener, $listeners[0]);
 31: 
 32:         $tdispatcher->removeListener('foo', $listener);
 33:         $this->assertCount(0, $dispatcher->getListeners('foo'));
 34:     }
 35: 
 36:     public function testGetListeners()
 37:     {
 38:         $dispatcher = new EventDispatcher();
 39:         $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
 40: 
 41:         $tdispatcher->addListener('foo', $listener = function () {; });
 42:         $this->assertSame($dispatcher->getListeners('foo'), $tdispatcher->getListeners('foo'));
 43:     }
 44: 
 45:     public function testHasListeners()
 46:     {
 47:         $dispatcher = new EventDispatcher();
 48:         $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
 49: 
 50:         $this->assertFalse($dispatcher->hasListeners('foo'));
 51:         $this->assertFalse($tdispatcher->hasListeners('foo'));
 52: 
 53:         $tdispatcher->addListener('foo', $listener = function () {; });
 54:         $this->assertTrue($dispatcher->hasListeners('foo'));
 55:         $this->assertTrue($tdispatcher->hasListeners('foo'));
 56:     }
 57: 
 58:     public function testAddRemoveSubscriber()
 59:     {
 60:         $dispatcher = new EventDispatcher();
 61:         $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
 62: 
 63:         $subscriber = new EventSubscriber();
 64: 
 65:         $tdispatcher->addSubscriber($subscriber);
 66:         $listeners = $dispatcher->getListeners('foo');
 67:         $this->assertCount(1, $listeners);
 68:         $this->assertSame(array($subscriber, 'call'), $listeners[0]);
 69: 
 70:         $tdispatcher->removeSubscriber($subscriber);
 71:         $this->assertCount(0, $dispatcher->getListeners('foo'));
 72:     }
 73: 
 74:     public function testGetCalledListeners()
 75:     {
 76:         $dispatcher = new EventDispatcher();
 77:         $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
 78:         $tdispatcher->addListener('foo', $listener = function () {; });
 79: 
 80:         $this->assertEquals(array(), $tdispatcher->getCalledListeners());
 81:         $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'type' => 'Closure', 'pretty' => 'closure')), $tdispatcher->getNotCalledListeners());
 82: 
 83:         $tdispatcher->dispatch('foo');
 84: 
 85:         $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'type' => 'Closure', 'pretty' => 'closure')), $tdispatcher->getCalledListeners());
 86:         $this->assertEquals(array(), $tdispatcher->getNotCalledListeners());
 87:     }
 88: 
 89:     public function testGetCalledListenersNested()
 90:     {
 91:         $tdispatcher = null;
 92:         $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
 93:         $dispatcher->addListener('foo', function (Event $event, $eventName, $dispatcher) use (&$tdispatcher) {
 94:             $tdispatcher = $dispatcher;
 95:             $dispatcher->dispatch('bar');
 96:         });
 97:         $dispatcher->addListener('bar', function (Event $event) {});
 98:         $dispatcher->dispatch('foo');
 99:         $this->assertSame($dispatcher, $tdispatcher);
100:         $this->assertCount(2, $dispatcher->getCalledListeners());
101:     }
102: 
103:     public function testLogger()
104:     {
105:         $logger = $this->getMock('Psr\Log\LoggerInterface');
106: 
107:         $dispatcher = new EventDispatcher();
108:         $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
109:         $tdispatcher->addListener('foo', $listener1 = function () {; });
110:         $tdispatcher->addListener('foo', $listener2 = function () {; });
111: 
112:         $logger->expects($this->at(0))->method('debug')->with("Notified event \"foo\" to listener \"closure\".");
113:         $logger->expects($this->at(1))->method('debug')->with("Notified event \"foo\" to listener \"closure\".");
114: 
115:         $tdispatcher->dispatch('foo');
116:     }
117: 
118:     public function testLoggerWithStoppedEvent()
119:     {
120:         $logger = $this->getMock('Psr\Log\LoggerInterface');
121: 
122:         $dispatcher = new EventDispatcher();
123:         $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
124:         $tdispatcher->addListener('foo', $listener1 = function (Event $event) { $event->stopPropagation(); });
125:         $tdispatcher->addListener('foo', $listener2 = function () {; });
126: 
127:         $logger->expects($this->at(0))->method('debug')->with("Notified event \"foo\" to listener \"closure\".");
128:         $logger->expects($this->at(1))->method('debug')->with("Listener \"closure\" stopped propagation of the event \"foo\".");
129:         $logger->expects($this->at(2))->method('debug')->with("Listener \"closure\" was not called for event \"foo\".");
130: 
131:         $tdispatcher->dispatch('foo');
132:     }
133: 
134:     public function testDispatchCallListeners()
135:     {
136:         $called = array();
137: 
138:         $dispatcher = new EventDispatcher();
139:         $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
140:         $tdispatcher->addListener('foo', $listener1 = function () use (&$called) { $called[] = 'foo1'; });
141:         $tdispatcher->addListener('foo', $listener2 = function () use (&$called) { $called[] = 'foo2'; });
142: 
143:         $tdispatcher->dispatch('foo');
144: 
145:         $this->assertEquals(array('foo1', 'foo2'), $called);
146:     }
147: 
148:     public function testDispatchNested()
149:     {
150:         $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
151:         $loop = 1;
152:         $dispatcher->addListener('foo', $listener1 = function () use ($dispatcher, &$loop) {
153:             ++$loop;
154:             if (2 == $loop) {
155:                 $dispatcher->dispatch('foo');
156:             }
157:         });
158: 
159:         $dispatcher->dispatch('foo');
160:     }
161: 
162:     public function testDispatchReusedEventNested()
163:     {
164:         $nestedCall = false;
165:         $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
166:         $dispatcher->addListener('foo', function (Event $e) use ($dispatcher) {
167:             $dispatcher->dispatch('bar', $e);
168:         });
169:         $dispatcher->addListener('bar', function (Event $e) use (&$nestedCall) {
170:             $nestedCall = true;
171:         });
172: 
173:         $this->assertFalse($nestedCall);
174:         $dispatcher->dispatch('foo');
175:         $this->assertTrue($nestedCall);
176:     }
177: }
178: 
179: class EventSubscriber implements EventSubscriberInterface
180: {
181:     public static function getSubscribedEvents()
182:     {
183:         return array('foo' => 'call');
184:     }
185: }
186: 
Omnipay Fat Zebra / Paystream Gateway Module API Documentation API documentation generated by ApiGen