Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
85.71% |
6 / 7 |
CRAP | |
90.48% |
19 / 21 |
| WebPage | |
0.00% |
0 / 1 |
|
85.71% |
6 / 7 |
11.10 | |
90.48% |
19 / 21 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| getUrl | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| hasForm | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| getFormByName | |
0.00% |
0 / 1 |
3.58 | |
60.00% |
3 / 5 |
|||
| queryDom | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| getDomQueryHandler | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
| getDom | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
| <?php | |
| namespace Luxian\Test; | |
| use DOMDocument; | |
| use DOMNode; | |
| use DOMNodeList; | |
| use DOMXPath; | |
| use Luxian\Http\Request; | |
| use Luxian\Http\Response; | |
| use Luxian\Type\Url; | |
| use UnexpectedValueException; | |
| 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(string $name): bool | |
| { | |
| $form = $this->queryDom('//form[@name="' . $name .'"]'); | |
| return $form->count() > 0; | |
| } | |
| public function getFormByName(string $name): DOMNode | |
| { | |
| $form = $this->queryDom('//form[@name="' . $name .'"]'); | |
| if ($form->length === 0 || $form->item(0) === null) { | |
| $error = sprintf('No form found with the name "%s"', $name); | |
| throw new UnexpectedValueException($error); | |
| } | |
| return $form->item(0); | |
| } | |
| private function queryDom(string $selector): DOMNodeList | |
| { | |
| $xpath = $this->getDomQueryHandler(); | |
| return $xpath->query($selector); | |
| } | |
| private function getDomQueryHandler(): DOMXPath | |
| { | |
| if (!isset($this->xpath)) { | |
| $this->xpath = new DOMXPath($this->getDom()); | |
| } | |
| return $this->xpath; | |
| } | |
| private function getDom(): DOMDocument | |
| { | |
| if (!isset($this->dom)) { | |
| $this->dom = new DOMDocument(); | |
| $this->dom->loadHTML($this->response->getBody()); | |
| } | |
| return $this->dom; | |
| } | |
| } |