[DomCrawler] refactored URLs management in Link and Form
This commit is contained in:
parent
5591f34c01
commit
8b74c6eb9c
@ -23,31 +23,20 @@ use Symfony\Component\CssSelector\Parser as CssParser;
|
||||
class Crawler extends \SplObjectStorage
|
||||
{
|
||||
private $uri;
|
||||
private $host;
|
||||
private $path;
|
||||
private $base;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param mixed $node A Node to use as the base for the crawling
|
||||
* @param string $uri The base URI to use for absolute links or form actions
|
||||
* @param string $base An optional base href for generating the uris for Form and Link.
|
||||
* This will be autodetected if $node has a <base> tag.
|
||||
* @param string $uri The current URI or the base href value
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function __construct($node = null, $uri = null, $base = null)
|
||||
public function __construct($node = null, $uri = null)
|
||||
{
|
||||
$this->uri = $uri;
|
||||
|
||||
list($this->host, $this->path) = $this->parseUri($this->uri);
|
||||
|
||||
$this->add($node);
|
||||
|
||||
if ($base) {
|
||||
$this->base = $base;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -131,7 +120,7 @@ class Crawler extends \SplObjectStorage
|
||||
$base = $this->filter('base')->extract(array('href'));
|
||||
|
||||
if (count($base)) {
|
||||
$this->base = current($base);
|
||||
$this->uri = current($base);
|
||||
}
|
||||
}
|
||||
|
||||
@ -498,7 +487,7 @@ class Crawler extends \SplObjectStorage
|
||||
|
||||
$domxpath = new \DOMXPath($document);
|
||||
|
||||
return new static($domxpath->query($xpath), $this->uri, $this->base);
|
||||
return new static($domxpath->query($xpath), $this->uri);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -579,7 +568,7 @@ class Crawler extends \SplObjectStorage
|
||||
|
||||
$node = $this->getNode(0);
|
||||
|
||||
return new Link($node, $method, $this->host, $this->path, $this->base);
|
||||
return new Link($node, $this->uri, $method);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -593,7 +582,7 @@ class Crawler extends \SplObjectStorage
|
||||
{
|
||||
$links = array();
|
||||
foreach ($this as $node) {
|
||||
$links[] = new Link($node, 'get', $this->host, $this->path);
|
||||
$links[] = new Link($node, $this->uri, 'get');
|
||||
}
|
||||
|
||||
return $links;
|
||||
@ -617,7 +606,7 @@ class Crawler extends \SplObjectStorage
|
||||
throw new \InvalidArgumentException('The current node list is empty.');
|
||||
}
|
||||
|
||||
$form = new Form($this->getNode(0), $method, $this->host, $this->path, $this->base);
|
||||
$form = new Form($this->getNode(0), $this->uri, $method);
|
||||
|
||||
if (null !== $values) {
|
||||
$form->setValues($values);
|
||||
@ -665,17 +654,6 @@ class Crawler extends \SplObjectStorage
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
private function parseUri($uri)
|
||||
{
|
||||
if ('http' !== substr($uri, 0, 4)) {
|
||||
return array(null, '/');
|
||||
}
|
||||
|
||||
$path = parse_url($uri, PHP_URL_PATH);
|
||||
|
||||
return array(preg_replace('#^(.*?//[^/]+)\/.*$#', '$1', $uri), $path);
|
||||
}
|
||||
|
||||
private function sibling($node, $siblingDir = 'nextSibling')
|
||||
{
|
||||
$nodes = array();
|
||||
|
@ -20,31 +20,31 @@ use Symfony\Component\DomCrawler\Field\FormField;
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class Form implements \ArrayAccess
|
||||
class Form extends Link implements \ArrayAccess
|
||||
{
|
||||
private $document;
|
||||
private $button;
|
||||
private $node;
|
||||
private $fields;
|
||||
private $method;
|
||||
private $host;
|
||||
private $path;
|
||||
private $base;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \DOMNode $node A \DOMNode instance
|
||||
* @param string $method The method to use for the link (if null, it defaults to the method defined by the form)
|
||||
* @param string $host The base URI to use for absolute links (like http://localhost)
|
||||
* @param string $path The base path for relative links (/ by default)
|
||||
* @param string $base An optional base href for generating the submit uri
|
||||
* @param \DOMNode $node A \DOMNode instance
|
||||
* @param string $currentUri The URI of the page where the form is embedded
|
||||
* @param string $method The method to use for the link (if null, it defaults to the method defined by the form)
|
||||
*
|
||||
* @throws \LogicException if the node is not a button inside a form tag
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function __construct(\DOMNode $node, $method = null, $host = null, $path = '/', $base = null)
|
||||
public function __construct(\DOMNode $node, $currentUri, $method = null)
|
||||
{
|
||||
parent::__construct($node, $currentUri, $method);
|
||||
|
||||
$this->initialize();
|
||||
}
|
||||
|
||||
protected function setNode(\DOMNode $node)
|
||||
{
|
||||
$this->button = $node;
|
||||
if ('button' == $node->nodeName || ('input' == $node->nodeName && in_array($node->getAttribute('type'), array('submit', 'button', 'image')))) {
|
||||
@ -57,13 +57,8 @@ class Form implements \ArrayAccess
|
||||
} else {
|
||||
throw new \LogicException(sprintf('Unable to submit on a "%s" tag.', $node->nodeName));
|
||||
}
|
||||
$this->node = $node;
|
||||
$this->method = $method;
|
||||
$this->host = $host;
|
||||
$this->path = empty($path) ? '/' : $path;
|
||||
$this->base = $base;
|
||||
|
||||
$this->initialize();
|
||||
$this->node = $node;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -179,48 +174,27 @@ class Form implements \ArrayAccess
|
||||
* This method merges the value if the method is GET to mimics
|
||||
* browser behavior.
|
||||
*
|
||||
* @param Boolean $absolute Whether to return an absolute URI or not (this only works if a base URI has been provided)
|
||||
*
|
||||
* @return string The URI
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function getUri($absolute = true)
|
||||
public function getUri()
|
||||
{
|
||||
$uri = $this->node->getAttribute('action');
|
||||
$urlHaveScheme = 'http' === substr($uri, 0, 4);
|
||||
|
||||
if (!$uri) {
|
||||
$uri = $this->path;
|
||||
}
|
||||
|
||||
if ('#' === $uri[0]) {
|
||||
$uri = $this->path.$uri;
|
||||
}
|
||||
$uri = parent::getUri();
|
||||
|
||||
if (!in_array($this->getMethod(), array('post', 'put', 'delete')) && $queryString = http_build_query($this->getValues(), null, '&')) {
|
||||
$sep = false === strpos($uri, '?') ? '?' : '&';
|
||||
$uri .= $sep.$queryString;
|
||||
}
|
||||
|
||||
$path = $this->path;
|
||||
if ('?' !== substr($uri, 0, 1) && '/' !== substr($path, -1)) {
|
||||
$path = substr($path, 0, strrpos($path, '/') + 1);
|
||||
}
|
||||
|
||||
if (!$this->base && $uri && '/' !== $uri[0] && !$urlHaveScheme) {
|
||||
$uri = $path.$uri;
|
||||
} elseif ($this->base) {
|
||||
$uri = $this->base.$uri;
|
||||
}
|
||||
|
||||
if (!$this->base && $absolute && null !== $this->host && !$urlHaveScheme) {
|
||||
return $this->host.$uri;
|
||||
}
|
||||
|
||||
return $uri;
|
||||
}
|
||||
|
||||
protected function getRawUri()
|
||||
{
|
||||
return $this->node->getAttribute('action');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the form method.
|
||||
*
|
||||
|
@ -20,36 +20,39 @@ namespace Symfony\Component\DomCrawler;
|
||||
*/
|
||||
class Link
|
||||
{
|
||||
private $node;
|
||||
private $method;
|
||||
private $host;
|
||||
private $path;
|
||||
private $base;
|
||||
protected $node;
|
||||
protected $method;
|
||||
protected $currentUri;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \DOMNode $node A \DOMNode instance
|
||||
* @param string $method The method to use for the link (get by default)
|
||||
* @param string $host The base URI to use for absolute links (like http://localhost)
|
||||
* @param string $path The base path for relative links (/ by default)
|
||||
* @param string $base An optional base href for generating the uri
|
||||
* @param \DOMNode $node A \DOMNode instance
|
||||
* @param string $currentUri The URI of the page where the link is embedded (or the base href)
|
||||
* @param string $method The method to use for the link (get by default)
|
||||
*
|
||||
* @throws \LogicException if the node is not a link
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function __construct(\DOMNode $node, $method = 'get', $host = null, $path = '/', $base = null)
|
||||
public function __construct(\DOMNode $node, $currentUri, $method = 'get')
|
||||
{
|
||||
if (!in_array(substr($currentUri, 0, 4), array('http', 'file'))) {
|
||||
throw new \InvalidArgumentException(sprintf('Current URI must be an asbolute URL ("%s").', $currentUri));
|
||||
}
|
||||
|
||||
$this->setNode($node);
|
||||
$this->method = $method;
|
||||
$this->currentUri = $currentUri;
|
||||
}
|
||||
|
||||
protected function setNode(\DOMNode $node)
|
||||
{
|
||||
if ('a' != $node->nodeName) {
|
||||
throw new \LogicException(sprintf('Unable to click on a "%s" tag.', $node->nodeName));
|
||||
}
|
||||
|
||||
$this->node = $node;
|
||||
$this->method = $method;
|
||||
$this->host = $host;
|
||||
$this->path = empty($path) ? '/' : $path;
|
||||
$this->base = $base;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,42 +68,41 @@ class Link
|
||||
/**
|
||||
* Gets the URI associated with this link.
|
||||
*
|
||||
* @param Boolean $absolute Whether to return an absolute URI or not (this only works if a base URI has been provided)
|
||||
*
|
||||
* @return string The URI
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function getUri($absolute = true)
|
||||
public function getUri()
|
||||
{
|
||||
$uri = $this->node->getAttribute('href');
|
||||
$urlHaveScheme = 'http' === substr($uri, 0, 4);
|
||||
$uri = $this->getRawUri();
|
||||
|
||||
// absolute URL?
|
||||
if ('http' === substr($uri, 0, 4)) {
|
||||
return $uri;
|
||||
}
|
||||
|
||||
// empty URI
|
||||
if (!$uri) {
|
||||
$uri = $this->path;
|
||||
return $this->currentUri;
|
||||
}
|
||||
|
||||
if ('#' === $uri[0]) {
|
||||
$uri = $this->path.$uri;
|
||||
// only an anchor or a query string
|
||||
if (in_array($uri[0], array('?', '#'))) {
|
||||
return $this->currentUri.$uri;
|
||||
}
|
||||
|
||||
$path = $this->path;
|
||||
if ('?' !== substr($uri, 0, 1) && '/' !== substr($path, -1)) {
|
||||
$path = substr($path, 0, strrpos($path, '/') + 1);
|
||||
// absolute path
|
||||
if ('/' === $uri[0]) {
|
||||
return preg_replace('#^(.*?//[^/]+)(?:\/.*)?$#', '$1', $this->currentUri).$uri;
|
||||
}
|
||||
|
||||
if (!$this->base && $uri && '/' !== $uri[0] && !$urlHaveScheme) {
|
||||
$uri = $path.$uri;
|
||||
} elseif ($this->base) {
|
||||
$uri = $this->base.$uri;
|
||||
}
|
||||
// relative path
|
||||
return substr($this->currentUri, 0, strrpos($this->currentUri, '/') + 1).$uri;
|
||||
}
|
||||
|
||||
if (!$this->base && $absolute && null !== $this->host && !$urlHaveScheme) {
|
||||
|
||||
return $this->host.$uri;
|
||||
}
|
||||
|
||||
return $uri;
|
||||
protected function getRawUri()
|
||||
{
|
||||
return $this->node->getAttribute('href');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -287,18 +287,11 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testLink()
|
||||
{
|
||||
$crawler = $this->createTestCrawler()->selectLink('Foo');
|
||||
$crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');
|
||||
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Link', $crawler->link(), '->link() returns a Link instance');
|
||||
|
||||
$this->assertEquals('/foo', $crawler->link()->getUri(), '->link() returns a Link instance');
|
||||
$this->assertEquals('post', $crawler->link('post')->getMethod(), '->link() takes a method as its argument');
|
||||
|
||||
$crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');
|
||||
$this->assertEquals('http://example.com/bar/foo', $crawler->link()->getUri(), '->link() returns a Link instance');
|
||||
|
||||
$crawler = $this->createTestCrawler('http://example.com/bar')->selectLink('Foo');
|
||||
$this->assertEquals('http://example.com/foo', $crawler->link()->getUri(), '->link() returns a Link instance');
|
||||
|
||||
$crawler = $this->createTestCrawler('http://example.com/bar')->selectLink('GetLink');
|
||||
$this->assertEquals('http://example.com/bar?get=param', $crawler->link()->getUri(), '->link() returns a Link instance');
|
||||
|
||||
@ -312,7 +305,7 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testLinks()
|
||||
{
|
||||
$crawler = $this->createTestCrawler()->selectLink('Foo');
|
||||
$crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');
|
||||
$this->assertInternalType('array', $crawler->links(), '->links() returns an array');
|
||||
|
||||
$this->assertEquals(4, count($crawler->links()), '->links() returns an array');
|
||||
@ -324,18 +317,11 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testForm()
|
||||
{
|
||||
$crawler = $this->createTestCrawler()->selectButton('FooValue');
|
||||
$crawler = $this->createTestCrawler('http://example.com/bar/')->selectButton('FooValue');
|
||||
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Form', $crawler->form(), '->form() returns a Form instance');
|
||||
|
||||
$this->assertEquals('/foo?FooName=FooValue', $crawler->form()->getUri(), '->form() returns a Form instance');
|
||||
$this->assertEquals(array('FooName' => 'FooBar'), $crawler->form(array('FooName' => 'FooBar'))->getValues(), '->form() takes an array of values to submit as its first argument');
|
||||
|
||||
$crawler = $this->createTestCrawler('http://example.com/bar/')->selectButton('FooValue');
|
||||
$this->assertEquals('http://example.com/bar/foo?FooName=FooValue', $crawler->form()->getUri(), '->form() returns a Form instance');
|
||||
|
||||
$crawler = $this->createTestCrawler('http://example.com/bar')->selectButton('FooValue');
|
||||
$this->assertEquals('http://example.com/foo?FooName=FooValue', $crawler->form()->getUri(), '->form() returns a Form instance');
|
||||
|
||||
try {
|
||||
$this->createTestCrawler()->filter('ol')->form();
|
||||
$this->fail('->form() throws an \InvalidArgumentException if the node list is empty');
|
||||
|
@ -31,14 +31,14 @@ class FormTest extends \PHPUnit_Framework_TestCase
|
||||
$nodes = $dom->getElementsByTagName('input');
|
||||
|
||||
try {
|
||||
$form = new Form($nodes->item(0));
|
||||
$form = new Form($nodes->item(0), 'http://example.com');
|
||||
$this->fail('__construct() throws a \\LogicException if the node has no form ancestor');
|
||||
} catch (\LogicException $e) {
|
||||
$this->assertTrue(true, '__construct() throws a \\LogicException if the node has no form ancestor');
|
||||
}
|
||||
|
||||
try {
|
||||
$form = new Form($nodes->item(1));
|
||||
$form = new Form($nodes->item(1), 'http://example.com');
|
||||
$this->fail('__construct() throws a \\LogicException if the input type is not submit, button, or image');
|
||||
} catch (\LogicException $e) {
|
||||
$this->assertTrue(true, '__construct() throws a \\LogicException if the input type is not submit, button, or image');
|
||||
@ -47,7 +47,7 @@ class FormTest extends \PHPUnit_Framework_TestCase
|
||||
$nodes = $dom->getElementsByTagName('button');
|
||||
|
||||
try {
|
||||
$form = new Form($nodes->item(0));
|
||||
$form = new Form($nodes->item(0), 'http://example.com');
|
||||
$this->fail('__construct() throws a \\LogicException if the input type is not submit, button, or image');
|
||||
} catch (\LogicException $e) {
|
||||
$this->assertTrue(true, '__construct() throws a \\LogicException if the input type is not submit, button, or image');
|
||||
@ -134,7 +134,7 @@ class FormTest extends \PHPUnit_Framework_TestCase
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadHTML('<html><form><input type="submit" /></form></html>');
|
||||
|
||||
$form = new Form($dom->getElementsByTagName('input')->item(0));
|
||||
$form = new Form($dom->getElementsByTagName('input')->item(0), 'http://example.com');
|
||||
|
||||
$this->assertSame($dom->getElementsByTagName('form')->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form');
|
||||
}
|
||||
@ -239,7 +239,7 @@ class FormTest extends \PHPUnit_Framework_TestCase
|
||||
$form = $this->createForm($form);
|
||||
$form->setValues($values);
|
||||
|
||||
$this->assertEquals($uri, $form->getUri(), '->getUri() '.$message);
|
||||
$this->assertEquals('http://example.com'.$uri, $form->getUri(), '->getUri() '.$message);
|
||||
}
|
||||
|
||||
public function testGetUriActionAbsolute()
|
||||
@ -252,46 +252,36 @@ class FormTest extends \PHPUnit_Framework_TestCase
|
||||
$form = $this->createForm($formHtml, null, 'https://login.foo.com');
|
||||
$this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
|
||||
|
||||
$form = $this->createForm($formHtml, null, 'https://login.foo.com', '/bar/');
|
||||
$form = $this->createForm($formHtml, null, 'https://login.foo.com/bar/');
|
||||
$this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
|
||||
|
||||
// The action URI haven't the same domain Host have an another domain as Host
|
||||
$form = $this->createForm($formHtml, null, 'https://www.foo.com');
|
||||
$this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
|
||||
|
||||
$form = $this->createForm($formHtml, null, 'https://www.foo.com', '/bar/');
|
||||
$form = $this->createForm($formHtml, null, 'https://www.foo.com/bar/');
|
||||
$this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
|
||||
}
|
||||
|
||||
public function testGetUriAbsolute()
|
||||
{
|
||||
$form = $this->createForm('<form action="foo"><input type="submit" /></form>', null, 'http://localhost', '/foo/');
|
||||
$this->assertEquals('http://localhost/foo/foo', $form->getUri(true), '->getUri() returns absolute URIs');
|
||||
$form = $this->createForm('<form action="foo"><input type="submit" /></form>', null, 'http://localhost/foo/');
|
||||
$this->assertEquals('http://localhost/foo/foo', $form->getUri(), '->getUri() returns absolute URIs');
|
||||
|
||||
$form = $this->createForm('<form action="/foo"><input type="submit" /></form>', null, 'http://localhost', '/foo/');
|
||||
$this->assertEquals('http://localhost/foo', $form->getUri(true), '->getUri() returns absolute URIs');
|
||||
|
||||
$form = $this->createForm('<form action="/foo"><input type="submit" /></form>');
|
||||
$this->assertEquals('/foo', $form->getUri(true), '->getUri() returns absolute URIs only if the host has been defined in the constructor');
|
||||
|
||||
$form = $this->createForm('<form action="foo"><input type="submit" /></form>');
|
||||
$this->assertEquals('/foo', $form->getUri(true), '->getUri() returns absolute URIs only if the host has been defined in the constructor');
|
||||
$form = $this->createForm('<form action="/foo"><input type="submit" /></form>', null, 'http://localhost/foo/');
|
||||
$this->assertEquals('http://localhost/foo', $form->getUri(), '->getUri() returns absolute URIs');
|
||||
}
|
||||
|
||||
public function testGetUriWithOnlyQueryString()
|
||||
{
|
||||
$form = $this->createForm('<form action="?get=param"><input type="submit" /></form>', null, 'http://localhost', '/foo/bar');
|
||||
$this->assertEquals('http://localhost/foo/bar?get=param', $form->getUri(true), '->getUri() returns absolute URIs only if the host has been defined in the constructor');
|
||||
|
||||
$this->assertEquals('/foo/bar?get=param', $form->getUri(false), '->getUri() returns absolute URIs only if the host has been defined in the constructor');
|
||||
$form = $this->createForm('<form action="?get=param"><input type="submit" /></form>', null, 'http://localhost/foo/bar');
|
||||
$this->assertEquals('http://localhost/foo/bar?get=param', $form->getUri(), '->getUri() returns absolute URIs only if the host has been defined in the constructor');
|
||||
}
|
||||
|
||||
public function testGetUriWithoutAction()
|
||||
{
|
||||
$form = $this->createForm('<form><input type="submit" /></form>', null, 'http://localhost', '/foo/bar');
|
||||
$this->assertEquals('http://localhost/foo/bar', $form->getUri(true), '->getUri() returns path if no action defined');
|
||||
|
||||
$this->assertEquals('/foo/bar', $form->getUri(false), '->getUri() returns path if no action defined');
|
||||
$form = $this->createForm('<form><input type="submit" /></form>', null, 'http://localhost/foo/bar');
|
||||
$this->assertEquals('http://localhost/foo/bar', $form->getUri(), '->getUri() returns path if no action defined');
|
||||
}
|
||||
|
||||
public function provideGetUriValues()
|
||||
@ -392,24 +382,28 @@ class FormTest extends \PHPUnit_Framework_TestCase
|
||||
$dom->loadHTML('<form method="post" action="foo.php"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
|
||||
|
||||
$nodes = $dom->getElementsByTagName('input');
|
||||
$form = new Form($nodes->item($nodes->length - 1), null, 'http://www.bar.com/foobar/', '/', 'http://www.foo.com/');
|
||||
$form = new Form($nodes->item($nodes->length - 1), 'http://www.foo.com/');
|
||||
$this->assertEquals('http://www.foo.com/foo.php', $form->getUri());
|
||||
}
|
||||
|
||||
public function testUriWithAnchor()
|
||||
{
|
||||
$form = $this->createForm('<form action="#foo"><input type="submit" /></form>', null, 'http://example.com', '/id/123');
|
||||
$form = $this->createForm('<form action="#foo"><input type="submit" /></form>', null, 'http://example.com/id/123');
|
||||
|
||||
$this->assertEquals('http://example.com/id/123#foo', $form->getUri());
|
||||
}
|
||||
|
||||
protected function createForm($form, $method = null, $host = null, $path = '/', $base = null)
|
||||
protected function createForm($form, $method = null, $currentUri = null)
|
||||
{
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadHTML('<html>'.$form.'</html>');
|
||||
|
||||
$nodes = $dom->getElementsByTagName('input');
|
||||
|
||||
return new Form($nodes->item($nodes->length - 1), $method, $host, $path, $base);
|
||||
if (null === $currentUri) {
|
||||
$currentUri = 'http://example.com/';
|
||||
}
|
||||
|
||||
return new Form($nodes->item($nodes->length - 1), $currentUri, $method);
|
||||
}
|
||||
}
|
||||
|
@ -15,78 +15,79 @@ use Symfony\Component\DomCrawler\Link;
|
||||
|
||||
class LinkTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testConstructor()
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
*/
|
||||
public function testConstructorWithANonATag()
|
||||
{
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadHTML('<html><div><div></html>');
|
||||
|
||||
$node = $dom->getElementsByTagName('div')->item(0);
|
||||
|
||||
try {
|
||||
new Link($node);
|
||||
$this->fail('__construct() throws a \LogicException if the node is not an "a" tag');
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('\LogicException', $e, '__construct() throws a \LogicException if the node is not an "a" tag');
|
||||
}
|
||||
new Link($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
|
||||
}
|
||||
|
||||
public function testGetters()
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testConstructorWithAnInvalidCurrentUri()
|
||||
{
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadHTML('<html><a href="/foo">foo</a></html>');
|
||||
|
||||
new Link($dom->getElementsByTagName('a')->item(0), 'example.com');
|
||||
}
|
||||
|
||||
public function testGetNode()
|
||||
{
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadHTML('<html><a href="/foo">foo</a></html>');
|
||||
|
||||
$node = $dom->getElementsByTagName('a')->item(0);
|
||||
$link = new Link($node);
|
||||
$link = new Link($node, 'http://example.com/');
|
||||
|
||||
$this->assertEquals('/foo', $link->getUri(), '->getUri() returns the URI of the link');
|
||||
$this->assertEquals($node, $link->getNode(), '->getNode() returns the node associated with the link');
|
||||
}
|
||||
|
||||
public function testGetMethod()
|
||||
{
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadHTML('<html><a href="/foo">foo</a></html>');
|
||||
|
||||
$node = $dom->getElementsByTagName('a')->item(0);
|
||||
$link = new Link($node, 'http://example.com/');
|
||||
|
||||
$this->assertEquals('get', $link->getMethod(), '->getMethod() returns the method of the link');
|
||||
|
||||
$link = new Link($node, 'post');
|
||||
$link = new Link($node, 'http://example.com/', 'post');
|
||||
$this->assertEquals('post', $link->getMethod(), '->getMethod() returns the method of the link');
|
||||
}
|
||||
|
||||
$link = new Link($node, 'get', 'http://localhost', '/bar/');
|
||||
$this->assertEquals('http://localhost/foo', $link->getUri(), '->getUri() returns the absolute URI of the link');
|
||||
$this->assertEquals('/foo', $link->getUri(false), '->getUri() returns the relative URI of the link if false is the first argument');
|
||||
|
||||
/**
|
||||
* @dataProvider getGetUriTests
|
||||
*/
|
||||
public function testGetUri($url, $currentUri, $expected)
|
||||
{
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadHTML('<html><a href="foo">foo</a></html>');
|
||||
$node = $dom->getElementsByTagName('a')->item(0);
|
||||
$dom->loadHTML(sprintf('<html><a href="%s">foo</a></html>', $url));
|
||||
$link = new Link($dom->getElementsByTagName('a')->item(0), $currentUri);
|
||||
|
||||
$link = new Link($node, 'get', 'http://localhost', '/bar/');
|
||||
$this->assertEquals('http://localhost/bar/foo', $link->getUri(), '->getUri() returns the absolute URI of the link for relative hrefs');
|
||||
$this->assertEquals('/bar/foo', $link->getUri(false), '->getUri() returns the relative URI of the link if false is the first argument');
|
||||
$this->assertEquals($expected, $link->getUri());
|
||||
}
|
||||
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadHTML('<html><a href="http://login.foo.com/foo">foo</a></html>');
|
||||
$node = $dom->getElementsByTagName('a')->item(0);
|
||||
public function getGetUriTests()
|
||||
{
|
||||
return array(
|
||||
array('/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
|
||||
array('/foo', 'http://localhost/bar/foo', 'http://localhost/foo'),
|
||||
|
||||
$link = new Link($node, 'get', 'http://www.foo.com');
|
||||
$this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');
|
||||
array('foo', 'http://localhost/bar/foo/', 'http://localhost/bar/foo/foo'),
|
||||
array('foo', 'http://localhost/bar/foo', 'http://localhost/bar/foo'),
|
||||
|
||||
$link = new Link($node, 'get');
|
||||
$this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');
|
||||
array('', 'http://localhost/bar/', 'http://localhost/bar/'),
|
||||
array('#', 'http://localhost/bar/', 'http://localhost/bar/#'),
|
||||
array('?a=b', 'http://localhost/bar/', 'http://localhost/bar/?a=b'),
|
||||
|
||||
$link = new Link($node, 'get', null, '/bar/');
|
||||
$this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');
|
||||
|
||||
$link = new Link($node, 'get','http://www.foo.com','/bar/');
|
||||
$this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');
|
||||
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadHTML('<html><a href="?get=param">foo</a></html>');
|
||||
$node = $dom->getElementsByTagName('a')->item(0);
|
||||
|
||||
$link = new Link($node, 'get', 'http://www.foo.com', '/foo/bar');
|
||||
$this->assertEquals('http://www.foo.com/foo/bar?get=param', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');
|
||||
|
||||
$link = new Link($node, 'get', 'http://www.foo.com', '/foo/bar');
|
||||
$this->assertEquals('/foo/bar?get=param', $link->getUri(false), '->getUri() returns the relative URI of the link if false is the first argument');
|
||||
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadHTML('<html><a href="test.html">foo</a></html>');
|
||||
$node = $dom->getElementsByTagName('a')->item(0);
|
||||
$link = new Link($node, 'get', null, '/foo/bar', 'http://www.foo.com/');
|
||||
$this->assertEquals('http://www.foo.com/test.html', $link->getUri());
|
||||
array('http://login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user