How to unit test a basic function?

Viewed 28

I have a very interesting test scenario..

At the heart of a system working with threads is a non-class function. In order to add a unit test to this function, I need this function to work and mock the classes it includes.

Function looks like this:

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);
    }
}

It's a somewhat complex system. Any idea how I can test such a function?

0 Answers
Related