Passed
Push — 1.x ( e91c3c...4cbda7 )
by Kevin
01:45
created

HtmlResponse::dump()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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