PHPUnit:如何从浏览器运行测试?

cod*_*ama 5 phpunit zend-framework

我知道 PHPUnit 测试可以从命令行执行,但是有没有一种简单的方法可以从浏览器运行它。例如,我有:

class testsSuite extends PHPUnit_Framework_TestSuite
{

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

    public static function suite ()
    {
        return new self();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我有:

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

}
Run Code Online (Sandbox Code Playgroud)

是否有可以在浏览器中输入的 URL 来执行此测试?就像是:

http://localhost/tests/testsSuite/calculateCost
Run Code Online (Sandbox Code Playgroud)

或者类似的东西

小智 2

我找到了一个效果很好的解决方案:

<?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..."

*/
Run Code Online (Sandbox Code Playgroud)