After using the Laravel framework to write the interface, when accepting the project, we need some testing skills to test the correctness of the interface code. Below, we will introduce how to test Laravel interfaces.
In Laravel, we can use PHPUnit for interface testing. PHPUnit is a powerful PHP unit testing tool that supports automated testing, API testing and even end-to-end testing. In Laravel 5.5 and above, PHPUnit has become a built-in testing tool of the framework, so no additional installation is required.
In Laravel, we can use thephp artisan make:test
command to generate test case files. This command will generate a test class in thetests/Feature
directory and inherit this class from thePHPUnit\Framework\TestCase
class. Each method of the test class will be automatically run as a test case. For example:
get('/'); $response->assertStatus(200); } }
In this sample code, we created a method oftestBasicTest
and used the$this->get()
method to send HTTP GET request, and then use the$response->assertStatus()
method to determine whether the response status code is 200.
In Laravel, we can use$this->get()
,$this->post()
,$this->put()
,$this->patch()
and$this->delete()
to send HTTP ask. These methods will automatically execute route and controller methods and return HTTP response objects. For example, to send a GET request to the/users
route, you can write this in the test class:
public function testGetUsers() { $response = $this->get('/users'); $response->assertStatus(200); }
In Laravel, we can use$response->assertJson()
,$response->assertStatus()
,$response->assertSeeText()
and$response-> ;assertDontSeeText()
and other methods to assert the response content.
If we want to determine whether the response content contains a JSON string, we can use the$response->assertJson()
method. If we want to determine whether the response status code is 200, we can use$response->assertStatus(200)
Method. If we want to determine whether the response content contains a certain text, we can use the$response->assertSeeText('Hello World')
method. If we want to determine whether the response content does not contain a certain text, we can use the$response->assertDontSeeText('Hello World')
method.
For example, the user list returned by the test/users
route should be an array, we can write like this:
public function testGetUsers() { $response = $this->get('/users'); $response->assertJson([ 'users' => [], ]); }
In In Laravel, we can use Laravel's ownAuth::
Facade to simulate authentication and authorization. For example, we can use theAuth::login()
method to simulate logging in a user, or we can use theAuth::shouldReceive()
method to simulate verifying whether a user has a certain permission. wait.
For example, if we want to test whether authentication is required to access the/dashboard
route, we can write like this:
public function testDashboardRequiresAuthentication() { $response = $this->get('/dashboard'); $response->assertRedirect('/login'); }
In Laravel, we can Use PHPUnit for interface testing, use$this->get()
,$this->post()
,$this->put()
,$this->patch()
and$this->delete()
to send HTTP requests, use$response->assertJson( )
,$response->assertStatus()
,$response->assertSeeText()
and$response->assertDontSeeText()
etc. Method to make assertions about the response content. You can also use Laravel's ownAuth::
Facade to simulate authentication and authorization.
The above is the detailed content of How to test Laravel interfaces. For more information, please follow other related articles on the PHP Chinese website!