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

HtmlResponse::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 1
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