Skip to content

Commit

Permalink
Merge branch '6.4' into 7.0
Browse files Browse the repository at this point in the history
* 6.4: (35 commits)
  fix tests
  [DomCrawler][FrameworkBundle] Add `assertAnySelectorText*`
  fix tests
  [SecurityBundle] Add `$badges` argument to `Security::login`
  [FrameworkBundle] Support APP_BUILD_DIR
  [Scheduler] Add --all to debug:schedule
  [Cache] minor cleanup
  [HttpFoundation] Use Symfony exception for request unexpected values
  [Scheduler] Display friendly dates in debug:schedule
  [RemoteEvent][Webhook] Add Brevo support
  [Webhook][RemoteEvent] Add Mailjet support.
  fix typo
  fix minimum required HttpKernel component version
  fix tests
  Remove remaining experimental classes
  [RemoteEvent] Mark component as non experimental
  [Webhook] Mark component as non experimental
  [Process] Fix return type
  [Scheduler] Mark component as non experimental
  [AssetMapper] Mark component as non experimental
  ...
  • Loading branch information
xabbuh committed Aug 4, 2023
2 parents 72681ec + 2463efd commit 714675d
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ CHANGELOG

* Add argument `$normalizeWhitespace` to `Crawler::innerText()`

6.4
---

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

6.3
---

Expand Down
69 changes: 69 additions & 0 deletions Test/Constraint/CrawlerAnySelectorTextContains.php
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 <fabien@symfony.com>
*
* 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();
}
}
57 changes: 57 additions & 0 deletions Test/Constraint/CrawlerAnySelectorTextSame.php
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 <fabien@symfony.com>
*
* 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();
}
}
49 changes: 49 additions & 0 deletions Tests/Test/Constraint/CrawlerAnySelectorTextContainsTest.php
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 <fabien@symfony.com>
*
* 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;
}
}
}
39 changes: 39 additions & 0 deletions Tests/Test/Constraint/CrawlerAnySelectorTextSameTest.php
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 <fabien@symfony.com>
*
* 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));
}
}
}

0 comments on commit 714675d

Please sign in to comment.