)
自2天以来,我对查询进行单元测试,但遇到了一些问题,下面是代码和测试:
请求:
<?php
namespace CampaignBundle\Service;
use Doctrine\ORM\EntityManager;
use AccessBundle\Model\UserInterface;
use AccessBundle\Service\CountryFilter;
class CampaignProvider
{
/** @var EntityManager */
protected $em;
/** @var CountryFilter */
protected $countryFiler;
/**
* CampaignProvider constructor.
* @param EntityManager $entityManager
* @param CountryFilter $countryFilter
*/
public function __construct(EntityManager $entityManager, CountryFilter $countryFilter)
{
$this->em = $entityManager;
$this->countryFiler = $countryFilter;
}
/**
* @return \Doctrine\ORM\EntityRepository|CampaignBundle\Entity\CampaignRepository
*/
protected function getRepository()
{
return $this->em->getRepository('CampaignBundle:Campaign');
}
/**
* @return array
* @throws \Exception
*/
public function getCampaign()
{
$queryBuilder = $this->getCampaignQb();
return $queryBuilder->getQuery()->getResult();
}
/**
* @return \Doctrine\ORM\QueryBuilder
* @throws \Exception
*/
public function getCampaignQb()
{
$repository = $this->getRepository();
$queryBuilder = $repository->createQueryBuilder('c');
$queryBuilder
->where('c.isDeleted = 0')
->addOrderBy('c.id', 'DESC');
return $queryBuilder;
}
}
测试:
<?php
/**
* Created by PhpStorm.
* User: mickael
* Date: 24/12/18
* Time: 14:10
*/
namespace CampaignBundle\Tests\Service;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use AccessBundle\Service\CountryFilter;
use CampaignBundle\Entity\Campaign;
use CampaignBundle\Service\CampaignProvider;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class CampaignProviderTest extends KernelTestCase
{
/** @var EntityManager */
private $entityManager;
/** @var CampaignProvider */
private $campaignProvider;
/** @var CountryFilter */
private $countryFilter;
/** @var TokenStorageInterface */
private $tokenStorage;
/** @var AuthorizationCheckerInterface */
private $authorizationChecker;
public function setUp()
{
$this->entityManager = $this->getMockBuilder('\Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock();
$this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
->disableOriginalConstructor()
->getMock();
$this->authorizationChecker = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')
->disableOriginalConstructor()
->getMock();
$this->countryFilter = new CountryFilter($this->tokenStorage, $this->authorizationChecker, $this->entityManager);
$this->campaignProvider = new CampaignProvider($this->entityManager, $this->countryFilter);
}
public function testGetCampaign()
{
$queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
->setMethods(array('getQuery', 'getCampaignQb'))
->disableOriginalConstructor()
->getMock();
$queryBuilder->expects($this->once())
->method('getCampaignQb')
->will($this->returnValue($queryBuilder));
$queryBuilder->expects($this->once())
->method('getQuery')
->will($this->returnValue($queryBuilder));
$queryBuilder->expects($this->once())
->method('getResult')
->will($this->returnValue($queryBuilder));
$this->campaignProvider->getCampaign();
}
public function testGetCampaignQb()
{
$repository = $this->getMockBuilder('\Doctrine\ORM\EntityRepository')
->disableOriginalConstructor()
->setMethods(array('createQueryBuilder'))
->getMock();
$queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
->setMethods(array('where', 'addOrderBy', 'createQueryBuilder'))
->disableOriginalConstructor()
->getMock();
$repository->expects($this->once())
->method('createQueryBuilder')
->will($this->returnValue($queryBuilder));
$queryBuilder->expects($this->once())
->method('where')
->will($this->returnValue($queryBuilder));
$queryBuilder->expects($this->once())
->method('addOrderBy')
->will($this->returnValue($queryBuilder));
$entityManager = $this->getMockBuilder('\Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock();
$entityManager->expects($this->once())
->method('getRepository')
->will($this->returnValue($repository));
$this->campaignProvider->getCampaignQb();
}
}
运行测试时,我得到:
PHP Fatal error: Call to a member function createQueryBuilder() on null in
我承认在测试查询时遇到一些问题,请您能帮我吗?
谢谢;)
编辑: 编辑我的所有帖子
ps:CountryFilter是我在CampaignProvider中调用的一项服务
答案 0 :(得分:1)
您正在嘲笑实体管理器,但没有在测试中将其传递给CampaignProvider:$this->campaignProvider
不了解本地$entityManager
变量。
如果使用构造函数注入,则需要将其放入构造函数调用中;如果使用方法注入,则需要将其置于setEntityManager
方法中。