-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26396c2
commit da4e2eb
Showing
24 changed files
with
1,006 additions
and
805 deletions.
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
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,82 @@ | ||
<?php | ||
|
||
namespace Laravel\BrowserKitTesting\Constraints\Concerns; | ||
|
||
use Symfony\Component\DomCrawler\Crawler; | ||
|
||
trait FormFieldConstraint | ||
{ | ||
/** | ||
* The name or ID of the element. | ||
* | ||
* @var string | ||
*/ | ||
protected readonly string $selector; | ||
|
||
/** | ||
* The expected value. | ||
* | ||
* @var string | ||
*/ | ||
protected readonly string $value; | ||
|
||
/** | ||
* Create a new constraint instance. | ||
* | ||
* @param string $selector | ||
* @param mixed $value | ||
* @return void | ||
*/ | ||
public function __construct($selector, $value) | ||
{ | ||
$this->selector = $selector; | ||
$this->value = (string) $value; | ||
} | ||
|
||
/** | ||
* Get the valid elements. | ||
* | ||
* Multiple elements should be separated by commas without spaces. | ||
* | ||
* @return string | ||
*/ | ||
abstract protected function validElements(); | ||
|
||
/** | ||
* Get the form field. | ||
* | ||
* @param \Symfony\Component\DomCrawler\Crawler $crawler | ||
* @return \Symfony\Component\DomCrawler\Crawler | ||
* | ||
* @throws \PHPUnit\Framework\ExpectationFailedException | ||
*/ | ||
protected function field(Crawler $crawler) | ||
{ | ||
$field = $crawler->filter(implode(', ', $this->getElements())); | ||
|
||
if ($field->count() > 0) { | ||
return $field; | ||
} | ||
|
||
$this->fail($crawler, sprintf( | ||
'There is no %s with the name or ID [%s]', | ||
$this->validElements(), $this->selector | ||
)); | ||
} | ||
|
||
/** | ||
* Get the elements relevant to the selector. | ||
* | ||
* @return array | ||
*/ | ||
protected function getElements() | ||
{ | ||
$name = str_replace('#', '', $this->selector); | ||
|
||
$id = str_replace(['[', ']'], ['\\[', '\\]'], $name); | ||
|
||
return collect(explode(',', $this->validElements()))->map(function ($element) use ($name, $id) { | ||
return "{$element}#{$id}, {$element}[name='{$name}']"; | ||
})->all(); | ||
} | ||
} |
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,99 @@ | ||
<?php | ||
|
||
namespace Laravel\BrowserKitTesting\Constraints\Concerns; | ||
|
||
use Symfony\Component\DomCrawler\Crawler; | ||
|
||
trait HasElement | ||
{ | ||
/** | ||
* The name or ID of the element. | ||
* | ||
* @var string | ||
*/ | ||
protected readonly string $selector; | ||
|
||
/** | ||
* The attributes the element should have. | ||
* | ||
* @var array | ||
*/ | ||
protected readonly array $attributes; | ||
|
||
/** | ||
* Create a new constraint instance. | ||
* | ||
* @param string $selector | ||
* @param array $attributes | ||
* @return void | ||
*/ | ||
public function __construct($selector, array $attributes = []) | ||
{ | ||
$this->selector = $selector; | ||
$this->attributes = $attributes; | ||
} | ||
|
||
/** | ||
* Check if the element is found in the given crawler. | ||
* | ||
* @param \Symfony\Component\DomCrawler\Crawler|string $crawler | ||
* @return bool | ||
*/ | ||
public function matches($crawler): bool | ||
{ | ||
$elements = $this->crawler($crawler)->filter($this->selector); | ||
|
||
if ($elements->count() == 0) { | ||
return false; | ||
} | ||
|
||
if (empty($this->attributes)) { | ||
return true; | ||
} | ||
|
||
$elements = $elements->reduce(function ($element) { | ||
return $this->hasAttributes($element); | ||
}); | ||
|
||
return $elements->count() > 0; | ||
} | ||
|
||
/** | ||
* Determines if the given element has the attributes. | ||
* | ||
* @param \Symfony\Component\DomCrawler\Crawler $element | ||
* @return bool | ||
*/ | ||
protected function hasAttributes(Crawler $element) | ||
{ | ||
foreach ($this->attributes as $name => $value) { | ||
if (is_numeric($name)) { | ||
if (is_null($element->attr($value))) { | ||
return false; | ||
} | ||
} else { | ||
if ($element->attr($name) != $value) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Returns a string representation of the object. | ||
* | ||
* @return string | ||
*/ | ||
public function toString(): string | ||
{ | ||
$message = "the element [{$this->selector}]"; | ||
|
||
if (! empty($this->attributes)) { | ||
$message .= ' with the attributes '.json_encode($this->attributes); | ||
} | ||
|
||
return $message; | ||
} | ||
} |
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,78 @@ | ||
<?php | ||
|
||
namespace Laravel\BrowserKitTesting\Constraints\Concerns; | ||
|
||
use Symfony\Component\DomCrawler\Crawler; | ||
|
||
trait HasInElement | ||
{ | ||
/** | ||
* The name or ID of the element. | ||
* | ||
* @var string | ||
*/ | ||
protected readonly string $element; | ||
|
||
/** | ||
* The text expected to be found. | ||
* | ||
* @var string | ||
*/ | ||
protected readonly string $text; | ||
|
||
/** | ||
* Create a new constraint instance. | ||
* | ||
* @param string $element | ||
* @param string $text | ||
* @return void | ||
*/ | ||
public function __construct($element, $text) | ||
{ | ||
$this->text = $text; | ||
$this->element = $element; | ||
} | ||
|
||
/** | ||
* Check if the source or text is found within the element in the given crawler. | ||
* | ||
* @param \Symfony\Component\DomCrawler\Crawler|string $crawler | ||
* @return bool | ||
*/ | ||
public function matches($crawler): bool | ||
{ | ||
$elements = $this->crawler($crawler)->filter($this->element); | ||
|
||
$pattern = $this->getEscapedPattern($this->text); | ||
|
||
foreach ($elements as $element) { | ||
$element = new Crawler($element); | ||
|
||
if (preg_match("/$pattern/i", $element->html())) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Returns the description of the failure. | ||
* | ||
* @return string | ||
*/ | ||
protected function getFailureDescription() | ||
{ | ||
return sprintf('[%s] contains %s', $this->element, $this->text); | ||
} | ||
|
||
/** | ||
* Returns the reversed description of the failure. | ||
* | ||
* @return string | ||
*/ | ||
protected function getReverseFailureDescription() | ||
{ | ||
return sprintf('[%s] does not contain %s', $this->element, $this->text); | ||
} | ||
} |
Oops, something went wrong.