<div class="title-bar" data-responsive-toggle="appMenu" data-hide-for="medium">
<button class="menu-icon" type="button" data-toggle></button>
<div class="title-bar-title">Menu</div>
</div>
<div class="top-bar" id="appMenu">
<div class="top-bar-left">
{# category_menu is global twig var filled from service, and later overriden by another module service #}
<ul class="menu">
<li><a href="{{ path('homepage') }}">HOME</a></li>
{% block category_menu %}
{% for link in category_menu.getItems() %}
<li><a href="{{ link.path }}">{{ link.label }}</li></a>
{% endfor %}
{% endblock %}
</ul>
</div>
<div class="top-bar-right">
<ul class="menu">
{# customer_menu is global twig var filled from service, and later overriden by another module service #}
{% block customer_menu %}
{% for link in customer_menu.getItems() %}
<li><a href="{{ link.path }}">{{ link.label }}</li></a>
{% endfor %}
{% endblock %}
{# checkout_menu is global twig var filled from service, and later overriden by another module service #}
{% block checkout_menu %}
{% for link in checkout_menu.getItems() %}
<li><a href="{{ link.path }}">{{ link.label }}</li></a>
{% endfor %}
{% endblock %}
</ul>
</div>
</div>
/** * @Route("/contact", name="contact") */publicfunctioncontactAction(Request $request) {// Build a form, with validation rules in place $form =$this->createFormBuilder()->add('name',TextType::class,array('constraints'=>newNotBlank() ))->add('email',EmailType::class,array('constraints'=>newEmail() ))->add('message',TextareaType::class,array('constraints'=>newLength(array('min'=>3)) ))->add('save',SubmitType::class,array('label'=>'Reach Out!','attr'=>array('class'=>'button'), ))->getForm();// Check if this is a POST type request and if so, handle formif ($request->isMethod('POST')) { $form->handleRequest($request);if ($form->isSubmitted()&& $form->isValid()) {$this->addFlash('success','Your form has been submitted. Thank you.');// todo: Send an email out...return$this->redirect($this->generateUrl('contact')); } }// Render "contact us" pagereturn$this->render('AppBundle:default:contact.html.twig',array('form'=> $form->createView() ));}
在这里,我们开始通过表单生成器构建表单。Add 方法同时接受字段定义和字段约束,可以基于这些约束进行验证。然后,我们添加了一个 HTTP POST 方法的检查,在这种情况下,我们向表单提供请求参数,并对其运行验证。
<testsuites>
<testsuite name="Project Test Suite">
<-- ... other elements ... -->
<directory>src/AppBundle/Tests</directory>
<-- ... other elements ... -->
</testsuite>
</testsuites>
{// @var \Symfony\Bundle\FrameworkBundle\Client $client =static::createClient();/** @var\Symfony\Component\DomCrawler\Crawler */ $crawler = $client->request('GET','/');// Check if homepage loads OK$this->assertEquals(200, $client->getResponse()->getStatusCode());// Check if top bar left menu is present$this->assertNotEmpty($crawler->filter('.top-bar-left li')->count());// Check if top bar right menu is present$this->assertNotEmpty($crawler->filter('.top-bar-right li')->count());// Check if footer is present$this->assertNotEmpty($crawler->filter('.footer li')->children()->count());}
publicfunctiontestStaticPages(){// @var \Symfony\Bundle\FrameworkBundle\Client $client =static::createClient();/** @var\Symfony\Component\DomCrawler\Crawler */// Test About Us page $crawler = $client->request('GET','/about');$this->assertEquals(200, $client->getResponse()->getStatusCode());$this->assertContains('About Us', $crawler->filter('h1')->text());// Test Customer Service page $crawler = $client->request('GET','/customer-service');$this->assertEquals(200, $client->getResponse()->getStatusCode());$this->assertContains('Customer Service', $crawler->filter('h1')->text());// Test Privacy and Cookie Policy page $crawler = $client->request('GET','/privacy-and-cookie-policy');$this->assertEquals(200, $client->getResponse()->getStatusCode());$this->assertContains('Privacy and Cookie Policy', $crawler->filter('h1')->text());// Test Orders and Returns page $crawler = $client->request('GET','/orders-and-returns');$this->assertEquals(200, $client->getResponse()->getStatusCode());$this->assertContains('Orders and Returns', $crawler->filter('h1')->text());// Test Contact Us page $crawler = $client->request('GET','/contact');$this->assertEquals(200, $client->getResponse()->getStatusCode());$this->assertContains('Contact Us', $crawler->filter('h1')->text());}
publicfunctiontestContactFormSubmit(){// @var \Symfony\Bundle\FrameworkBundle\Client $client =static::createClient();/** @var\Symfony\Component\DomCrawler\Crawler */ $crawler = $client->request('GET','/contact');// Find a button labeled as "Reach Out!" $form = $crawler->selectButton('Reach Out!')->form();// Note this does not validate form, it merely tests against submission and response page $crawler = $client->submit($form);$this->assertEquals(200, $client->getResponse()->getStatusCode());}