Passed
Pull Request — 1.x (#28)
by Kevin
02:58 queued 48s
created

HtmlResponse::crawler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Zenstruck\Browser\Response;
4
5
use Behat\Mink\Element\NodeElement;
6
use Symfony\Component\DomCrawler\Crawler;
7
use Symfony\Component\VarDumper\VarDumper;
8
use Zenstruck\Browser\Response;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 *
13
 * @internal
14
 */
15
class HtmlResponse extends Response
16
{
17
    final public function crawler(): Crawler
18
    {
19
        $crawler = new Crawler();
20
        $crawler->addHtmlContent($this->body());
21
22
        return $crawler;
23
    }
24
25
    final public function dump(?string $selector = null): void
26
    {
27
        if (null === $selector) {
28
            parent::dump();
29
30
            return;
31
        }
32
33
        $elements = $this->session()->getPage()->findAll('css', $selector);
34
        $elements = \array_map(static fn(NodeElement $node) => $node->getHtml(), $elements);
35
36
        if (empty($elements)) {
37
            throw new \RuntimeException("Element \"{$selector}\" not found.");
38
        }
39
40
        foreach ($elements as $element) {
41
            VarDumper::dump($element);
42
        }
43
    }
44
}
45