id = $id; $this->title = $title; } } /** * @author Qiang Xue * @since 2.0 */ 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; } public function testFormatScalars() { $head = "\n"; $xml = $head . "\n"; $this->assertEquals($xml, $this->formatter->format($this->response, null)); $xml = $head . "1\n"; $this->assertEquals($xml, $this->formatter->format($this->response, 1)); $xml = $head . "abc\n"; $this->assertEquals($xml, $this->formatter->format($this->response, 'abc')); $xml = $head . "1\n"; $this->assertEquals($xml, $this->formatter->format($this->response, true)); } public function testFormatArrays() { $head = "\n"; $xml = $head . "\n"; $this->assertEquals($xml, $this->formatter->format($this->response, array())); $xml = $head . "1abc\n"; $this->assertEquals($xml, $this->formatter->format($this->response, array(1, 'abc'))); $xml = $head . "1abc\n"; $this->assertEquals($xml, $this->formatter->format($this->response, array( 'a' => 1, 'b' => 'abc', ))); $xml = $head . "1abc2def1\n"; $this->assertEquals($xml, $this->formatter->format($this->response, array( 1, 'abc', array(2, 'def'), true, ))); $xml = $head . "1abc2def1\n"; $this->assertEquals($xml, $this->formatter->format($this->response, array( 'a' => 1, 'b' => 'abc', 'c' => array(2, 'def'), true, ))); } public function testFormatObjects() { $head = "\n"; $xml = $head . "123abc\n"; $this->assertEquals($xml, $this->formatter->format($this->response, new Post(123, 'abc'))); $xml = $head . "123abc456def\n"; $this->assertEquals($xml, $this->formatter->format($this->response, array( new Post(123, 'abc'), new Post(456, 'def'), ))); $xml = $head . "123abc456def\n"; $this->assertEquals($xml, $this->formatter->format($this->response, array( new Post(123, 'abc'), 'a' => new Post(456, 'def'), ))); } }