Skip to content

Commit

Permalink
Merge pull request #41 from stevegrunwell/simplify-composer-constraints
Browse files Browse the repository at this point in the history
Switch from laminas/laminas-dom to symfony/dom-crawler
  • Loading branch information
stevegrunwell authored Dec 2, 2023
2 parents 590e375 + 154d751 commit c75b965
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
},
"require": {
"php": "^5.6 || ^7.0 || ^8.0",
"laminas/laminas-dom": "~2.7.2 || ^2.8"
"symfony/css-selector": "^3.4|^4.4|^5.4|^6.0",
"symfony/dom-crawler": "^3.4|^4.4|^5.4|^6.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^5.2 || ^6.2"
Expand Down
42 changes: 20 additions & 22 deletions src/MarkupAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

namespace SteveGrunwell\PHPUnit_Markup_Assertions;

use DOMDocument;
use Laminas\Dom\Document;
use Laminas\Dom\Document\Query;
use PHPUnit\Framework\RiskyTestError;
use Symfony\Component\DomCrawler\Crawler;

trait MarkupAssertionsTrait
{
Expand Down Expand Up @@ -75,10 +73,11 @@ public function assertSelectorCount($count, $selector, $markup = '', $message =
*
* @since 1.0.0
*
* @param array $attributes An array of HTML attributes that should be found on the element.
* @param string $markup The output that should contain an element with the
* provided $attributes.
* @param string $message A message to display if the assertion fails.
* @param array<string, scalar> $attributes An array of HTML attributes that should be found
* on the element.
* @param string $markup The output that should contain an element with the
* provided $attributes.
* @param string $message A message to display if the assertion fails.
*
* @return void
*/
Expand All @@ -96,10 +95,11 @@ public function assertHasElementWithAttributes($attributes = [], $markup = '', $
*
* @since 1.0.0
*
* @param array $attributes An array of HTML attributes that should be found on the element.
* @param string $markup The output that should not contain an element with the
* provided $attributes.
* @param string $message A message to display if the assertion fails.
* @param array<string, scalar> $attributes An array of HTML attributes that should be found
* on the element.
* @param string $markup The output that should not contain an element with
* the provided $attributes.
* @param string $message A message to display if the assertion fails.
*
* @return void
*/
Expand Down Expand Up @@ -220,15 +220,13 @@ public function assertElementNotRegExp($regexp, $selector = '', $markup = '', $m
* @param string $markup The HTML for the DOMDocument.
* @param string $query The DOM selector query.
*
* @return \Laminas\Dom\Document\NodeList
* @return Crawler
*/
protected function executeDomQuery($markup, $query)
private function executeDomQuery($markup, $query)
{
return Query::execute(
$query,
new Document('<?xml encoding="UTF-8">' . $markup, Document::DOC_HTML, 'UTF-8'),
Query::TYPE_CSS
);
$dom = new Crawler($markup);

return $dom->filter($query);
}

/**
Expand All @@ -238,11 +236,11 @@ protected function executeDomQuery($markup, $query)
*
* @throws RiskyTestError When the $attributes array is empty.
*
* @param array $attributes HTML attributes and their values.
* @param array<string, scalar> $attributes HTML attributes and their values.
*
* @return string A XPath attribute query selector.
*/
protected function flattenAttributeArray(array $attributes)
private function flattenAttributeArray(array $attributes)
{
if (empty($attributes)) {
throw new RiskyTestError('Attributes array is empty.');
Expand Down Expand Up @@ -270,14 +268,14 @@ protected function flattenAttributeArray(array $attributes)
*
* @return string The concatenated innerHTML of any matched selectors.
*/
protected function getInnerHtmlOfMatchedElements($markup, $query)
private function getInnerHtmlOfMatchedElements($markup, $query)
{
$results = $this->executeDomQuery($markup, $query);
$contents = [];

// Loop through results and collect their innerHTML values.
foreach ($results as $result) {
$document = new DOMDocument();
$document = new \DOMDocument();
$document->appendChild($document->importNode($result->firstChild, true));

$contents[] = trim(html_entity_decode($document->saveHTML()));
Expand Down

0 comments on commit c75b965

Please sign in to comment.