Passed
Pull Request — 1.x (#27)
by Kevin
02:02
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
class HtmlResponse extends Response
14
{
15
    public function crawler(): Crawler
16
    {
17
        $crawler = new Crawler();
18
        $crawler->addHtmlContent($this->body());
19
20
        return $crawler;
21
    }
22
23
    /**
24
     * @internal
25
     */
26
    final public function dump(?string $selector = null): void
27
    {
28
        if (null === $selector) {
29
            parent::dump();
30
31
            return;
32
        }
33
34
        $elements = $this->session()->getPage()->findAll('css', $selector);
35
        $elements = \array_map(static fn(NodeElement $node) => $node->getHtml(), $elements);
36
37
        if (empty($elements)) {
38
            throw new \RuntimeException("Element \"{$selector}\" not found.");
39
        }
40
41
        foreach ($elements as $element) {
42
            VarDumper::dump($element);
43
        }
44
    }
45
}
46