{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"API test helpers","type":"basic","slug":"api-test-helpers","excerpt":"","body":"This repository makes it easier for you to test REST APIs.\nWe've added a couple of integration testing helpers that you're to use.\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"test helper\",\n    \"h-1\": \"Usage\",\n    \"0-0\": \"seeApiSuccess()\",\n    \"0-1\": \"Asserts response returned using `success` response macro:\\n`return response()->success(..)`\",\n    \"1-0\": \"seeValidationError()\",\n    \"1-1\": \"Asserts response code is 422 and that the error is returned (you get that when Laravel throws a validation error or when you `return response()->error('message', 422);`\",\n    \"2-0\": \"seeApiError($status_code)\",\n    \"2-1\": \"Asserts for specific status code error.. for example 401.\",\n    \"3-0\": \"seeJsonKey($key)\",\n    \"3-1\": \"equivalent to `->see('\\\"'.$key.'\\\"'):`\",\n    \"4-0\": \"seeJsonValue($value)\",\n    \"4-1\": \"equivalent to `->see('\\\"'.$value.'\\\"')`\",\n    \"5-0\": \"seeJsonArray($entity)\",\n    \"5-1\": \"equivalent to `->see('\\\"'.$entity.'\\\":[')`\\nWhich is useful when you expect an array of **Orders** for example.\",\n    \"6-0\": \"seeJsonObject($entity)\",\n    \"6-1\": \"equivalent to `->see('\\\"'.$entity.'\\\":{')`\\nWhich is useful when you expect a single **Order** for example.\"\n  },\n  \"cols\": 2,\n  \"rows\": 7\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"Avoid using bcrypt() in your model factory. As it'll slow down your tests. Only use it if you want to know the password of the user model created (e.g.: in login, reset password tests).\"\n}\n[/block]\nHere's an example controller and integration test for it\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\n$api->group([], function ($api) {\\n\\n    $api->post('posts', 'PostsController:::at:::create');\\n\\n});\",\n      \"language\": \"php\",\n      \"name\": \"Routes.php\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nuse App\\\\Post;\\n\\nclass PostsController{\\n \\n   public function create(Request $request)\\n   {\\n       $this->validate($request, [\\n         'name'   => 'required',\\n       ]);\\n     \\n       $post = Post::create([\\n         'name' => $request->input('name'),\\n         ]);\\n     \\n       return response()->success(compact('post'));\\n   }\\n  \\n}\",\n      \"language\": \"php\",\n      \"name\": \"PostsController.php\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass CreatePostTest extends TestCase\\n{ \\n    public function testCreatingNewPostSuccessfully()\\n    {\\n        $post = factory(App\\\\Post::class)->make();//we're not saving it in the database\\n      \\n        $this->post('/api/posts', ['name' => $post->name])\\n        ->seeApiSuccess()\\n        ->seeJsonObject('post')\\n        ->seeJson(['name' => $post->name]);\\n        \\n        $this->seeInDatabase('posts', [\\n           'name' => $post->name,\\n        ]);\\n    }\\n}\\n\",\n      \"language\": \"php\",\n      \"name\": \"CreatePostTest.php\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"Don't forget the `/api/` at the beginning of the route in your test file.\"\n}\n[/block]","updates":["5708dfd0cc231e17009279d8","578c913cfa06e319003e069d"],"order":0,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"57ffd85f755a2e1700c2bb1c","parentDoc":null,"project":"567083021dabd80d00b122ae","user":"567082433a32d20d00c45cab","category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Testing","slug":"testing","order":6,"from_sync":false,"reference":false,"_id":"57ffd85f755a2e1700c2bb0c","project":"567083021dabd80d00b122ae","createdAt":"2015-12-18T23:14:31.389Z","version":"57ffd85f755a2e1700c2bb05","__v":0},"createdAt":"2015-12-18T23:26:11.648Z","githubsync":"","__v":0,"version":{"version":"3.4","version_clean":"3.4.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["57ffd85f755a2e1700c2bb06","57ffd85f755a2e1700c2bb07","57ffd85f755a2e1700c2bb08","57ffd85f755a2e1700c2bb09","57ffd85f755a2e1700c2bb0a","57ffd85f755a2e1700c2bb0b","57ffd85f755a2e1700c2bb0c","57ffd85f755a2e1700c2bb0d"],"_id":"57ffd85f755a2e1700c2bb05","project":"567083021dabd80d00b122ae","releaseDate":"2016-10-13T18:54:23.018Z","__v":1,"createdAt":"2016-10-13T18:54:23.018Z"}}
This repository makes it easier for you to test REST APIs. We've added a couple of integration testing helpers that you're to use. [block:parameters] { "data": { "h-0": "test helper", "h-1": "Usage", "0-0": "seeApiSuccess()", "0-1": "Asserts response returned using `success` response macro:\n`return response()->success(..)`", "1-0": "seeValidationError()", "1-1": "Asserts response code is 422 and that the error is returned (you get that when Laravel throws a validation error or when you `return response()->error('message', 422);`", "2-0": "seeApiError($status_code)", "2-1": "Asserts for specific status code error.. for example 401.", "3-0": "seeJsonKey($key)", "3-1": "equivalent to `->see('\"'.$key.'\"'):`", "4-0": "seeJsonValue($value)", "4-1": "equivalent to `->see('\"'.$value.'\"')`", "5-0": "seeJsonArray($entity)", "5-1": "equivalent to `->see('\"'.$entity.'\":[')`\nWhich is useful when you expect an array of **Orders** for example.", "6-0": "seeJsonObject($entity)", "6-1": "equivalent to `->see('\"'.$entity.'\":{')`\nWhich is useful when you expect a single **Order** for example." }, "cols": 2, "rows": 7 } [/block] [block:callout] { "type": "info", "body": "Avoid using bcrypt() in your model factory. As it'll slow down your tests. Only use it if you want to know the password of the user model created (e.g.: in login, reset password tests)." } [/block] Here's an example controller and integration test for it [block:code] { "codes": [ { "code": "<?php\n\n$api->group([], function ($api) {\n\n $api->post('posts', '[email protected]');\n\n});", "language": "php", "name": "Routes.php" } ] } [/block] [block:code] { "codes": [ { "code": "<?php\n\nuse App\\Post;\n\nclass PostsController{\n \n public function create(Request $request)\n {\n $this->validate($request, [\n 'name' => 'required',\n ]);\n \n $post = Post::create([\n 'name' => $request->input('name'),\n ]);\n \n return response()->success(compact('post'));\n }\n \n}", "language": "php", "name": "PostsController.php" } ] } [/block] [block:code] { "codes": [ { "code": "<?php\n\nclass CreatePostTest extends TestCase\n{ \n public function testCreatingNewPostSuccessfully()\n {\n $post = factory(App\\Post::class)->make();//we're not saving it in the database\n \n $this->post('/api/posts', ['name' => $post->name])\n ->seeApiSuccess()\n ->seeJsonObject('post')\n ->seeJson(['name' => $post->name]);\n \n $this->seeInDatabase('posts', [\n 'name' => $post->name,\n ]);\n }\n}\n", "language": "php", "name": "CreatePostTest.php" } ] } [/block] [block:callout] { "type": "info", "body": "Don't forget the `/api/` at the beginning of the route in your test file." } [/block]