Book a Free Consultation

How to test PHP applications

how to test php applications

How to test PHP applications

You can write even a hundred programs and you still can’t guarantee they all will operate correctly. Despite the programmer’s experience and skills, testing is an integral element of app and website development. PHP is no different. How to perform such tests, so that they give a proper amount of information and allow us to make changes? Check for yourself.

Why should we perform tests?

Software tests should be a fixed point in creating every app and website. And there are multiple reasons why:

  • improving software quality
  • removing errors
  • reduced risk in case of code changes
  • better security and fixing vulnerabilities
  • and many more

Testing websites and programs became a standard these days, and it should never be disregarded.

Unit testing

The most common testing is unit testing. It’s connected to individual code elements, such as class or function. Those are small code fragments, which perform only one particular task, therefore it’s easier to check them and fix bugs. A specific number of tests is created and that number is equal to the number of possible paths the program can take while operating. That way each and every function can be tested thoroughly.

PHPUnit library is used in PHP testing. It allows to create tests performing a specific function and checking how the website or app will response. For example a function reducing the number of products in a store should cause actual reduction in number of items. To verify this value you can compare an achieved value to the expected value. 

When it comes to unit testing, it’s worth to follow a certain pattern, called AAA – Arrange, Act and Assert. Arrange means preparing data that allows for testing specific functions fully. Act means testing by function calls. Assert means to compare results to expectations and evaluating if the function works correctly.

That way the course of the tests is more legible and results unequivocal.

In unit testing, it’s worth to remember about unique values, which usually can cause errors. Those can be bounding values, zero or incorrect input data. Tests show how a specific function will response if it will receive an incorrect data.

Double tests

You don’t have to test every dependency in a separate test. You can also create a decoy, which substitutes a real object for the purpose of testing. Thanks to additional mockups you can carefully control the order of code execution, controlling all the dependencies at the same time. It’s easy to overlook a single function, which gives back the same value over and over, that’s why additional elements are necessary. 

That kind of testing is usually used when new solutions are implemented. Integration testing often complements it.

Integration testing

It’s a great complement for unit testing. They focus on a higher level, where individual modules connect. Despite them being dependent on certain configurations, related, for example to databases, they can detect much more. Unfortunately, they require more time.

In that case PHPUnit works perfect.

Acceptance testing

Acceptance testing works on a slightly different principle. In that case the focus is put on app requirements. One of the often used acceptance tests is releasing a trial version, which is tested by the end users. 

Different method is using BDD, or Behavior-Driven Development. It’s based on writing tests and documentation I a natural language called Gherkin. It was created specially for describing behaviors. In case of PHP, there are Behat and PHPSpec – tools for creating acceptance tests using BDD. Creating tests using PHPUnit much less popular, but not impossible.

Rules of testing in Gherkin are very similar to unit testing. There are also 3 principles, named Given, When and Then. First means accumulating data for testing, second – launching function and the last one means comparing results to expectations.

Thanks to a natural language, acceptance tests are understandable by non-technical people. It allows to determine if the program runs correctly, according to a customer’s concept. Whereas Behat transforms a logic, natural language for codes and functions, which allows PHP to perform all the tasks according to plan.

Mutation tests

It’s a good way of increasing quality of unit tests. They introduce mutations, meaning single changes in source code. It should cause malfunction of the testing unit. It’s a part of statistical analysis of the code, which isn’t a test in the strict sense, but it allows to improve an existing code significantly. Programs such as PHPStan, EasyCodingStandard or PHP Mess Detector are very helpful in such analysis. It’s worth getting familiar with this subject to improve your code even further.

Architecture tests

Architecture tests are also a part of statistical analysis of the code. It allows to make sure there aren’t any unwanted connections between individual structures of the code. With the help of DEPTRAC you can examine a code’s layout and optimize it, for it to operate even better.

Manual tests

Manual testing is equally important. Though it seems useless with automated testing, it is an important element verifying a program or website. Examine every possible path a potential customer can take to check if any of them reveals any kind of problem.

Efficiency

Correct operation of individual functions and giving correct results is only half the success. System’s efficiency also important. An app has to respond to thousand requests every second. To determine if it will manage to do that you can use a program called K6. With it’s help you can create a simple script in JavaScript, which will create virtual users sending requests to server.

How long should a test run?

There are many types of tests used to examine various aspects of app operation. Every type is equally important and shouldn’t be skipped. Precise assessment which tests are the most important and have to be performed is possible thanks to testing pyramid. It’s a simple tool, the basis of which are unit tests. Their main advantages are execution speed and ease of creation. Every higher level means more complicated tests, which verify more sophisticated aspects of the code. Up until testing real users’ scenarios.

Leave a Comment

Your email address will not be published.

Facebook
LinkedIn