Testing PHP

Dr Andres Baravalle

Testing PHP

  • Software testing
  • Using Putty
  • Using Codeception

Software testing

Software testing

"Software testing is an investigation conducted to provide stakeholders with information about the quality of the product or service under test.

Software testing can also provide an objective, independent view of the software [...]. Test techniques include, but are not limited to the process of executing a program or application with the intent of finding software bugs". (Wikipedia, 2013)

Software testing (2)

"Software testing can be stated as the process of validating and verifying that a computer program/application/product:

  • meets the requirements that guided its design and development,
  • works as expected,
  • can be implemented with the same characteristics,
  • and satisfies the needs of stakeholders."

(Wikipedia, 2013)

Static vs Dynamic testing

Static testing is a form of software testing focusing on checking the sanity of the code. This type of testing is often used by the developer who wrote the code, but can be used also in team environments.

Common approaches used for static testing include code reviewsinspections and software walkthroughs. Most modern editors for programmers support some automatic code reviews to some extent.

Dynamic testing refers to the examination of the response from the system when a software runs. It involves working with the software, giving input values and checking if the output is as expected by executing specific test cases.

Testing levels

The main levels of testing during the development process are:

  • Unit testing
  • Integration testing
  • System testing
  • Acceptance testing

Unit testing

Unit testing refers to tests that verify the functionality of a specific section of code, usually at the function level (procedural programming) or at class level (OO programming).

These types of tests are usually written by developers as they work on code, to ensure that the specific function is working as expected.

In PHP, unit testing is typically done using PHPUnit.

Integration testing

Integration testing is used to verify the interfaces between components. Integration tests aim to test a web application as a whole and is specifically important when different developers are working at different parts of the system.

"Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily – leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly." (Fowler, 2006) 

Integration testing (2)

There is no de-facto standard for integration testing in PHP applications; tools that can be used to do integration testing of PHP applications include PHPUnit and Hudson.

Systems testing

System testing tests a completely integrated system to verify that it meets its requirements.  A PHP as well as working as expected in an environment that reflects the real-use conditions.

Example of system testing tecniques used to test web applications include:

  • Graphical user interface testing
  • Usability testing
  • Software performance testing
  • Load testing and stress testing
  • Security testing
  • Installation testing

Acceptance testing

Acceptance testing is the final  test conducted to determine if the requirements of a specification are met.

Expecially when it comes to web applications, testing the controller, or the model in isolation doesn't prove your application is working.

Testing your web applications

At this stage, we are not going to cover unit, integration or system testing any further - but we are going to focus on acceptance testing using CodeCeption.

Before we start using CodeCeption, you need to familiarise yourself with another tool - Putty.

Using Putty


PuTTY is an Open Source software that can be used as a terminal emulator.

It allows remote access to a server, and we are going to use it to connect through SSH to mastodon.

This will allow us to run some PHP scripts that require command-line access.

Connecting to a server

In the Host Name (or IP address) field enter the name of the computer you wish to connect to, select SSH as protocol and open the connection.

Now I'm connected - what's next?

You should already be familiar with the main main Linux shell commands (cd, mkdir, rm, chmod) but we are going to review them in the next slides.

You are going to need them. If you need a full guide, you can start with this tutorial.

Linux shell commands: cd, mkdir & rm

Command Explanation Example
cd To switch to any other directory cd /home/andres2/
mkdir To create folder mkdir /home/andres/tmp
rm To delete any file rm test.php
To delete a folder and all the files inside rm -Rf /home/andres/tmp

Linux shell commands: chmod

chmod is used to set the permissions for your files.

Permissions tell UNIX what can be done with that file and by whom. There are three operations you can (or can't) perform with a given file or folder:

  • read it
  • write (modify) it and
  • execute it

Unix permissions specify what can the owner do, what can the owner's group do, and what can everybody else do with the file.

Linux shell commands: chmod (2)

Basic permissions are set using a triplet. The first number identifies the permission for the user, the second for the group and the third for everyone else.

Your folders should have a permission of 755 (see below) and your PHP files should have a permission of 644.

read 4
write 2
execute 1

Each permission digit is calculated by summing the value permissions that should be allocated. For now, you should not need to change the permissions - and you can use WinSCP to do it through a graphical interface.

Linux shell commands: running PHP from the command line

We have already been invoking PHP from the command line (e.g. in Notepad++ or in the dos prompt).

The next activities use a similar approach - but require you to have access to the development environment in Mastodon.

Using CodeCeption


In the next slides we are looking at how to use CodeCeption to test our PHP applications.

CodeCeption can be used to run a wide range of tests, but we are going to focus on acceptance testing.

To use CodeCeption you need command line access. You cannot run the tests simply from a browser interface, but you will need a shell to bootstrap your test environment and create your skeleton files.

CodeCeptions: testing a home page

$I = new AcceptanceTester($scenario);
$I->wantTo('ensure that frontpage works');

Using CodeCeption

To install and bootstrap CodeCeption, please complete the next activity.

You will also need an application to test; as a first step, we can test some of the activities that we have written earlier on.

Activity #1: installing and bootstrapping CodeCeption (1 of 2)

  1. Log in into Mastodon using Putty and get into your public_html folder
  2. Download and install Composer (we will learn more about Composer in the next weeks)
  3. Install CodeCeption 1.8.7 in the root of your application, using composer (follow the tutorial here; if you are using a version of PHP >5.5, install the latest CodeCeption)

The next slide includes the step-by-step instructions to run the installation of CodeCeption.

Activity #1: installing and bootstrapping CodeCeption (2 of 2)

Here is a possible step-by-step solution. Do not just copy and run the code - make sure that you understand it:

php -r "readfile('https://getcomposer.org/installer');" | php
php composer.phar require "codeception/codeception: 1.8.7"
wget http://codeception.com/releases/1.7.4/codecept.phar
php codecept.phar bootstrap

Now edit tests/acceptance.suite.yml : the variable url should point to your web home in mastodon (e.g. http://mastodon.uel.ac.uk/~u1234567/)

Activity #2: writing your first test

The first step to write your first step is to generate a stub (you can replace Welcome in the code sample below with the name of your page/activity/feature):

php codecept.phar generate:cept acceptance Welcome

Edit tests/acceptance/WelcomeCept.php to test the page (please refer to the CodeCeption quickstart and to the documentation).

To run the test, run:

php codecept.phar run

Activity #3: testing your old activities

Write some basic tests for your older activities from the "PHP and NoSQL" lessons:

  • Activity #1: you can test if student u1111112 appears in the page
  • Activity #3: test if the data is saved to MongoDB (by looking at any "success" strings)

CodeCeptions: testing a form

You can use CodeCeption to test is features work as expected. For example, to see if a form is accepting a correct combination of user names and passwords (or refusing a wrong one).

CodeCeptions: testing a form (2)

$I = new AcceptanceTester($scenario);
$I->wantTo('perform actions and see result');
$I->wantTo('log in as regular user');
$I->expect('to be logged in');
$I->see('You are now logged in in the system.')

CodeCeptions: testing a form (3)

This is what you can see after calling the CodeCeption test for that form page:

Test, test, test!

This work

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License

Creative Commons License