|
1 | | -<?php namespace CodeIgniter\API; |
| 1 | +<?php |
| 2 | +namespace CodeIgniter\API; |
2 | 3 |
|
3 | 4 | use CodeIgniter\HTTP\URI; |
4 | 5 | use CodeIgniter\HTTP\UserAgent; |
|
9 | 10 |
|
10 | 11 | class ResponseTraitTest extends \CIUnitTestCase |
11 | 12 | { |
| 13 | + |
12 | 14 | protected $request; |
13 | 15 | protected $response; |
14 | 16 |
|
@@ -45,8 +47,8 @@ protected function makeController(array $userConfig = [], string $uri = 'http:// |
45 | 47 |
|
46 | 48 | if (is_null($this->request)) |
47 | 49 | { |
48 | | - $this->request = new MockIncomingRequest((object)$config, new URI($uri), null, new UserAgent()); |
49 | | - $this->response = new MockResponse((object)$config); |
| 50 | + $this->request = new MockIncomingRequest((object) $config, new URI($uri), null, new UserAgent()); |
| 51 | + $this->response = new MockResponse((object) $config); |
50 | 52 | } |
51 | 53 |
|
52 | 54 | // Insert headers into request. |
@@ -102,15 +104,66 @@ public function testNoFormatterJSON() |
102 | 104 | $this->assertEquals($expected, $this->response->getBody()); |
103 | 105 | } |
104 | 106 |
|
105 | | - public function testNoFormatterHTML() |
| 107 | + public function testNoFormatter() |
106 | 108 | { |
107 | 109 | $this->formatter = null; |
108 | | - $controller = $this->makeController(); |
| 110 | + $controller = $this->makeController([], 'http://codeigniter.com', ['Accept' => 'application/json']); |
109 | 111 | $controller->respondCreated('A Custom Reason'); |
110 | 112 |
|
111 | 113 | $this->assertEquals('A Custom Reason', $this->response->getBody()); |
112 | 114 | } |
113 | 115 |
|
| 116 | + public function testAssociativeArrayPayload() |
| 117 | + { |
| 118 | + $this->formatter = null; |
| 119 | + $controller = $this->makeController(); |
| 120 | + $payload = ['answer' => 42]; |
| 121 | + $expected = <<<EOH |
| 122 | +{ |
| 123 | + "answer": 42 |
| 124 | +} |
| 125 | +EOH; |
| 126 | + $controller->respond($payload); |
| 127 | + $this->assertEquals($expected, $this->response->getBody()); |
| 128 | + } |
| 129 | + |
| 130 | + public function testArrayPayload() |
| 131 | + { |
| 132 | + $this->formatter = null; |
| 133 | + $controller = $this->makeController(); |
| 134 | + $payload = [ |
| 135 | + 1, |
| 136 | + 2, |
| 137 | + 3, |
| 138 | + ]; |
| 139 | + $expected = <<<EOH |
| 140 | +[ |
| 141 | + 1, |
| 142 | + 2, |
| 143 | + 3 |
| 144 | +] |
| 145 | +EOH; |
| 146 | + $controller->respond($payload); |
| 147 | + $this->assertEquals($expected, $this->response->getBody()); |
| 148 | + } |
| 149 | + |
| 150 | + public function testPHPtoArrayPayload() |
| 151 | + { |
| 152 | + $this->formatter = null; |
| 153 | + $controller = $this->makeController(); |
| 154 | + $payload = new \stdClass(); |
| 155 | + $payload->name = 'Tom'; |
| 156 | + $payload->id = 1; |
| 157 | + $expected = <<<EOH |
| 158 | +{ |
| 159 | + "name": "Tom", |
| 160 | + "id": 1 |
| 161 | +} |
| 162 | +EOH; |
| 163 | + $controller->respond((array)$payload); |
| 164 | + $this->assertEquals($expected, $this->response->getBody()); |
| 165 | + } |
| 166 | + |
114 | 167 | public function testRespondSets404WithNoData() |
115 | 168 | { |
116 | 169 | $controller = $this->makeController(); |
|
0 commit comments