Вход Регистрация
Файл: vkolhoze.com/vendor/symfony/event-dispatcher/Symfony/Component/EventDispatcher/Tests/ContainerAwareEventDispatcherTest.php
Строк: 469
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace SymfonyComponentEventDispatcherTests;

use 
SymfonyComponentDependencyInjectionContainer;
use 
SymfonyComponentDependencyInjectionScope;
use 
SymfonyComponentEventDispatcherContainerAwareEventDispatcher;
use 
SymfonyComponentEventDispatcherEvent;
use 
SymfonyComponentEventDispatcherEventSubscriberInterface;

class 
ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest
{
    protected function 
createEventDispatcher()
    {
        
$container = new Container();

        return new 
ContainerAwareEventDispatcher($container);
    }

    public function 
testAddAListenerService()
    {
        
$event = new Event();

        
$service $this->getMock('SymfonyComponentEventDispatcherTestsService');

        
$service
            
->expects($this->once())
            ->
method('onEvent')
            ->
with($event)
        ;

        
$container = new Container();
        
$container->set('service.listener'$service);

        
$dispatcher = new ContainerAwareEventDispatcher($container);
        
$dispatcher->addListenerService('onEvent', array('service.listener''onEvent'));

        
$dispatcher->dispatch('onEvent'$event);
    }

    public function 
testAddASubscriberService()
    {
        
$event = new Event();

        
$service $this->getMock('SymfonyComponentEventDispatcherTestsSubscriberService');

        
$service
            
->expects($this->once())
            ->
method('onEvent')
            ->
with($event)
        ;

        
$container = new Container();
        
$container->set('service.subscriber'$service);

        
$dispatcher = new ContainerAwareEventDispatcher($container);
        
$dispatcher->addSubscriberService('service.subscriber''SymfonyComponentEventDispatcherTestsSubscriberService');

        
$dispatcher->dispatch('onEvent'$event);
    }

    public function 
testPreventDuplicateListenerService()
    {
        
$event = new Event();

        
$service $this->getMock('SymfonyComponentEventDispatcherTestsService');

        
$service
            
->expects($this->once())
            ->
method('onEvent')
            ->
with($event)
        ;

        
$container = new Container();
        
$container->set('service.listener'$service);

        
$dispatcher = new ContainerAwareEventDispatcher($container);
        
$dispatcher->addListenerService('onEvent', array('service.listener''onEvent'), 5);
        
$dispatcher->addListenerService('onEvent', array('service.listener''onEvent'), 10);

        
$dispatcher->dispatch('onEvent'$event);
    }

    
/**
     * @expectedException InvalidArgumentException
     */
    
public function testTriggerAListenerServiceOutOfScope()
    {
        
$service $this->getMock('SymfonyComponentEventDispatcherTestsService');

        
$scope = new Scope('scope');
        
$container = new Container();
        
$container->addScope($scope);
        
$container->enterScope('scope');

        
$container->set('service.listener'$service'scope');

        
$dispatcher = new ContainerAwareEventDispatcher($container);
        
$dispatcher->addListenerService('onEvent', array('service.listener''onEvent'));

        
$container->leaveScope('scope');
        
$dispatcher->dispatch('onEvent');
    }

    public function 
testReEnteringAScope()
    {
        
$event = new Event();

        
$service1 $this->getMock('SymfonyComponentEventDispatcherTestsService');

        
$service1
            
->expects($this->exactly(2))
            ->
method('onEvent')
            ->
with($event)
        ;

        
$scope = new Scope('scope');
        
$container = new Container();
        
$container->addScope($scope);
        
$container->enterScope('scope');

        
$container->set('service.listener'$service1'scope');

        
$dispatcher = new ContainerAwareEventDispatcher($container);
        
$dispatcher->addListenerService('onEvent', array('service.listener''onEvent'));
        
$dispatcher->dispatch('onEvent'$event);

        
$service2 $this->getMock('SymfonyComponentEventDispatcherTestsService');

        
$service2
            
->expects($this->once())
            ->
method('onEvent')
            ->
with($event)
        ;

        
$container->enterScope('scope');
        
$container->set('service.listener'$service2'scope');

        
$dispatcher->dispatch('onEvent'$event);

        
$container->leaveScope('scope');

        
$dispatcher->dispatch('onEvent');
    }

    public function 
testHasListenersOnLazyLoad()
    {
        
$event = new Event();

        
$service $this->getMock('SymfonyComponentEventDispatcherTestsService');

        
$container = new Container();
        
$container->set('service.listener'$service);

        
$dispatcher = new ContainerAwareEventDispatcher($container);
        
$dispatcher->addListenerService('onEvent', array('service.listener''onEvent'));

        
$event->setDispatcher($dispatcher);
        
$event->setName('onEvent');

        
$service
            
->expects($this->once())
            ->
method('onEvent')
            ->
with($event)
        ;

        
$this->assertTrue($dispatcher->hasListeners());

        if (
$dispatcher->hasListeners('onEvent')) {
            
$dispatcher->dispatch('onEvent');
        }
    }

    public function 
testGetListenersOnLazyLoad()
    {
        
$service $this->getMock('SymfonyComponentEventDispatcherTestsService');

        
$container = new Container();
        
$container->set('service.listener'$service);

        
$dispatcher = new ContainerAwareEventDispatcher($container);
        
$dispatcher->addListenerService('onEvent', array('service.listener''onEvent'));

        
$listeners $dispatcher->getListeners();

        
$this->assertTrue(isset($listeners['onEvent']));

        
$this->assertCount(1$dispatcher->getListeners('onEvent'));
    }

    public function 
testRemoveAfterDispatch()
    {
        
$service $this->getMock('SymfonyComponentEventDispatcherTestsService');

        
$container = new Container();
        
$container->set('service.listener'$service);

        
$dispatcher = new ContainerAwareEventDispatcher($container);
        
$dispatcher->addListenerService('onEvent', array('service.listener''onEvent'));

        
$dispatcher->dispatch('onEvent', new Event());
        
$dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
        
$this->assertFalse($dispatcher->hasListeners('onEvent'));
    }

    public function 
testRemoveBeforeDispatch()
    {
        
$service $this->getMock('SymfonyComponentEventDispatcherTestsService');

        
$container = new Container();
        
$container->set('service.listener'$service);

        
$dispatcher = new ContainerAwareEventDispatcher($container);
        
$dispatcher->addListenerService('onEvent', array('service.listener''onEvent'));

        
$dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
        
$this->assertFalse($dispatcher->hasListeners('onEvent'));
    }
}

class 
Service
{
    public function 
onEvent(Event $e)
    {
    }
}

class 
SubscriberService implements EventSubscriberInterface
{
    public static function 
getSubscribedEvents()
    {
        return array(
            
'onEvent' => array('onEvent'),
        );
    }

    public function 
onEvent(Event $e)
    {
    }
}
Онлайн: 0
Реклама