This commit is contained in:
2020-10-06 14:27:47 +07:00
commit 586be80cf6
16613 changed files with 3274099 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
<?php
namespace Codeception\PHPUnit\Constraint;
use Codeception\Exception\ElementNotFound;
use Codeception\Lib\Console\Message;
use Symfony\Component\DomCrawler\Crawler as DomCrawler;
use SebastianBergmann\Comparator\ComparisonFailure;
class Crawler extends Page
{
protected function matches($nodes)
{
/** @var $nodes DomCrawler * */
if (!$nodes->count()) {
return false;
}
if ($this->string === '') {
return true;
}
foreach ($nodes as $node) {
if (parent::matches($node->nodeValue)) {
return true;
}
}
return false;
}
protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure = null)
{
/** @var $nodes DomCrawler * */
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 \PHPUnit\Framework\ExpectationFailedException(
$output,
$comparisonFailure
);
}
protected function failureDescription($other)
{
$desc = '';
foreach ($other as $o) {
$desc .= parent::failureDescription($o->textContent);
}
return $desc;
}
protected function nodesList(DomCrawler $nodes, $contains = null)
{
$output = "";
foreach ($nodes as $node) {
if ($contains && strpos($node->nodeValue, $contains) === false) {
continue;
}
$output .= "\n+ " . $node->C14N();
}
return $output;
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace Codeception\PHPUnit\Constraint;
use SebastianBergmann\Comparator\ComparisonFailure;
class CrawlerNot extends Crawler
{
protected function matches($nodes)
{
return !parent::matches($nodes);
}
protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure = null)
{
if (!$this->string) {
throw new \PHPUnit\Framework\ExpectationFailedException(
"Element '$selector' was found",
$comparisonFailure
);
}
/** @var $nodes DomCrawler * */
$output = "There was '$selector' element";
$output .= $this->uriMessage('on page');
$output .= $this->nodesList($nodes, $this->string);
$output .= "\ncontaining '{$this->string}'";
throw new \PHPUnit\Framework\ExpectationFailedException(
$output,
$comparisonFailure
);
}
public function toString()
{
if ($this->string) {
return 'that contains text "' . $this->string . '"';
}
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace Codeception\PHPUnit\Constraint;
use SebastianBergmann\Comparator\ComparisonFailure;
use SebastianBergmann\Comparator\ArrayComparator;
use SebastianBergmann\Comparator\Factory;
use Codeception\Util\JsonArray;
class JsonContains extends \PHPUnit\Framework\Constraint\Constraint
{
/**
* @var
*/
protected $expected;
public function __construct(array $expected)
{
parent::__construct();
$this->expected = $expected;
}
/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
*
* @param mixed $other Value or object to evaluate.
*
* @return bool
*/
protected function matches($other)
{
$jsonResponseArray = new JsonArray($other);
if (!is_array($jsonResponseArray->toArray())) {
throw new \PHPUnit\Framework\AssertionFailedError('JSON response is not an array: ' . $other);
}
if ($jsonResponseArray->containsArray($this->expected)) {
return true;
}
$comparator = new ArrayComparator();
$comparator->setFactory(new Factory);
try {
$comparator->assertEquals($this->expected, $jsonResponseArray->toArray());
} catch (ComparisonFailure $failure) {
throw new \PHPUnit\Framework\ExpectationFailedException(
"Response JSON does not contain the provided JSON\n",
$failure
);
}
}
/**
* Returns a string representation of the constraint.
*
* @return string
*/
public function toString()
{
//unused
return '';
}
protected function failureDescription($other)
{
//unused
return '';
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace Codeception\PHPUnit\Constraint;
use Codeception\Util\JsonType as JsonTypeUtil;
use Codeception\Util\JsonArray;
class JsonType extends \PHPUnit\Framework\Constraint\Constraint
{
protected $jsonType;
private $match;
public function __construct(array $jsonType, $match = true)
{
parent::__construct();
$this->jsonType = $jsonType;
$this->match = $match;
}
/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
*
* @param mixed $jsonArray Value or object to evaluate.
*
* @return bool
*/
protected function matches($jsonArray)
{
if ($jsonArray instanceof JsonArray) {
$jsonArray = $jsonArray->toArray();
}
$matched = (new JsonTypeUtil($jsonArray))->matches($this->jsonType);
if ($this->match) {
if ($matched !== true) {
throw new \PHPUnit\Framework\ExpectationFailedException($matched);
}
} else {
if ($matched === true) {
throw new \PHPUnit\Framework\ExpectationFailedException('Unexpectedly response matched: ' . json_encode($jsonArray));
}
}
return true;
}
/**
* Returns a string representation of the constraint.
*
* @return string
*/
public function toString()
{
//unused
return '';
}
protected function failureDescription($other)
{
//unused
return '';
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace Codeception\PHPUnit\Constraint;
use Codeception\Lib\Console\Message;
class Page extends \PHPUnit\Framework\Constraint\Constraint
{
protected $uri;
protected $string;
public function __construct($string, $uri = '')
{
parent::__construct();
$this->string = $this->normalizeText((string)$string);
$this->uri = $uri;
}
/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
*
* @param mixed $other Value or object to evaluate.
*
* @return bool
*/
protected function matches($other)
{
$other = $this->normalizeText($other);
return mb_stripos($other, $this->string, null, 'UTF-8') !== false;
}
/**
* @param $text
* @return string
*/
private function normalizeText($text)
{
$text = strtr($text, "\r\n", " ");
return trim(preg_replace('/\\s{2,}/', ' ', $text));
}
/**
* Returns a string representation of the constraint.
*
* @return string
*/
public function toString()
{
return sprintf(
'contains "%s"',
$this->string
);
}
protected function failureDescription($pageContent)
{
$message = $this->uriMessage('on page');
$message->append("\n--> ");
$message->append(substr($pageContent, 0, 300));
if (strlen($pageContent) > 300) {
$debugMessage = new Message(
"[Content too long to display. See complete response in '" . codecept_output_dir() . "' directory]"
);
$message->append("\n")->append($debugMessage);
}
$message->append("\n--> ");
return $message->getMessage() . $this->toString();
}
protected function uriMessage($onPage = "")
{
if (!$this->uri) {
return new Message('');
}
$message = new Message($this->uri);
$message->prepend(" $onPage ");
return $message;
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace Codeception\PHPUnit\Constraint;
use Codeception\Exception\ElementNotFound;
use Codeception\Lib\Console\Message;
use Codeception\Util\Locator;
use SebastianBergmann\Comparator\ComparisonFailure;
class WebDriver extends Page
{
protected function matches($nodes)
{
if (!count($nodes)) {
return false;
}
if ($this->string === '') {
return true;
}
foreach ($nodes as $node) {
/** @var $node \WebDriverElement * */
if (!$node->isDisplayed()) {
continue;
}
if (parent::matches(htmlspecialchars_decode($node->getText()))) {
return true;
}
}
return false;
}
protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure = null)
{
if (!count($nodes)) {
throw new ElementNotFound($selector, 'Element located either by name, CSS or XPath');
}
$output = "Failed asserting that any element by " . Locator::humanReadableString($selector);
$output .= $this->uriMessage('on page');
if (count($nodes) < 5) {
$output .= "\nElements: ";
$output .= $this->nodesList($nodes);
} else {
$message = new Message("[total %s elements]");
$output .= $message->with(count($nodes));
}
$output .= "\ncontains text '" . $this->string . "'";
throw new \PHPUnit\Framework\ExpectationFailedException(
$output,
$comparisonFailure
);
}
protected function failureDescription($nodes)
{
$desc = '';
foreach ($nodes as $node) {
$desc .= parent::failureDescription($node->getText());
}
return $desc;
}
protected function nodesList($nodes, $contains = null)
{
$output = "";
foreach ($nodes as $node) {
if ($contains && strpos($node->getText(), $contains) === false) {
continue;
}
/** @var $node \WebDriverElement * */
$message = new Message("\n+ <%s> %s");
$output .= $message->with($node->getTagName(), $node->getText());
}
return $output;
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Codeception\PHPUnit\Constraint;
use SebastianBergmann\Comparator\ComparisonFailure;
use Codeception\Util\Locator;
class WebDriverNot extends WebDriver
{
protected function matches($nodes)
{
return !parent::matches($nodes);
}
protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure = null)
{
$selectorString = Locator::humanReadableString($selector);
if (!$this->string) {
throw new \PHPUnit\Framework\ExpectationFailedException(
"Element $selectorString was found",
$comparisonFailure
);
}
$output = "There was $selectorString element";
$output .= $this->uriMessage("on page");
$output .= $this->nodesList($nodes, $this->string);
$output .= "\ncontaining '{$this->string}'";
throw new \PHPUnit\Framework\ExpectationFailedException(
$output,
$comparisonFailure
);
}
public function toString()
{
if ($this->string) {
return 'that contains text "' . $this->string . '"';
}
}
}