duanlei20082008 2014-10-07 16:11
浏览 38
已采纳

在调用时,模拟security.context被视为非对象

I'm trying to test my service. This service calls other service like security.context. When the AlbumHandler is call, the mock fail at the security.context step.

Error:

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

Code:

     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);                                    

      }

The following code is the constuctor of 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();         
  } 
  • 写回答

1条回答 默认 最新

  • dongqi6964 2014-10-07 16:25
    关注

    You must mock the container get call too. Try Substitute this:

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

    with

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

    Hope this help

    EDIT:

    You muck the wrong Token Object. Substitute:

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

    With:

        // I create a Token for mock getUser()
        $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上