Manual:Writing testable PHP code/fr

Voici quelques notes sur la manière d'écrire du code testable avec des séquences à développer.

Ne présumez pas du contexte global
Accessing global variables (e.g. ) without declaring them first with the   keyword will cause failures and E_NOTICE messages to be generated if they are accessed in a non-global context.

Ne créez pas de variables globales supplémentaires
While putting information in global variables seems easy, it makes the code less predictable. By relying on global variables, you are making it difficult to isolate functionality. A singleton class is better for testing (but, still, less than ideal).

Appuyez-vous sur les entrées directes
While this is not always achievable, it is best to write code that depends only on direct inputs. That is, a class only uses the information it is passed and does not rely on singletons or globals to get “out-of-band” information.

N'utilisez pas
Exiting from a script abruptly should almost never be done. Doing so will make your code untestable by PHPUnit. If your code encounters an unexpected error, the proper thing to do is throw an exception like:

This will allow PHPUnit and MediaWiki to exit properly and provide informative information such as stack traces to developers.

Ressources externes

 * Discussion technique sur la façon d'écrire du code propre et testable par Miško Hevery