Skip to content

Commit

Permalink
Update assertions.
Browse files Browse the repository at this point in the history
Use assertXmlStringEqualsXmlString() it gives
better error reporting.  Also update string concat
into heredocs.
  • Loading branch information
markstory committed May 4, 2012
1 parent 73b0345 commit 4ab6d37
Showing 1 changed file with 168 additions and 71 deletions.
239 changes: 168 additions & 71 deletions lib/Cake/Test/Case/Utility/XmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,14 @@ public function testBuild() {
$obj = Xml::build($xml, array('return' => 'domdocument'));
$this->assertEquals('tags', $obj->firstChild->nodeName);

$this->assertEquals(Xml::build($xml, array('return' => 'domdocument')), Xml::build(file_get_contents($xml), array('return' => 'domdocument')));
$this->assertEquals(Xml::build($xml, array('return' => 'simplexml')), Xml::build($xml, 'simplexml'));
$this->assertEquals(
Xml::build($xml, array('return' => 'domdocument')),
Xml::build(file_get_contents($xml), array('return' => 'domdocument'))
);
$this->assertEquals(
Xml::build($xml, array('return' => 'simplexml')),
Xml::build($xml, 'simplexml')
);

$xml = array('tag' => 'value');
$obj = Xml::build($xml);
Expand Down Expand Up @@ -234,15 +240,33 @@ public function testFromArray() {
$this->assertTrue($obj instanceof SimpleXMLElement);
$this->assertEquals('tags', $obj->getName());
$this->assertEquals(2, count($obj));
$xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag id="1" name="defect"/><tag id="2" name="enhancement"/></tags>';
$this->assertEquals(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
$xmlText = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<tags>
<tag id="1" name="defect"/>
<tag id="2" name="enhancement"/>
</tags>
XML;
$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());

$obj = Xml::fromArray($xml);
$this->assertTrue($obj instanceof SimpleXMLElement);
$this->assertEquals('tags', $obj->getName());
$this->assertEquals(2, count($obj));
$xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag><id>1</id><name>defect</name></tag><tag><id>2</id><name>enhancement</name></tag></tags>';
$this->assertEquals(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
$xmlText = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<tags>
<tag>
<id>1</id>
<name>defect</name>
</tag>
<tag>
<id>2</id>
<name>enhancement</name>
</tag>
</tags>
XML;
$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());

$xml = array(
'tags' => array(
Expand Down Expand Up @@ -286,8 +310,18 @@ public function testFromArray() {
)
);
$obj = Xml::fromArray($xml, 'tags');
$xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag id="1"><name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>';
$this->assertEquals(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
$xmlText = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<tags>
<tag id="1">
<name>defect</name>
</tag>
<tag id="2">
<name>enhancement</name>
</tag>
</tags>
XML;
$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());

$xml = array(
'tags' => array(
Expand All @@ -306,8 +340,11 @@ public function testFromArray() {
)
);
$obj = Xml::fromArray($xml, 'tags');
$xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags>All tags<tag id="1">Tag 1<name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>';
$this->assertEquals(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
$xmlText = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<tags>All tags<tag id="1">Tag 1<name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>
XML;
$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());

$xml = array(
'tags' => array(
Expand All @@ -319,7 +356,7 @@ public function testFromArray() {
);
$obj = Xml::fromArray($xml, 'attributes');
$xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag id="1">defect</tag></tags>';
$this->assertEquals(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
}

/**
Expand Down Expand Up @@ -514,9 +551,11 @@ public function testToArray() {
$this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, 'attributes')));
$this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, array('format' => 'attributes', 'return' => 'domdocument'))));

$xml = '<root>';
$xml .= '<tag id="1">defect</tag>';
$xml .= '</root>';
$xml = <<<XML
<root>
<tag id="1">defect</tag>
</root>
XML;
$obj = Xml::build($xml);

$expected = array(
Expand All @@ -529,11 +568,13 @@ public function testToArray() {
);
$this->assertEquals($expected, Xml::toArray($obj));

$xml = '<root>';
$xml .= '<table xmlns="http://www.w3.org/TR/html4/"><tr><td>Apples</td><td>Bananas</td></tr></table>';
$xml .= '<table xmlns="http://www.cakephp.org"><name>CakePHP</name><license>MIT</license></table>';
$xml .= '<table>The book is on the table.</table>';
$xml .= '</root>';
$xml = <<<XML
<root>
<table xmlns="http://www.w3.org/TR/html4/"><tr><td>Apples</td><td>Bananas</td></tr></table>
<table xmlns="http://www.cakephp.org"><name>CakePHP</name><license>MIT</license></table>
<table>The book is on the table.</table>
</root>
XML;
$obj = Xml::build($xml);

$expected = array(
Expand All @@ -547,10 +588,12 @@ public function testToArray() {
);
$this->assertEquals($expected, Xml::toArray($obj));

$xml = '<root xmlns:cake="http://www.cakephp.org/">';
$xml .= '<tag>defect</tag>';
$xml .= '<cake:bug>1</cake:bug>';
$xml .= '</root>';
$xml = <<<XML
<root xmlns:cake="http://www.cakephp.org/">
<tag>defect</tag>
<cake:bug>1</cake:bug>
</root>
XML;
$obj = Xml::build($xml);

$expected = array(
Expand Down Expand Up @@ -614,18 +657,27 @@ public function testRss() {
)
);
$rssAsSimpleXML = Xml::fromArray($rss);
$xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?>';
$xmlText .= '<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">';
$xmlText .= '<channel>';
$xmlText .= '<atom:link href="http://bakery.cakephp.org/articles/rss" rel="self" type="application/rss+xml"/>';
$xmlText .= '<title>The Bakery: </title>';
$xmlText .= '<link>http://bakery.cakephp.org/</link>';
$xmlText .= '<description>Recent Articles at The Bakery.</description>';
$xmlText .= '<pubDate>Sun, 12 Sep 2010 04:18:26 -0500</pubDate>';
$xmlText .= '<item><title>CakePHP 1.3.4 released</title><link>http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released</link></item>';
$xmlText .= '<item><title>Wizard Component 1.2 Tutorial</title><link>http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial</link></item>';
$xmlText .= '</channel></rss>';
$this->assertEquals(str_replace(array("\r", "\n"), '', $rssAsSimpleXML->asXML()), $xmlText);
$xmlText = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<atom:link href="http://bakery.cakephp.org/articles/rss" rel="self" type="application/rss+xml"/>
<title>The Bakery: </title>
<link>http://bakery.cakephp.org/</link>
<description>Recent Articles at The Bakery.</description>
<pubDate>Sun, 12 Sep 2010 04:18:26 -0500</pubDate>
<item>
<title>CakePHP 1.3.4 released</title>
<link>http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released</link>
</item>
<item>
<title>Wizard Component 1.2 Tutorial</title>
<link>http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial</link>
</item>
</channel>
</rss>
XML;
$this->assertXmlStringEqualsXmlString($xmlText, $rssAsSimpleXML->asXML());
}

/**
Expand Down Expand Up @@ -667,7 +719,27 @@ public function testXmlRpc() {
);
$this->assertSame(Xml::toArray($xml), $expected);

$xmlText = '<?xml version="1.0" encoding="UTF-8"?><methodResponse><params><param><value><array><data><value><int>1</int></value><value><string>testing</string></value></data></array></value></param></params></methodResponse>';
$xmlText = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<array>
<data>
<value>
<int>1</int>
</value>
<value>
<string>testing</string>
</value>
</data>
</array>
</value>
</param>
</params>
</methodResponse>
XML;
$xml = Xml::build($xmlText);
$expected = array(
'methodResponse' => array(
Expand All @@ -690,7 +762,7 @@ public function testXmlRpc() {
$this->assertSame(Xml::toArray($xml), $expected);

$xml = Xml::fromArray($expected, 'tags');
$this->assertEquals(str_replace(array("\r", "\n"), '', $xml->asXML()), $xmlText);
$this->assertXmlStringEqualsXmlString($xmlText, $xml->asXML());
}

/**
Expand Down Expand Up @@ -738,12 +810,15 @@ public function testSoap() {
)
);
$xmlRequest = Xml::fromArray($xml, array('encoding' => null));
$xmlText = '<' . '?xml version="1.0"?>';
$xmlText .= '<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">';
$xmlText .= '<soap:Body xmlns:m="http://www.example.org/stock">';
$xmlText .= '<m:GetStockPrice><m:StockName>IBM</m:StockName></m:GetStockPrice>';
$xmlText .= '</soap:Body></soap:Envelope>';
$this->assertEquals(str_replace(array("\r", "\n"), '', $xmlRequest->asXML()), $xmlText);
$xmlText = <<<XML
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPrice><m:StockName>IBM</m:StockName></m:GetStockPrice>
</soap:Body>
</soap:Envelope>
XML;
$this->assertXmlStringEqualsXmlString($xmlText, $xmlRequest->asXML());
}

/**
Expand All @@ -752,7 +827,16 @@ public function testSoap() {
* @return void
*/
public function testNamespace() {
$xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:tag id="1"><child>good</child><otherchild>bad</otherchild></ns:tag><tag>Tag without ns</tag></root>');
$xml = <<<XML
<root xmlns:ns="http://cakephp.org">
<ns:tag id="1">
<child>good</child>
<otherchild>bad</otherchild>
</ns:tag>
<tag>Tag without ns</tag>
</root>
XML;
$xmlResponse = Xml::build($xml);
$expected = array(
'root' => array(
'ns:tag' => array(
Expand Down Expand Up @@ -812,9 +896,17 @@ public function testNamespace() {
)
)
);
$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><tag xmlns:pref="http://cakephp.org"><pref:item>item 1</pref:item><pref:item>item 2</pref:item></tag></root>';
$expected = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<tag xmlns:pref="http://cakephp.org">
<pref:item>item 1</pref:item>
<pref:item>item 2</pref:item>
</tag>
</root>
XML;
$xmlResponse = Xml::fromArray($xml);
$this->assertEquals(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
$this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());

$xml = array(
'root' => array(
Expand All @@ -825,7 +917,7 @@ public function testNamespace() {
);
$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><tag xmlns="http://cakephp.org"/></root>';
$xmlResponse = Xml::fromArray($xml);
$this->assertEquals(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
$this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());

$xml = array(
'root' => array(
Expand All @@ -834,7 +926,7 @@ public function testNamespace() {
);
$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns="http://cakephp.org"/>';
$xmlResponse = Xml::fromArray($xml);
$this->assertEquals(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
$this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());

$xml = array(
'root' => array(
Expand All @@ -843,7 +935,7 @@ public function testNamespace() {
);
$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns:ns="http://cakephp.org"/>';
$xmlResponse = Xml::fromArray($xml);
$this->assertEquals(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
$this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
}

/**
Expand Down Expand Up @@ -893,34 +985,38 @@ public function testWithModel() {
$data = $user->read(null, 1);

$obj = Xml::build(compact('data'));
$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><data>';
$expected .= '<User><id>1</id><user>mariano</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>';
$expected .= '<created>2007-03-17 01:16:23</created><updated>2007-03-17 01:18:31</updated></User>';
$expected .= '<Article><id>1</id><user_id>1</user_id><title>First Article</title><body>First Article Body</body>';
$expected .= '<published>Y</published><created>2007-03-18 10:39:23</created><updated>2007-03-18 10:41:31</updated></Article>';
$expected .= '<Article><id>3</id><user_id>1</user_id><title>Third Article</title><body>Third Article Body</body>';
$expected .= '<published>Y</published><created>2007-03-18 10:43:23</created><updated>2007-03-18 10:45:31</updated></Article>';
$expected .= '</data>';
$this->assertEquals($expected, str_replace(array("\r", "\n"), '', $obj->asXML()));
$expected = <<<XML
<?xml version="1.0" encoding="UTF-8"?><data>
<User><id>1</id><user>mariano</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
<created>2007-03-17 01:16:23</created><updated>2007-03-17 01:18:31</updated></User>
<Article><id>1</id><user_id>1</user_id><title>First Article</title><body>First Article Body</body>
<published>Y</published><created>2007-03-18 10:39:23</created><updated>2007-03-18 10:41:31</updated></Article>
<Article><id>3</id><user_id>1</user_id><title>Third Article</title><body>Third Article Body</body>
<published>Y</published><created>2007-03-18 10:43:23</created><updated>2007-03-18 10:45:31</updated></Article>
</data>
XML;
$this->assertXmlStringEqualsXmlString($expected, $obj->asXML());

//multiple model results - without a records key it would fatal error
$data = $user->find('all', array('limit' => 2));
$data = array('records' => $data);
$obj = Xml::build(compact('data'));
$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><data>';
$expected .= '<records>';
$expected .= '<User><id>1</id><user>mariano</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>';
$expected .= '<created>2007-03-17 01:16:23</created><updated>2007-03-17 01:18:31</updated></User>';
$expected .= '<Article><id>1</id><user_id>1</user_id><title>First Article</title><body>First Article Body</body>';
$expected .= '<published>Y</published><created>2007-03-18 10:39:23</created><updated>2007-03-18 10:41:31</updated></Article>';
$expected .= '<Article><id>3</id><user_id>1</user_id><title>Third Article</title><body>Third Article Body</body>';
$expected .= '<published>Y</published><created>2007-03-18 10:43:23</created><updated>2007-03-18 10:45:31</updated></Article>';
$expected .= '</records><records><User><id>2</id><user>nate</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>';
$expected .= '<created>2007-03-17 01:18:23</created><updated>2007-03-17 01:20:31</updated></User><Article/>';
$expected .= '</records>';
$expected .= '</data>';
$expected = <<<XML
<?xml version="1.0" encoding="UTF-8"?><data>
<records>
<User><id>1</id><user>mariano</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
<created>2007-03-17 01:16:23</created><updated>2007-03-17 01:18:31</updated></User>
<Article><id>1</id><user_id>1</user_id><title>First Article</title><body>First Article Body</body>
<published>Y</published><created>2007-03-18 10:39:23</created><updated>2007-03-18 10:41:31</updated></Article>
<Article><id>3</id><user_id>1</user_id><title>Third Article</title><body>Third Article Body</body>
<published>Y</published><created>2007-03-18 10:43:23</created><updated>2007-03-18 10:45:31</updated></Article>
</records><records><User><id>2</id><user>nate</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
<created>2007-03-17 01:18:23</created><updated>2007-03-17 01:20:31</updated></User><Article/>
</records>
</data>
XML;
$result = $obj->asXML();
$this->assertEquals($expected, str_replace(array("\r", "\n"), '', $obj->asXML()));
$this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
}

/**
Expand All @@ -938,4 +1034,5 @@ public function testAmpInText() {
$result = $obj->asXml();
$this->assertContains('mark &amp; mark', $result);
}

}

0 comments on commit 4ab6d37

Please sign in to comment.