2013-01-16 6 views
8

का उपयोग कर नियंत्रक के लिए सरल ZF2 यूनिट टेस्ट मुझे प्रमाणीकरण के लिए ZfcUser का उपयोग करने वाली क्रिया का परीक्षण करने की कोशिश करने में समस्याएं आ रही हैं। मुझे ZfcUser नियंत्रक प्लगइन का नकल करने के लिए कुछ तरीका चाहिए लेकिन मुझे यह सुनिश्चित नहीं है कि यह कैसे करें। मैंने तालिकाओं और मॉडलों के लिए कुछ यूनिट परीक्षणों को सफलतापूर्वक उत्पादन करने में कामयाब रहा है लेकिन नियंत्रक को बहुत से इंजेक्शन वाली वस्तुओं की आवश्यकता होती है और समस्याएं पैदा होती हैं। क्या किसी को पता है कि कैसे एक नियंत्रक परीक्षण सफलतापूर्वक इकाई के लिए ZfcUser mocks स्थापित करने के लिए?ZfcUser

<?php 
namespace SmsTest\Controller; 

use SmsTest\Bootstrap; 
use Sms\Controller\SmsController; 
use Zend\Http\Request; 
use Zend\Http\Response; 
use Zend\Mvc\MvcEvent; 
use Zend\Mvc\Router\RouteMatch; 
use Zend\Mvc\Router\Http\TreeRouteStack as HttpRouter; 
use PHPUnit_Framework_TestCase; 

class SmsControllerTest extends PHPUnit_Framework_TestCase 
{ 
    protected $controller; 
    protected $request; 
    protected $response; 
    protected $routeMatch; 
    protected $event; 

    protected function setUp() 
    { 

     $serviceManager = Bootstrap::getServiceManager(); 

     $this->controller = new SmsController(); 
     $this->request = new Request(); 
     $this->routeMatch = new RouteMatch(array('controller' => 'index')); 
     $this->event  = new MvcEvent(); 
     $config = $serviceManager->get('Config'); 
     $routerConfig = isset($config['router']) ? $config['router'] : array(); 
     $router = HttpRouter::factory($routerConfig); 
     $this->event->setRouter($router); 
     $this->event->setRouteMatch($this->routeMatch); 
     $this->controller->setEvent($this->event); 
     $this->controller->setServiceLocator($serviceManager); 
    } 


    /* Test all actions can be accessed */ 

    public function testIndexActionCanBeAccessed() 
    { 
     $this->routeMatch->setParam('action', 'index'); 

     $result = $this->controller->dispatch($this->request); 
     $response = $this->controller->getResponse(); 

     $this->assertEquals(200, $response->getStatusCode()); 
    } 
} 

मैंने कोशिश की स्थापना के विधि में निम्नलिखित:

यहाँ मेरी परीक्षण (ZF2 ट्यूटोरियल से नकल) है

$mockAuth = $this->getMock('ZfcUser\Entity\UserInterface'); 


    $authMock = $this->getMock('Zend\Authentication\AuthenticationService'); 
    $authMock->expects($this->any()) 
     ->method('hasIdentity') 
     ->will($this->returnValue(true)); 

    $authMock->expects($this->any()) 
     ->method('getIdentity') 
     ->will($this->returnValue(array('user_id' => 1))); 

लेकिन मुझे यकीन है कि कैसे में इस सुई नहीं कर रहा हूँ नियंत्रक उदाहरण के लिए।

चलें नाटक मेरी सूचकांक कार्रवाई कोड इस प्रकार बस के रूप में है:

public function indexAction() { 
    //Check if logged in 
    if (!$this->zfcUserAuthentication()->hasIdentity()) { 
     return $this->redirect()->toRoute('zfcuser/login'); 
    } 

    return new ViewModel(array(
     'success' => true, 
    )); 
} 

टेस्ट परिणाम:

1) SmsTest\Controller\SmsControllerTest::testIndexActionCanBeAccessed 
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for zfcUserAuthentication 

/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:450 
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/ServiceManager/AbstractPluginManager.php:110 
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/PluginManager.php:90 
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php:276 
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php:291 
/var/www/soap-app.localhost/Zend/module/Sms/src/Sms/Controller/SmsController.php:974 
/var/www/soap-app.localhost/Zend/module/Sms/src/Sms/Controller/SmsController.php:974 
/var/www/soap-app.localhost/Zend/module/Sms/src/Sms/Controller/SmsController.php:158 
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php:87 
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:468 
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:208 
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php:108 
/var/www/soap-app.localhost/Zend/module/Sms/test/SmsTest/Controller/SmsControllerTest.php:57 

लाइन जो इस अपवाद का कारण बनता है नियंत्रक है है: अगर (!$this->zfcUserAuthentication()->hasIdentity()) { कि लाइन से संबंधित है SmsController में लाइन 974।

यह स्पष्ट है कि मुझे ZfcUser प्रमाणीकरण सेवा तक पहुंच नहीं है, इसलिए सवाल है, मैं ZfcUser प्रमाणीकरण सेवा का नकल कैसे करूं और इसे अपने नियंत्रक में इंजेक्ट कर सकता हूं?

विषय जारी रखने के लिए मैं सफलतापूर्वक परीक्षण करने के लिए उपयोगकर्ता में लॉग इन करने के बारे में कैसे जाउंगा, मेरी कार्रवाई का परीक्षण करने के लिए काम कर रहा है?

उत्तर

10

जेएफसीयूसर दस्तावेज बताता है कि यह एक प्लगइन है इसलिए आपको इसे नियंत्रक में इंजेक्ट करने की आवश्यकता है।

आप अपने वर्ग के नाम में संशोधन करने का ZfcUser कक्षाएं

आपका mocks भी getIdenty रूप addapted करने की आवश्यकता होगी लेने की आवश्यकता होगी एक अलग वस्तु देता है।

निम्नलिखित मेरे लिए काम किया - अपने phpunit setUp() विधि में डालें।

$serviceManager = Bootstrap::getServiceManager(); 
$this->controller = new RegisterController(); 
$this->request = new Request(); 
$this->routeMatch = new RouteMatch(array('controller' => 'add')); 
$this->event  = new MvcEvent(); 
$config = $serviceManager->get('Config'); 
$routerConfig = isset($config['router']) ? $config['router'] : array(); 
$router = HttpRouter::factory($routerConfig); 
$this->event->setRouter($router); 
$this->event->setRouteMatch($this->routeMatch); 
$this->controller->setEvent($this->event); 
$this->controller->setServiceLocator($serviceManager); 
$mockAuth = $this->getMock('ZfcUser\Entity\UserInterface'); 

$ZfcUserMock = $this->getMock('ZfcUser\Entity\User'); 

$ZfcUserMock->expects($this->any()) 
      ->method('getId') 
      ->will($this->returnValue('1')); 

$authMock = $this->getMock('ZfcUser\Controller\Plugin\ZfcUserAuthentication'); 

$authMock->expects($this->any()) 
     ->method('hasIdentity') 
      -> will($this->returnValue(true)); 

$authMock->expects($this->any()) 
     ->method('getIdentity') 
     ->will($this->returnValue($ZfcUserMock)); 

$this->controller->getPluginManager() 
    ->setService('zfcUserAuthentication', $authMock); 

अन्य विचारों का स्वागत करने का एक आसान तरीका हो सकता है।

0

इस तरह मैंने इसे किया।

<?php 

namespace IssueTest\Controller; 

use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; 

class IssueControllerTest extends AbstractHttpControllerTestCase 
{ 
    protected $serviceManager; 

    public function setUp() 
    { 
     $this->setApplicationConfig(
      include '/media/policybubble/config/application.config.php' 
     ); 
     parent::setUp(); 
     $ZfcUserMock = $this->getMock('ZfcUser\Entity\User'); 
     $ZfcUserMock->expects($this->any()) 
      ->method('getId') 
      ->will($this->returnValue('1')); 
     $authMock = $this->getMock(
      'ZfcUser\Controller\Plugin\ZfcUserAuthentication' 
     ); 
     $authMock->expects($this->any()) 
      ->method('hasIdentity') 
      ->will($this->returnValue(true)); 
     $authMock->expects($this->any()) 
      ->method('getIdentity') 
      ->will($this->returnValue($ZfcUserMock)); 
     $this->serviceManager = $this->getApplicationServiceLocator(); 
     $this->serviceManager->setAllowOverride(true); 
     $this->serviceManager->get('ControllerPluginManager')->setService(
      'zfcUserAuthentication', $authMock 
     ); 
    } 

    public function testIndexActionCanBeAccessed() 
    { 
     $this->dispatch('/issue'); 
     $this->assertResponseStatusCode(200); 
     $this->assertModuleName('Issue'); 
     $this->assertControllerName('Issue\Controller\Issue'); 
     $this->assertControllerClass('IssueController'); 
     $this->assertMatchedRouteName('issue'); 
    } 

    public function testAddActionRedirectsAfterValidPost() 
    { 
     $issueTableMock = $this->getMockBuilder('Issue\Model\IssueTable') 
      ->disableOriginalConstructor() 
      ->getMock(); 
     $issueTableMock->expects($this->once()) 
      ->method('saveIssue') 
      ->will($this->returnValue(null)); 
     $this->serviceManager->setService('Issue\Model\IssueTable', $issueTableMock); 
     $postData = array(
      'title' => 'Gun Control', 
      'id' => '', 
     ); 
     $this->dispatch('/issue/add', 'POST', $postData); 
     $this->assertResponseStatusCode(302); 
     $this->assertRedirectTo('/issue'); 
    } 

    public function testEditActionRedirectsAfterValidPost() 
    { 
     $issueTableMock = $this->getMockBuilder('Issue\Model\IssueTable') 
      ->disableOriginalConstructor() 
      ->getMock(); 
     $issueTableMock->expects($this->once()) 
      ->method('saveIssue') 
      ->will($this->returnValue(null)); 
     $this->serviceManager->setService('Issue\Model\IssueTable', $issueTableMock); 
     $issueTableMock->expects($this->once()) 
      ->method('getIssue') 
      ->will($this->returnValue(new \Issue\Model\Issue())); 
     $postData = array(
      'title' => 'Gun Control', 
      'id' => '1', 
     ); 
     $this->dispatch('/issue/edit/1', 'POST', $postData); 
     $this->assertResponseStatusCode(302); 
     $this->assertRedirectTo('/issue'); 
    } 

    public function testDeleteActionRedirectsAfterValidPost() 
    { 
     $postData = array(
      'title' => 'Gun Control', 
      'id' => '1', 
     ); 
     $this->dispatch('/issue/delete/1', 'POST', $postData); 
     $this->assertResponseStatusCode(302); 
     $this->assertRedirectTo('/issue'); 
    } 
} 

<?php 

namespace Issue\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use Issue\Model\Issue; 
use Issue\Form\IssueForm; 

class IssueController extends AbstractActionController 
{ 
    protected $issueTable; 

    public function indexAction() 
    { 
     if (!$this->zfcUserAuthentication()->hasIdentity()) { 
      return; 
     } 
     return new ViewModel(
      array(
       'issues' => $this->getIssueTable()->fetchAll(
        $this->zfcUserAuthentication()->getIdentity()->getId() 
       ), 
      ) 
     ); 
    } 

    public function addAction() 
    { 
     if (!$this->zfcUserAuthentication()->hasIdentity()) { 
      return $this->redirect()->toRoute('issue'); 
     } 
     $form = new IssueForm(); 
     $form->get('submit')->setValue('Add'); 
     $request = $this->getRequest(); 
     if ($request->isPost()) { 
      $issue = new Issue(); 
      $form->setInputFilter($issue->getInputFilter()); 
      $form->setData($request->getPost()); 
      if ($form->isValid()) { 
       $issue->exchangeArray($form->getData()); 
       $this->getIssueTable()->saveIssue(
        $issue, 
        $this->zfcUserAuthentication()->getIdentity()->getId() 
       ); 
       // Redirect to list of issues 
       return $this->redirect()->toRoute('issue'); 
      } 
     } 
     return array('form' => $form); 
    } 

    public function editAction() 
    { 
     if (!$this->zfcUserAuthentication()->hasIdentity()) { 
      return $this->redirect()->toRoute('issue'); 
     } 
     $id = (int)$this->params()->fromRoute('id', 0); 
     if (!$id) { 
      return $this->redirect()->toRoute(
       'issue', array(
       'action' => 'add' 
      ) 
      ); 
     } 
     // Get the Issue with the specified id. An exception is thrown 
     // if it cannot be found, in which case go to the index page. 
     try { 
      $issue = $this->getIssueTable()->getIssue($id); 
     } catch (\Exception $ex) { 
      return $this->redirect()->toRoute(
       'issue', array(
       'action' => 'index' 
      ) 
      ); 
     } 
     $form = new IssueForm(); 
     $form->bind($issue); 
     $form->get('submit')->setAttribute('value', 'Edit'); 
     $request = $this->getRequest(); 
     if ($request->isPost()) { 
      $form->setInputFilter($issue->getInputFilter()); 
      $form->setData($request->getPost()); 
      if ($form->isValid()) { 
       $this->getIssueTable()->saveIssue(
        $issue, 
        $this->zfcUserAuthentication()->getIdentity()->getId() 
       ); 
       // Redirect to list of issues 
       return $this->redirect()->toRoute('issue'); 
      } 
     } 
     return array(
      'id' => $id, 
      'form' => $form, 
     ); 
    } 

    public function deleteAction() 
    { 
     if (!$this->zfcUserAuthentication()->hasIdentity()) { 
      return $this->redirect()->toRoute('issue'); 
     } 
     $id = (int)$this->params()->fromRoute('id', 0); 
     if (!$id) { 
      return $this->redirect()->toRoute('issue'); 
     } 
     $request = $this->getRequest(); 
     if ($request->isPost()) { 
      $del = $request->getPost('del', 'No'); 
      if ($del == 'Yes') { 
       $id = (int)$request->getPost('id'); 
       $this->getIssueTable()->deleteIssue($id); 
      } 
      // Redirect to list of issues 
      return $this->redirect()->toRoute('issue'); 
     } 
     return array(
      'id' => $id, 
      'issue' => $this->getIssueTable()->getIssue($id) 
     ); 
    } 

    public function getIssueTable() 
    { 
     if (!$this->issueTable) { 
      $sm = $this->getServiceLocator(); 
      $this->issueTable = $sm->get('Issue\Model\IssueTable'); 
     } 
     return $this->issueTable; 
    } 
}