)
Так как 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