2015-04-24 75 views
2

我開發與Zend框架2.一些應用服務通知其引發其他服務的一些方法有興趣的聽衆,例如應用程序:如何測試事件監聽Zend框架2 PHPUnit的

UserService

<?php 

class UserService implements EventManagerAwareInterface 
{ 
    public function create($data = array()) 
    { 
     // user is already created here 
     $params = array("user" => $user); 
     $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, $params); 
    } 
} 

UserListener

<?php 

use Zend\EventManager\EventManagerInterface; 
use Zend\EventManager\ListenerAggregateInterface; 
use Zend\EventManager\EventInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 

class UserListener implements ListenerAggregateInterface 
{ 
    protected $serviceManager; 

    protected $listeners = array(); 

    public function __construct(ServiceLocatorInterface $serviceManager) 
    { 
     $this->serviceManager = $serviceManager; 
    }  

    public function attach(EventManagerInterface $events) 
    { 
     $evm = $events->getSharedManager(); 

     $this->listeners[] = $evm->attach('Application\Service\UserService', 'create.post', array($this, 'notify')); 
    } 

    public function detach(EventManagerInterface $events) 
    { 
     foreach ($this->listeners as $index => $listener) { 
      if ($events->detach($listener)) { 
       unset($this->listeners[$index]); 
      } 
     } 
    } 

    public function notify(EventInterface $events) 
    { 
     $user = $events->getParam('user'); 
     $mailService = $this->serviceManager->get('MailService'); 
     $mailService->sendWelcomeMessage($user); 
    } 
} 

所以,我的問題是什麼是用PHPUnit測試Zend Framework事件監聽器的最佳做法,應如何正確測試所有監聽器方法附件(),detach(),notify()等等?

任何幫助將不勝感激!提前致謝!

回答

3

如果你看看apfility zf-mvc-auth來源,你會看到很多聽衆測試的例子。

https://github.com/zfcampus/zf-mvc-auth/tree/master/test/Authorization

雖然一些可以使要求這些都不是完全正確的單元測試,以及他們那種走路單元測試和功能測試之間的模糊界限,我發現以這種方式測試聽衆是最簡單也是最有效的方式來做到這一點,而不會陷入過度測試混亂。它們很乾淨,易於書寫,而且清晰易懂。

0

以下是附件()detach()的一些有用的提示。第一監聽器的方法被作爲一個例子進行了測試:

/** 
* {@inheritdoc} 
*/ 
public function attach(EventManagerInterface $events) 
{ 
    $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH, 
             [$this, 'doAction']); 
} 

/** 
* {@inheritdoc} 
*/ 
public function detach(EventManagerInterface $events) 
{ 
    foreach ($this->listeners as $index => $listener) { 
     if ($events->detach($listener)) { 
      unset($this->listeners[$index]); 
     } 
    } 
} 

public function doAction($event) 
{ 
    // Code truncated. 
} 

而單元測試有助於稱爲$這 - 收聽實例>聽者

/** 
* @covers Application\Event\MyListenerClass::attach 
*/ 
public function testAttach() 
{ 
    $eventManager = new EventManager(); 
    $this->listener->attach($eventManager); 
    $events = $eventManager->getEvents(); 
    // Only one event attached to. 
    $priorityQueue = $eventManager->getListeners($events[0]); 

    $callBackHandler = $priorityQueue->extract(); 
    $callBackArray = $callBackHandler->getCallback(); 
    $this->assertEquals($this->listener, $callBackArray[0]); 
    $this->assertEquals('doAction', $callBackArray[1]); 
} 

/** 
* @covers Application\Event\MyListenerClass::detach 
*/ 
public function testDetach() 
{ 
    $eventManager = new EventManager(); 
    $this->listener->attach($eventManager); 
    $this->listener->detach($eventManager); 
    $events = $eventManager->getEvents(); 
    // Only one event attached to. 
    $priorityQueue = $eventManager->getListeners($events[0]); 

    $this->assertEmpty($priorityQueue->toArray()); 
} 

最後,也可以從應用程序的ServiceManager中使用EventManager,而不是代碼中的示例實例,但這足以讓某人啓動。

+0

我使用[samsonasik™]的幫助實現了Listener(https://samsonasik.wordpress.com/2013/05/18/zend-framework-2-register-event-listeners-in-configuration-file/) – m1st0

+0

$ eventManager-> getListeners()將在版本3.0中刪除 – Hooli