Growing the PHP core
- one test at a time -

Florian Engelhardt

Husband and dad

Nobody will ever use this
— Florian Engelhardt

If I have seen further it is only by standing on the shoulders of giants
— Sir Isaac Newton

PHP TestFest 2017

Compiling PHP from source

git clone [email protected]:php/php-src.git
cd php-src
./buildconf
./configure
make -j
./sapi/cli/php -v

* fork before cloning

Run the tests

make TEST_PHP_ARGS=-j`nproc` test

Test result summary

=====================================================================
TEST RESULT SUMMARY
---------------------------------------------------------------------
Exts skipped    :    47 (gmp, ftp, imap, dl_test, calendar, pspell, snmp, sysvshm, sockets, iconv, sysvmsg, intl, sysvsem, dba, ldap, skeleton, readline, mysqlnd, bz2, pdo_odbc, soap, pdo_mysql, zip, curl, enchant, tidy, oci8, pdo_pgsql, ffi, odbc, gettext, mysqli, exif, pgsql, zend_test, gd, sodium, pcntl, com_dotnet, shmop, openssl, xsl, bcmath, pdo_dblib, pdo_oci, pdo_firebird, mbstring)
Exts tested     :    26
---------------------------------------------------------------------

Number of tests : 17774             12397
Tests skipped   :  5377 ( 30.3%) --------
Tests warned    :     7 (  0.0%) (  0.1%)
Tests failed    :     0 (  0.0%) (  0.0%)
Expected fail   :    25 (  0.1%) (  0.2%)
Tests passed    : 12365 ( 69.6%) ( 99.7%)
---------------------------------------------------------------------
Time taken      :    52 seconds
=====================================================================

Basic Test

--TEST--
strlen() function
--EXTENSIONS--
core
--SKIPIF--
<?php
if (!function_exists('strlen')) echo 'skip';
?>
--FILE--
<?php
var_dump(strlen('Hello World!'));
?>
--EXPECT--
int(12)
--CLEAN--
<?php
unlink(__DIR__.'/file.tmp');
?>

What to test?

https://app.codecov.io/github/php/php-src

Find something red

ext/zlib/zlib.c in PHP 7.1

Write a test

  • the absence of the Accept-Encoding header
  • the Accept-Encoding being gzip
  • the Accept-Encoding being deflate
  • the Accept-Encoding being anything else

DEMO TIME!

--TEST--
zlib_get_coding_type() with gzip encoding
--EXTENSIONS--
zlib
--ENV--
HTTP_ACCEPT_ENCODING=gzip
--FILE--
<?php
ini_set('zlib.output_compression', 'Off');
$off = zlib_get_coding_type();
ini_set('zlib.output_compression', 'On');
$on = zlib_get_coding_type();
ini_set('zlib.output_compression', 'Off');
var_dump($off, $on);
?>
--EXPECT--
bool(false)
string(4) "gzip"

ext/zlib/zlib.c in PHP 7.3

What did I learn

  • what looks simple, might be complex
  • super globals are copy-on-write
  • there are modify handlers for ini settings

What do you gain?

  • PHP will be more stable and reliable
  • deeper understanding of how PHP works internaly
  • phpt test format can be used in PHPUnit

You can call yourself
a PHP contributor!

Want to know more?