Symfony 2.3 PHP单元测试使用模拟对象

时间:2016-02-05 07:09:37

标签: symfony phpunit

我是Symfony和phpunit的新手,我试图对以下代码进行phpunit测试。

/**
     * Creates a form to delete a Product entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('product_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    }

/**
     * Finds and displays a Product entity.
     *
     * @Route("/{id}", name="product_show")
     * @Method("GET")
     * @Template()
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('AppBundle:Product')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Product entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        );
    }

任何人都可以帮助我

我的单元测试代码(只是部分内容)

$mockFormBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
            ->disableOriginalConstructor()
            ->getMock();
$mockFormBuilder->expects($this->any())
->method('setAction')
->will($this->returnValue(true));

是的我股票..我不知道如何测试setMethod,add和getForm返回一个表格,我可以再次使用createView方法。

0 个答案:

没有答案