phpunit test中的getRepository为null

时间:2017-08-17 14:25:03

标签: symfony doctrine-orm phpunit

我在某些课程上运行phpunit测试,我收到此错误

  

在null中调用成员函数getRepository()   第429行的src / MyApp / MyBundle / CopyList.php

该行是

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

namespace MyApp\MyBundle\Classes\Action;

use Symfony\Component\HttpFoundation\Session;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ParameterBag;
use MyApp\MyBundle\Entity\CopyVirtualList;
use Doctrine\ORM\EntityManager;

class CopyList extends Action
{
    private $em;

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

    public function __clone() {
        $task = $this->getTask();
        $this->__copy($task);
    }

    public function __copy($task) 
    {
        $list = $this->em->getRepository('MyAppMyBundle:CopyList')->findByName($task);

        if (!$list) {
            $list = new CopyVirtualList();
            $this->em->persist($list);
            $this->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 createInstance()
    {
        $task = $this->getMockBuilder('MyApp\MyBundle\Model\TaskContext')
            ->disableOriginalConstructor()
            ->getMockForAbstractClass();

        $container = $task->createElement(
            $this->getContainerKind(),
            $this->getContainerType()
        );

        $container->setContext($task);

        return $container;
    }
}

TestCase是

namespace tests\src\MyApp\MyBundle;

use MyApp\Environment as Env;

require_once dirname(__DIR__) . '/../../../../app/AppKernel.php';

class TestCase extends \PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
        try {
            $kernel = new \AppKernel('test', true);
            $kernel->boot();

            new \MyApp\Environment();  // init single env-instance

            Env::getInstance()->setContainer($kernel->getContainer());

        } catch (\Exception $e) {

        }

        parent::setUp();
    }
}

namespace MyApp\MyBundle\Tests\Units\Classes\Action;

use MyApp\MyBundle\Classes\Action\CopyList;
use tests\src\MyApp\MyBundle\Classes\Action\ActionTestCase as TestCase;

class CopyListTest extends TestCase {

    private $object;

    public function setUp()
    {
        parent::setUp(); 
        $task = $this->getMockBuilder('MyApp\MyBundle\Model\TaskContext')
        ->disableOriginalConstructor()
        ->getMock();
        $this->object = new CopyList($task);
    }

    public function testCreateInstance() {

        $task = $this->getMockBuilder('MyApp\MyBundle\Model\TaskContext')
        ->disableOriginalConstructor()
        ->getMockForAbstractClass();
        $container = $task->createElement($this->getContainerKind(), $this->getContainerType());

        $container->setContext($task);
        return $container;
    }

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

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

        $clone = clone $container;

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

    }
}

有人能指出我正确的方向吗?

0 个答案:

没有答案