diff --git a/composer.json b/composer.json index 6122761..a280d4a 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/src/MarkupAssertionsTrait.php b/src/MarkupAssertionsTrait.php index 257706d..052cfe0 100644 --- a/src/MarkupAssertionsTrait.php +++ b/src/MarkupAssertionsTrait.php @@ -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 { @@ -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 $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 */ @@ -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 $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 */ @@ -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('' . $markup, Document::DOC_HTML, 'UTF-8'), - Query::TYPE_CSS - ); + $dom = new Crawler($markup); + + return $dom->filter($query); } /** @@ -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 $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.'); @@ -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()));