Skip to content

Commit cd1f464

Browse files
committed
Use lib-web, add Crawler constraints
1 parent a216c76 commit cd1f464

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
"ext-json": "*",
2424
"ext-mbstring": "*",
2525
"codeception/codeception": "*@dev",
26+
"codeception/lib-web": "^1.0",
2627
"symfony/browser-kit": "^4.4.24 || ^5.4 || ^6.0",
2728
"symfony/dom-crawler": "^4.4.30 || ^5.4 || ^6.0"
2829
},
2930
"require-dev": {
3031
"codeception/util-universalframework": "dev-master"
3132
},
3233
"conflict": {
33-
"codeception/codeception": "<5.0"
34+
"codeception/codeception": "<5.0.0-alpha3"
3435
},
3536
"minimum-stability": "dev",
3637
"autoload": {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Constraint;
6+
7+
use Codeception\Exception\ElementNotFound;
8+
use Codeception\Lib\Console\Message;
9+
use DOMElement;
10+
use PHPUnit\Framework\ExpectationFailedException;
11+
use SebastianBergmann\Comparator\ComparisonFailure;
12+
use Symfony\Component\DomCrawler\Crawler as SymfonyDomCrawler;
13+
14+
use function strpos;
15+
16+
class Crawler extends Page
17+
{
18+
/**
19+
* @param SymfonyDomCrawler $nodes
20+
* @return bool
21+
*/
22+
protected function matches($nodes): bool
23+
{
24+
if (!$nodes->count()) {
25+
return false;
26+
}
27+
if ($this->string === '') {
28+
return true;
29+
}
30+
31+
foreach ($nodes as $node) {
32+
if (parent::matches($node->nodeValue)) {
33+
return true;
34+
}
35+
}
36+
return false;
37+
}
38+
39+
/**
40+
* @param SymfonyDomCrawler $nodes
41+
* @param string $selector
42+
* @param ComparisonFailure|null $comparisonFailure
43+
*/
44+
protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure = null): void
45+
{
46+
if (!$nodes->count()) {
47+
throw new ElementNotFound($selector, 'Element located either by name, CSS or XPath');
48+
}
49+
50+
$output = "Failed asserting that any element by '{$selector}'";
51+
$output .= $this->uriMessage('on page');
52+
$output .= " ";
53+
54+
if ($nodes->count() < 10) {
55+
$output .= $this->nodesList($nodes);
56+
} else {
57+
$message = new Message("[total %s elements]");
58+
$output .= $message->with($nodes->count())->getMessage();
59+
}
60+
$output .= "\ncontains text '{$this->string}'";
61+
62+
throw new ExpectationFailedException(
63+
$output,
64+
$comparisonFailure
65+
);
66+
}
67+
68+
/**
69+
* @param DOMElement[] $other
70+
* @return string
71+
*/
72+
protected function failureDescription($other): string
73+
{
74+
$description = '';
75+
foreach ($other as $o) {
76+
$description .= parent::failureDescription($o->textContent);
77+
}
78+
return $description;
79+
}
80+
81+
protected function nodesList(SymfonyDomCrawler $domCrawler, string $contains = null): string
82+
{
83+
$output = '';
84+
foreach ($domCrawler as $node) {
85+
if ($contains && strpos($node->nodeValue, $contains) === false) {
86+
continue;
87+
}
88+
$output .= "\n+ " . $node->C14N();
89+
}
90+
return $output;
91+
}
92+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\PHPUnit\Constraint;
6+
7+
use PHPUnit\Framework\ExpectationFailedException;
8+
use SebastianBergmann\Comparator\ComparisonFailure;
9+
use Symfony\Component\DomCrawler\Crawler as SymfonyCrawler;
10+
11+
class CrawlerNot extends Crawler
12+
{
13+
/**
14+
* @param SymfonyCrawler $nodes
15+
* @return bool
16+
*/
17+
protected function matches($nodes): bool
18+
{
19+
return !parent::matches($nodes);
20+
}
21+
22+
/**
23+
* @param SymfonyCrawler $nodes
24+
* @param string $selector
25+
* @param ComparisonFailure|null $comparisonFailure
26+
*/
27+
protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure = null): void
28+
{
29+
if (!$this->string) {
30+
throw new ExpectationFailedException(
31+
"Element '{$selector}' was found",
32+
$comparisonFailure
33+
);
34+
}
35+
36+
$output = "There was '{$selector}' element";
37+
$output .= $this->uriMessage('on page');
38+
$output .= $this->nodesList($nodes, $this->string);
39+
$output .= "\ncontaining '{$this->string}'";
40+
41+
throw new ExpectationFailedException(
42+
$output,
43+
$comparisonFailure
44+
);
45+
}
46+
47+
public function toString(): string
48+
{
49+
if ($this->string) {
50+
return 'that contains text "' . $this->string . '"';
51+
}
52+
return '';
53+
}
54+
}

0 commit comments

Comments
 (0)