-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApiTestingExample.php
More file actions
145 lines (130 loc) · 4.8 KB
/
ApiTestingExample.php
File metadata and controls
145 lines (130 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Made with love.
*/
declare(strict_types = 1);
namespace FallegaHQ\JsonTestUtils\Examples;
use FallegaHQ\JsonTestUtils\JsonAssertions;
use PHPUnit\Framework\TestCase;
class ApiTestingExample extends TestCase {
use JsonAssertions;
/**
* Example of testing a REST API response
*
* This is a mocked example but demonstrates how you would test
* real API responses in your application.
*
* @throws \JsonException
*/
public function testApiEndpoint(): void {
// In a real test, you might use Guzzle, Symfony HttpClient, or your framework's
// testing tools to make an actual HTTP request.
// Here we're simulating a response:
$response = $this->getMockedApiResponse();
// First, validate the structure
$this->assertValidJson($response)
->hasKey('success')
->equals('success', true)
->hasKey('data')
->isType('data', 'array')
->hasKey('data.users')
->isType('data.users', 'array')
->assert('API response structure is invalid');
// Then validate pagination details
$this->assertValidJson($response)
->hasKey('data.pagination')
->isType('data.pagination.current_page', 'integer')
->isType('data.pagination.total_pages', 'integer')
->isType('data.pagination.total_items', 'integer')
->assert('Pagination information is missing or invalid');
// Finally, validate individual users in the response
$this->assertValidJson($response)
->passes('data.users', function ($users) {
if (empty($users)) {
return 'Users array cannot be empty';
}
foreach ($users as $user) {
if (! isset($user['id']) || ! is_numeric($user['id'])) {
return 'Each user must have a numeric ID';
}
if (! isset($user['email']) || false === filter_var($user['email'], FILTER_VALIDATE_EMAIL)) {
return 'Each user must have a valid email address';
}
}
return true;
})
->assert('User data validation failed');
}
/**
* Example of testing a JSON API error response
*
* @throws \JsonException
*/
public function testApiErrorResponse(): void {
// Simulate an error response from API
$errorResponse = $this->getMockedErrorResponse();
$this->assertValidJson($errorResponse)
->hasKey('success')
->equals('success', false)
->hasKey('error')
->isType('error.code', 'integer')
->isType('error.message', 'string')
->notEmpty('error.message')
->assert('Error response format is invalid');
}
/**
* This method mocks a successful API response for testing purposes
* In a real test, you would make an actual API request
*
* @throws \JsonException
*/
private function getMockedApiResponse(): string {
return json_encode([
'success' => true,
'data' => [
'users' => [
[
'id' => 1,
'name' => 'Alice Johnson',
'email' => 'alice@example.com',
'role' => 'admin',
'registered_at' => '2024-12-15',
],
[
'id' => 2,
'name' => 'Bob Smith',
'email' => 'bob@example.com',
'role' => 'user',
'registered_at' => '2025-01-20',
],
[
'id' => 3,
'name' => 'Carol White',
'email' => 'carol@example.com',
'role' => 'editor',
'registered_at' => '2025-03-05',
],
],
'pagination' => [
'current_page' => 1,
'total_pages' => 3,
'total_items' => 27,
],
],
], JSON_THROW_ON_ERROR);
}
/**
* This method mocks an error API response for testing purposes
*
* @throws \JsonException
*/
private function getMockedErrorResponse(): string {
return json_encode([
'success' => false,
'error' => [
'code' => 404,
'message' => 'Resource not found',
],
], JSON_THROW_ON_ERROR);
}
}