| You may need some kind of unit testing when you develop a project composed of multiple PHP files. Below code block is what I am using for unit testing if (is_array($argv) && ($argv[0]==basename(__FILE__))) foreach($argv as $v) if ($v=="-d")
{
// write your test code here
echo <<< CLI_MESSAGE
===================
TEST COMPLETED !
\n\n
CLI_MESSAGE;
}
You can test your code as following on the command line: php <your filename> -d
Below is the class template I use <?php
# Copyright by Chun Kang <http://qsok.com>
#
# @file yourfilename.php
# @brief your class library
# @author Chun Kang (ck@ckii.com)
#
# @notes
# 2024.06.03 created
#
#
global $ck;
$__class_obj_name = pathinfo(__FILE__)['filename'];
if (@is_object($ck[ $__class_obj_name ]) == false)
{
// beginning of class ------------------------------------------------------------------------------------------------------
class TCKYOURCLASS
{
function __construct()
{
}
};
// end of class ------------------------------------------------------------------------------------------------------
if (!is_array($ck)) $ck=[];
$ck[ $__class_obj_name ] = new TCKYOURCLASS;
if (!function_exists( 'MYFUNCTION' ) )
{
function MYFUNCTION()
{
}
}
/*
test code
-----------
php <YOURFILENAME> -d
*/
if (is_array($argv) && ($argv[0]==basename(__FILE__))) foreach($argv as $v) if ($v=="-d")
{
// write your test code here
echo <<< CLI_MESSAGE
===================
TEST COMPLETED !
\n\n
CLI_MESSAGE;
}
}
// removed end of PHP tag intentionally to avoid unintended CR+LF at the code
I hope this is helpful for you |