-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
5 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
Tests/Test/Constraint/CrawlerAnySelectorTextContainsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |