Skip to content

Commit

Permalink
Rename od_url_metric_collected action to od_url_metric_stored and use…
Browse files Browse the repository at this point in the history
… OD_URL_Metric_Stored_Context instead of array
  • Loading branch information
westonruter committed Oct 3, 2024
1 parent b833e1b commit 1cf24f1
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 18 deletions.
1 change: 1 addition & 0 deletions plugins/optimization-detective/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ static function ( string $version ): void {
require_once __DIR__ . '/storage/class-od-storage-lock.php';
require_once __DIR__ . '/storage/data.php';
require_once __DIR__ . '/storage/rest-api.php';
require_once __DIR__ . '/storage/class-od-url-metric-stored-context.php';

// Detection logic.
require_once __DIR__ . '/detection.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Optimization Detective: OD_URL_Metric_Stored_Context class
*
* @package optimization-detective
* @since n.e.x.t
*/

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Context for when a URL metric is successfully stored via the REST API.
*
* @since n.e.x.t
* @access private
*/
final class OD_URL_Metric_Stored_Context {

/**
* Request.
*
* @var WP_REST_Request<array<string, mixed>>
* @readonly
*/
public $request;

/**
* ID for the URL metric post.
*
* @var int
* @readonly
*/
public $post_id;

/**
* URL metric group collection.
*
* @var OD_URL_Metric_Group_Collection
* @readonly
*/
public $url_metric_group_collection;

/**
* URL metric group.
*
* @var OD_URL_Metric_Group
* @readonly
*/
public $url_metric_group;

/**
* URL metric.
*
* @var OD_URL_Metric
* @readonly
*/
public $url_metric;

/**
* Constructor.
*
* @phpstan-param WP_REST_Request<array<string, mixed>> $request
*
* @param WP_REST_Request $request REST API request.
* @param int $post_id ID for the URL metric post.
* @param OD_URL_Metric_Group_Collection $url_metric_group_collection URL metric group collection.
* @param OD_URL_Metric_Group $url_metric_group URL metric group.
* @param OD_URL_Metric $url_metric URL metric.
*/
public function __construct( WP_REST_Request $request, int $post_id, OD_URL_Metric_Group_Collection $url_metric_group_collection, OD_URL_Metric_Group $url_metric_group, OD_URL_Metric $url_metric ) {
$this->request = $request;
$this->post_id = $post_id;
$this->url_metric_group_collection = $url_metric_group_collection;
$this->url_metric_group = $url_metric_group;
$this->url_metric = $url_metric;
}
}
28 changes: 10 additions & 18 deletions plugins/optimization-detective/storage/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,28 +172,20 @@ function od_handle_rest_request( WP_REST_Request $request ) {
$post_id = $result;

/**
* Fires whenever a URL Metric was successfully collected.
* Fires whenever a URL Metric was successfully stored.
*
* @since 0.6.0
* @since n.e.x.t
*
* @param array $context {
* Context about the successful URL Metric collection.
*
* @type int $post_id ID for URL metrics post.
* @type WP_REST_Request<array<string, mixed>> $request Storage request.
* @type OD_Strict_URL_Metric $url_metric URL metric.
* @type OD_URL_Metric_Group $url_metric_group URL metric group.
* @type OD_URL_Metric_Group_Collection $url_metric_group_collection URL metric group collection.
* }
* @param OD_URL_Metric_Stored_Context $context Context.
*/
do_action(
'od_url_metric_collected',
array(
'post_id' => $post_id,
'request' => $request,
'url_metric' => $url_metric,
'url_metric_group' => $url_metric_group,
'url_metric_group_collection' => $url_metric_group_collection,
'od_url_metric_stored',
new OD_URL_Metric_Stored_Context(
$request,
$post_id,
$url_metric_group_collection,
$url_metric_group,
$url_metric
)
);

Expand Down
17 changes: 17 additions & 0 deletions plugins/optimization-detective/tests/storage/test-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ static function ( array $properties ): array {
* @covers ::od_handle_rest_request
*/
public function test_rest_request_good_params( Closure $set_up ): void {
add_action(
'od_url_metric_stored',
function ( OD_URL_Metric_Stored_Context $context ): void {
$this->assertInstanceOf( OD_URL_Metric_Group_Collection::class, $context->url_metric_group_collection );
$this->assertInstanceOf( OD_URL_Metric_Group::class, $context->url_metric_group );
$this->assertInstanceOf( OD_URL_Metric::class, $context->url_metric );
$this->assertInstanceOf( WP_REST_Request::class, $context->request );
$this->assertIsInt( $context->post_id );
}
);

$valid_params = $set_up();
$this->assertCount( 0, get_posts( array( 'post_type' => OD_URL_Metrics_Post_Type::SLUG ) ) );
$request = $this->create_request( $valid_params );
Expand All @@ -87,6 +98,7 @@ public function test_rest_request_good_params( Closure $set_up ): void {
$expected_data,
wp_array_slice_assoc( $url_metrics[0]->jsonSerialize(), array_keys( $expected_data ) )
);
$this->assertSame( 1, did_action( 'od_url_metric_stored' ) );
}

/**
Expand Down Expand Up @@ -225,6 +237,7 @@ public function test_rest_request_bad_params( array $params ): void {
$this->assertSame( 'rest_invalid_param', $response->get_data()['code'], 'Response: ' . wp_json_encode( $response ) );

$this->assertNull( OD_URL_Metrics_Post_Type::get_post( $params['slug'] ) );
$this->assertSame( 0, did_action( 'od_url_metric_stored' ) );
}

/**
Expand All @@ -239,6 +252,7 @@ public function test_rest_request_not_json_data(): void {
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 400, $response->get_status(), 'Response: ' . wp_json_encode( $response ) );
$this->assertSame( 'missing_array_json_body', $response->get_data()['code'], 'Response: ' . wp_json_encode( $response ) );
$this->assertSame( 0, did_action( 'od_url_metric_stored' ) );
}

/**
Expand All @@ -254,6 +268,7 @@ public function test_rest_request_not_json_content_type(): void {
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 400, $response->get_status(), 'Response: ' . wp_json_encode( $response ) );
$this->assertSame( 'rest_missing_callback_param', $response->get_data()['code'], 'Response: ' . wp_json_encode( $response ) );
$this->assertSame( 0, did_action( 'od_url_metric_stored' ) );
}

/**
Expand All @@ -269,6 +284,7 @@ public function test_rest_request_empty_array_json_body(): void {
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 400, $response->get_status(), 'Response: ' . wp_json_encode( $response ) );
$this->assertSame( 'rest_missing_callback_param', $response->get_data()['code'], 'Response: ' . wp_json_encode( $response ) );
$this->assertSame( 0, did_action( 'od_url_metric_stored' ) );
}

/**
Expand All @@ -284,6 +300,7 @@ public function test_rest_request_non_array_json_body(): void {
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 400, $response->get_status(), 'Response: ' . wp_json_encode( $response ) );
$this->assertSame( 'rest_missing_callback_param', $response->get_data()['code'], 'Response: ' . wp_json_encode( $response ) );
$this->assertSame( 0, did_action( 'od_url_metric_stored' ) );
}

/**
Expand Down

0 comments on commit 1cf24f1

Please sign in to comment.