processwire / processwire-requests

ProcessWire feature requests.
40 stars 0 forks source link

Make ProcessPageView/Session more friendly to automated testing #67

Open LostKobrakai opened 7 years ago

LostKobrakai commented 7 years ago

Short description of the enhancement

The ProcessPageView/Session classes are currently not very friendly in terms of usage inside a testing environment. They're using global calls to header() or session_start(), which do fail in test suites, where processwire is not the only/first thing running (and outputting data).

It would be nice if ProcessPageView could be used to just return the relevant data (output, status code, headers) in an object format for testing. Also the setup of a request could be streamlined, so one does not have to be so verbose as shown below.

Why would the enhancement be useful to users?

It would allow automated tests for the html markup rendered by ProcessWire.

Optional: Screenshots/Links that demonstrate the enhancement

Currently I'm playing with something like this in my testsuite (sessions disabled for now), but with those header() calls all over the place it's obviously not working.

class Crawler
{
    /** @var ProcessWire */
    private $wire;

    public function __construct(ProcessWire $wire)
    {
        $this->wire = $wire;
    }

    public function visit($uri, $method = 'get')
    {
        $_GET = [];
        $_POST = [];

        // Prepare Globals
        $_GET['it'] = $uri;
        $_SERVER['REQUEST_METHOD'] = strtoupper($method);
        $_SERVER['HTTP_HOST'] = $this->wire->config->httpHost;

        // Reset $input
        $this->wire->wire('fuel')->remove('input');
        $this->wire->wire('input', new WireInput(), true);

        $process = $this->wire->modules->get('ProcessPageView');
        $this->wire->wire('process', $process);

        $result = $process->execute(true);
        $process->finished();

        return $result;
    }
}
LostKobrakai commented 7 years ago

Maybe using something similar to this: https://github.com/symfony/http-foundation/blob/master/Response.php