Skip to content

Commit 78d6861

Browse files
committed
simplify tests bootstrap
1 parent 03c67f5 commit 78d6861

File tree

2 files changed

+50
-72
lines changed

2 files changed

+50
-72
lines changed

tests/bootstrap.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
use WP_UnitTestCase;
66

7-
// Determine the tests directory
8-
if ( false !== getenv( 'WP_TESTS_DIR' ) ) {
7+
$_tests_dir = false;
8+
9+
if ( getenv( 'WP_TESTS_DIR' ) ) {
910
$_tests_dir = getenv( 'WP_TESTS_DIR' );
10-
} elseif ( false !== getenv( 'WP_DEVELOP_DIR' ) ) {
11+
} elseif ( getenv( 'WP_DEVELOP_DIR' ) ) {
1112
$_tests_dir = getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit';
12-
} elseif ( false !== getenv( 'WP_PHPUNIT__DIR' ) ) {
13+
} elseif ( getenv( 'WP_PHPUNIT__DIR' ) ) {
1314
$_tests_dir = getenv( 'WP_PHPUNIT__DIR' );
1415
} elseif ( file_exists( '/tmp/wordpress-tests-lib/includes/functions.php' ) ) {
1516
$_tests_dir = '/tmp/wordpress-tests-lib';
@@ -18,31 +19,18 @@
1819
}
1920

2021
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
21-
echo "Could not find $_tests_dir/includes/functions.php\n";
22-
echo "Please run: bash tests/install-wp-tests.sh <db-name> <db-user> <db-pass> [db-host] [wp-version]\n";
23-
exit( 1 );
22+
die( "WordPress test suite not found. Run: bash tests/install-wp-tests.sh <db-name> <db-user> <db-pass>\n" );
2423
}
2524

26-
// Give access to tests_add_filter() function.
2725
require_once $_tests_dir . '/includes/functions.php';
2826

29-
/**
30-
* Manually load the plugin being tested.
31-
*/
32-
function _manually_load_plugin() {
33-
$plugin_dir = dirname( __DIR__ );
34-
require $plugin_dir . '/src/code-snippets.php';
35-
}
27+
tests_add_filter( 'muplugins_loaded', function() {
28+
require dirname( __DIR__ ) . '/src/code-snippets.php';
29+
} );
3630

37-
tests_add_filter( 'muplugins_loaded', __NAMESPACE__ . '\\_manually_load_plugin' );
38-
39-
// Start up the WP testing environment.
4031
require $_tests_dir . '/includes/bootstrap.php';
4132

4233
/**
4334
* Base test case for all Code Snippets tests.
4435
*/
45-
class TestCase extends WP_UnitTestCase {
46-
// Put convenience methods here
47-
48-
}
36+
class TestCase extends WP_UnitTestCase {}

tests/test-rest-api-snippets.php

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use WP_REST_Request;
77
use function Code_Snippets\code_snippets;
88
use function Code_Snippets\save_snippet;
9-
use function Code_Snippets\delete_snippet;
109

1110
/**
1211
* Tests for the Snippets REST API endpoint.
@@ -15,13 +14,6 @@
1514
*/
1615
class REST_API_Snippets_Test extends TestCase {
1716

18-
/**
19-
* Array of created snippet IDs for cleanup.
20-
*
21-
* @var int[]
22-
*/
23-
protected $created_snippet_ids = [];
24-
2517
/**
2618
* REST API namespace and base route.
2719
*
@@ -53,30 +45,12 @@ public static function wpSetUpBeforeClass( $factory ) {
5345
*/
5446
public function set_up() {
5547
parent::set_up();
56-
57-
// Set current user as admin for REST API permissions.
58-
wp_set_current_user( self::$admin_user_id );
5948

60-
// Clear all existing snippets first for clean testing environment.
49+
wp_set_current_user( self::$admin_user_id );
6150
$this->clear_all_snippets();
62-
63-
// Seed 25 test snippets.
6451
$this->seed_test_snippets( 25 );
6552
}
6653

67-
/**
68-
* Clean up after each test.
69-
*/
70-
public function tear_down() {
71-
// Delete all created snippets.
72-
foreach ( $this->created_snippet_ids as $snippet_id ) {
73-
delete_snippet( $snippet_id );
74-
}
75-
$this->created_snippet_ids = [];
76-
77-
parent::tear_down();
78-
}
79-
8054
/**
8155
* Clear all snippets from the database.
8256
*/
@@ -102,11 +76,7 @@ protected function seed_test_snippets( $count = 25 ) {
10276
'tags' => [ 'test', "batch-{$i}" ],
10377
] );
10478

105-
$saved_snippet = save_snippet( $snippet );
106-
107-
if ( $saved_snippet && $saved_snippet->id ) {
108-
$this->created_snippet_ids[] = $saved_snippet->id;
109-
}
79+
save_snippet( $snippet );
11080
}
11181
}
11282

@@ -132,14 +102,16 @@ protected function make_request( $endpoint, $params = [] ) {
132102
* Test that we can retrieve all snippets without pagination.
133103
*/
134104
public function test_get_all_snippets_without_pagination() {
105+
// Arrange.
135106
$endpoint = "/{$this->namespace}/{$this->base_route}";
107+
108+
// Act.
136109
$response = $this->make_request( $endpoint, [ 'network' => false ] );
137110

138-
// Should return all 25 snippets.
111+
// Assert.
139112
$this->assertIsArray( $response );
140113
$this->assertCount( 25, $response, 'Should return all 25 snippets when no pagination params are provided' );
141114

142-
// Verify first snippet structure.
143115
$this->assertArrayHasKey( 'id', $response[0] );
144116
$this->assertArrayHasKey( 'name', $response[0] );
145117
$this->assertArrayHasKey( 'code', $response[0] );
@@ -149,17 +121,19 @@ public function test_get_all_snippets_without_pagination() {
149121
* Test pagination with per_page parameter only (first page).
150122
*/
151123
public function test_get_snippets_with_per_page() {
124+
// Arrange.
152125
$endpoint = "/{$this->namespace}/{$this->base_route}";
126+
127+
// Act.
153128
$response = $this->make_request( $endpoint, [
154129
'network' => false,
155130
'per_page' => 2,
156131
] );
157132

158-
// Should return only 2 snippets (first page).
133+
// Assert.
159134
$this->assertIsArray( $response );
160135
$this->assertCount( 2, $response, 'Should return exactly 2 snippets when per_page=2' );
161136

162-
// Verify we got the first 2 snippets.
163137
$this->assertStringContainsString( 'Test Snippet 1', $response[0]['name'] );
164138
$this->assertStringContainsString( 'Test Snippet 2', $response[1]['name'] );
165139
}
@@ -168,19 +142,20 @@ public function test_get_snippets_with_per_page() {
168142
* Test pagination with per_page and page parameters.
169143
*/
170144
public function test_get_snippets_with_per_page_and_page() {
145+
// Arrange.
171146
$endpoint = "/{$this->namespace}/{$this->base_route}";
147+
148+
// Act.
172149
$response = $this->make_request( $endpoint, [
173150
'network' => false,
174151
'per_page' => 2,
175152
'page' => 3,
176153
] );
177154

178-
// Should return 2 snippets from page 3.
179-
// Page 1: snippets 1-2, Page 2: snippets 3-4, Page 3: snippets 5-6.
155+
// Assert.
180156
$this->assertIsArray( $response );
181157
$this->assertCount( 2, $response, 'Should return exactly 2 snippets for page 3 with per_page=2' );
182158

183-
// Verify we got snippets 5 and 6.
184159
$this->assertStringContainsString( 'Test Snippet 5', $response[0]['name'] );
185160
$this->assertStringContainsString( 'Test Snippet 6', $response[1]['name'] );
186161
}
@@ -189,45 +164,46 @@ public function test_get_snippets_with_per_page_and_page() {
189164
* Test pagination with page parameter only (should use default per_page).
190165
*/
191166
public function test_get_snippets_with_page_only() {
167+
// Arrange.
192168
$endpoint = "/{$this->namespace}/{$this->base_route}";
193169

194-
// First, let's get page 1 to see the default per_page behavior.
170+
// Act.
195171
$page_1_response = $this->make_request( $endpoint, [
196172
'network' => false,
197173
'page' => 1,
198174
] );
199175

200-
// WordPress REST API default per_page is typically 10.
176+
// Assert.
201177
$this->assertIsArray( $page_1_response );
202178
$this->assertGreaterThan( 0, count( $page_1_response ), 'Page 1 should have snippets' );
203179

204-
// Now get page 2.
180+
// Act.
205181
$page_2_response = $this->make_request( $endpoint, [
206182
'network' => false,
207183
'page' => 2,
208184
] );
209185

186+
// Assert.
210187
$this->assertIsArray( $page_2_response );
211-
212-
// With 25 snippets and default per_page of 10, page 2 should have 10 snippets.
213188
$this->assertCount( 10, $page_2_response, 'Page 2 with default per_page should have 10 snippets' );
214189
}
215190

216191
/**
217192
* Test that headers contain correct pagination metadata.
218193
*/
219194
public function test_pagination_headers() {
195+
// Arrange.
220196
$endpoint = "/{$this->namespace}/{$this->base_route}";
221197
$request = new WP_REST_Request( 'GET', $endpoint );
222198
$request->set_param( 'network', false );
223199
$request->set_param( 'per_page', 5 );
224200
$request->set_param( 'page', 1 );
225201

202+
// Act.
226203
$response = rest_do_request( $request );
227-
228-
// Check headers.
229204
$headers = $response->get_headers();
230205

206+
// Assert.
231207
$this->assertEquals( 25, $headers['X-WP-Total'], 'X-WP-Total header should show 25 total snippets' );
232208
$this->assertEquals( 5, $headers['X-WP-TotalPages'], 'X-WP-TotalPages should be 5 (25 snippets / 5 per_page)' );
233209
}
@@ -236,15 +212,17 @@ public function test_pagination_headers() {
236212
* Test that last page returns correct number of snippets.
237213
*/
238214
public function test_last_page_with_partial_results() {
215+
// Arrange.
239216
$endpoint = "/{$this->namespace}/{$this->base_route}";
240217

241-
// With 25 snippets and per_page=10, page 3 should have 5 snippets.
218+
// Act.
242219
$response = $this->make_request( $endpoint, [
243220
'network' => false,
244221
'per_page' => 10,
245222
'page' => 3,
246223
] );
247224

225+
// Assert.
248226
$this->assertIsArray( $response );
249227
$this->assertCount( 5, $response, 'Last page should have only 5 remaining snippets (25 % 10)' );
250228
}
@@ -253,15 +231,17 @@ public function test_last_page_with_partial_results() {
253231
* Test that requesting a page beyond available pages returns empty array.
254232
*/
255233
public function test_page_beyond_available_returns_empty() {
234+
// Arrange.
256235
$endpoint = "/{$this->namespace}/{$this->base_route}";
257236

258-
// Request page 100 (way beyond our 25 snippets).
237+
// Act.
259238
$response = $this->make_request( $endpoint, [
260239
'network' => false,
261240
'per_page' => 10,
262241
'page' => 100,
263242
] );
264243

244+
// Assert.
265245
$this->assertIsArray( $response );
266246
$this->assertCount( 0, $response, 'Requesting page beyond available should return empty array' );
267247
}
@@ -270,13 +250,17 @@ public function test_page_beyond_available_returns_empty() {
270250
* Test per_page with value of 1.
271251
*/
272252
public function test_per_page_one() {
253+
// Arrange.
273254
$endpoint = "/{$this->namespace}/{$this->base_route}";
255+
256+
// Act.
274257
$response = $this->make_request( $endpoint, [
275258
'network' => false,
276259
'per_page' => 1,
277260
'page' => 5,
278261
] );
279262

263+
// Assert.
280264
$this->assertIsArray( $response );
281265
$this->assertCount( 1, $response, 'Should return exactly 1 snippet when per_page=1' );
282266
$this->assertStringContainsString( 'Test Snippet 5', $response[0]['name'] );
@@ -286,13 +270,17 @@ public function test_per_page_one() {
286270
* Test that per_page larger than total returns all snippets.
287271
*/
288272
public function test_per_page_larger_than_total() {
273+
// Arrange.
289274
$endpoint = "/{$this->namespace}/{$this->base_route}";
275+
276+
// Act.
290277
$response = $this->make_request( $endpoint, [
291278
'network' => false,
292279
'per_page' => 100,
293280
'page' => 1,
294281
] );
295282

283+
// Assert.
296284
$this->assertIsArray( $response );
297285
$this->assertCount( 25, $response, 'Should return all 25 snippets when per_page exceeds total' );
298286
}
@@ -301,24 +289,26 @@ public function test_per_page_larger_than_total() {
301289
* Test that snippet data structure is correct.
302290
*/
303291
public function test_snippet_data_structure() {
292+
// Arrange.
304293
$endpoint = "/{$this->namespace}/{$this->base_route}";
294+
295+
// Act.
305296
$response = $this->make_request( $endpoint, [
306297
'network' => false,
307298
'per_page' => 1,
308299
] );
309300

301+
// Assert.
310302
$this->assertIsArray( $response );
311303
$this->assertCount( 1, $response );
312304

313305
$snippet = $response[0];
314306

315-
// Check required fields exist.
316307
$required_fields = [ 'id', 'name', 'desc', 'code', 'scope', 'active', 'tags' ];
317308
foreach ( $required_fields as $field ) {
318309
$this->assertArrayHasKey( $field, $snippet, "Snippet should have '{$field}' field" );
319310
}
320311

321-
// Verify field types.
322312
$this->assertIsInt( $snippet['id'] );
323313
$this->assertIsString( $snippet['name'] );
324314
$this->assertIsString( $snippet['code'] );

0 commit comments

Comments
 (0)