merged branch entering/crawler-add-node-argument (PR #7924)

This PR was squashed before being merged into the master branch (closes #7924).

Discussion
----------

[Crawler] Add proper validation of node argument of method add

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Crawler method add, fix PHPdoc and

Commits
-------

41805c0 [Crawler] Add proper validation of node argument of method add
This commit is contained in:
Fabien Potencier 2013-05-06 08:24:44 +02:00
commit 616e0ac2f9
2 changed files with 21 additions and 3 deletions

View File

@ -58,7 +58,9 @@ class Crawler extends \SplObjectStorage
* This method uses the appropriate specialized add*() method based
* on the type of the argument.
*
* @param null|\DOMNodeList|array|\DOMNode $node A node
* @param \DOMNodeList|\DOMNode|array|string|null $node A node
*
* @throws \InvalidArgumentException When node is not the expected type.
*
* @api
*/
@ -66,12 +68,19 @@ class Crawler extends \SplObjectStorage
{
if ($node instanceof \DOMNodeList) {
$this->addNodeList($node);
} elseif ($node instanceof \DOMNode) {
$this->addNode($node);
} elseif (is_array($node)) {
$this->addNodes($node);
} elseif (is_string($node)) {
$this->addContent($node);
} elseif (is_object($node)) {
$this->addNode($node);
} elseif ($node !== null) {
throw new \InvalidArgumentException(
sprintf(
'Expecting node to be DOMNodeList|DOMNode|array|string|null, but got %s',
(is_object($node)) ? get_class($node) : gettype($node)
)
);
}
}

View File

@ -53,6 +53,15 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Foo', $crawler->filterXPath('//body')->text(), '->add() adds nodes from a string');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testAddInvalidNode()
{
$crawler = new Crawler();
$crawler->add(1);
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
*/