Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DomCrawler][FrameworkBundle] Add assertAnySelectorText* #50306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2023
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
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ CHANGELOG
* Add native return type to `Translator` and to `Application::reset()`
* Deprecate the integration of Doctrine annotations, either uninstall the `doctrine/annotations` package or disable the integration by setting `framework.annotations` to `false`
* Enable `json_decode_detailed_errors` context for Serializer by default if `kernel.debug` is true and the `seld/jsonlint` package is installed
* Add `DomCrawlerAssertionsTrait::assertAnySelectorTextContains(string $selector, string $text)`
* Add `DomCrawlerAssertionsTrait::assertAnySelectorTextSame(string $selector, string $text)`
* Add `DomCrawlerAssertionsTrait::assertAnySelectorTextNotContains(string $selector, string $text)`

6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public static function assertSelectorTextContains(string $selector, string $text
), $message);
}

public static function assertAnySelectorTextContains(string $selector, string $text, string $message = ''): void
{
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new DomCrawlerConstraint\CrawlerSelectorExists($selector),
new DomCrawlerConstraint\CrawlerAnySelectorTextContains($selector, $text)
), $message);
}

public static function assertSelectorTextSame(string $selector, string $text, string $message = ''): void
{
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
Expand All @@ -55,6 +63,14 @@ public static function assertSelectorTextSame(string $selector, string $text, st
), $message);
}

public static function assertAnySelectorTextSame(string $selector, string $text, string $message = ''): void
{
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new DomCrawlerConstraint\CrawlerSelectorExists($selector),
new DomCrawlerConstraint\CrawlerAnySelectorTextSame($selector, $text)
), $message);
}

public static function assertSelectorTextNotContains(string $selector, string $text, string $message = ''): void
{
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
Expand All @@ -63,6 +79,14 @@ public static function assertSelectorTextNotContains(string $selector, string $t
), $message);
}

public static function assertAnySelectorTextNotContains(string $selector, string $text, string $message = ''): void
{
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new DomCrawlerConstraint\CrawlerSelectorExists($selector),
new LogicalNot(new DomCrawlerConstraint\CrawlerAnySelectorTextContains($selector, $text))
), $message);
}

public static function assertPageTitleSame(string $expectedTitle, string $message = ''): void
{
self::assertSelectorTextSame('title', $expectedTitle, $message);
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,30 @@ public function testAssertSelectorTextNotContains()
$this->getCrawlerTester(new Crawler('<html><body><h1>Foo'))->assertSelectorTextNotContains('body > h1', 'Foo');
}

public function testAssertAnySelectorTextContains()
{
$this->getCrawlerTester(new Crawler('<ul><li>Bar</li><li>Foo Baz'))->assertAnySelectorTextContains('ul li', 'Foo');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('matches selector "ul li" and the text of any node matching selector "ul li" contains "Foo".');
$this->getCrawlerTester(new Crawler('<ul><li>Bar</li><li>Baz'))->assertAnySelectorTextContains('ul li', 'Foo');
}

public function testAssertAnySelectorTextSame()
{
$this->getCrawlerTester(new Crawler('<ul><li>Bar</li><li>Foo'))->assertAnySelectorTextSame('ul li', 'Foo');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('matches selector "ul li" and has at least a node matching selector "ul li" with content "Foo".');
$this->getCrawlerTester(new Crawler('<ul><li>Bar</li><li>Baz'))->assertAnySelectorTextSame('ul li', 'Foo');
}

public function testAssertAnySelectorTextNotContains()
{
$this->getCrawlerTester(new Crawler('<ul><li>Bar</li><li>Baz'))->assertAnySelectorTextNotContains('ul li', 'Foo');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('matches selector "ul li" and the text of any node matching selector "ul li" does not contain "Foo".');
$this->getCrawlerTester(new Crawler('<ul><li>Bar</li><li>Foo'))->assertAnySelectorTextNotContains('ul li', 'Foo');
}

public function testAssertPageTitleSame()
{
$this->getCrawlerTester(new Crawler('<html><head><title>Foo'))->assertPageTitleSame('Foo');
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"symfony/console": "^5.4.9|^6.0.9|^7.0",
"symfony/clock": "^6.2|^7.0",
"symfony/css-selector": "^5.4|^6.0|^7.0",
"symfony/dom-crawler": "^6.3|^7.0",
"symfony/dom-crawler": "^6.4|^7.0",
"symfony/dotenv": "^5.4|^6.0|^7.0",
"symfony/polyfill-intl-icu": "~1.0",
"symfony/form": "^5.4|^6.0|^7.0",
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/DomCrawler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

6.4
---

* Add `CrawlerAnySelectorTextContains` test constraint
* Add `CrawlerAnySelectorTextSame` test constraint

6.3
---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DomCrawler\Test\Constraint;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\DomCrawler\Crawler;

final class CrawlerAnySelectorTextContains extends Constraint
{
private string $selector;
private string $expectedText;
private bool $hasNode = false;

public function __construct(string $selector, string $expectedText)
{
$this->selector = $selector;
$this->expectedText = $expectedText;
}

public function toString(): string
{
if ($this->hasNode) {
return sprintf('the text of any node matching selector "%s" contains "%s"', $this->selector, $this->expectedText);
}

return sprintf('the Crawler has a node matching selector "%s"', $this->selector);
}

protected function matches($other): bool
{
if (!$other instanceof Crawler) {
throw new \InvalidArgumentException(sprintf('"%s" constraint expected an argument of type "%s", got "%s".', self::class, Crawler::class, get_debug_type($other)));
}

$other = $other->filter($this->selector);
if (!\count($other)) {
$this->hasNode = false;

return false;
}

$this->hasNode = true;

$nodes = $other->each(fn (Crawler $node) => $node->text(null, true));
$matches = array_filter($nodes, function (string $node): bool {
return str_contains($node, $this->expectedText);
});

return 0 < \count($matches);
}

protected function failureDescription($other): string
{
if (!$other instanceof Crawler) {
throw new \InvalidArgumentException(sprintf('"%s" constraint expected an argument of type "%s", got "%s".', self::class, Crawler::class, get_debug_type($other)));
}

return $this->toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DomCrawler\Test\Constraint;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\DomCrawler\Crawler;

final class CrawlerAnySelectorTextSame extends Constraint
{
private string $selector;
private string $expectedText;

public function __construct(string $selector, string $expectedText)
{
$this->selector = $selector;
$this->expectedText = $expectedText;
}

public function toString(): string
{
return sprintf('has at least a node matching selector "%s" with content "%s"', $this->selector, $this->expectedText);
}

protected function matches($other): bool
{
if (!$other instanceof Crawler) {
throw new \InvalidArgumentException(sprintf('"%s" constraint expected an argument of type "%s", got "%s".', self::class, Crawler::class, get_debug_type($other)));
}

$other = $other->filter($this->selector);
if (!\count($other)) {
return false;
}

$nodes = $other->each(fn (Crawler $node) => trim($node->text(null, true)));

return \in_array($this->expectedText, $nodes, true);
}

protected function failureDescription($other): string
{
if (!$other instanceof Crawler) {
throw new \InvalidArgumentException(sprintf('"%s" constraint expected an argument of type "%s", got "%s".', self::class, Crawler::class, get_debug_type($other)));
}

return 'the Crawler '.$this->toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DomCrawler\Tests\Test\Constraint;

use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestFailure;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerAnySelectorTextContains;

class CrawlerAnySelectorTextContainsTest extends TestCase
{
public function testConstraint()
{
$constraint = new CrawlerAnySelectorTextContains('ul li', 'Foo');

self::assertTrue($constraint->evaluate(new Crawler('<ul><li>Foo</li>'), '', true));
self::assertTrue($constraint->evaluate(new Crawler('<ul><li>Bar</li><li>Foo'), '', true));
self::assertTrue($constraint->evaluate(new Crawler('<ul><li>Bar</li><li>Foo Bar Baz'), '', true));
self::assertFalse($constraint->evaluate(new Crawler('<ul><li>Bar</li><li>Baz'), '', true));

try {
$constraint->evaluate(new Crawler('<ul><li>Bar</li><li>Baz'));

self::fail();
} catch (ExpectationFailedException $e) {
self::assertEquals("Failed asserting that the text of any node matching selector \"ul li\" contains \"Foo\".\n", TestFailure::exceptionToString($e));
}

try {
$constraint->evaluate(new Crawler('<html><head><title>Foobar'));

self::fail();
} catch (ExpectationFailedException $e) {
self::assertEquals("Failed asserting that the Crawler has a node matching selector \"ul li\".\n", TestFailure::exceptionToString($e));

return;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DomCrawler\Tests\Test\Constraint;

use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestFailure;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerAnySelectorTextSame;

final class CrawlerAnySelectorTextSameTest extends TestCase
{
public function testConstraint()
{
$constraint = new CrawlerAnySelectorTextSame('ul li', 'Foo');

self::assertTrue($constraint->evaluate(new Crawler('<ul><li>Foo</li>'), '', true));
self::assertTrue($constraint->evaluate(new Crawler('<ul><li>Bar</li><li>Foo'), '', true));
self::assertFalse($constraint->evaluate(new Crawler('<ul><li>Bar</li><li>Foo Bar Baz'), '', true));
self::assertFalse($constraint->evaluate(new Crawler('<ul><li>Bar</li><li>Baz'), '', true));

try {
$constraint->evaluate(new Crawler('<ul><li>Bar</li><li>Baz'));

self::fail();
} catch (ExpectationFailedException $e) {
self::assertEquals("Failed asserting that the Crawler has at least a node matching selector \"ul li\" with content \"Foo\".\n", TestFailure::exceptionToString($e));
}
}
}