Testing in the PHP w orld
Marcus Börger
PHP Québec Conference 2007
Testing in the PHP w orld Marcus Brger PHP Qubec Conference 2007 - - PDF document
Testing in the PHP w orld Marcus Brger PHP Qubec Conference 2007 The need for Testing Why Test? Introduction to phpt Testing Marcus Brger Testing in the PHP World 2 Why Test? Programming often comes along with code
PHP Québec Conference 2007
Marcus Börger Testing in the PHP World 2
Marcus Börger Testing in the PHP World 3
Code re-use comes along with code changes Code changes are changes
Names can mislead Code may have non obvious side effects
Increasing/ Changing that domain is error prone
A bug fix in one function may affect other functions
Marcus Börger Testing in the PHP World 4
Record problematic input actions and replay them
Integration/ System testing Function testing Unit testing Acceptance/ Requirements testing Regression Testing
Marcus Börger Testing in the PHP World 5
Major is to verify all parts work together When working on real data it can detect system issues
A manual or automated log is required Usually performed/ organized by QA
Marcus Börger Testing in the PHP World 6
Marcus Börger Testing in the PHP World 7
From single routines, to parts (usually not the whole) Test private stuff Analyze untouched code to write more tests
Marcus Börger Testing in the PHP World 8
Develop tests from requirements
Marcus Börger Testing in the PHP World 9
Verify input against expected output
Marcus Börger Testing in the PHP World 10
Marcus Börger Testing in the PHP World 11
Marcus Börger Testing in the PHP World 12
Marcus Börger Testing in the PHP World 13
Marcus Börger Testing in the PHP World 14
Marcus Börger Testing in the PHP World 15
Marcus Börger Testing in the PHP World 16
Marcus Börger Testing in the PHP World 17
Marcus Börger Testing in the PHP World 18
bug< bugid> .phpt bug17123.phpt
< functionname> .phpt dba_open.phpt
< extname> _< num> .phpt dba_003.phpt
Marcus Börger Testing in the PHP World 19
Name Input Expected output
Hel l o W
Hel l o W
Hel l o W
Always output something that can be verified.
Marcus Börger Testing in the PHP World 20
Hel l o W
<?php echo " Hel l o W
Hel l o W
Use only the long version
Marcus Börger Testing in the PHP World 21
Scanf-like expressions
Hel l o W
<?php echo " Hel l o W
Par se er r or : synt ax er r or , unexpect ed $end i n %
d
Do not check directories in error messages.
Marcus Börger Testing in the PHP World 22
Scanf-like expressions
Hel l o W
<?php echo " Hel l o W
Par se er r or : synt ax er r or , unexpect ed $end i n %
d
When executed, the test file has .php ending.
Marcus Börger Testing in the PHP World 23
Scanf-like expressions Regular expressoins
Hel l o W
<?php echo " Hel l o W
EX- - Par se er r or : ( par se| synt ax) er r or , unexpect ed $end i n . * on . *
You can - but don't drop too much: It is "on line".
Marcus Börger Testing in the PHP World 24
Allows to check for exact type Allows to check for private/ protected properties
Var _dum p
<?php var _dum p( NULL) ; Var _dum p( 0) ; Var _dum p( f al se) ; Var _dum p( " " ) ; ?>
NULL i nt ( 0) bool ( f al se) st r i ng( 0) " "
When checking object IDs, use scanf/regex.
Marcus Börger Testing in the PHP World 25
% s Any string % i Integers (includes "–") % d Numbers % f Floating point values % c Single characters % x Hexadecimal values % w Any amount of Whitespace % e DIRECTORY_SEPARATOR ('\ ' or '/ ').
M
<?php $s = ' 123' ; var _dum p( st r _shuf f l e( $s) ) ; var _dum p( $s) ; ?>
st r i ng( 3) " % s" st r i ng( 3) " 123"
Do not use %d for string length, unless you have to.
Marcus Börger Testing in the PHP World 26
M
<?php $s = ' 123' ; var _dum p( st r _shuf f l e( $s) ) ; var _dum p( $s) ; ?>
EX- - st r i ng\ ( 3\ ) " [ 123] { 3} " st r i ng\ ( 3\ ) " 123"
Be as precise as possible in matching expressions.
Marcus Börger Testing in the PHP World 27
O ut put val i dat i on usi ng m d5
<?php $dest = di r nam e( __FI LE__) . ' / bug22544. png' ; @ unl i nk( $dest ) ; i m agePng( i m ageCr eat eTr uecol or ( 640, 100) , $dest ) ; Var _dum p( m d5_f i l e( $dest ) ) ; @ unl i nk( $dest ) ; ?>
St r i ng( 32) " 10a57d09a2c63f ad87b85b38d6b258d6"
Use dirname(__FILE__) as temporary directory.
Marcus Börger Testing in the PHP World 28
O ut put val i dat i on usi ng m d5
<?php $dest = di r nam e( __FI LE__) . ' / bug22544. png' ; @ unl i nk( $dest ) ; i m agePng( i m ageCr eat eTr uecol or ( 640, 100) , $dest ) ; Var _dum p( m d5_f i l e( $dest ) ) ; ?>
<?php @ unl i nk( di r nam e( __FI LE__) . ' / bug22544. png' ) ; ?>
St r i ng( 32) " 10a57d09a2c63f ad87b85b38d6b258d6"
Hide potential notices using the @ operator.
Marcus Börger Testing in the PHP World 29
M
<?php $s = ' 123' ; var _dum p( st r _shuf f l e( $s) ) ; var _dum p( $s) ; ?> ===DO NE=== <?php exi t ( 0) ; ?>
st r i ng( 3) " % s" st r i ng( 3) " 123"
With exit() in tests, no memleaks get reported.
Marcus Börger Testing in the PHP World 30
Si m pl eXM L: At t r i but e cr eat i on
<?php $xm l = ' <?xm l ver si on=" 1. 0" encodi ng=" I SO
$sxe = si m pl exm l _l oad_st r i ng( $xm l ) ; $sxe[ " at t r " ] = " val ue" ; echo $sxe- >asXM L( ) ; __HALT_CO M PI LER( ) ; ?>
<?xm l ver si on=" 1. 0" encodi ng=" I SO
<f oo at t r =" val ue" / >
Here the '<?' in the output would prevent execution.
Marcus Börger Testing in the PHP World 31
Bug #35382 ( Com m ent i n end of f i l e pr oduces f at al er r or )
F- - <?php eval ( " echo ' Hel l o' ; / / com m ent " ) ; echo " W
/ / l ast l i ne com m ent
Hel l o W
Here the 't' of 'comment' is the very last test file byte.
Marcus Börger Testing in the PHP World 32
Check f or exi f _r ead_dat a, unusual I FD st ar t
<?php i f ( ! ext ensi on_l oaded( ' exi f ' ) ) di e( ' ski p exi f n/ a' ) ; ?>
<?php $e=exi f _r ead_dat a( di r nam e( __TEST__) . ' / t est . j pg' , ' ' , t r ue, f al se) ; var _dum p( $e[ ' I FD0' ] [ 0] , $e[ ' I FD0' ] [ 1] ) ; ?>
st r i ng( 11) " I f d00000009" st r i ng( 19) " 2002: 10: 18 20: 06: 00"
Use die() and an explanation in –SKIPIF--.
Marcus Börger Testing in the PHP World 33
It gets evaluated and must return an array Entry ENV contains the environment Entry TESTS contains the test directory/ files
SQ Li t e
<?php # vi m : f t =php i f ( ! ext ensi on_l oaded( ' pdo_sql i t e' ) ) pr i nt ' ski p' ; ?>
/ / no st ar t t ag needed r et ur n ar r ay( ' ENV' => ar r ay( ' PDO TEST_DSN' => ' sql i t e: : m em
' TESTS' => ' ext / pdo/ t est s' ) ;
There is no --FILE-- section in redirect tests.
Marcus Börger Testing in the PHP World 34
POST variables to be passed to the test script.
RAW POST data (doesn't set the Content-Type).
GET variables to be passed to the test script.
Data to be fed to the test script's standard input.
php.ini settings (use one line per setting e.g. foo= bar).
A single line defining the arguments passed to PHP.
Configures the environment to be used for PHP.
Marcus Börger Testing in the PHP World 35
$> expor t TEST_PHP_EXECUTABLE=/ pat h/ t o/ m y/ php $> expor t TEST_PHP_CG I _EXECUTABLE=/ usr / bi n/ php- cgi $> expor t TEST_PHP_USER=/ m y/ t est / f i l e/ di r $> expor t TEST_PHP_ARG S=" - n –q" $> expor t TEST_PHP_LO G _FO RM AT=" " $> m ake t est
All environment variables can be used together.
Marcus Börger Testing in the PHP World 36
$> php r un- t est s. php $> php r un- t est s. php t est s sapi ext $> php r un- t est s. php m yt est . phpt
For help use: php run-tests.php -h
Marcus Börger Testing in the PHP World 37
$> php r un- t est s. php $> php r un- t est s. php t est s sapi ext $> php r un- t est s. php –w m yer r . l st m yt est . phpt $> php r un- t est s. php –l m yer r . l st
There is also –r and -a to work with lists.
Marcus Börger Testing in the PHP World 38
$> php r un- t est s. php - n $> php r un- t est s. php –d zend. ze1_com pat i bi l i t y_m
$> php r un- t est s. php –q $> php r un- t est s. php –s m yt est . r es $> php r un- t est s. php - m
Files + directories must be put right of all options.
Marcus Börger Testing in the PHP World 39
Marcus Börger Testing in the PHP World 40
L Log file, all information in one file E Expected output (--EXPECT--) O Actual output D Difference from expected and actual output
Marcus Börger Testing in the PHP World 41
http: / / somabo.de/ talks/
http: / / qa.php.net/ write-test.php
http: / / sebastian-bergmann.de/ talks/ 2006-11-02- PHPUnit.pdf
http: / / www.lastcraft.com/ simple_test.php
http: / / brainbulb.com/ power-php-testing.pdf