/**
  * testGETCustomRouteTowParamPreFilterRequest
  *
  * tests that a GET request with custom route and two params and pre-filter
  * returns the correct response
  */
 public function testGETCustomRouteTwoParamPreFilterRequest()
 {
     $request = new ERestTestRequestHelper();
     $request['config'] = ['url' => 'http://api/user/testing/a/b', 'type' => 'GET', 'data' => null, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request->addEvent('req.get.testing.render', function ($param1, $param2) {
         echo CJSON::encode([$param1 => $param2]);
     });
     $request->addEvent('pre.filter.req.get.testing.render', function ($param1, $param2) {
         return ["pre_1_{$param1}", "pre_2_{$param2}"];
     });
     $request_response = $request->send();
     $expected_response = '{"pre_1_a":"pre_2_b"}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }
 /**
  * testDELETESubresourceHiddenProperties
  *
  * tests that a DELETE request for a 'User' resource
  * With exluded visible fields only
  * returns the correct response
  */
 public function testDELETESubresourceHiddenProperties()
 {
     $request = new ERestTestRequestHelper();
     $request->addEvent('model.hidden.properties', function () {
         return ['id'];
     });
     $request->addEvent('model.with.relations', function () {
         return [];
     });
     $request['config'] = ['url' => 'http://api/category/1/posts/1', 'type' => 'DELETE', 'data' => null, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request_response = $request->send();
     $expected_response = '{"success":true,"message":"Sub-Resource Deleted","data":{"totalCount":1,"category":{"name":"cat1","posts":[]}}}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }
 /**
  * testDELETECustomRouteTowParamPreFilterRequest
  *
  * tests that a DELETE request with custom route and two params and pre-filter
  * returns the correct response
  */
 public function testDELETECustomRouteTwoParamPreFilterRequest()
 {
     $request = new ERestTestRequestHelper();
     $request['config'] = ['url' => 'http://api/user/testing/1/2', 'type' => 'DELETE', 'data' => '{"test":"data"}', 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request->addEvent('req.delete.testing.render', function ($param1, $param2) {
         echo '{"test":"data", "param1":"' . $param1 . '", "param2":"' . $param2 . '"}';
     });
     $request->addEvent('pre.filter.req.delete.testing.render', function ($param1, $param2) {
         return [$param1 + 1, $param2 + 1];
     });
     $request_response = $request->send();
     $expected_response = '{"test":"data", "param1":"2", "param2":"3"}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }
 /**
  * testDELETEResourceHiddenProperties
  *
  * tests that a DELETE request for a 'User' resource
  * With exluded visible fields only
  * returns the correct response
  */
 public function testDELETEResourceHiddenProperties()
 {
     $request = new ERestTestRequestHelper();
     $request->addEvent('model.hidden.properties', function () {
         return ['password', 'id'];
     });
     $request->addEvent('model.with.relations', function () {
         return [];
     });
     $request['config'] = ['url' => 'http://api/user/1', 'type' => 'DELETE', 'data' => null, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request_response = $request->send();
     $expected_response = '{"success":true,"message":"Record Deleted","data":{"totalCount":1,"user":{"username":"******","email":"*****@*****.**"}}}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }
 /**
  * testPOSTResourceHiddenProperties
  *
  * tests that a POST request for a 'Profile' resource
  * With exluded visible fields only
  * returns the correct response
  */
 public function testPOSTResourceHiddenProperties()
 {
     $request = new ERestTestRequestHelper();
     $request->addEvent('model.hidden.properties', function () {
         return ['website', 'photo', 'id'];
     });
     $request->addEvent('model.with.relations', function () {
         return [];
     });
     $data = '{"user_id":"4","photo":"0","website":"mysite4.com"}';
     $request['config'] = ['url' => 'http://api/profile', 'type' => 'POST', 'data' => $data, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request_response = $request->send();
     $expected_response = '{"success":true,"message":"Record Created","data":{"totalCount":1,"profile":{"user_id":"4"}}}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }
 /**
  * testGETSubresourcesCategoryUserRequestForHasManyWithEventOverrideRequest
  *
  * tests that a get request for a non sub-resources (HAS_MANY) with event override
  * returns the correct response
  */
 public function testGETSubresourcesCategoryUserRequestForHasManyWithEventOverrideRequest()
 {
     $request = new ERestTestRequestHelper();
     $request['config'] = ['url' => 'http://api/user/1/posts', 'type' => 'GET', 'data' => null, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request->addEvent('req.is.subresource', function ($model, $subresource_name, $http_verb) {
         return true;
     });
     $request_response = $request->send();
     $expected_response = '{"success":true,"message":"Record(s) Found","data":{"totalCount":1,"post":[{"id":"1","title":"title1","content":"content1","create_time":"2013-08-07 10:09:41","author_id":"1"}]}}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }
Esempio n. 7
0
 /**
  * testGETResourceRequestWithRelations
  *
  * tests that a get request for a single resource
  * returns the correct response
  */
 public function testGETResourceRequestWithRelations()
 {
     $request = new ERestTestRequestHelper();
     $request['config'] = ['url' => 'http://api/post/6', 'type' => 'GET', 'data' => null, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request->addEvent(ERestEvent::POST_FILTER_MODEL_WITH_RELATIONS, function () {
         return ['author', 'categories'];
     });
     $request_response = $request->send();
     $expected_response = '{"success":true,"message":"Record Found","data":{"totalCount":1,"post":{"id":"6","title":"title6","content":"content6","create_time":"2013-08-07 10:09:46","author_id":"6","author":{"id":"6","username":"******","password":"******","email":"*****@*****.**"},"categories":[{"id":"6","name":"cat6"}]}}}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }
Esempio n. 8
0
 /**
  * testPUTResourceUserRequest
  *
  * tests that a PUT request
  * correctly updates a resource
  */
 public function testPUTResourceUserRequest()
 {
     $request = new ERestTestRequestHelper();
     $data = '{"id":"2","username":"******","password":"******","email":"*****@*****.**"}';
     $request['config'] = ['url' => 'http://api/user/2', 'type' => 'PUT', 'data' => $data, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request->addEvent(ERestEvent::MODEL_WITH_RELATIONS, function () {
         return [];
     });
     $request_response = $request->send();
     $expected_response = '{"success":"true","message":"Record Updated","data":{"totalCount":"1","user":'******'}}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }
 /**
  * testPOSTResourceProfileRequest
  *
  * tests that a POST request
  * correctly creates a resource
  */
 public function testPOSTResourceProfileRequest()
 {
     $request = new ERestTestRequestHelper();
     $data = '{"user_id":"4","photo":"0","website":"mysite4.com"}';
     $request['config'] = ['url' => 'http://api/profile', 'type' => 'POST', 'data' => $data, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request->addEvent(ERestEvent::MODEL_WITH_RELATIONS, function () {
         return [];
     });
     $request_response = $request->send();
     $expected_response = '{"success":"true","message":"Record Created","data":{"totalCount":"1","profile":{"id":"7","user_id":"4","photo":"0","website":"mysite4.com"}}}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }
 /**
  * testPOSTCustomRouteTowParamPreFilterRequest
  *
  * tests that a POST request with custom route and two params and pre-filter
  * returns the correct response
  */
 public function testPOSTCustomRouteTwoParamPreFilterRequest()
 {
     $request = new ERestTestRequestHelper();
     $request['config'] = ['url' => 'http://api/user/testing/1/2', 'type' => 'POST', 'data' => '{"test":"data"}', 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request->addEvent('req.post.testing.render', function ($data, $param1, $param2) {
         $data['param1'] = $param1;
         $data['param2'] = $param2;
         echo CJSON::encode($data);
     });
     $request->addEvent('pre.filter.req.post.testing.render', function ($data, $param1, $param2) {
         return [$data, $param1 + 1, $param2 + 1];
     });
     $request_response = $request->send();
     $expected_response = '{"test":"data", "param1":"2", "param2":"3"}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }
 /**
  * tests that specific URI's may be accepted or denied 
  */
 public function testRequestAuthUri()
 {
     $request = new ERestTestRequestHelper();
     $request->addEvent(ERestEvent::REQ_AUTH_URI, function ($uri, $verb) {
         if ($uri == '/api/post' && $verb == 'GET') {
             return false;
         }
         return true;
     });
     $request['config'] = ['url' => 'http://api/post', 'type' => 'GET', 'data' => NULL, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request_response = $request->send();
     $this->assertInstanceOf('Exception', $request_response);
     $this->assertExceptionHasMessage('Unauthorized', $request_response);
     $request['config'] = ['url' => 'http://api/post/1', 'type' => 'GET', 'data' => NULL, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request_response = $request->send();
     $this->assertJSONFormat($request_response);
 }
 /**
  * testDELETESubresourceCategoryPostsPostFilterRequest
  *
  * tests that a DELETE request
  * correctly deletes a Sub-Resource
  * with new property added durring post filter
  */
 public function testDELETESubresourceCategoryPostsPostFilterRequest()
 {
     $request = new ERestTestRequestHelper();
     $request['config'] = ['url' => 'http://api/category/1/posts/1', 'type' => 'DELETE', 'data' => null, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request->addEvent('post.filter.req.delete.subresource.render', function ($json) {
         $j = CJSON::decode($json);
         $j['new_key'] = 'NEW VALUE';
         return $j;
     });
     $request_response = $request->send();
     $expected_response = '{"success":true,"message":"Sub-Resource Deleted","new_key":"NEW VALUE","data":{"totalCount":1,"category":{"id":"1","name":"cat1","posts":[]}}}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }
 /**
  * testGETResourcesVisibleSpecificRelatedProperties
  *
  * tests that a GET request for a list of 'User' resources
  * With exluded visible fields only
  * returns the correct response
  */
 public function testGETResourcesVisibleSpecificRelatedProperties()
 {
     $request = new ERestTestRequestHelper();
     $request->addEvent('model.visible.properties', function () {
         return ['profile.photo', 'profile.website'];
     });
     $request->addEvent('model.with.relations', function () {
         return ['profile'];
     });
     $request['config'] = ['url' => 'http://api/user', 'type' => 'GET', 'data' => null, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request_response = $request->send();
     $expected_response = '{"success":true,"message":"Record(s) Found","data":{"totalCount":"6","user":[{"id":"1","username":"******","password":"******","email":"*****@*****.**","profile":{"photo":"1","website":"mysite1.com"}},{"id":"2","username":"******","password":"******","email":"*****@*****.**","profile":{"photo":"0","website":"mysite2.com"}},{"id":"3","username":"******","password":"******","email":"*****@*****.**","profile":{"photo":"1","website":"mysite3.com"}},{"id":"4","username":"******","password":"******","email":"*****@*****.**","profile":{"photo":"0","website":"mysite4.com"}},{"id":"5","username":"******","password":"******","email":"*****@*****.**","profile":{"photo":"1","website":"mysite5.com"}},{"id":"6","username":"******","password":"******","email":"*****@*****.**","profile":{"photo":"0","website":"mysite6.com"}}]}}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }
 /**
  * Test that cors auth round trip works
  */
 public function testRequestCORSAuth()
 {
     $request = new ERestTestRequestHelper();
     $request->addEvent(ERestEvent::REQ_CORS_ACCESS_CONTROL_ALLOW_ORIGIN, function () {
         return ['http://rest.test'];
     });
     $request['config'] = ['url' => 'http://api/post/1', 'type' => 'OPTIONS', 'data' => NULL, 'headers' => ['ORIGIN' => 'http://rest.test', 'X_REST_CORS' => 'ALLOW']];
     $request_response = $request->send();
     $expected_response = '{"Access-Control-Allow-Origin:":"http:\\/\\/rest.test","Access-Control-Max-Age":3628800,"Access-Control-Allow-Methods":"GET, POST","Access-Control-Allow-Headers: ":"X_REST_CORS"}';
     $this->assertJsonStringEqualsJsonString($expected_response, $request_response);
     $request['config'] = ['url' => 'http://api/post/1', 'type' => 'GET', 'data' => NULL, 'headers' => ['ORIGIN' => 'http://rest.test', 'X_REST_CORS' => 'ALLOW']];
     $request_response = $request->send();
     $expected_response = '{"success":true,"message":"Record Found","data":{"totalCount":1,"post":{"id":"1","title":"title1","content":"content1","create_time":"2013-08-07 10:09:41","author_id":"1","categories":[{"id":"1","name":"cat1"},{"id":"2","name":"cat2"}],"author":{"id":"1","username":"******","password":"******","email":"*****@*****.**"}}}}';
     $this->assertJsonStringEqualsJsonString($expected_response, $request_response);
 }
    /**
     * testPUTResourceWithHasManyRequestRemoveAll
     *
     * tests that a PUT request with new Has Many And Empty Array
     * correctly updates a resource
     */
    public function testPUTResourceWithHasManyRequestRemoveAll()
    {
        $request = new ERestTestRequestHelper();
        $data = '{
				"email": "*****@*****.**",
				"id": "1",
				"password": "******",
				"posts": [],
				"username": "******"
		}';
        $request['config'] = ['url' => 'http://api/user/1', 'type' => 'PUT', 'data' => $data, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
        $request->addEvent(ERestEvent::MODEL_WITH_RELATIONS, function () {
            return ['posts'];
        });
        $request_response = $request->send();
        $expected_response = '{"success":true,"message":"Record Updated","data":{"totalCount":1,"user":{"id":"1","username":"******","password":"******","email":"*****@*****.**","posts":[]}}}';
        $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
    }
    /**
     * testPUTResourceWithManyManyRequest
     *
     * tests that a PUT request with belongsTo
     * correctly updates a resource
     */
    public function testPUTResourceWithManyManyRequest()
    {
        $request = new ERestTestRequestHelper();
        $data = '{
			"id": "1",
			"name": "cat1",
			"posts": [
				{
					"author_id": "1",
					"content": "content1_UPDATED",
					"create_time": "2013-08-07 10:09:41",
					"id": "1",
					"title": "title1"
				}, {
					"author_id": "1",
					"content": "content7",
					"create_time": "2013-08-07 10:09:47",
					"title": "title1"
				}
			]
		}';
        $request['config'] = ['url' => 'http://api/category/1', 'type' => 'PUT', 'data' => $data, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
        $request->addEvent(ERestEvent::MODEL_WITH_RELATIONS, function () {
            return ['posts'];
        });
        $expected_data = CJSON::decode($data);
        $expected_data['posts'][1]['id'] = 7;
        $request_response = $request->send();
        $expected_response = '{"success":"true","message":"Record Updated","data":{"totalCount":"1","category":' . CJSON::encode($expected_data) . '}}';
        $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
    }
 /**
  * testGETResourcesHiddenRelatedProperties
  *
  * tests that a GET request for a list of 'User' resources
  * With exluded visible fields only
  * returns the correct response
  */
 public function testGETResourceHiddenRelatedProperties()
 {
     $request = new ERestTestRequestHelper();
     $request->addEvent('model.hidden.properties', function () {
         return ['password', 'id', '*.title', 'posts.id', '*.website'];
     });
     $request->addEvent('model.with.relations', function () {
         return ['posts', 'profile'];
     });
     $request['config'] = ['url' => 'http://api/user/1', 'type' => 'GET', 'data' => null, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request_response = $request->send();
     $expected_response = '{"success":true,"message":"Record Found","data":{"totalCount":1,"user":{"username":"******","email":"*****@*****.**","posts":[{"content":"content1","create_time":"2013-08-07 10:09:41","author_id":"1"}],"profile":{"id":"1","user_id":"1","photo":"1"}}}}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }
 /**
  * testDELETESubresourceCategoryPostsOverrideAttributes
  *
  * tests that a DELETE request
  * correctly deletes a Sub-Resource
  * with new property model added durring model.post.override.attributes
  */
 public function testDELETESubresourceCategoryPostsOverrideAttributes()
 {
     $request = new ERestTestRequestHelper();
     $request['config'] = ['url' => 'http://api/category/1/posts/2', 'type' => 'PUT', 'data' => null, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request->send();
     $request['config'] = ['url' => 'http://api/category/1/posts/1', 'type' => 'DELETE', 'data' => null, 'headers' => ['X_REST_USERNAME' => 'admin@restuser', 'X_REST_PASSWORD' => 'admin@Access']];
     $request->addEvent('model.post.override.attributes', function ($model) {
         return array_merge($model->attributes, ['next_id' => $model->id + 1]);
     });
     $request_response = $request->send();
     $expected_response = '{"success":true,"message":"Sub-Resource Deleted","data":{"totalCount":1,"category":{"id":"1","name":"cat1","posts":[{"id":"2","title":"title2","content":"content2","create_time":"2013-08-07 10:09:42","author_id":"2","next_id":3}]}}}';
     $this->assertJsonStringEqualsJsonString($request_response, $expected_response);
 }