Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/Browser/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,21 @@ final public function assertHtml(): HtmlResponse
return $this;
}

/**
* @internal
*/
final public function raw(): string
{
return "{$this->rawMetadata()}\n{$this->rawBody()}";
}

/**
* @internal
*/
final public function isSuccessful(): bool
{
return $this->statusCode() >= 200 && $this->statusCode() < 300;
}

/**
* @internal
*/
final public function isRedirect(): bool
{
return $this->statusCode() >= 300 && $this->statusCode() < 400;
}

/**
* @internal
*/
public function dump(?string $selector = null): void
{
if (null !== $selector) {
Expand Down
34 changes: 34 additions & 0 deletions src/Browser/Response/DomResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Zenstruck\Browser\Response;

use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\VarDumper\VarDumper;
use Zenstruck\Browser\Response;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
abstract class DomResponse extends Response
{
abstract public function crawler(): Crawler;

final public function dump(?string $selector = null): void
{
if (null === $selector) {
parent::dump();

return;
}

$elements = $this->crawler()->filter($selector);

if (0 === $elements->count()) {
throw new \RuntimeException("Element \"{$selector}\" not found.");
}

$elements->each(function(Crawler $node) {
VarDumper::dump($node->outerHtml());
});
}
}
26 changes: 1 addition & 25 deletions src/Browser/Response/HtmlResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
namespace Zenstruck\Browser\Response;

use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\VarDumper\VarDumper;
use Zenstruck\Browser\Response;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
class HtmlResponse extends Response
class HtmlResponse extends DomResponse
{
public function crawler(): Crawler
{
Expand All @@ -18,26 +16,4 @@ public function crawler(): Crawler

return $crawler;
}

/**
* @internal
*/
final public function dump(?string $selector = null): void
{
if (null === $selector) {
parent::dump();

return;
}

$elements = $this->crawler()->filter($selector);

if (0 === $elements->count()) {
throw new \RuntimeException("Element \"{$selector}\" not found.");
}

$elements->each(function(Crawler $node) {
VarDumper::dump($node->html());
});
}
}
3 changes: 0 additions & 3 deletions src/Browser/Response/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public function json()
return \json_decode($this->body(), true, 512, \JSON_THROW_ON_ERROR);
}

/**
* @internal
*/
public function dump(?string $selector = null): void
{
if (null === $selector) {
Expand Down
26 changes: 1 addition & 25 deletions src/Browser/Response/XmlResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
namespace Zenstruck\Browser\Response;

use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\VarDumper\VarDumper;
use Zenstruck\Browser\Response;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class XmlResponse extends Response
final class XmlResponse extends DomResponse
{
public function crawler(): Crawler
{
Expand All @@ -18,26 +16,4 @@ public function crawler(): Crawler

return new Crawler($dom);
}

/**
* @internal
*/
public function dump(?string $selector = null): void
{
if (null === $selector) {
parent::dump();

return;
}

$elements = $this->crawler()->filter($selector);

if (!\count($elements)) {
throw new \RuntimeException("Element \"{$selector}\" not found.");
}

foreach ($elements as $element) {
VarDumper::dump($element->textContent);
}
}
}
4 changes: 2 additions & 2 deletions tests/BrowserKitBrowserTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ public function can_dump_xml_selector(): void
});

$this->assertCount(2, $output);
$this->assertSame('https://www.example.com/page1', $output[0]);
$this->assertSame('https://www.example.com/page2', $output[1]);
$this->assertSame('<loc>https://www.example.com/page1</loc>', $output[0]);
$this->assertSame('<loc attr="attribute">https://www.example.com/page2</loc>', $output[1]);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/BrowserTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ public function can_dump_html_element(): void
});

$this->assertCount(1, $output);
$this->assertSame('<a href="/page2">a link</a> not a link', $output[0]);
$this->assertSame('<p id="link"><a href="/page2">a link</a> not a link</p>', $output[0]);
}

/**
Expand All @@ -415,8 +415,8 @@ public function if_dump_selector_matches_multiple_elements_all_are_dumped(): voi
});

$this->assertCount(2, $output);
$this->assertSame('list 1', $output[0]);
$this->assertSame('list 2', $output[1]);
$this->assertSame('<li>list 1</li>', $output[0]);
$this->assertSame('<li>list 2</li>', $output[1]);
}

/**
Expand Down