Functional testing

Before we start writing our functional tests, we need to edit the phpunit.xml.dist file by adding our bundle Tests directory to the testsuite paths, as follows:

<testsuites>
  <testsuite name="Project Test Suite">
    <-- ... other elements ... -->
      <directory>src/AppBundle/Tests</directory>
    <-- ... other elements ... -->
  </testsuite>
</testsuites>

Our functional tests will cover only one controller, since we have no other. We start off by creating a src/AppBundle/Tests/Controller/DefaultControllerTest.php file with content as follows:

namespace AppBundleTestsController;

use SymfonyBundleFrameworkBundleTestWebTestCase;

class DefaultControllerTest extends WebTestCase
{
//…
}

The next step is to test each and every one of our controller actions. At the very least we should test if the page content is being outputted properly.

Tip

To get an auto-complete in our IDE we can download the PHPUnitphar file from the official site here https://phpunit.de. Once downloaded, we can simply add it to the root of our project, so that IDE, like PHPStorm, picks it up. This makes it easy to follow up on all those $this->assert method calls and their parameters.

The first thing we want to test is our home page. We do so by adding the following to the body of the DefaultControllerTest class.

public function testHomepage()
{
  // @var SymfonyBundleFrameworkBundleClient
  $client = static::createClient();
  /** @var SymfonyComponentDomCrawlerCrawler */
  $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());
}

Here we are checking several things at once. We are checking with the page loads OK, with HTTP 200 status. Then we are grabbing the left and right menu and counting their the items to see if they have any. If all of the individual checks pass, the testHomepage test is considered to have passed.

We further test all of the static pages by adding the following to the DefaultControllerTest class:

public function testStaticPages()
{
  // @var SymfonyBundleFrameworkBundleClient
  $client = static::createClient();
  /** @var SymfonyComponentDomCrawlerCrawler */

  // 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());
}

Here we are running the same assertEquals and assertContains functions for all of our pages. We are merely trying to confirm that each page is loaded with HTTP 200, and that the proper value is returned for the page title, that is to say, the h1 element.

Finally, we address the form submission test which we perform by adding the following into the DefaultControllerTest class:

public function testContactFormSubmit()
{
  // @var SymfonyBundleFrameworkBundleClient
  $client = static::createClient();
  /** @var SymfonyComponentDomCrawlerCrawler */
  $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());
}

Here we are grabbing the form element through its Reach Out! submit button. Once the form is fetched, we trigger the submit method on the client passing it the instance from element. It is worth noting that the actual form validation is not being tested here. Even so, the submitted form should result in an HTTP 200 status.

These tests are conclusive. We can write them to be much more robust if we wanted to, as there are numerous elements we can test against.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.93.210