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

HtmlResponse   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
dl 0
loc 29
rs 10
c 1
b 0
f 1
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dump() 0 17 4
A find() 0 7 2
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