首页 > 解决方案 > 如何对基本功能进行单元测试?

问题描述

我有一个非常有趣的测试场景..

使用线程的系统的核心是一个非类函数。为了向这个函数添加单元测试,我需要这个函数来工作并模拟它包含的类。

函数如下所示:

include_once(__DIR__."/../core/debug.php");
...(A lot of include_once)


/*
 * Run Task
 * This is a PHP script that is ran from the command line.
 * It runs the entire time
 */

ini_set("serialize_precision", -1);


$settings = settings::getInstance();
$settings->load("main");

new debug();

$db = db::getInstance();
$db->connect();

$mongo = mongo::getInstance();
$mongo->connect();
putenv("RUNNING_AS_CLI=1");

\publish\Log::notice("RunTask","Starting command line publish tasks");

putenv("GRACEFULL_EXIT=0");
function gracefullExit($signo, $signinfo){
    echo "Executing gracefull exit... \n";
    putenv("GRACEFULL_EXIT=1");
}

pcntl_async_signals(true);
pcntl_signal(SIGTERM, "gracefullExit");
pcntl_signal(SIGINT, "gracefullExit");
pcntl_signal(SIGHUP, "gracefullExit");
pcntl_signal(SIGQUIT, "gracefullExit");
pcntl_signal(SIGABRT, "gracefullExit");

$stackCounter = 0;
while(true) {
    try {
        // Run task
        \publish\JobEngine::runTask();
    } catch (Exception $e) {
        error_log($e);
    }

    if (getenv("GRACEFULL_EXIT") == "1"){
        echo "Exiting now...\n";
        exit(0);
    }
    // Sleep 300ms
    usleep( 300 * 1000 );

    $stackCounter++;
    if ($stackCounter > 1000){
        // If we runned this more than 1000 times (5 minutes if the thread is idle) restart this thread
        exit(0);
    }
}

这是一个有点复杂的系统。知道如何测试这样的功能吗?

标签: unit-testingphpunitmockery

解决方案


推荐阅读