|
| 1 | +<?php |
| 2 | +Yii::import('RestfullYii.tests.ERestTestRequestHelper'); |
| 3 | + |
| 4 | +/** |
| 5 | + * ETPUTPOSTDELETEPostFilterRenderEventTest |
| 6 | + * |
| 7 | + * Tests GET request for a list of resources |
| 8 | + * |
| 9 | + * @category PHP |
| 10 | + * @package Starship |
| 11 | + * @subpackage Restfullyii/Tests |
| 12 | + * @copyright Copyright (c) 2013 Evan Frohlich (https://github.com/evan108108) |
| 13 | + * @license https://github.com/evan108108 OSS |
| 14 | + * @version Release: 1.2.0 |
| 15 | + */ |
| 16 | +class ETPUTPOSTDELETEPostFilterRenderEventTest extends ERestTestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * testGETResourceRequestPostFilterRequest |
| 20 | + * |
| 21 | + * tests that a get request for a single resource |
| 22 | + * with new property added durring post filter |
| 23 | + * returns the correct response |
| 24 | + */ |
| 25 | + public function testGETResourceRequestPostFilterRequest() |
| 26 | + { |
| 27 | + $request = new ERestTestRequestHelper(); |
| 28 | + |
| 29 | + $request['config'] = [ |
| 30 | + 'url' => 'http://api/post/6', |
| 31 | + 'type' => 'GET', |
| 32 | + 'data' => null, |
| 33 | + 'headers' => [ |
| 34 | + 'X_REST_USERNAME' => 'admin@restuser', |
| 35 | + 'X_REST_PASSWORD' => 'admin@Access', |
| 36 | + ], |
| 37 | + ]; |
| 38 | + |
| 39 | + $request->addEvent(ERestEvent::POST_FILTER_MODEL_WITH_RELATIONS, function() { |
| 40 | + return []; |
| 41 | + }); |
| 42 | + |
| 43 | + $request->addEvent('post.filter.req.get.resource.render', function($json) { |
| 44 | + $j = CJSON::decode($json); |
| 45 | + $j['new_key'] = 'NEW VALUE'; |
| 46 | + return $j; |
| 47 | + }); |
| 48 | + |
| 49 | + $request_response = $request->send(); |
| 50 | + $expected_response = '{"success":true,"message":"Record Found","new_key":"NEW VALUE","data":{"totalCount":1,"post":{"id":"6","title":"title6","content":"content6","create_time":"2013-08-07 10:09:46","author_id":"6"}}}'; |
| 51 | + $this->assertJsonStringEqualsJsonString($request_response, $expected_response); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * testGETResourcesCategoriesPostFilterRequest |
| 56 | + * |
| 57 | + * tests that a GET request for a list of 'Category' resources |
| 58 | + * with new property added durring post filter |
| 59 | + * returns the correct response |
| 60 | + */ |
| 61 | + public function testGETResourcesCategoriesPostFilterRequest() |
| 62 | + { |
| 63 | + $request = new ERestTestRequestHelper(); |
| 64 | + |
| 65 | + $request['config'] = [ |
| 66 | + 'url' => 'http://api/category', |
| 67 | + 'type' => 'GET', |
| 68 | + 'data' => null, |
| 69 | + 'headers' => [ |
| 70 | + 'X_REST_USERNAME' => 'admin@restuser', |
| 71 | + 'X_REST_PASSWORD' => 'admin@Access', |
| 72 | + ], |
| 73 | + ]; |
| 74 | + |
| 75 | + $request->addEvent('post.filter.req.get.resources.render', function($json) { |
| 76 | + $j = CJSON::decode($json); |
| 77 | + $j['new_key'] = 'NEW VALUE'; |
| 78 | + return $j; |
| 79 | + }); |
| 80 | + |
| 81 | + $request_response = $request->send(); |
| 82 | + $expected_response = '{"success":true,"message":"Record(s) Found", "new_key":"NEW VALUE", "data":{"totalCount":6,"category":[{"id":"1","name":"cat1","posts":[{"id":"1","title":"title1","content":"content1","create_time":"2013-08-07 10:09:41","author_id":"1"}]},{"id":"2","name":"cat2","posts":[{"id":"1","title":"title1","content":"content1","create_time":"2013-08-07 10:09:41","author_id":"1"},{"id":"2","title":"title2","content":"content2","create_time":"2013-08-07 10:09:42","author_id":"2"}]},{"id":"3","name":"cat3","posts":[{"id":"3","title":"title3","content":"content3","create_time":"2013-08-07 10:09:43","author_id":"3"}]},{"id":"4","name":"cat4","posts":[{"id":"4","title":"title4","content":"content4","create_time":"2013-08-07 10:09:44","author_id":"4"}]},{"id":"5","name":"cat5","posts":[{"id":"5","title":"title5","content":"content5","create_time":"2013-08-07 10:09:45","author_id":"5"}]},{"id":"6","name":"cat6","posts":[{"id":"6","title":"title6","content":"content6","create_time":"2013-08-07 10:09:46","author_id":"6"}]}]}}'; |
| 83 | + $this->assertJsonStringEqualsJsonString($request_response, $expected_response); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * testPUTResourceCategoryPostFilterRequest |
| 88 | + * |
| 89 | + * tests that a PUT request |
| 90 | + * correctly updates a resource |
| 91 | + * with new property added durring post filter |
| 92 | + */ |
| 93 | + public function testPUTResourceCategoryPostFilterRequest() |
| 94 | + { |
| 95 | + $request = new ERestTestRequestHelper(); |
| 96 | + |
| 97 | + $data = '{"id":"1","name":"update_cat_name"}'; |
| 98 | + |
| 99 | + $request['config'] = [ |
| 100 | + 'url' => 'http://api/category/1', |
| 101 | + 'type' => 'PUT', |
| 102 | + 'data' => $data, |
| 103 | + 'headers' => [ |
| 104 | + 'X_REST_USERNAME' => 'admin@restuser', |
| 105 | + 'X_REST_PASSWORD' => 'admin@Access', |
| 106 | + ], |
| 107 | + ]; |
| 108 | + |
| 109 | + $request->addEvent(ERestEvent::MODEL_WITH_RELATIONS, function() { |
| 110 | + return []; |
| 111 | + }); |
| 112 | + |
| 113 | + $request->addEvent('post.filter.req.put.resource.render', function($json) { |
| 114 | + $j = CJSON::decode($json); |
| 115 | + $j['new_key'] = 'NEW VALUE'; |
| 116 | + return $j; |
| 117 | + }); |
| 118 | + |
| 119 | + $request_response = $request->send(); |
| 120 | + $expected_response = '{"success":true,"message":"Record Updated","new_key":"NEW VALUE","data":{"totalCount":1,"category":' . $data . '}}'; |
| 121 | + $this->assertJsonStringEqualsJsonString($request_response, $expected_response); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * testPOSTResourceCategoryPostFilterRequest |
| 126 | + * |
| 127 | + * tests that a POST request |
| 128 | + * correctly creates a resource |
| 129 | + * with new property added durring post filter |
| 130 | + */ |
| 131 | + public function testPOSTResourceCategoryPostFilterRequest() |
| 132 | + { |
| 133 | + $request = new ERestTestRequestHelper(); |
| 134 | + |
| 135 | + $data = '{"name":"new_cat_name"}'; |
| 136 | + |
| 137 | + $request['config'] = [ |
| 138 | + 'url' => 'http://api/category', |
| 139 | + 'type' => 'POST', |
| 140 | + 'data' => $data, |
| 141 | + 'headers' => [ |
| 142 | + 'X_REST_USERNAME' => 'admin@restuser', |
| 143 | + 'X_REST_PASSWORD' => 'admin@Access', |
| 144 | + ], |
| 145 | + ]; |
| 146 | + |
| 147 | + $request->addEvent('post.filter.req.post.resource.render', function($json) { |
| 148 | + $j = CJSON::decode($json); |
| 149 | + $j['new_key'] = 'NEW VALUE'; |
| 150 | + return $j; |
| 151 | + }); |
| 152 | + |
| 153 | + $request_response = $request->send(); |
| 154 | + $expected_response = '{"success":true,"message":"Record Created","new_key":"NEW VALUE","data":{"totalCount":1,"category":{"id":"7","name":"new_cat_name","posts":[]}}}'; |
| 155 | + $this->assertJsonStringEqualsJsonString($request_response, $expected_response); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * testDELETEResourceCategoryPostFilterRequest |
| 160 | + * |
| 161 | + * tests that a DELETE request |
| 162 | + * correctly deletes a resource |
| 163 | + * with new property added durring post filter |
| 164 | + */ |
| 165 | + public function testDELETEResourceCategoryPostFilterRequest() |
| 166 | + { |
| 167 | + $request = new ERestTestRequestHelper(); |
| 168 | + |
| 169 | + $request['config'] = [ |
| 170 | + 'url' => 'http://api/category/4', |
| 171 | + 'type' => 'DELETE', |
| 172 | + 'data' => null, |
| 173 | + 'headers' => [ |
| 174 | + 'X_REST_USERNAME' => 'admin@restuser', |
| 175 | + 'X_REST_PASSWORD' => 'admin@Access', |
| 176 | + ], |
| 177 | + ]; |
| 178 | + |
| 179 | + $request->addEvent('post.filter.req.delete.resource.render', function($json) { |
| 180 | + $j = CJSON::decode($json); |
| 181 | + $j['new_key'] = 'NEW VALUE'; |
| 182 | + return $j; |
| 183 | + }); |
| 184 | + |
| 185 | + $request_response = $request->send(); |
| 186 | + $expected_response = '{"success":true,"message":"Record Deleted","new_key":"NEW VALUE","data":{"totalCount":1,"category":{"id":"4","name":"cat4"}}}'; |
| 187 | + $this->assertJsonStringEqualsJsonString($request_response, $expected_response); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * testGETSubresourcePostCategoryPostFilterRequest |
| 192 | + * |
| 193 | + * tests that a get request for a single sub-resource |
| 194 | + * returns the correct response |
| 195 | + */ |
| 196 | + public function testGETSubresourcePostCategoryPostFilterRequest() |
| 197 | + { |
| 198 | + $request = new ERestTestRequestHelper(); |
| 199 | + |
| 200 | + $request['config'] = [ |
| 201 | + 'url' => 'http://api/post/1/categories/1', |
| 202 | + 'type' => 'GET', |
| 203 | + 'data' => null, |
| 204 | + 'headers' => [ |
| 205 | + 'X_REST_USERNAME' => 'admin@restuser', |
| 206 | + 'X_REST_PASSWORD' => 'admin@Access', |
| 207 | + ], |
| 208 | + ]; |
| 209 | + |
| 210 | + $request->addEvent('post.filter.req.get.subresource.render', function($json) { |
| 211 | + $j = CJSON::decode($json); |
| 212 | + $j['new_key'] = 'NEW VALUE'; |
| 213 | + return $j; |
| 214 | + }); |
| 215 | + |
| 216 | + $request_response = $request->send(); |
| 217 | + $expected_response = '{"success":true,"message":"Record(s) Found","new_key":"NEW VALUE","data":{"totalCount":1,"category":{"id":"1","name":"cat1"}}}'; |
| 218 | + $this->assertJsonStringEqualsJsonString($request_response, $expected_response); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * testGETSubresourcesPostCategoryPostFilterRequest |
| 223 | + * |
| 224 | + * tests that a get request for a single sub-resource |
| 225 | + * returns the correct response |
| 226 | + */ |
| 227 | + public function testGETSubresourcesPostCategoryPostFilterRequest() |
| 228 | + { |
| 229 | + $request = new ERestTestRequestHelper(); |
| 230 | + |
| 231 | + $request['config'] = [ |
| 232 | + 'url' => 'http://api/post/1/categories', |
| 233 | + 'type' => 'GET', |
| 234 | + 'data' => null, |
| 235 | + 'headers' => [ |
| 236 | + 'X_REST_USERNAME' => 'admin@restuser', |
| 237 | + 'X_REST_PASSWORD' => 'admin@Access', |
| 238 | + ], |
| 239 | + ]; |
| 240 | + |
| 241 | + $request->addEvent('post.filter.req.get.subresources.render', function($json) { |
| 242 | + $j = CJSON::decode($json); |
| 243 | + $j['new_key'] = 'NEW VALUE'; |
| 244 | + return $j; |
| 245 | + }); |
| 246 | + |
| 247 | + $request_response = $request->send(); |
| 248 | + $expected_response = '{"success":true,"message":"Record(s) Found","new_key":"NEW VALUE","data":{"totalCount":2,"category":[{"id":"1","name":"cat1"},{"id":"2","name":"cat2"}]}}'; |
| 249 | + $this->assertJsonStringEqualsJsonString($request_response, $expected_response); |
| 250 | + } |
| 251 | + |
| 252 | + /** |
| 253 | + * testPUTSubresourceCategoryPostsPostFilterRequest |
| 254 | + * |
| 255 | + * tests that a PUT request with belongsTo |
| 256 | + * correctly updates a resource |
| 257 | + * with new property added durring post filter |
| 258 | + */ |
| 259 | + public function testPUTSubresourceCategoryPostsPostFilterRequest() |
| 260 | + { |
| 261 | + $request = new ERestTestRequestHelper(); |
| 262 | + |
| 263 | + $request['config'] = [ |
| 264 | + 'url' => 'http://api/category/4/posts/1', |
| 265 | + 'type' => 'PUT', |
| 266 | + 'data' => null, |
| 267 | + 'headers' => [ |
| 268 | + 'X_REST_USERNAME' => 'admin@restuser', |
| 269 | + 'X_REST_PASSWORD' => 'admin@Access', |
| 270 | + ], |
| 271 | + ]; |
| 272 | + |
| 273 | + $request->addEvent('post.filter.req.put.subresource.render', function($json) { |
| 274 | + $j = CJSON::decode($json); |
| 275 | + $j['new_key'] = 'NEW VALUE'; |
| 276 | + return $j; |
| 277 | + }); |
| 278 | + |
| 279 | + $request_response = $request->send(); |
| 280 | + $expected_response = '{"success":true,"message":"Subresource Added","new_key":"NEW VALUE","data":{"totalCount":1,"category":{"id":"4","name":"cat4","posts":[{"id":"1","title":"title1","content":"content1","create_time":"2013-08-07 10:09:41","author_id":"1"},{"id":"4","title":"title4","content":"content4","create_time":"2013-08-07 10:09:44","author_id":"4"}]}}}'; |
| 281 | + $this->assertJsonStringEqualsJsonString($request_response, $expected_response); |
| 282 | + } |
| 283 | + |
| 284 | + /** |
| 285 | + * testDELETESubresourceCategoryPostsPostFilterRequest |
| 286 | + * |
| 287 | + * tests that a DELETE request |
| 288 | + * correctly deletes a Sub-Resource |
| 289 | + * with new property added durring post filter |
| 290 | + */ |
| 291 | + public function testDELETESubresourceCategoryPostsPostFilterRequest() |
| 292 | + { |
| 293 | + $request = new ERestTestRequestHelper(); |
| 294 | + |
| 295 | + $request['config'] = [ |
| 296 | + 'url' => 'http://api/category/1/posts/1', |
| 297 | + 'type' => 'DELETE', |
| 298 | + 'data' => null, |
| 299 | + 'headers' => [ |
| 300 | + 'X_REST_USERNAME' => 'admin@restuser', |
| 301 | + 'X_REST_PASSWORD' => 'admin@Access', |
| 302 | + ], |
| 303 | + ]; |
| 304 | + |
| 305 | + $request->addEvent('post.filter.req.delete.subresource.render', function($json) { |
| 306 | + $j = CJSON::decode($json); |
| 307 | + $j['new_key'] = 'NEW VALUE'; |
| 308 | + return $j; |
| 309 | + }); |
| 310 | + |
| 311 | + $request_response = $request->send(); |
| 312 | + $expected_response = '{"success":true,"message":"Sub-Resource Deleted","new_key":"NEW VALUE","data":{"totalCount":1,"category":{"id":"1","name":"cat1","posts":[]}}}'; |
| 313 | + $this->assertJsonStringEqualsJsonString($request_response, $expected_response); |
| 314 | + } |
| 315 | +} |
0 commit comments