id = $id; $this->title = $title; } } /** * @author Qiang Xue * @since 2.0 * * @group web */ class XmlResponseFormatterTest extends \yiiunit\TestCase { /** * @var Response */ public $response; /** * @var XmlResponseFormatter */ public $formatter; protected function setUp() { $this->mockApplication(); $this->response = new Response; $this->formatter = new XmlResponseFormatter; } /** * @param mixed $data the data to be formatted * @param string $xml the expected XML body * @dataProvider formatScalarDataProvider */ public function testFormatScalar($data, $xml) { $head = "\n"; $this->response->data = $data; $this->formatter->format($this->response); $this->assertEquals($head . $xml, $this->response->content); } public function formatScalarDataProvider() { return array( array(null, "\n"), array(1, "1\n"), array('abc', "abc\n"), array(true, "1\n"), array("<>", "<>\n"), ); } /** * @param mixed $data the data to be formatted * @param string $xml the expected XML body * @dataProvider formatArrayDataProvider */ public function testFormatArrays($data, $xml) { $head = "\n"; $this->response->data = $data; $this->formatter->format($this->response); $this->assertEquals($head . $xml, $this->response->content); } public function formatArrayDataProvider() { return array( array(array(), "\n"), array(array(1, 'abc'), "1abc\n"), array(array( 'a' => 1, 'b' => 'abc', ), "1abc\n"), array(array( 1, 'abc', array(2, 'def'), true, ), "1abc2def1\n"), array(array( 'a' => 1, 'b' => 'abc', 'c' => array(2, '<>'), true, ), "1abc2<>1\n"), ); } /** * @param mixed $data the data to be formatted * @param string $xml the expected XML body * @dataProvider formatObjectDataProvider */ public function testFormatObjects($data, $xml) { $head = "\n"; $this->response->data = $data; $this->formatter->format($this->response); $this->assertEquals($head . $xml, $this->response->content); } public function formatObjectDataProvider() { return array( array(new Post(123, 'abc'), "123abc\n"), array(array( new Post(123, 'abc'), new Post(456, 'def'), ), "123abc456def\n"), array(array( new Post(123, '<>'), 'a' => new Post(456, 'def'), ), "123<>456def\n"), ); } }