Skip to content

Commit

Permalink
Merge pull request #41 from laminas/drop-laminas-dom
Browse files Browse the repository at this point in the history
Drop laminas-dom for symfony/dom-crawler
  • Loading branch information
Ocramius authored Dec 3, 2021
2 parents 52f3e58 + 4487b5f commit 72e879d
Show file tree
Hide file tree
Showing 8 changed files with 1,003 additions and 872 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"require": {
"php": "^7.3 || ~8.0.0",
"laminas/laminas-console": "^2.6",
"laminas/laminas-dom": "^2.8",
"laminas/laminas-eventmanager": "^3.0",
"laminas/laminas-http": "^2.13",
"laminas/laminas-mvc": "^3.0",
Expand All @@ -32,7 +31,9 @@
"laminas/laminas-uri": "^2.5",
"laminas/laminas-view": "^2.6.3",
"laminas/laminas-zendframework-bridge": "^1.1",
"phpunit/phpunit": "^8.0 || ^9.0"
"phpunit/phpunit": "^8.4.3 || ^9.0",
"symfony/css-selector": "^5.4 || ^6.0",
"symfony/dom-crawler": "^5.4 || ^6.0"
},
"require-dev": {
"laminas/laminas-coding-standard": "~2.2.1",
Expand Down
1,758 changes: 915 additions & 843 deletions composer.lock

Large diffs are not rendered by default.

31 changes: 16 additions & 15 deletions src/PHPUnit/Controller/AbstractHttpControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace Laminas\Test\PHPUnit\Controller;

use ArrayIterator;
use Laminas\Dom\Document;
use Laminas\Http\Header\HeaderInterface;
use Laminas\Test\PHPUnit\Constraint\HasRedirectConstraint;
use Laminas\Test\PHPUnit\Constraint\IsRedirectedRouteNameConstraint;
use PHPUnit\Framework\ExpectationFailedException;
use Symfony\Component\DomCrawler\Crawler;

use function count;
use function implode;
Expand All @@ -28,7 +28,7 @@ abstract class AbstractHttpControllerTestCase extends AbstractControllerTestCase
/**
* XPath namespaces
*
* @var array
* @var array<string,string>
*/
protected $xpathNamespaces = [];

Expand Down Expand Up @@ -409,7 +409,7 @@ public function assertNotRedirectRegex($pattern)
/**
* Register XPath namespaces
*
* @param array $xpathNamespaces
* @param array<string,string> $xpathNamespaces
* @return void
*/
public function registerXpathNamespaces(array $xpathNamespaces)
Expand All @@ -422,30 +422,28 @@ public function registerXpathNamespaces(array $xpathNamespaces)
*
* @param string $path
* @param bool $useXpath
* @return Document\NodeList
* @return Crawler
*/
private function query($path, $useXpath = false)
{
$response = $this->getResponse();
$document = new Document($response->getContent());
$document = new Crawler($response->getContent());

if ($useXpath) {
$document->registerXpathNamespaces($this->xpathNamespaces);
foreach ($this->xpathNamespaces as $prefix => $namespace) {
$document->registerNamespace($prefix, $namespace);
}
}

return Document\Query::execute(
$path,
$document,
$useXpath ? Document\Query::TYPE_XPATH : Document\Query::TYPE_CSS
);
return $useXpath ? $document->filterXPath($path) : $document->filter($path);
}

/**
* Execute a xpath query
*
* @param string $path
*/
private function xpathQuery($path): Document\NodeList
private function xpathQuery($path): Crawler
{
return $this->query($path, true);
}
Expand All @@ -469,7 +467,7 @@ private function queryCount($path)
*/
private function xpathQueryCount($path)
{
return count($this->xpathQuery($path));
return $this->xpathQuery($path)->count();
}

/**
Expand Down Expand Up @@ -938,14 +936,17 @@ private function notQueryContentRegexAssertion($path, $pattern, $useXpath = fals
$path
)));
}
if (preg_match($pattern, $result->current()->nodeValue)) {
$node = $result->getNode(0);
$nodeValue = $node ? $node->nodeValue : null;

if ($nodeValue === null || preg_match($pattern, $nodeValue)) {
throw new ExpectationFailedException($this->createFailureMessage(sprintf(
'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content MATCHING "%s"',
$path,
$pattern
)));
}
$this->assertFalse((bool) preg_match($pattern, $result->current()->nodeValue));
$this->assertFalse((bool) preg_match($pattern, $nodeValue));
}

/**
Expand Down
28 changes: 28 additions & 0 deletions test/PHPUnit/Controller/AbstractControllerTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,34 @@ public function testQueryParamsDelete(): void
$this->assertEquals('foo=bar', $this->getRequest()->getQuery()->toString());
}

public function testRegisterXpathNamespaceAndFound(): void
{
$this->dispatch('/register-xpath-namespace');

$this->registerXpathNamespaces(['m' => 'http://search.yahoo.com/mrss/']);

$this->assertXpathQueryCount('//m:group//yt:aspectRatio', 1);
$this->assertXpathQueryContentContains('//m:group//yt:aspectRatio', 'widescreen');
}

public function testRegisterXpathNamespaceAndNotFound(): void
{
$this->dispatch('/register-xpath-namespace');

$this->registerXpathNamespaces(['m' => 'http://search.yahoo.com/mrss/']);

$this->assertXpathQueryCount('//m:group//yt:aspectRatios', 0);
}

public function testRegisterNotExistingXpathNamespace(): void
{
$this->dispatch('/register-xpath-namespace');

$this->registerXpathNamespaces(['m' => 'https://getlaminas.org/']);

$this->assertXpathQueryCount('//m:group//yt:aspectRatio', 0);
}

/**
* @return Generator
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ public function testAssertXpathQueryWithBadXpathUsage(): void
{
$this->dispatch('/tests');

$this->expectedException('ErrorException');
$this->expectError();

$this->assertXpathQuery('form#myform');
}

Expand Down Expand Up @@ -548,8 +549,7 @@ public function testAssertQueryWithDynamicPostParams(): void
public function testAssertQueryWithDynamicPostParamsInDispatchMethod(): void
{
$this->dispatch('/tests', 'POST', ['num_post' => 5]);
$request = $this->getRequest();
$this->assertEquals($request->getMethod(), 'POST');

$this->assertQueryCount('div.post', 5);
$this->assertXpathQueryCount('//div[@class="post"]', 5);
$this->assertQueryCount('div.get', 0);
Expand Down
28 changes: 19 additions & 9 deletions test/_files/Baz/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
],
'router' => [
'routes' => [
'myroute' => [
'myroute' => [
'type' => 'literal',
'options' => [
'route' => '/tests',
Expand All @@ -59,7 +59,7 @@
],
],
],
'myroutebis' => [
'myroutebis' => [
'type' => 'literal',
'options' => [
'route' => '/tests-bis',
Expand All @@ -69,7 +69,7 @@
],
],
],
'persistence' => [
'persistence' => [
'type' => 'literal',
'options' => [
'route' => '/tests-persistence',
Expand All @@ -79,7 +79,7 @@
],
],
],
'exception' => [
'exception' => [
'type' => 'literal',
'options' => [
'route' => '/exception',
Expand All @@ -89,7 +89,7 @@
],
],
],
'redirect' => [
'redirect' => [
'type' => 'literal',
'options' => [
'route' => '/redirect',
Expand All @@ -99,7 +99,7 @@
],
],
],
'redirectToRoute' => [
'redirectToRoute' => [
'type' => 'literal',
'options' => [
'route' => '/redirect-to-route',
Expand All @@ -109,7 +109,7 @@
],
],
],
'dnsroute' => [
'dnsroute' => [
'type' => 'hostname',
'options' => [
'route' => ':subdomain.domain.tld',
Expand All @@ -122,7 +122,7 @@
],
],
],
'custom-response' => [
'custom-response' => [
'type' => 'literal',
'options' => [
'route' => '/custom-response',
Expand All @@ -132,7 +132,17 @@
],
],
],
'parametrized' => [
'register-xpath-namespace' => [
'type' => 'literal',
'options' => [
'route' => '/register-xpath-namespace',
'defaults' => [
'controller' => 'baz_index',
'action' => 'registerxpathnamespace',
],
],
],
'parametrized' => [
'type' => 'segment',
'options' => [
'route' => '/with-param/:param',
Expand Down
5 changes: 5 additions & 0 deletions test/_files/Baz/src/Baz/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ public function customResponseAction()

return $response;
}

/** @return void */
public function registerxpathnamespaceAction()
{
}
}
14 changes: 14 additions & 0 deletions test/_files/Baz/view/baz/index/registerxpathnamespace.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<entry
xmlns="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:yt="http://gdata.youtube.com/schemas/2007"
>
<id>tag:youtube.com,2008:video:kgZRZmEc9j4</id>
<yt:accessControl action="comment" permission="allowed"/>
<yt:accessControl action="videoRespond" permission="moderated"/>
<media:group>
<media:title type="plain">Chordates - CrashCourse Biology #24</media:title>
<yt:aspectRatio>widescreen</yt:aspectRatio>
</media:group>
</entry>

0 comments on commit 72e879d

Please sign in to comment.