Skip to content

Commit

Permalink
tests: Updated integration tests to latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
matapatos committed May 16, 2024
1 parent ea5d987 commit 5fbb040
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
4 changes: 2 additions & 2 deletions tests/Integration/AppMultipleRoutersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
->toBeArray()
->toHaveKeys([
'/my-api/v1',
'/my-api/v1/my-posts/v1/(?P<postId>[\\d]+)',
'/my-api/v1/my-posts/v1/(?P<ID>[\\d]+)',
'/my-api/v1/my-actions/v2/middleware/on-request/(?P<action>\w+)',
'/my-api/v1/my-actions/v2/middleware/on-response/(?P<action>\w+)',
'/my-api/v1/my-actions/v2/permission/(?P<action>\w+)',
])
->and($routes['/my-api/v1/my-posts/v1/(?P<postId>[\\d]+)'])
->and($routes['/my-api/v1/my-posts/v1/(?P<ID>[\\d]+)'])
->toBeArray()
->toHaveCount(3)
->and($routes['/my-api/v1/my-actions/v2/middleware/on-request/(?P<action>\w+)'])
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/AppSingleRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
->toBeArray()
->toHaveKeys([
'/my-posts/v1',
'/my-posts/v1/(?P<postId>[\\d]+)',
'/my-posts/v1/(?P<ID>[\\d]+)',
])
->and($routes['/my-posts/v1/(?P<postId>[\\d]+)'])
->and($routes['/my-posts/v1/(?P<ID>[\\d]+)'])
->toBeArray()
->toHaveCount(3);
})->group('single');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Routers/ActionsRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Wp\FastEndpoints\Tests\Integration\Routers\Middlewares\OnResponseErrorActionMiddleware;

$router = new Router('my-actions', 'v2');
$router->appendSchemaDir(\SCHEMAS_DIR);
$router->appendSchemaDir(\SCHEMAS_DIR, 'https://www.wp-fastendpoints.com');

// Triggers onRequest middleware
$router->get('/middleware/on-request/(?P<action>\w+)', function (): bool {
Expand Down
25 changes: 10 additions & 15 deletions tests/Integration/Routers/PostsRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,33 @@
use Wp\FastEndpoints\Router;

$router = new Router('my-posts', 'v1');
$router->appendSchemaDir(\SCHEMAS_DIR);
$router->appendSchemaDir(\SCHEMAS_DIR, 'https://www.wp-fastendpoints.com');

// Fetches a single post
$router->get('(?P<postId>[\d]+)', function (string $postId) {
return get_post($postId);
$router->get('(?P<ID>[\d]+)', function (string $ID) {
return get_post($ID);
})
->returns('Posts/Get')
->hasCap('read');

// Updates a post
$router->post('(?P<postId>[\d]+)', function (string $postId, \WP_REST_Request $request) {
$router->post('(?P<ID>[\d]+)', function (\WP_REST_Request $request, $ID) {
$payload = $request->get_params();
$payload['ID'] = $postId;
$error = wp_update_post($payload, true);

$postId = wp_update_post($payload);
if (is_wp_error($postId)) {
return $postId;
}

return get_post($postId);
return is_wp_error($error) ? $error : get_post($ID);
})
->schema('Posts/Update')
->returns('Posts/Get')
->hasCap('edit_post', '{postId}');
->hasCap('edit_post', '{ID}');

// Deletes a post
$router->delete('(?P<postId>[\d]+)', function (string $postId) {
$result = wp_delete_post($postId);
$router->delete('(?P<ID>[\d]+)', function (string $ID) {
$result = wp_delete_post($ID);
if ($result === false or $result === null) {
return new WpError(500, 'Unable to delete post');
}

return esc_html__('Post deleted with success');
})
->hasCap('delete_post', '{postId}');
->hasCap('delete_post', '{ID}');

0 comments on commit 5fbb040

Please sign in to comment.