Comparé à Python or Ruby, php n'est pas le meilleur language en matière de tests. Heureusement runkit améliore la situation car il permet d'accéder à des méthodes privées ou de redéfinir des méthodes ou fonctions internes. Même si cette extension souffre de bugs génant et n'est pas activement maintenue, elle reste une aide précieuse pour utiliser des mocks et ainsi mettre en place une solide suite de test. Voici comment installer php runkit sous linux debian squeeze.

Récupération et modification de php runkit

La dernière version du package runkit ne fonctionne pas avec PHP >= 5.2, je vais donc prendre la source la plus à jour depuis le svn.

~$ svn co http://svn.php.net/repository/pecl/runkit/trunk runkit

Avant de compiler le code il faut le patcher pour corriger un vilain bug: http://pecl.php.net/bugs/bug.php?id=14086

--- php_runkit.h        31 Mar 2008 10:11:36 -0000      1.31
+++ php_runkit.h        22 Aug 2008 14:09:03 -0000
@@ -159,7 +159,7 @@
#define PHP_RUNKIT_DECL_STRING_PARAM(param)            void *param;int32_t param##_len; zend_uchar param##_type;
#define PHP_RUNKIT_STRING_SPEC                                 "t"
#define PHP_RUNKIT_STRING_PARAM(param)                 &param,&param##_len, &param##_type
-#define PHP_RUNKIT_STRTOLOWER(param)                  php_u_strtolower((UChar*)&param, &param##_len, UG(default_locale))
+#define PHP_RUNKIT_STRTOLOWER(param)                  php_u_strtolower(param, &param##_len, UG(default_locale))
#define PHP_RUNKIT_STRING_LEN(param,addtl)             (param##_type ==IS_UNICODE ? UBYTES(param##_len + (addtl)) : (param##_len + (addtl)))
#define PHP_RUNKIT_STRING_TYPE(param)                  (param##_type)
#define PHP_RUNKIT_HASH_FIND(hash,param,ppvar) zend_u_hash_find(hash,param##_type, (UChar *)param, param##_len + 1, (void**)ppvar)
@@ -173,7 +173,7 @@
#define PHP_RUNKIT_DECL_STRING_PARAM(p)                        char *p;int p##_len;
#define PHP_RUNKIT_STRING_SPEC                                 "s"
#define PHP_RUNKIT_STRING_PARAM(p)                             &p,&p##_len
-#define PHP_RUNKIT_STRTOLOWER(p)                              php_strtolower(&p, &p##_len)
+#define PHP_RUNKIT_STRTOLOWER(p)                              php_strtolower(p, p##_len)
#define PHP_RUNKIT_STRING_LEN(param,addtl)             (param##_len +(addtl))
#define PHP_RUNKIT_STRING_TYPE(param)                  IS_STRING
#define PHP_RUNKIT_HASH_FIND(hash,param,ppvar) zend_hash_find(hash,param, param##_len + 1, (void**)ppvar)
@@ -188,7 +188,7 @@
#define PHP_RUNKIT_DECL_STRING_PARAM(p)                        char *p;int p##_len;
#define PHP_RUNKIT_STRING_SPEC                                 "s"
#define PHP_RUNKIT_STRING_PARAM(p)                             &p,&p##_len
-#define PHP_RUNKIT_STRTOLOWER(p)                              php_strtolower(&p, &p##_len)
+#define PHP_RUNKIT_STRTOLOWER(p)                              php_strtolower(p, p##_len)
#define PHP_RUNKIT_STRING_LEN(param,addtl)             (param##_len +(addtl))
#define PHP_RUNKIT_STRING_TYPE(param)                  IS_STRING
#define PHP_RUNKIT_HASH_FIND(hash,param,ppvar) zend_hash_find(hash,param, param##_len + 1, (void**)ppvar)

Compilation et installation de php runkit

Avant de créer cette extension PHP il nous faut quelques paquets debian.

~$ aptitude install dh-make-php php5-dev

dh-make-php peut télécharger un paquet pour nous mais nous l'avons déjà Smile. A la place il nous faut préparer un faux paquet PECL.

mkdir -p package/runkit-0.9
mv runkit/package*xml package/
mv runkit/* package/runkit-0.9/
cd package/; tar cfz ../runkit-cvs-`date +%Y%m%d%H%M`.tar.gz *; cd ..

Maintenant nous pouvons executer dh-make-pecl avec ce faux paquet et en faire un .deb.

dh-make-pecl --only 5 runkit-cvs*tar.gz
cd php-runkit-0.9
dpkg-buildpackage -rfakeroot

Le paquet est maintenant créé, il ne reste qu'à l'installer et vérifier que le fichier /etc/php5/conf.d/runkit.ini contient :

extension=runkit.so
runkit.internal_override = 1

Vous êtes maintenant prêt à utiliser runkit ! N'oubliez pas de consulter la documentation de référence pour utiliser toutes les possibilités de runkit.

Source : http://www.sebastian.himberger.de/blog/2008/11/23/runkit-with-php-on-linux-debian-etch/