Dennais
Dennais

Reputation: 506

Symfony 2 test xml with Symfony\Component\DomCrawler\Crawler

I've got an url that return an xml but I have some problem to extract "link" element.

<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
  <channel>
    <item>
      <id>123</id>
      <title>my title</title>
      <link>
        http://example.org
      </link>
    </item>
  </channel>
</rss>

I need to test it with

Symfony\Component\DomCrawler\Crawler

These are my tests:

$crawler = $this->client->get('/my-feed');

$items = $crawler->filterXPath('//channel/item');
$this->assertGreaterThanOrEqual(1, $items->count()); // ok pass

// ...
$titles = $items->filterXPath('//title')->extract(array('_text'));
$this->assertContains("my title", $titles);  // ok pass


// ...
$links = $items->filterXPath('//link')->extract(array('_text'));
$this->assertContains("example.org", $links);  // KO!!! don't pass

var_dump($links); // empty string

"link" is a reserved word?

Upvotes: 1

Views: 1605

Answers (1)

j0k
j0k

Reputation: 22756

Your XML is broken:

  1. you don't have a closing channel node </channel>
  2. you don't have a closing rss node </rss>

Here is corrected XML :

<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        <item>
            <id>123</id>
            <title>my title</title>
            <link>http://example.org</link>
        </item>
    </channel>
</rss>

Then, ->extract() returns An array of extracted values. So you shouldn't directly try to see its contain but get the first element and do your test:

$this->assertContains("my title", $titles[0]);
// ...
$this->assertContains("example.org", $links[0]);

Upvotes: 2

Related Questions