{"id":27451,"date":"2022-06-08T11:01:09","date_gmt":"2022-06-08T11:01:09","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/blog\/?p=27451"},"modified":"2024-12-27T08:28:31","modified_gmt":"2024-12-27T08:28:31","slug":"feature-testing-in-laravel","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/blog\/feature-testing-in-laravel","title":{"rendered":"Implement Feature Testing in Laravel for REST APIs"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Is working with test cases intimidating for you? Are you looking for a simple tutorial to get started with <b>feature testing in Laravel<\/b>? Then, we insist you stick to this step-by-step guide to clear your doubts and learn testing in the Laravel application.<\/p>\n<h2>Feature Testing in Laravel: What, Why, and How?<\/h2>\n<h3>What is Feature Testing in Laravel?<\/h3>\n<p>When developing an application, you tend to worry about code breakage while executing a feature or modules. To avoid scenarios, it is significant to implement testing in your application.<\/p>\n<p>Feature testing is one of the most used and important types of testing. It allows you to test a major portion of your application\u2019s code that has objects interacting with each other, HTTP requests, JSON, etc.<\/p>\n<h3>Why Feature Testing in Laravel?<\/h3>\n<ul class=\"bullets text-left\">\n<li>To make smooth working functionality<\/li>\n<li>Avoid breaking your entire application<\/li>\n<li>Code maintainability<\/li>\n<li>Application stability<\/li>\n<li>Easy debugging when an application crashes<\/li>\n<li>Easy fixating of the reason behind app break-down<\/li>\n<\/ul>\n<p>The best part is testing is it is automated. It finds the gap in your code and allows you to develop features right.<\/p>\n<h3>How Does Feature Testing in Laravel Work?<\/h3>\n<p>Laravel supports PHPUnit tests. Your web app comes with a phpunit.xml file with all the settings you need to test your Laravel application. Your phpunit.xml file sets your laravel environment for testing. So there is no need to create a new XML file!<\/p>\n<p>Below is a sample phpunit.xml file that comes with the Laravel 8 framework.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/phpunit.xml-file-min.png\" alt=\"phpunit.xml file\" width=\"933\" height=\"881\" class=\"aligncenter size-full wp-image-27467\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/phpunit.xml-file-min.png 933w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/phpunit.xml-file-min-300x283.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/phpunit.xml-file-min-768x725.png 768w\" sizes=\"auto, (max-width: 933px) 100vw, 933px\" \/><\/p>\n<h2>Initial Set-Up<\/h2>\n<p>For this tutorial, we will be working with a hotel review api. The end-user can give reviews for a hotel so that hotel reviews can be added, updated, deleted, and hotel review list operation also.<\/p>\n<p>Create the laravel application by the below command<\/p>\n<pre>composer create-project --prefer-dist laravel\/laravel hotel_review_api_test\r\ncd hotel_review_api_test\r\n<\/pre>\n<p>Open the .env file and update the database details.<\/p>\n<pre>DB_CONNECTION=mysql\r\nDB_HOST=127.0.0.1\r\nDB_PORT=3306\r\nDB_DATABASE=hotel_test_api\r\nDB_USERNAME=root\r\nDB_PASSWORD=<\/pre>\n<h2>Create Model and Migration<\/h2>\n<p>For model, run the below commands.<\/p>\n<pre>php artisan make:model Hotel\r\nphp artisan make:model Review<\/pre>\n<p>For migration, run the below commands.<\/p>\n<pre>php artisan make:migration create_hotel_table\t\r\nphp artisan make:migration create_review_table<\/pre>\n<p class=\"boxed bg--secondary\" style=\"border: 1px solid #c7c7c7; box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);\"><strong><i><span style=\"font-size:22px; color:#000;\">Develop. Maintain. Optimize. Deploy \u2013 with Bacancy!<\/span><br \/>\nAre you looking for proficient developers to build highly-optimized applications? Get in touch with us to <a href=\"https:\/\/www.bacancytechnology.com\/hire-laravel-developer\" target=\"_blank\" rel=\"noopener\">hire Laravel developer<\/a>. Contact the best, to get the best! Period!<\/i><\/strong><\/p>\n<h2>Create Database Table<\/h2>\n<p>Now update the migration files by below code mentioned below.<\/p>\n<p>We will update the user table, hotel table, and review table files. Add the columns as directed. You can edit the columns as per your requirement.<\/p>\n<h3>User Table<\/h3>\n<pre>public function up()\r\n{\r\n  Schema::create('users', function (Blueprint $table) {\r\n      $table->bigIncrements('id')->unsinged();\r\n      $table->string('name');\r\n      $table->string('email')->unique();\r\n      $table->timestamps();\r\n   });\r\n }\r\n<\/pre>\n<h3>Hotel Table<\/h3>\n<pre>public function up()\r\n{\r\n  Schema::create('hotels', function (Blueprint $table) {\r\n     $table->bigIncrements('id')->unsinged();\r\n     $table->string('name', 100);\r\n     $table->text('address')->nullable();\r\n     $table->float('star')->nullable();\r\n    $table->tinyInteger('active')->default(1)->comment = '1 = active,0 = Inactive';\r\n     $table->timestamps();\r\n     $table->softDeletes();\r\n  });\r\n}\r\n<\/pre>\n<h3>Review Table<\/h3>\n<pre>public function up()\r\n{\r\n   Schema::create('reviews', function (Blueprint $table) {\r\n      $table->bigIncrements('id')->unsinged();\r\n      $table->string('title', 100);\r\n      $table->text('description', 100);\r\n      $table->unsignedBigInteger('user_id')->nullable();\r\n      $table->unsignedBigInteger('hotel_id')->nullable();\r\n      $table->timestamps(); \r\n$table->softDeletes();                       $table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE')->onUpdate('CASCADE');            $table->foreign('hotel_id')->references('id')->on('hotels')->onDelete('CASCADE')->onUpdate('CASCADE');\r\n   });\r\n }\r\n<\/pre>\n<h3>Run Migration<\/h3>\n<pre>php artisan migrate<\/pre>\n<h2>Generate API Route and CRUD Business Logic<\/h2>\n<p>Open \\routes\\api.php and use the below code snippet to generate routes for our APIs. We would have five APIs.<\/p>\n<p><strong>routes\\api.php<\/strong><\/p>\n<pre>\/\/ Fetch all active hotels data\r\nRoute::get('hotels', [HotelController::class,'getAllHotelData']);\r\n \r\n\/\/ Fetch particular hotel data\r\nRoute::get('hotel\/{hotel_id}', [HotelController::class,'getHotelDataById']);\r\n \r\n\/\/ Add a new hotel review\r\nRoute::post('save-hotel-review', [HotelController::class,'storeHotelReviewData']);\r\n \r\n\/\/ Update hotel review\r\nRoute::put('update-hotel-review\/{review_id}', [HotelController::class,'updateHotelReviewData']);\r\n \r\n\/\/ Delete hotel review\r\nRoute::delete('review\/{review_id}', [HotelController::class,'deleteHotelReview']);<\/pre>\n<h3>CRUD Business Logic<\/h3>\n<p>For the business logic of CRUD operations, create a new HotelController.php  file. We won\u2019t be specifying the code snippet for the APIs. But here is the Github link to  <a href=\"https:\/\/github.com\/Nirav-webs-99\/hotel_review_api_test\/blob\/master\/app\/Http\/Controllers\/Api\/HotelController.php\" target=\"_blank\" rel=\"noopener\">HotelController.php<\/a> if you wish to have a look. We would be following these five APIs.<\/p>\n<ul class=\"bullets text-left\">\n<li>\/api\/hotels: Returns all hotel data<\/li>\n<li>\/api\/hotel\/{hotel_id}: Returns the data of a particular hotel<\/li>\n<li>\/api\/save-hotel-review: Add a hotel review<\/li>\n<li>\/api\/update-hotel-review\/{review_id}: Update a hotel review<\/li>\n<li>\/api\/review\/{review_id}: Delete a hotel review<\/li>\n<\/ul>\n<h2>Create Feature Test in Laravel Application<\/h2>\n<p>This section will create our first feature test for our app. Run the below command to generate a file for testing named HotelTest.<\/p>\n<pre>php artisan make:test HotelTest<\/pre>\n<p>The command will create a new file named HotelTest.php  in the tests > Feature folder with the below code.<\/p>\n<pre>( ? p h  p\r\n\r\nnamespace Tests \\ Feature;\r\n\r\nuse Tests\\TestCase;\r\nuse Illuminate\\Foundation\\Testing\\WithFaker;\r\nuse Illuminate\\Foundation\\Testing\\RefreshDatabase;\r\n\r\nclass HotelTest extends TestCase\r\n{\r\n   \/**\r\n     * A basic test example.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function testExample()\r\n    {\r\n        $this->assertTrue(true);\r\n    }\r\n}\r\n<\/pre>\n<h3>Update HotelTest.php<\/h3>\n<p>We will be writing tests for each unit of our application for application. We have written tests to ensure that: <strong>a hotel review can be added, deleted, updated, and listed that are valid<\/strong>. Update the <em>HotelTest.php<\/em> with the following code.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/Update-HotelTest.php-min.jpg\" alt=\"Update HotelTest.php\" width=\"1100\" height=\"353\" class=\"aligncenter size-full wp-image-27547\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/Update-HotelTest.php-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/Update-HotelTest.php-min-300x96.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/Update-HotelTest.php-min-1024x329.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/Update-HotelTest.php-min-768x246.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p><u><strong>To get active hotel data of specific hotel<\/strong><\/u><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-active-hotel-data-of-specific-hotel-min.jpg\" alt=\"To get active hotel data of specific hotel\" width=\"1100\" height=\"937\" class=\"aligncenter size-full wp-image-27548\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-active-hotel-data-of-specific-hotel-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-active-hotel-data-of-specific-hotel-min-300x256.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-active-hotel-data-of-specific-hotel-min-1024x872.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-active-hotel-data-of-specific-hotel-min-768x654.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p><u><strong>To get all active hotel data <\/strong><\/u><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-all-active-hotel-data-min.jpg\" alt=\"To get all active hotel data\" width=\"1100\" height=\"1037\" class=\"aligncenter size-full wp-image-27549\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-all-active-hotel-data-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-all-active-hotel-data-min-300x283.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-all-active-hotel-data-min-1024x965.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-all-active-hotel-data-min-768x724.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p><u><strong>To get all inactive hotel data of specific hotel<\/strong><\/u><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-all-inactive-hotel-data-of-specific-hotel-min.jpg\" alt=\"To get all inactive hotel data of specific hotel\" width=\"1100\" height=\"533\" class=\"aligncenter size-full wp-image-27550\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-all-inactive-hotel-data-of-specific-hotel-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-all-inactive-hotel-data-of-specific-hotel-min-300x145.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-all-inactive-hotel-data-of-specific-hotel-min-1024x496.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-get-all-inactive-hotel-data-of-specific-hotel-min-768x372.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p><u><strong>To add a new review<\/strong><\/u><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-add-a-new-review-min.jpg\" alt=\"To add a new review\" width=\"1100\" height=\"1011\" class=\"aligncenter size-full wp-image-27551\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-add-a-new-review-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-add-a-new-review-min-300x276.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-add-a-new-review-min-1024x941.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-add-a-new-review-min-768x706.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p><u><strong>To update specific review<\/strong><\/u><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-specific-review-min.jpg\" alt=\"To update specific review\" width=\"1100\" height=\"1297\" class=\"aligncenter size-full wp-image-27552\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-specific-review-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-specific-review-min-254x300.jpg 254w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-specific-review-min-868x1024.jpg 868w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-specific-review-min-768x906.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p><u><strong>To delete hotel review data<\/strong><\/u><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-delete-hotel-review-data-min.jpg\" alt=\"To delete hotel review data\" width=\"1100\" height=\"1072\" class=\"aligncenter size-full wp-image-27553\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-delete-hotel-review-data-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-delete-hotel-review-data-min-300x292.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-delete-hotel-review-data-min-1024x998.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-delete-hotel-review-data-min-768x748.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p><u><strong>To store new review required data<\/strong><\/u><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-store-new-review-required-data-min.jpg\" alt=\"To store new review required data\" width=\"1100\" height=\"514\" class=\"aligncenter size-full wp-image-27554\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-store-new-review-required-data-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-store-new-review-required-data-min-300x140.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-store-new-review-required-data-min-1024x478.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-store-new-review-required-data-min-768x359.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p><u><strong>To update review required data<\/strong><\/u><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-review-required-data-min.jpg\" alt=\"To update review required data\" width=\"1100\" height=\"1181\" class=\"aligncenter size-full wp-image-27555\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-review-required-data-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-review-required-data-min-279x300.jpg 279w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-review-required-data-min-954x1024.jpg 954w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-review-required-data-min-768x825.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p><u><strong>To update reviews that do not exist<\/strong><\/u><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-reviews-that-do-not-exist-min.jpg\" alt=\"To update reviews that do not exist\" width=\"1100\" height=\"645\" class=\"aligncenter size-full wp-image-27557\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-reviews-that-do-not-exist-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-reviews-that-do-not-exist-min-300x176.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-reviews-that-do-not-exist-min-1024x600.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-update-reviews-that-do-not-exist-min-768x450.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p><u><strong>To delete review that does not exist<\/strong><\/u><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-delete-review-that-does-not-exist-min.jpg\" alt=\"To delete review that does not exist\" width=\"1100\" height=\"543\" class=\"aligncenter size-full wp-image-27558\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-delete-review-that-does-not-exist-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-delete-review-that-does-not-exist-min-300x148.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-delete-review-that-does-not-exist-min-1024x505.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/06\/To-delete-review-that-does-not-exist-min-768x379.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p><strong>Explanation<\/strong><\/p>\n<ul class=\"bullets text-left\">\n<li>test_get_all_active_hotels() is the main function that would run test for the app. Now, what are we testing in this function? It tests all the data of active hotels, which will be returned by the endpoint \/api\/hotels.<\/li>\n<li>$this->get(&#8216;\/api\/hotels&#8217;) fetches hotel data<\/li>\n<li>->assertStatus(200) validates the HTTP status code returned from the API.<\/li>\n<li>->assertJsonStructure( \u2026  ); validates the JSON structure of the response returned from the endpoint. Here\u2019s the sample response of the API.<\/li>\n<\/ul>\n<pre>{\r\n    \"code\": \"200\",\r\n    \"message\": \"Hotel Data\",\r\n    \"data\": [\r\n        {\r\n            \"id\": 1,\r\n            \"name\": \"Hyaat\",\r\n            \"address\": \"17\/A, Ashram Rd, Usmanpura, Ahmedabad, Gujarat 380014\",\r\n            \"star\": 4,\r\n            \"create_at\": \"11\/Jan\/2022 15:40:56\",\r\n            \"update_at\": \"11\/Jan\/2022 15:40:56\",\r\n            \"active\": \"Active\",\r\n            \"review\": [\r\n                {\r\n                    \"id\": 2,\r\n                    \"title\": \"Good Hotel Hyaat\",\r\n                    \"description\": \"Hyaat is a very nice hotel.\",\r\n                    \"author\": \"Parth\",\r\n                    \"create_at\": \"11\/Jan\/2022 15:40:56\",\r\n                    \"update_at\": \"11\/Jan\/2022 15:40:56\"\r\n                }\r\n            ]\r\n        },\r\n   ]    \r\n}\r\n<\/pre>\n<ul class=\"bullets text-left\">\n<li><em>test_get_active_hotel_by_id<\/em> validates the hotel review data based on hotel ID.<\/li>\n<li><em>test_for_get_inactive_hotel_by_id<\/em> validates the hotel review data when passed the inactive hotel ID.<\/li>\n<li><em>test_for_add_hotel_review<\/em> validates the add hotel review functionality when all data are passed valid<\/li>\n<li>Function <em>test_for_update_hotel_review<\/em> validates the updated hotel review functionality when all data are passed valid<\/li>\n<li>Function <em>test_for_delete_hotel_review<\/em> validate the delete hotel review functionality when passed valid hotel review ID.<\/li>\n<li>Function <em>test_for_add_hotel_review_required_fields<\/em> validates the required field validation when no data passed when adding a new hotel review API called.<\/li>\n<li>Function <em>test_for_update_hotel_review_required_fields<\/em> validates the required field validation when no data passed when updating the new hotel review API called.<\/li>\n<li>Function <em>test_for_update_hotel_review_that_not_exist<\/em> validates the case when hotel review id passed that does not exist in database for update hotel review functionality.<\/li>\n<li>Function <em>test_for_delete_review_that_not_exist<\/em> validates the case when a hotel review id passed that does not exist in the database for delete hotel review functionality.<\/li>\n<li>Other assertJson like below<\/li>\n<pre>     ->assertJson([\r\n           'code' => '401',\r\n            'message' => 'Review not found, Please try again',\r\n       ]);\r\n<\/pre>\n<ul class=\"bullets text-left\">\n<li>assertEquals() and assertNull()<\/li>\n<\/ul>\n<p>assertEquals() is used to compare the actual value of the variable to the expected value. Unlike assertTrue(), assertFalse(), and assertNull(), assertEquals() takes two parameters. The first being the expected value, and the second being the actual value.<\/p>\n<ul class=\"bullets text-left\">\n<li>assertContains(), assertCount(), and assertEmpty()<\/li>\n<\/ul>\n<p>assertContains() asserts that an expected value exists within the provided array, assertCount() asserts the number of items in the array matches the specified amount, and assertEmpty() asserts that the provided array is empty.<\/p>\n<p>Visit <a href=\"https:\/\/laravel.com\/docs\/6.x\/http-tests#available-assertions\" target=\"_blank\" rel=\"noopener\">Laravel documentation<\/a> to learn about other functions.<\/p>\n<h2>Run the Test Cases<\/h2>\n<p>Use the below command at the root folder for running the test cases.<\/p>\n<pre>php artisan test<\/pre>\n<h2>Github Repository: Feature Testing in Laravel Example<\/h2>\n<p>You can visit the <a href=\"https:\/\/github.com\/Nirav-webs-99\/hotel_review_api_test.git\" target=\"_blank\" rel=\"noopener\">source code<\/a> and clone the repository to play around with the code.<\/p>\n<h2>Conclusion<\/h2>\n<p>I hope the tutorial for implementing Feature Testing in Laravel for REST APIs has served you as you wanted. We have several Laravel tutorials for enthusiasts like you. If you want to learn and explore more about Laravel, please visit the <a href=\"https:\/\/www.bacancytechnology.com\/tutorials\/laravel\" target=\"_blank\" rel=\"noopener\">Laravel tutorials page<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Is working with test cases intimidating for you? Are you looking for a simple tutorial to get started with feature testing in Laravel? Then, we insist you stick to this step-by-step guide to clear your doubts and learn testing in the Laravel application. Feature Testing in Laravel: What, Why, and How? What is Feature [&hellip;]<\/p>\n","protected":false},"author":150,"featured_media":27480,"comment_status":"open","ping_status":"open","sticky":false,"template":"blog-new-template.php","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[1142],"tags":[],"coauthors":[2325],"class_list":["post-27451","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel"],"acf":[],"modified_by":"Binal Prajapati","_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/27451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/users\/150"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=27451"}],"version-history":[{"count":0,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/27451\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media\/27480"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=27451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=27451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=27451"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/coauthors?post=27451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}