我正在尝试使用PhpUnit(在本地)测试我在Symfony中构建的应用程序。在我的主页上,我有一个简单的表单提交。我按照各种文档(Symfony,phpUnit)中的所有步骤进行操作,并且我已经从各个论坛测试了很多不同的解决方案,但我仍然卡住了......
这是我的测试类:
namespace tests\FormBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class FormControllerTest extends WebTestCase
{
public function testReservationAction()
{
$client = static::createClient();
$crawler = $client->request('GET', '/tp4_louvres/web/app.php/info_reservation');
$bouton = $crawler->selectButton('Valider');
$form = $bouton->form(); //Here is the line 18 of the error
$validationFormulaire = $client->submit($form, array(
'#form_jour_jour' => '12/12/2017',
'#form_nom_reservation' => 'Robert',
'#form_prenom_reservation' => 'Paul',
'#form_email' => 'paul.robert@gmail.com'
));
$this->assertTrue($client->getResponse()->isRedirect('/tp4_louvres/web/app.php/info_billet'));
} // End of testReservationAction()
} // End class
这是我的html页面的形式:
<form name="form" method="post">
<div class="form-group">
<label class="control-label required">Date de visite</label>
<div id="form_jour" class="col-3 p-0">
<div class="form-group">
<input type="text" id="form_jour_jour" name="form[jour][jour]" required="required" class="datepicker form-control" readonly="readonly" value="14/12/2017" />
</div>
</div>
</div>
<div class="form-group">
<label class="control-label required" for="form_nom_reservation">Nom de la réservation</label>
<input type="text" id="form_nom_reservation" name="form[nom_reservation]" required="required" class="form-control" value="FIACRE" />
</div>
<div class="form-group">
<label class="control-label required" for="form_prenom_reservation">Prénom de la réservation</label>
<input type="text" id="form_prenom_reservation" name="form[prenom_reservation]" required="required" class="form-control" value="Thibault" />
</div>
<div class="form-group">
<label class="control-label required" for="form_email">Email de la réservation</label>
<input type="email" id="form_email" name="form[email]" required="required" class="form-control" value="thibault.fiacre@gmail.com" />
</div>
<div class="form-group">
<button type="submit" id="bouton-valider" name="form[submit]" value="Valider" class="bg-info bouton-formulaire btn">Valider</button>
</div>
</form>
当我在控制台中执行测试时出现此错误:
1) tests\FormBundle\Controller\FormControllerTest::testReservationAction
InvalidArgumentException: The current node list is empty.
C:\wamp64\www\tp4_louvres\vendor\symfony\symfony\src\Symfony\Component\DomCrawler\Crawler.php:849
C:\wamp64\www\tp4_louvres\tests\FormBundle\Controller\FormControllerTest.php:18
它与form()
中的$form=$bouton->form();
函数一样,在页面上找不到该表单,但找到了按钮...我尝试var_dump($client->getResponse()->getContent());
我从Symfony那里得到了这个错误:
Uncaught PHP Exception Symfony\Component\Config\Exception\FileLoaderLoadException:
"Cannot load resource ".". Make sure there is a loader supporting the "extra" type."
at C:\wamp64\www\tp4_louvres\vendor\symfony\symfony\src\Symfony\Component\Config\Loader\Loader.php line 73
我在这个问题上迷失了2天......如果有人能帮助我,我将非常感激!非常感谢!