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 [ [null, "\n"], [1, "1\n"], ['abc', "abc\n"], [true, "1\n"], ["<>", "<>\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 [ [[], "\n"], [[1, 'abc'], "1abc\n"], [[ 'a' => 1, 'b' => 'abc', ], "1abc\n"], [[ 1, 'abc', [2, 'def'], true, ], "1abc2def1\n"], [[ 'a' => 1, 'b' => 'abc', 'c' => [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 [ [new Post(123, 'abc'), "123abc\n"], [[ new Post(123, 'abc'), new Post(456, 'def'), ], "123abc456def\n"], [[ new Post(123, '<>'), 'a' => new Post(456, 'def'), ], "123<>456def\n"], ]; } }