在phpunit test中获取doctrine.orm.entity_manager为null

时间:2017-08-17 10:50:46

标签: symfony doctrine-orm phpunit

我正在某些类上运行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

但我不认为我做得对。例如,我已将该代码放在setUp()的{​​{1}}函数中。任何指针都非常感激。

1 个答案:

答案 0 :(得分:0)

我看到的问题在于......

$em = $dic->get('doctrine.orm.entity_manager');
你的CopyList类的

。如果你想模拟de entity manager。你应该做一个依赖注入。

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

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

的实例

看看这个关键点,他们帮了我很多...

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