Skip to content

Commit 2946701

Browse files
committed
added PHPUnit assertions in various components
1 parent 7bf22ea commit 2946701

9 files changed

+390
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
4.3.0
55
-----
66

7+
* Added PHPUnit constraints: `CrawlerSelectorAttributeValueSame`, `CrawlerSelectorExists`, `CrawlerSelectorTextContains``
8+
and `CrawlerSelectorTextSame`
79
* Added return of element name (`_name`) in `extract()` method.
810
* Added ability to return a default value in `text()` and `html()` instead of throwing an exception when node is empty.
911

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DomCrawler\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\DomCrawler\Crawler;
16+
17+
final class CrawlerSelectorAttributeValueSame extends Constraint
18+
{
19+
private $selector;
20+
private $attribute;
21+
private $expectedText;
22+
23+
public function __construct(string $selector, string $attribute, string $expectedText)
24+
{
25+
$this->selector = $selector;
26+
$this->attribute = $attribute;
27+
$this->expectedText = $expectedText;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function toString(): string
34+
{
35+
return sprintf('has a node matching selector "%s" with attribute "%s" of value "%s"', $this->selector, $this->attribute, $this->expectedText);
36+
}
37+
38+
/**
39+
* @param Crawler $crawler
40+
*
41+
* {@inheritdoc}
42+
*/
43+
protected function matches($crawler): bool
44+
{
45+
$crawler = $crawler->filter($this->selector);
46+
if (!\count($crawler)) {
47+
return false;
48+
}
49+
50+
return $this->expectedText === trim($crawler->getNode(0)->getAttribute($this->attribute));
51+
}
52+
53+
/**
54+
* @param Crawler $crawler
55+
*
56+
* {@inheritdoc}
57+
*/
58+
protected function failureDescription($crawler): string
59+
{
60+
return 'the Crawler '.$this->toString();
61+
}
62+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DomCrawler\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\DomCrawler\Crawler;
16+
17+
final class CrawlerSelectorExists extends Constraint
18+
{
19+
private $selector;
20+
21+
public function __construct(string $selector)
22+
{
23+
$this->selector = $selector;
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function toString(): string
30+
{
31+
return sprintf('matches selector "%s"', $this->selector);
32+
}
33+
34+
/**
35+
* @param Crawler $crawler
36+
*
37+
* {@inheritdoc}
38+
*/
39+
protected function matches($crawler): bool
40+
{
41+
return 0 < \count($crawler->filter($this->selector));
42+
}
43+
44+
/**
45+
* @param Crawler $crawler
46+
*
47+
* {@inheritdoc}
48+
*/
49+
protected function failureDescription($crawler): string
50+
{
51+
return 'the Crawler '.$this->toString();
52+
}
53+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DomCrawler\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\DomCrawler\Crawler;
16+
17+
final class CrawlerSelectorTextContains extends Constraint
18+
{
19+
private $selector;
20+
private $expectedText;
21+
22+
public function __construct(string $selector, string $expectedText)
23+
{
24+
$this->selector = $selector;
25+
$this->expectedText = $expectedText;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function toString(): string
32+
{
33+
return sprintf('has a node matching selector "%s" with content containing "%s"', $this->selector, $this->expectedText);
34+
}
35+
36+
/**
37+
* @param Crawler $crawler
38+
*
39+
* {@inheritdoc}
40+
*/
41+
protected function matches($crawler): bool
42+
{
43+
$crawler = $crawler->filter($this->selector);
44+
if (!\count($crawler)) {
45+
return false;
46+
}
47+
48+
return false !== \mb_strpos($crawler->text(), $this->expectedText);
49+
}
50+
51+
/**
52+
* @param Crawler $crawler
53+
*
54+
* {@inheritdoc}
55+
*/
56+
protected function failureDescription($crawler): string
57+
{
58+
return 'the Crawler '.$this->toString();
59+
}
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DomCrawler\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\DomCrawler\Crawler;
16+
17+
final class CrawlerSelectorTextSame extends Constraint
18+
{
19+
private $selector;
20+
private $expectedText;
21+
22+
public function __construct(string $selector, string $expectedText)
23+
{
24+
$this->selector = $selector;
25+
$this->expectedText = $expectedText;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function toString(): string
32+
{
33+
return sprintf('has a node matching selector "%s" with content "%s"', $this->selector, $this->expectedText);
34+
}
35+
36+
/**
37+
* @param Crawler $crawler
38+
*
39+
* {@inheritdoc}
40+
*/
41+
protected function matches($crawler): bool
42+
{
43+
$crawler = $crawler->filter($this->selector);
44+
if (!\count($crawler)) {
45+
return false;
46+
}
47+
48+
return $this->expectedText === trim($crawler->text());
49+
}
50+
51+
/**
52+
* @param Crawler $crawler
53+
*
54+
* {@inheritdoc}
55+
*/
56+
protected function failureDescription($crawler): string
57+
{
58+
return 'the Crawler '.$this->toString();
59+
}
60+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DomCrawler\Tests\Test\Constraint;
13+
14+
use PHPUnit\Framework\ExpectationFailedException;
15+
use PHPUnit\Framework\TestCase;
16+
use PHPUnit\Framework\TestFailure;
17+
use Symfony\Component\DomCrawler\Crawler;
18+
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorAttributeValueSame;
19+
20+
class CrawlerSelectorAttributeValueSameTest extends TestCase
21+
{
22+
public function testConstraint(): void
23+
{
24+
$constraint = new CrawlerSelectorAttributeValueSame('input[name="username"]', 'value', 'Fabien');
25+
$this->assertTrue($constraint->evaluate(new Crawler('<html><body><form><input type="text" name="username" value="Fabien">'), '', true));
26+
$this->assertFalse($constraint->evaluate(new Crawler('<html><head><title>Bar'), '', true));
27+
28+
try {
29+
$constraint->evaluate(new Crawler('<html><head><title>Bar'));
30+
} catch (ExpectationFailedException $e) {
31+
$this->assertEquals("Failed asserting that the Crawler has a node matching selector \"input[name=\"username\"]\" with attribute \"value\" of value \"Fabien\".\n", TestFailure::exceptionToString($e));
32+
33+
return;
34+
}
35+
36+
$this->fail();
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DomCrawler\Tests\Test\Constraint;
13+
14+
use PHPUnit\Framework\ExpectationFailedException;
15+
use PHPUnit\Framework\TestCase;
16+
use PHPUnit\Framework\TestFailure;
17+
use Symfony\Component\DomCrawler\Crawler;
18+
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorExists;
19+
20+
class CrawlerSelectorExistsTest extends TestCase
21+
{
22+
public function testConstraint(): void
23+
{
24+
$constraint = new CrawlerSelectorExists('title');
25+
$this->assertTrue($constraint->evaluate(new Crawler('<html><head><title>'), '', true));
26+
$constraint = new CrawlerSelectorExists('h1');
27+
$this->assertFalse($constraint->evaluate(new Crawler('<html><head><title>'), '', true));
28+
29+
try {
30+
$constraint->evaluate(new Crawler('<html><head><title>'));
31+
} catch (ExpectationFailedException $e) {
32+
$this->assertEquals("Failed asserting that the Crawler matches selector \"h1\".\n", TestFailure::exceptionToString($e));
33+
34+
return;
35+
}
36+
37+
$this->fail();
38+
}
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DomCrawler\Tests\Test\Constraint;
13+
14+
use PHPUnit\Framework\ExpectationFailedException;
15+
use PHPUnit\Framework\TestCase;
16+
use PHPUnit\Framework\TestFailure;
17+
use Symfony\Component\DomCrawler\Crawler;
18+
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorTextContains;
19+
20+
class CrawlerSelectorTextContainsTest extends TestCase
21+
{
22+
public function testConstraint(): void
23+
{
24+
$constraint = new CrawlerSelectorTextContains('title', 'Foo');
25+
$this->assertTrue($constraint->evaluate(new Crawler('<html><head><title>Foobar'), '', true));
26+
$this->assertFalse($constraint->evaluate(new Crawler('<html><head><title>Bar'), '', true));
27+
28+
try {
29+
$constraint->evaluate(new Crawler('<html><head><title>Bar'));
30+
} catch (ExpectationFailedException $e) {
31+
$this->assertEquals("Failed asserting that the Crawler has a node matching selector \"title\" with content containing \"Foo\".\n", TestFailure::exceptionToString($e));
32+
33+
return;
34+
}
35+
36+
$this->fail();
37+
}
38+
}

0 commit comments

Comments
 (0)