6

I know PHPUnit tests can be executed from the command line, but is there an easy way to run it from the browser. For example, I have:

class testsSuite extends PHPUnit_Framework_TestSuite
{

    public function __construct ()
    {
        $this->setName('testsSuite');
        $this->addTestSuite('MyFirstTest');
    }

    public static function suite ()
    {
        return new self();
    }
}

And then I have:

class MyFirstTest extends PHPUnit_Framework_TestCase
{

    protected function setUp ()
    {        
        parent::setUp();
    }

    protected function tearDown ()
    {
        parent::tearDown();
    }

    public function __construct ()
    {

    }

    public function test__construct ()
    {

    }

    public function test__destruct () {

    }

    public function testCalculateCost ()
    {
        print 1; die();
    }

}

Is there a URL I can enter in my browser to execute this test? Something like:

http://localhost/tests/testsSuite/calculateCost

Or something similar

1

2 Answers 2

4

There is VisualPHPUnit.

At work we sometimes run from browser, using in its basic form a php-script containing:

$argv = array(
    '--configuration', 'phpunit.xml',
    './unit',
);
$_SERVER['argv'] = $argv;


PHPUnit_TextUI_Command::main(false);

So you basically put all parameters you normally type on the commandline in an array, and set it in the $_SERVER-global. PHPUnit_TextUI_Cmmand::main(false); then runs your tests. The false-parameter makes sure no exit() is called. In the PHPUnit.xml I configure to log to a JSON file, and the php-script reads that file to show the results (which it can do after the tests because no exit was called).

Note: this is very barebone, simplistic and crude.

Sign up to request clarification or add additional context in comments.

Comments

2

I have found a solution that works well:

<?php

define("PHPUNIT_COMPOSER_INSTALL","vendor/autoload.php");

require PHPUNIT_COMPOSER_INSTALL;

$query = $_SERVER["QUERY_STRING"];

//$_SERVER["QUERY_STRING"] to $_SERVER['argv'];

$_SERVER['argv'] = explode("&",$query);

//PHPUnit use $_SERVER['argv'] for input value

PHPUnit\TextUI\Command::main(false);

/*Use

command line "./vendor/bin/phpunit param1 param2 param..."

browser URI "localhost?param1&param2&param..."

*/

1 Comment

Code only answers are discouraged. Please add some explanation as to how this solves the problem, or how this differs from the existing answers. From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.