PHPUnit + Symfony,模拟

时间:2017-03-22 16:07:51

标签: symfony unit-testing


我在测试我的应用时遇到问题 这是存储库:

class DocumentRepository extends \Doctrine\ORM\EntityRepository
{
    public function getDocuments($type)
    {
        switch($type)
        {
            case 'invoices':
                $where = "WHERE d.type = 'FV'";
                break;
            case 'bills':
                $where = "WHERE d.type = 'Rachunek'";
                break;
            default:
                $where = "";
                break;
        }
        return $this->getEntityManager()
            ->createQuery("SELECT
                    d.number,
                    d.type
                FROM
                    ApiBundle:Document d".$where)
        ->getArrayResult();
    }
} 

控制器类:

class DocumentController extends Controller
{
    /**
    * @Route("/list/{type}")
    * @Method({"GET"})
    */
    public function documentListAction($type = 'all')
    {
        $documents = $this->getDoctrine()
            ->getRepository('ApiBundle:Document')
            ->getDocuments($type);

        $response = new JsonResponse();
        return $response->setData($documents);
    }
}

这是测试:

public function testDocument()
{
        $document = $this->createMock(Document::class);
        $document->expects($this->once())
            ->method('getNumber')
            ->will($this->returnValue('FV 00/00/0000'));
        $document->expects($this->once())
            ->method('getType')
            ->will($this->returnValue('FV'));


        $documentRepository = $this
            ->getMockBuilder(EntityRepository::class)
            ->disableOriginalConstructor()
            ->getMock();
        $documentRepository->expects($this->once())
            ->method('getDocuments')
            ->will($this->returnValue($document));


        $entityManager = $this
            ->getMockBuilder(ObjectManager::class)
            ->disableOriginalConstructor()
            ->getMock();
        $entityManager->expects($this->once())
            ->method('getRepository')
            ->will($this->returnValue($documentRepository));

        $documentController = new DocumentController($entityManager);
}

当我开始测试时,我总是看到警报,警告和错误。 我不太了解应用程序测试,我不知道测试写入是好的。

我想学习如何测试,但我遇到了模拟实体的问题。如果有人给我一些提示,那就好了。我很少在互联网上询问有关编程的问题,我总是通过互联网自己解决问题,但现在我已经不再有力量了。在这个应用程序中我也使用JWT,但是对于测试,我禁用了身份验证。

我将非常感谢你的帮助。

(对不起我的英文)

0 个答案:

没有答案