2012-09-27 55 views
3

我知道我做錯了什麼,因爲這很奇怪,我幾天以來就在學習PHPUnit。所述測試受試者是控制器動作:

class ReportController 
{ 
    /** 
    * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface 
    */ 
    private $dispatcher; 

    public function __construct(EventDispatcherInterface $dispatcher) 
    { 
     $this->dispatcher = $dispatcher; 
    } 

    /** 
    * @param \Gremo\SkebbyBundle\Message\InboudSkebbyMessage $message 
    * @return \Symfony\Component\HttpFoundation\Response 
    */ 
    public function receive(InboudSkebbyMessage $message) 
    { 
     $this->dispatcher->dispatch(SkebbyEvents::MESSAGE_RECEIVED, 
      new InboundMessageEvent($message) 
     ); 

     return new Response(); 
    } 
} 

由於控制器引發事件,我需要模擬事件訂戶(實現​​)。有一個靜態方法getSubscribedEvents()。試驗方法幫手(獲取模擬):

public function getMockSubscriber(array $events) 
{ 
    $class = $this->getMockClass(
     'Symfony\Component\EventDispatcher\EventSubscriberInterface', 
     array_merge(array_values($events), array('getSubscribedEvents')) 
    ); 

    // Static stub method 
    $class::staticExpects($this->once()) 
     ->method('getSubscribedEvents') 
     ->will($this->returnValue($events)) 
    ; 

    return new $class; 
} 

然後在我的測試方法,我註冊(模擬)用戶提出請求並檢查onMessageReceived()調用正好一次。 (大的,實際上)的問題是即使我將模擬更改爲​​,測試總是成功。執行:

public function testApiCall() 
{ 
    $client = $this->createClient(); 

    // Router (for route generation) and dispatcher (for subscribing the mock) 
    $router = $client->getContainer()->get('router'); 
    $dispatcer = $client->getContainer()->get('event_dispatcher'); 

    // Get mock event subscriber 
    $subscriber = $this->getMockSubscriber(array(
     'messsage.received' => 'onMessageReceived' 
    )); 

    // Register the mock subscriber with the dispatcher 
    $subscriber->expects($this->once())->method('onMessageReceived'); 
    $dispatcer->addSubscriber($subscriber); 

    // Make the request 
    $request = Request::create(
     $router->generate('controller_receive'), 
     'POST', 
     array(
      'sender' => 'sender', 
      'receiver' => 'receiver', 
      'text'  => 'text', 
      'timestamp' => time(), 
      'smsType' => 'smsType' 
     ) 
    ); 

    $client->getKernel()->handle($request); 
} 

編輯:模擬用戶正確與調度員註冊。 var_dump($dispatcher->getListeners())

array(7) { 
    'messsage.received' => 
    array(1) { 
    [0] => 
    array(2) { 
     [0] => 
     class Mock_EventSubscriberInterface_19b191af#34 (2) { 
     ... 
     } 
     [1] => 
     string(17) "onMessageReceived" 
    } 
    } 

回答

2

我會用嘲笑的對象,而不是從類中創建名一個新問題:

public function getMockSubscriber(array $events) 
{ 
    $subscriberMock = $this->getMock(
     'Symfony\Component\EventDispatcher\EventSubscriberInterface', 
     array_merge(array_values($events), array('getSubscribedEvents')) 
    ); 

    // Static stub method 
    $class = get_class(subscriberMock); 
    $class::staticExpects($this->once()) 
     ->method('getSubscribedEvents') 
     ->will($this->returnValue($events)) 
    ; 

    return $subscriberMock; 
} 
+0

作品!作品!作品!謝謝......顯然我需要弄清楚我錯過了什麼。 – gremo