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

标签 php unit-testing symfony testing phpunit

我正在尝试测试我的服务。此服务调用其他服务,例如 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();         
  } 

最佳答案

您也必须模拟容器 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');

关于php - 模拟 security.context 在调用时被视为非对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26240535/

相关文章:

php - 在输入邮政编码之前,WooCommerce 会阻止结帐

php - 在 PHP 中缓存操作图像

php - ISO 3166-1 alpha-2 国家/地区代码的区域名称

php - 奇怪的 cURL 错误 - WHMCS 中的 errno 104(代码 56)

symfony - FOSUserBundle 覆盖注册表格

c# - 统一触控输入

java - 如何为在 run() 中具有无限循环的 Runnable java 类编写 UT?

unit-testing - 禁止在单元测试时弹出的对话框

Symfony2 - 从编译器 channel 访问内核

jQuery UI CSS 导入似乎不起作用