XmlResponseFormatterTest.php 3.95 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yiiunit\framework\web;

use yii\base\Object;
use yii\web\Response;
use yii\web\XmlResponseFormatter;

class Post extends Object
{
16 17
    public $id;
    public $title;
Qiang Xue committed
18

19 20 21 22 23
    public function __construct($id, $title)
    {
        $this->id = $id;
        $this->title = $title;
    }
Qiang Xue committed
24 25 26 27 28
}

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
29 30
 *
 * @group web
Qiang Xue committed
31 32 33
 */
class XmlResponseFormatterTest extends \yiiunit\TestCase
{
34 35 36 37 38 39 40 41
    /**
     * @var Response
     */
    public $response;
    /**
     * @var XmlResponseFormatter
     */
    public $formatter;
Qiang Xue committed
42

43 44 45 46 47 48
    protected function setUp()
    {
        $this->mockApplication();
        $this->response = new Response;
        $this->formatter = new XmlResponseFormatter;
    }
Qiang Xue committed
49

50 51 52 53 54 55 56 57 58 59 60 61
    /**
     * @param mixed  $data the data to be formatted
     * @param string $xml  the expected XML body
     * @dataProvider formatScalarDataProvider
     */
    public function testFormatScalar($data, $xml)
    {
        $head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        $this->response->data = $data;
        $this->formatter->format($this->response);
        $this->assertEquals($head . $xml, $this->response->content);
    }
Qiang Xue committed
62

63 64 65 66 67 68 69 70 71 72
    public function formatScalarDataProvider()
    {
        return [
            [null, "<response></response>\n"],
            [1, "<response>1</response>\n"],
            ['abc', "<response>abc</response>\n"],
            [true, "<response>1</response>\n"],
            ["<>", "<response>&lt;&gt;</response>\n"],
        ];
    }
Qiang Xue committed
73

74 75 76 77 78 79 80 81 82 83 84 85
    /**
     * @param mixed  $data the data to be formatted
     * @param string $xml  the expected XML body
     * @dataProvider formatArrayDataProvider
     */
    public function testFormatArrays($data, $xml)
    {
        $head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        $this->response->data = $data;
        $this->formatter->format($this->response);
        $this->assertEquals($head . $xml, $this->response->content);
    }
Qiang Xue committed
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    public function formatArrayDataProvider()
    {
        return [
            [[], "<response/>\n"],
            [[1, 'abc'], "<response><item>1</item><item>abc</item></response>\n"],
            [[
                'a' => 1,
                'b' => 'abc',
            ], "<response><a>1</a><b>abc</b></response>\n"],
            [[
                1,
                'abc',
                [2, 'def'],
                true,
            ], "<response><item>1</item><item>abc</item><item><item>2</item><item>def</item></item><item>1</item></response>\n"],
            [[
                'a' => 1,
                'b' => 'abc',
                'c' => [2, '<>'],
                true,
            ], "<response><a>1</a><b>abc</b><c><item>2</item><item>&lt;&gt;</item></c><item>1</item></response>\n"],
        ];
    }
Qiang Xue committed
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    /**
     * @param mixed  $data the data to be formatted
     * @param string $xml  the expected XML body
     * @dataProvider formatObjectDataProvider
     */
    public function testFormatObjects($data, $xml)
    {
        $head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\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'), "<response><Post><id>123</id><title>abc</title></Post></response>\n"],
            [[
                new Post(123, 'abc'),
                new Post(456, 'def'),
            ], "<response><Post><id>123</id><title>abc</title></Post><Post><id>456</id><title>def</title></Post></response>\n"],
            [[
                new Post(123, '<>'),
                'a' => new Post(456, 'def'),
            ], "<response><Post><id>123</id><title>&lt;&gt;</title></Post><a><Post><id>456</id><title>def</title></Post></a></response>\n"],
        ];
    }
Qiang Xue committed
138
}