簡體   English   中英

Symfony單元測試的問題

[英]Problems with Unit test on symfony

自從兩天以來,我對查詢進行單元測試,但遇到了一些問題,這是代碼和測試:

請求:

<?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中調用的服務

您在嘲笑實體管理器,但沒有在測試中將其傳遞給CampaignProvider: $this->campaignProvider不了解本地$entityManager變量。

如果使用構造函數注入,則需要將其放入構造函數調用中;如果使用方法注入,則需要將其置於setEntityManager方法中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM