How to implement integration testing of RESTful API in PHP
With the development of web applications and the popularity of RESTful API, integration testing of API has become more and more important. In PHP, we can use some tools and techniques to implement such integration testing. This article will introduce how to implement integration testing of RESTful API in PHP and provide some sample code to help you understand.
use PHPUnitFrameworkTestCase; class MyApiTest extends TestCase { private $httpClient; protected function setUp(): void { $this->httpClient = new GuzzleHttpClient([ 'base_uri' => 'http://example.com/api/', ]); } public function testGetUsers() { $response = $this->httpClient->get('users'); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($response->getBody(), true); $this->assertNotEmpty($data); } public function testUpdateUser() { $response = $this->httpClient->put('users/1', [ 'json' => [ 'name' => 'John Doe', 'email' => 'john.doe@example.com', ], ]); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($response->getBody(), true); $this->assertEquals('John Doe', $data['name']); $this->assertEquals('john.doe@example.com', $data['email']); } }
use PHPUnitFrameworkTestCase; use GuzzleHttpHandlerMockHandler; use GuzzleHttpHandlerStack; use GuzzleHttpClient; class MyApiTest extends TestCase { private $httpClient; protected function setUp(): void { $mockHandler = new MockHandler([ new GuzzleHttpPsr7Response(200, [], json_encode(['name' => 'John Doe'])), new GuzzleHttpPsr7Response(404), new GuzzleHttpExceptionConnectException('Connection error', new GuzzleHttpPsr7Request('GET', 'users')), ]); $handlerStack = HandlerStack::create($mockHandler); $this->httpClient = new Client(['handler' => $handlerStack]); } public function testGetUser() { $response = $this->httpClient->get('users/1'); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($response->getBody(), true); $this->assertEquals('John Doe', $data['name']); } public function testGetNonExistentUser() { $response = $this->httpClient->get('users/999'); $this->assertEquals(404, $response->getStatusCode()); } public function testConnectionError() { $this->expectException(GuzzleHttpExceptionConnectException::class); $this->httpClient->get('users'); } }
By using Mock HTTP client, we can modify and control the response of the API at any time to meet our testing needs.
Summary:
Integration testing of a RESTful API in PHP can be accomplished by using PHPUnit or Mock HTTP client. No matter which method you choose, you can effectively simulate HTTP requests and verify the validity of responses. These integration tests will help us ensure that our API works properly in a variety of scenarios and provide a reliable way to verify the functionality and performance of the API.
The above is the detailed content of How to implement integration testing of RESTful API in PHP. For more information, please follow other related articles on the PHP Chinese website!