-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from Codeception/use-lib-web
Extract constraints from codeception/codeception
- Loading branch information
Showing
3 changed files
with
148 additions
and
1 deletion.
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,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codeception\Constraint; | ||
|
||
use Codeception\Exception\ElementNotFound; | ||
use Codeception\Lib\Console\Message; | ||
use DOMElement; | ||
use PHPUnit\Framework\ExpectationFailedException; | ||
use SebastianBergmann\Comparator\ComparisonFailure; | ||
use Symfony\Component\DomCrawler\Crawler as SymfonyDomCrawler; | ||
|
||
use function strpos; | ||
|
||
class Crawler extends Page | ||
{ | ||
/** | ||
* @param SymfonyDomCrawler $nodes | ||
* @return bool | ||
*/ | ||
protected function matches($nodes): bool | ||
{ | ||
if (!$nodes->count()) { | ||
return false; | ||
} | ||
if ($this->string === '') { | ||
return true; | ||
} | ||
|
||
foreach ($nodes as $node) { | ||
if (parent::matches($node->nodeValue)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @param SymfonyDomCrawler $nodes | ||
* @param string $selector | ||
* @param ComparisonFailure|null $comparisonFailure | ||
*/ | ||
protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure = null): void | ||
{ | ||
if (!$nodes->count()) { | ||
throw new ElementNotFound($selector, 'Element located either by name, CSS or XPath'); | ||
} | ||
|
||
$output = "Failed asserting that any element by '{$selector}'"; | ||
$output .= $this->uriMessage('on page'); | ||
$output .= " "; | ||
|
||
if ($nodes->count() < 10) { | ||
$output .= $this->nodesList($nodes); | ||
} else { | ||
$message = new Message("[total %s elements]"); | ||
$output .= $message->with($nodes->count())->getMessage(); | ||
} | ||
$output .= "\ncontains text '{$this->string}'"; | ||
|
||
throw new ExpectationFailedException( | ||
$output, | ||
$comparisonFailure | ||
); | ||
} | ||
|
||
/** | ||
* @param DOMElement[] $other | ||
* @return string | ||
*/ | ||
protected function failureDescription($other): string | ||
{ | ||
$description = ''; | ||
foreach ($other as $o) { | ||
$description .= parent::failureDescription($o->textContent); | ||
} | ||
return $description; | ||
} | ||
|
||
protected function nodesList(SymfonyDomCrawler $domCrawler, string $contains = null): string | ||
{ | ||
$output = ''; | ||
foreach ($domCrawler as $node) { | ||
if ($contains && strpos($node->nodeValue, $contains) === false) { | ||
continue; | ||
} | ||
$output .= "\n+ " . $node->C14N(); | ||
} | ||
return $output; | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codeception\PHPUnit\Constraint; | ||
|
||
use PHPUnit\Framework\ExpectationFailedException; | ||
use SebastianBergmann\Comparator\ComparisonFailure; | ||
use Symfony\Component\DomCrawler\Crawler as SymfonyCrawler; | ||
|
||
class CrawlerNot extends Crawler | ||
{ | ||
/** | ||
* @param SymfonyCrawler $nodes | ||
* @return bool | ||
*/ | ||
protected function matches($nodes): bool | ||
{ | ||
return !parent::matches($nodes); | ||
} | ||
|
||
/** | ||
* @param SymfonyCrawler $nodes | ||
* @param string $selector | ||
* @param ComparisonFailure|null $comparisonFailure | ||
*/ | ||
protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure = null): void | ||
{ | ||
if (!$this->string) { | ||
throw new ExpectationFailedException( | ||
"Element '{$selector}' was found", | ||
$comparisonFailure | ||
); | ||
} | ||
|
||
$output = "There was '{$selector}' element"; | ||
$output .= $this->uriMessage('on page'); | ||
$output .= $this->nodesList($nodes, $this->string); | ||
$output .= "\ncontaining '{$this->string}'"; | ||
|
||
throw new ExpectationFailedException( | ||
$output, | ||
$comparisonFailure | ||
); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
if ($this->string) { | ||
return 'that contains text "' . $this->string . '"'; | ||
} | ||
return ''; | ||
} | ||
} |