我的 symfony 应用程序有一个 phpunit 测试套件。在那个测试文件中,我在不同的测试之间有一些依赖关系,并在依赖关系之间传递一个 DOMCrawler 对象,这样我就不必每次都导航到它。
但是,在采用我所做的方法时,您似乎无法使用这些传递的对象提交表单,但您可以单击它们上的链接。是否有一个原因?我的设计很差吗?如果是,我应该如何改变它?欢迎任何反馈。我在下面附上了一些代码。
<?php
namespace someBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* blah Controller Test
*
*/
class BlahControllerTest extends WebTestCase
{
private $adminUrl;
/**
* Constructs basic information for a audit report controller test suite
*
*/
public function __construct()
{
$this->adminUrl = '/admin/';
}
/**
* Starts a test suite
*
* @return Crawler
*/
public function testAdd()
{
// Create a new client to browse the application
$client = static::createClient();
// Go to site specific admin url
$crawler = $client->request('GET', $this->adminUrl);
$this->assertTrue(200 === $client->getResponse()->getStatusCode());
// do stuff here
// goes to edit page
$crawler = $client->request('GET', $editPage);
return $crawler;
}
/**
* Tests the edit functionality
*
* @param Crawler $crawler Crawler for the show view
*
* @depends testAdd
*/
public function testEdit($crawler)
{
// Create a new client to browse the application
$client = static::createClient();
//Line below is included if the crawler points to the show view
//$crawler = $client->click($crawler->selectLink('Edit')->link());
// Fill in the form and submit it
$form = $crawler->selectButton('Edit')->form(array(
$foo => $bar,
));
// The following line doesn't work properly if testEdit is passed the
// edit page. However, if it is passed the show page, and the
// edit link above is clicked, then the form will submit fine.
$client->submit($form);
$crawler = $client->followRedirect();
// more code here...
}
}