【问题标题】:Get doctrine.orm.entity_manager is null in phpunit test获取 phpunit 测试中的学说.orm.entity_manager 为空
【发布时间】:2018-01-25 18:03:27
【问题描述】:

我在一些类上运行 phpunit 测试,我得到了这个错误

PHP Fatal error:  Call to a member function get() on null in src/MyApp/MyBundle/Classes/Action/CopyList.php on line 415

在那一行是

$em = $dic->get('doctrine.orm.entity_manager');

来自

namespace MyApp\MyBundle\Classes\Action;

use Symfony\Component\HttpFoundation\Session;   
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ParameterBag;

class CopyList extends Action {

public function __clone($task) {
    $dic = Environment::getInstance()->getContainer();

    $em = $dic->get('doctrine.orm.entity_manager');

    $list = $em->getRepository('MyAppMyBundle:CopyList')->findByName($task);

    if (!$list) {
        $list = new CopyList();
        $em->persist($list);
        $em->flush();
    }  
}

从该行调用的

$clone = clone $container;

在

namespace tests\src\MyApp\MyBundle\Classes;

use Symfony\Component\HttpFoundation\Request;

abstract class TaskContextContainerTestCase extends TestCase {

public function testCloning() {
    $container = $this->createInstance();
    $container->setImmutable(true);

    $this->assertTrue($container->isImmutable());

    $clone = clone $container;

    $this->assertNotEquals($container, $clone);
    $this->assertFalse($clone->isImmutable());
}

我试过做类似的事情

protected function getEmMock()
{   
    $emMock  = $this->getMock('\Doctrine\ORM\EntityManager',
        array('getRepository', 'getClassMetadata', 'persist', 'flush'), array(), '', false);
    $emMock->expects($this->any())
        ->method('getRepository')
        ->will($this->returnValue(new FakeRepository()));
    $emMock->expects($this->any())
        ->method('getClassMetadata')
        ->will($this->returnValue((object)array('name' => 'aClass')));
    $emMock->expects($this->any())
        ->method('persist')
        ->will($this->returnValue(null));
    $emMock->expects($this->any())
        ->method('flush')
        ->will($this->returnValue(null));
    return $emMock;  // it tooks 13 lines to achieve mock!
 }

来自here,但我认为我做得不对。例如,我将该代码放在TaskContextContainerTestCase 的setUp() 函数中。任何指针都非常感谢。

【问题讨论】:

  • 我删除了我的答案,因为它不是解决您问题的好方法。

标签: symfony doctrine-orm phpunit


【解决方案1】:

我看到的问题就在这条线上……

$em = $dic->get('doctrine.orm.entity_manager');

您的 CopyList 类。如果你想模拟实体管理器。您应该进行依赖注入。

namespace MyApp\MyBundle\Classes\Action;

use Symfony\Component\HttpFoundation\Session;   
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ParameterBag;
use Doctrine\ORM\EntityManager;

class CopyList extends Action {

   /**
    * @var EntityManager
    */
   private $em;

   public function __construct(EntityManager $em) {
       $this->em = $em;
   }

   public function __clone($task) {
      $dic = Environment::getInstance()->getContainer();

      //Where is this $listName coming from? shoudn't be $task?
      $list = $this->em->getRepository('MyAppMyBundle:CopyList')->findByName($listName); 

      if (!$list) {
         $list = new CopyList();
         $em->persist($list);
         $em->flush();
      } 
   }
}

要模拟实体管理器,请使用...

protected function getEmMock()
{   
    $emMock  = $this->getMockBuilder('\Doctrine\ORM\EntityManager')
        ->disableOriginalConstructor()
        ->setMethods(array())
        ->getMock;
    $emMock->expects($this->any())
        ->method('getRepository')
        ->will($this->returnValue(new FakeRepository()));
    $emMock->expects($this->any())
        ->method('getClassMetadata')
        ->will($this->returnValue((object)array('name' => 'aClass')));
    $emMock->expects($this->any())
        ->method('persist')
        ->will($this->returnValue(null));
    $emMock->expects($this->any())
        ->method('flush')
        ->will($this->returnValue(null));
    return $emMock;  // it tooks 13 lines to achieve mock!
 }

我将重建测试构建一个新实例

$copyList = new CopyList($this->getEmMock());

然后您将拥有模拟实体管理器的实例

看看这个 tuts,它们对我很有帮助......

https://jtreminio.com/2013/03/unit-testing-tutorial-introduction-to-phpunit/

【讨论】:

  • 与此相关,$this->em->getRepository('MyAppMyBundle:CopyList') 行现在给我Call to a member function getRepository() on null。你知道怎么解决吗?
  • 那么我在哪里放置/调用 getEmMock?我的意思是,我只是了解该函数如何适合测试类和正在测试的类。
  • 是的,我已经阅读了他的教程几次,感谢您的时间,我会努力使它工作,如果没有,我会发布另一个问题
猜你喜欢
  • 1970-01-01
  • 2019-02-02
  • 2018-05-08
  • 2021-06-30
  • 1970-01-01
  • 2021-03-20
  • 1970-01-01
  • 2015-10-27
  • 2018-07-29
相关资源
最近更新 更多