Testing in PHP: The Ultimate Guide

Testing in web development

Any programmer with experience in web development is well aware of the effort involved in testing: Creating test cases, running them, analyzing the results… is a tedious task.

With agile methodologies, it is also common for application requirements to constantly vary, with a consequent increase in the number of versions and code refactoring. In this context, it is very likely that new errors will appear.

Testing in web development - jokiruiz.com
Testing in web development – jokiruiz.com

Unit and Functional testing

Unit tests ensure that any component of the application produces a correct output for a given input, for each case. It handles a single case at a time.

And Functional testing not only validates the transformation of an input into an output, but also validates a complete feature. A cache system for example can only be validated by a functional test, since it comprises more than 1 single step. So functional tests validate processes and movements of a scenario.

TDD: test-driven development

TDD states that tests are written before the code. Creating tests before code helps you think and focus on how a method works before you code it. It is a good practice, and it is also recommended by other methodologies such as XP (Extreme Programming).

Testing automation - jokiruiz.com
Testing automation – jokiruiz.com

Unit tests in Symfony (PHPUnit)

PHPUnit is included in Symfony out of the box, as a separate library for testing. Each test is a PHP class that must be located in the “/tests” directory, in this way it is enough to execute the following command:

phpunit

The PHPUnit settings are in the file phpunit.xml.dist, in the root directory of the Symfony application.

Unit tests check for a specific class, also called a unit. You can create Symfony unit tests in the same way as to create PHPUnit tests.

To know more about PHPUnit here.

For example we have a Misco class in the “/Util” directory of the AppBundle:

// tests/AppBundle/Util/MiscoTest.php
namespace Tests\AppBundle\Util;

use AppBundle\Util\Misco;

class MiscoTest extends \PHPUnit_Framework_TestCase
{
    public function testHello()
    {
        $misco = new Misco();
        $result = $misco->sayHello();

        $this->assertEquals("Hello man", $result);
    }
}

As in a real application, autoloading is automatically activated through the autoload.php file (as configured by default in the phpunit.xml.dist file).

Functional tests

A functional test usually begins with initializing the browser object for testing. Then, we make a request to an application action, and we can verify that some elements are present in the response.

To do that, Symfony has an extension called sfTestBrowser, which is specially designed for functional testing and has all the characteristics of sfBrowser, in addition to some very useful methods for assertions.

This would be an example using sfTestBrowser:

$browser = new sfTestBrowser();

$browser->
  get('/misco/index')->
  isStatusCode(200)->
  isRequestParameter('module', 'misco')->
  isRequestParameter('action', 'index')->
  checkResponseElement('body', '!/Hey man/')
;
php symfony test:functional frontend miscoActions

Know more about sfTestBrowser here.

Apart from that, when you have complex websites, with background requests (Ajax, SPA, etc), you need to test the behaviour of multiple requests (Behavioural Testing). For this scenario we recommend to test the application using Jest.

Behavioural testing - jokiruiz.com
Behavioural testing – jokiruiz.com

Jest is a test runner, based in NodeJS, that allows fast parallel running of tests, by typing: npm test Take a look of an example of the Jest Syntax:

// App.test.jsdescribe('Checking the syntax of Jest tests', () => {
  it('misco sum', () => {
      expect(1 + 2).toEqual(3);
      expect(2 + 2).toEqual(4);
   });
});

Know more about Jest here.

And that’s all! what do you think about this post? how do you perform your testing? Did you use this technologies? Let me know in the comments, and share the knowledge!

In:

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x