Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
15 / 15
WebPage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
15 / 15
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 getUrl
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 hasForm
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 getDom
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 getDomQueryHandler
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
<?php
namespace Luxian\Test;
use DOMDocument;
use DOMXPath;
use Luxian\Http\Request;
use Luxian\Http\Response;
use Luxian\Type\Url;
class WebPage
{
    /** @var Request */
    private $request;
    /** @var Response */
    private $response;
    /** @var WebPage|null  */
    private $previous;
    /** @var DOMDocument|null */
    private $dom;
    /** @var DOMXPath|null */
    private $xpath;
    public function __construct(
        Request $request,
        Response $response,
        WebPage $previous = null
    )
    {
        $this->request = $request;
        $this->response = $response;
        $this->previous = $previous;
    }
    public function getUrl(): Url
    {
        return Url::fromString($this->request->getRequestUriPath());
    }
    public function hasForm(): bool
    {
        $xpath = $this->getDomQueryHandler();
        $form = $xpath->query('//form');
        return $form !== false && $form->count();
    }
    private function getDom(): DOMDocument
    {
        if (!isset($this->dom)) {
            $this->dom = new DOMDocument();
            $this->dom->loadHTML($this->response->getBody());
        }
        return $this->dom;
    }
    private function getDomQueryHandler(): DOMXPath
    {
        if (!isset($this->xpath)) {
            $this->xpath = new DOMXPath($this->getDom());
        }
        return $this->xpath;
    }
}