PHP Classes

How to Contribute to PHP Development Writing phpt Tests

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Contribute to ...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  


Viewers: 752

Last month viewers: 4

Categories: PHP Tutorials

The main PHP development comes with test suite that evolved over the years to test all sorts of aspects of the PHP features. The definition of the tests are written in text files in a format called phpt.

Read this article to learn how to create phpt tests, so you can help making PHP a more robust language with the least bugs as possible.

Loaded Article



What is the phpt Test Format?

What Skills are Necessary to Write a phpt Test?

How to Start Writing phpt Tests?

Structure of the phpt file

Writing .phpt Tests and Contribute to PHP Core Development

Are you ready to Help? Then Go!

How do you know if a .phpt test worked?

What is the best way to write a good test?

Scripts to generate test reports

What is PFTT?

.phpt good pratices



There are several types of tests for various purposes, behavior, integration, among others, but when we talk about writing tests, always with an application in mind, let's see how we can write tests using phpt format files.

What is the phpt Test Format?

A phpt test is a little script used by PHP core developers and the QA (Quality Assurance) teams to test the features of PHP. It can be used with new releases to assure that those PHP features continue working as in previous versions. These tests can also help finding bugs in current versions.

What Skills are Necessary to Write a phpt Test?

All that is really needed to write a phpt test is a basic knowledge of the PHP language, a text editor and a way to get the results of your code. That's it.

So if you have written and run PHP scripts, you have everything you need.

How to Start Writing phpt Tests?

The phpt tests files follow a very strict naming convention. This is done to easily identify what each phpt test does. The tests should be named according to the following list:

Test for bugs

This type of test is often used when we have to write a test to ensure that a particular bug already solved actually is working. In this case use the name of the file, bug <bugid>.phpt.

Here is an example of a reported bug and an example of a bug file .phpt written to ensure that this bug this fixed correctly.

Test for a basic behavior of the function

We use this type of test to perform basic tests of a particular functionality of a function and the name of the file is like this, <functionname> _ basic. phpt.

Here is an example of a basic test

Test for a function with error

We use this type of test to write some cases that will result in errors, file nomenclature is so <functionname> _ error. phpt . Here you can check out an example of this type of test.

Test for the variation of a function

In this case, is when we want to perform various types of tests, more beyond _ basic. phpt, in this case we have the option to test variations. So we write a test case in a file <functionname> _ variation1. phpt. Another type of test case in another file <functionname> _ variation2. phpt and so on as in the examples below:







Tests for Extensions

This type of test is used to test for extensions of PHP. Its naming convention is <extname> <no> .phpt . Here is an example of a test for broker. Here example for the mysqli extension.

Structure of the phpt file

Unlike other platforms, in PHP it is suggested that each script is tested anything specific with few lines and to be as objective as possible. This is because when a bug is opened or a refactoring process is necessary, it becomes faster, less expensive and easy to perform debug in a file like this than this for a Python test. :-)

For a complete list of all the tags that you can use to to write phpt scripts, check here.

This is the tag test used to be will's signature appears to be tested.
This tag is priority, if tested something that has external dependencies, the test only continues if this lib or dependence exists.
if (PHP_INT_SIZE != 4) die("skip this test is for 32bit platforms only");
Here you can put data from the dev who wrote the test:
marcosptf - <>
#phptestfest PHPSP on Google - Sao Paulo - Brazil - 2014-06-05
Here's the entire content of the test actually being executed
/* Do not change this test it is a README.TESTING example. */
$trans = array("hello"=>"hi", "hi"=>"hello", "a"=>"A", "world"=>"planet");
var_dump(strtr("# hi all, I said hello world! #", $trans));
string(32) "# hello All, I sAid hi planet! #"
test result when in float values
string(3) "%s"
test result when in regex values
string\(18\) \"nica\x00turska panica\"
string\(19\) \" nica\x00turska panica\"

Writing .phpt Tests and Contribute to PHP Core Development

Now let's apply these concepts to write tests in the official PHP core project.

This is the project that takes care of the main source code of the PHP language, reviews, new features, merge from branches, application of bug fixes, among other things.

I'm sure if you have been programming PHP a while, you already have a notion of this.

PHP is a programming language with the source code open to open source community. Anyone who has interest to help can go in the project pages, download the source, study it, correct, change and propose improvements.

Who takes care of the project are volunteers, people like us who offers a part of his time to commit changes, perform merges of pull request sent, answer some questions of open bugs among other things. There are also the architect heads, which in some cases work for Zend.

In a project of this size and global reach, there are numerous people reporting bugs and creating pull requests all the time, but the number of reviewers is very limited.

If you consider yourself an expert programmer, why not take a few minutes of your time to help PHP getting better? Remember that you used it until today to study and work.

Contributing to the project, is nothing more than saying, "thank you" to the open source community who worked days and nights with the goal to leave the project in a level of maturity that is so great that today it is used and respected by large companies and corporations.

Are you ready to Help? Then Go!

Basically, you can write a phpt test for any one of many PHP functions that are available.

You can write a test for a function of the language basically speaking, a string function, or an array function, or a function provided by one of the countless PHP extensions, a mysqli function, or an image, or a mcrypt function, etc...

You can find the functions that already have phpt tests looking at the HTML version git repository. ext/standard/tests/ is a good place to start looking, though not all the tests are currently there.

If you look at the gcov pages you can see which functions have lots of tests and which need more, although these pages only show which lines of code are covered by test cases.

Even if the coverage information is correct, it can be interesting to have more tests, for example to cover more error cases.

There is also a table that shows which functions and methods are called from PHPT tests.

Look here where you can get a complete list of general coverage of the gcov.

Now if you want to show function tests, is more objective to look if there is any test without coverage or if you want to maintain the tests that failed and help PHP QA to make tests more resilient.

If you want now you can view the project php-src Jenkins and see how is this project status in the different PHP versions.

How do you know if a .phpt test worked?

When a test is called by the run_tests.php, a script takes the various parts of the phpt file to
name and create a .php file. This .php file is executed then. The output of the .php file is then compared with a different section of the phpt file. If the output of the script "matches" the output provided in the .phpt file script, it passed.

What is the best way to write a good test?

Basically you– should try and break the PHP function. It should check not only the parameters of normal functions, but it should also check different use cases. Intentionally generating an error is allowed and encouraged.

After writing the test case, use preferably the phpunit to see if this test pass. Create a fork from php-src project on github.

Create a new branch and perform the commit to that branch. Head over to github and click the option to make a pull request. Your submission will appear here.

Once the pull request sent, Travis-ci kicks in. That is responsible for checking if the branch you submitted will not conflict as rest of the project.

Here follows an example created by Travis-ci containing errors of a branch.

Be patient until a core developers will look at your pull request and verifies that your commit is approved to be integrated.

Scripts to generate test reports

The run-tests.php script is the standard tool for running tests in the PHP core based on .phpt on the command line (CLI scenario). Whenever users compile PHP from the source, they must run ' make test ', whcih executes the run-test.php script on your PHP environment.

What is PFTT?

PFTT is a platform functional test tool to the PHP core and applications developed by Microsoft, primarily for PHP on Microsoft Windows, Windows Server and Azure.

PFTT covers the PHP ecosystem and is designed for convenience and speed.

PFTT, PHPT and PHPUnit tests can run through a variety of scenarios, including in Apache.

.phpt good pratices

Unlike other platforms, in PHP it is suggested that each script tests something specific, with just a few lines and to be as much objective as possible.

Don't forget to perform, commits, creation of branch and submit pull request messages in English, because the core of the project commiters are from different countries around the world, and the English is what we use to communicate.


Nowadays PHP is a language with a complex feature set. There is no shortage of aspects that need to be test so PHP becomes a much bug free as possible.

This article provide a nice introduction to create tests using the .phpt so now you should be ready to also help PHP development by adding much needed tests where they are still lacking.

If you like this article, please share it. If you have a question about PHP tests, post a comment to this article.

You need to be a registered user or login to post a comment

Login Immediately with your account on:


No comments were submitted yet.

  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Contribute to ...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)