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

Gur*_*ura 1 php testing phpunit unit-testing symfony

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

错误:

 PHP Fatal error:  Call to a member function getToken() on a non-object
Run Code Online (Sandbox Code Playgroud)

码:

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

      }
Run Code Online (Sandbox Code Playgroud)

以下代码是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();         
  } 
Run Code Online (Sandbox Code Playgroud)

Mat*_*teo 5

你也必须模拟容器调用.尝试替换这个:

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

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

希望这有帮助

编辑:

你搞错了Token对象.替代:

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

附:

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