Skip to content

Commit

Permalink
Merge pull request #56 from Codeception/use-lib-web
Browse files Browse the repository at this point in the history
Extract constraints from codeception/codeception
  • Loading branch information
Naktibalda authored Mar 11, 2022
2 parents a216c76 + cd1f464 commit e2fa224
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
"ext-json": "*",
"ext-mbstring": "*",
"codeception/codeception": "*@dev",
"codeception/lib-web": "^1.0",
"symfony/browser-kit": "^4.4.24 || ^5.4 || ^6.0",
"symfony/dom-crawler": "^4.4.30 || ^5.4 || ^6.0"
},
"require-dev": {
"codeception/util-universalframework": "dev-master"
},
"conflict": {
"codeception/codeception": "<5.0"
"codeception/codeception": "<5.0.0-alpha3"
},
"minimum-stability": "dev",
"autoload": {
Expand Down
92 changes: 92 additions & 0 deletions src/Codeception/Constraint/Crawler.php
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;
}
}
54 changes: 54 additions & 0 deletions src/Codeception/Constraint/CrawlerNot.php
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 '';
}
}

0 comments on commit e2fa224

Please sign in to comment.