【问题标题】:Mocking security.context is considered like a non-object when calling调用时模拟 security.context 被视为非对象
【发布时间】:2014-12-02 03:32:16
【问题描述】:

我正在尝试测试我的服务。该服务调用其他服务,如security.context。当调用AlbumHandler 时,模拟在security.context 步骤失败。

错误:

 PHP Fatal error:  Call to a member function getToken() on a non-object

代码:

     public function setUp()
     {
        $this->container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerInterface');
     }


      public function testAdd()                                                                 
      { 

        // The user I want to return                                                                                        
        $user = $this->getMock('\MyProject\Bundle\UserBundle\Entity\User');  

        // I create a Token for mock getUser()                    
        $token = $this->getMock('\Symfony\Component\Security\Core\Authentication\Token');       
        $token->expects($this->once())                                                          
              ->method('getUser')                                                               
              ->will($this->returnValue($user));                                                

        // I mock the service. PHPUnit don't return an error here.
        $service = $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContextInterface')
              ->disableOriginalConstructor()                                                    
              ->getMock();  
        $service->expects($this->once())                                                        
              ->method('getToken')                                                              
              ->will($this->returnValue($token));                                               

        // I replace the real service by the mock
        $this->container->set('security.context', $service);                                

        // It fails at the constructor of this service.
        $albumHandler = new AlbumHandler($entityManager, $this->container, 'MyProject\Bundle\AlbumBundle\Entity\Album');

        $this->assertEquals($albumHandler, $albumHandler);                                    

      }

以下代码是 AlbumHandler 的构造函数

  public function       __construct(ObjectManager $om, Container $container, $entityClass)  
  {                                                                                         
    $this->om = $om;                                                                        
    $this->entityClass = $entityClass;                                                      
    $this->repository = $this->om->getRepository($this->entityClass);                       
    $this->container = $container;

    // fail here                                                       
    $this->user = $this->container->get('security.context')->getToken()->getUser();         
  } 

【问题讨论】:

    标签: php unit-testing symfony testing phpunit


    【解决方案1】:

    您也必须模拟容器的 get 调用。试试用这个代替:

        // I replace the real service by the mock
        $this->container->set('security.context', $service);                                
    

        $this->container
            ->expects($this->once())
            ->method('get')
            ->with('security.context')
            ->will($this->returnValue($service));
    

    希望有帮助

    编辑:

    你弄错了Token 对象。替补:

        // I create a Token for mock getUser()
        $token = $this->getMock('\Symfony\Component\Security\Core\Authentication\Token');
    

    与:

        // I create a Token for mock getUser()
        $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
    

    【讨论】:

    • 感谢@Matteo!但现在,我在下一步出现错误:.PHP Fatal error: Call to undefined method Mock_Token_66794207::getUser()。你发现我的代码有问题吗?
    • 顺便说一句,请改用$this->container->get('security.authorization_checker')(2.6 中的新功能)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 2013-09-23
    相关资源
    最近更新 更多