1

I'm new to PHPUnit. I'm developing on an existing system, adding new features. I want to use PHPUnit to test codes I made. However, the system runs only on server (CGI) environment (access from a browser), and everything breaks while running from command-line.

Is it possible to setting PHPUnit to make a test suite which can be invoked from web browser?

Jackey Cheung
  • 177
  • 2
  • 10

1 Answers1

0

Personally, I'm using windows task manager (cron can do the same thing on linux) to generate my unit test and send it to a text file every night.

Instead of display it directly in the web browser like I do, you could parse the result file directly on your server and then send the html output by email. So you could simply examine the result page on your local machine every morning like I do. Here's some code to start up your solution (on windows/PHP).

In task manager:

Action 1: C:\xampp\htdocs\PC_administration_interface\Controler\Script\launch_automatic_test.bat

Action 2: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

Argument action 2: http://localhost/PC_administration_interface/view/unit_test.php?DISPLAY_RESULT=TRUE

Launch_automatic_test.bat

@echo off

call %~DP0\launch_xampp.bat

call %~DP0\..\..\..\..\xampp_shell.bat

cls

call %~DP0\run_unit_test.bat > "%~DP0\..\test_result.txt"

launch_xampp_bat

@echo off
tasklist | find /i "xampp-control.exe" && goto :eof

start /b "xampp-control.exe" "C:\xampp\xampp-control.exe"

And here's a sample of run_unit_tets.bat

@ECHO OFF

CLS

REM GEM_MECHANIC_TESTS

ECHO.
ECHO FILE : GEM_MECHANIC_MANAGER_TEST.PHP
ECHO.
CALL PHPUNIT %~DP0/../../../GEM_MECHANIC/CONTROLER/TEST/GEM_MECHANIC_MANAGER_TEST.PHP
ECHO.
ECHO.

REM GEM_MECHANIC_INTERFACE_BUILDER_TESTS
ECHO.
ECHO FILE : HTML_GEM_MECHANIC_MANAGER_TEST.PHP
ECHO.
CALL PHPUNIT %~DP0/../../../GEM_MECHANIC/CONTROLER/TEST/TEST_INTERFACE_BUILDER/HTML_GEM_MECHANIC_MANAGER_TEST.PHP
ECHO.
ECHO.

Then I'm launching a web page that parse my result and display it in the web browser:

    public static function fetchTestResult(&$errorMessage){

    $echoString = '';
    $failure = false;
    $fileContent = '';
    $errorMessage = '';

    $dbManager = GeneralDbManager::getInstance();

    if (file_exists(realpath(dirname(__FILE__)) . '/test_result.txt')) {

        @$fileContent = file(realpath(dirname(__FILE__)) . '/test_result.txt');

        if ($fileContent === false){

            $errorMessage = $dbManager->getErrorMessage('UNIT_TEST_RESULT_LOG_ERR', "An error happened while reading the unit tests results log."); 
        }
        else{

            unlink(realpath(dirname(__FILE__)) . '/test_result.txt');

            if (file_exists(realpath(dirname(__FILE__)) . '/script/temp_unit_test.bat')) {

                unlink(realpath(dirname(__FILE__)) . '/script/temp_unit_test.bat');
            }

            $echoString = HtmlTagBuilder::createCustomTextArea("TA_UNIT_TEST_RESULT", TextAlign::ALIGN_LEFT, false, 1100, 500);

            foreach ($fileContent as $line){

                if (StringManager::stringStartWith($line, "FILE :")){

                    $failure = false;

                    $echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::TITLE_LINE);
                }
                elseif (StringManager::stringStartWith($line, "time:")){

                    $echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::WARNING_LINE);
                }
                elseif (StringManager::stringStartWith($line, "OK (")){

                    $echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::SUCCESS_LINE);
                }
                elseif ((StringManager::stringStartWith($line, "There ") and strpos($line, "failure") !== false)
                    or $failure === true){

                    $failure = true;

                    $echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::ERROR_LINE);
                }
                elseif (strpos(strtolower($line), "failure") !== false){

                    $echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::ERROR_LINE);
                }
                else{

                    $echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::REGULAR_LINE);  
                }
            }

            $echoString .= '</DIV><br><br>';
        }

    }
    else{
        $errorMessage = $dbManager->getErrorMessage('UNIT_TEST_NO_RESULT_LOG_ERR', "You must run the unit test and generate the test log before displaying it.");
    }

    return $echoString;
}

Additional note: I'm currently getting some information about CGI and it seem that it is pretty inefficient. You might also consider take a look at this post (question and answers):

What is Common Gateway Interface (CGI)?

Community
  • 1
  • 1