Index: write-test.php =================================================================== RCS file: /repository/qaweb/write-test.php,v retrieving revision 1.12 diff -u -p -d -r1.12 write-test.php --- write-test.php 9 Dec 2006 22:13:01 -0000 1.12 +++ write-test.php 4 Jun 2007 11:32:01 -0000 @@ -107,6 +107,49 @@ as the body of the .php file, so don't f the part used as a comparison to see if the test passes. It is a good idea to generate output with var_dump() calls.
+Sometimes test cases create files or directories as part of the test case and it's important to remove these after the test ends, the --CLEAN-- +section is provided to help with this.
+The PHP code in the --CLEAN-- section is executed separately from the code in the --FILE-- section. For example, this code:
++--TEST-- +Will fail to clean up +--FILE-- +<?php + $temp_filename = "fred.tmp"; + $fp = fopen ($temp_filename, "w"); + fwrite($fp, "Hello Boys!"); + fclose($fp); +?> +--CLEAN-- +<?php + unlink $temp_filename; +?> +--EXPECT-- ++
will not remove the temporary file because the variable $filename is not defined in the --CLEAN-- section.
+Here is a better way to write the code: +
+--TEST-- +This will remove temporary files +--FILE-- +<?php + $temp_filename = dirname(__FILE__)."/fred.tmp"; + $fp = fopen($temp_filename, "w"); + fwrite ($fp, "Hello Boys!\n"); + fclose($fp); +?> +--CLEAN-- +<?php + $temp_filename = dirname(__FILE__)."/fred.tmp"; + unlink($temp_filename); +?> +--EXPECT-- ++
Note the use of the dirname(__FILE__) construct which will ensure that the temporary file is created in the same directory as +the phpt test script. +
+