Skip to content

Commit

Permalink
PHPCS: Fix phpunit files (#995)
Browse files Browse the repository at this point in the history
* PHPCS: Fix phpunit files

* Fix warnings
  • Loading branch information
obenland authored Nov 15, 2024
1 parent 74b3e23 commit 5ab5659
Show file tree
Hide file tree
Showing 32 changed files with 1,811 additions and 795 deletions.
1 change: 0 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<exclude-pattern>*\.(inc|css|js|svg)</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>*.asset.php</exclude-pattern>

<arg value="ps"/>
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
>
<testsuites>
<testsuite name="ActivityPub">
<directory prefix="test-" suffix=".php">./tests/</directory>
<directory prefix="class-test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
Expand Down
6 changes: 6 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
/**
* Bootstrap file for ActivityPub.
*
* @package Activitypub
*/

\define( 'ACTIVITYPUB_DISABLE_REACTIONS', false );
\define( 'ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS', false );

Expand Down
58 changes: 53 additions & 5 deletions tests/class-activitypub-testcase-cache-http.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
<?php
/**
* Test file for Activitypub Cache HTTP.
*
* @package Activitypub
*/

/**
* Test class for Activitypub Cache HTTP.
*/
class ActivityPub_TestCase_Cache_HTTP extends \WP_UnitTestCase {
/**
* The REST server.
*
* @var \Spy_REST_Server
*/
public $server;

/**
* Set up the test.
*/
public function set_up() {
parent::set_up();

Expand All @@ -21,20 +39,35 @@ function () {
add_filter( 'http_response', array( get_called_class(), 'http_response' ), 10, 3 );
}

/**
* Tear down the test.
*/
public function tear_down() {
remove_filter( 'pre_http_request', array( get_called_class(), 'pre_http_request' ) );
remove_filter( 'http_response', array( get_called_class(), 'http_response' ) );
parent::tear_down();
}


/**
* Filters the return value of an HTTP request.
*
* @param bool $preempt Whether to preempt an HTTP request's return value.
* @param array $request {
* Array of HTTP request arguments.
*
* @type string $method Request method.
* @type string $body Request body.
* }
* @param string $url The request URL.
* @return array|bool|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance. A boolean false value.
*/
public static function pre_http_request( $preempt, $request, $url ) {
$p = wp_parse_url( $url );
$p = wp_parse_url( $url );
$cache = __DIR__ . '/fixtures/' . sanitize_title( $p['host'] . '-' . $p['path'] ) . '.json';
if ( file_exists( $cache ) ) {
return apply_filters(
'fake_http_response',
json_decode( file_get_contents( $cache ), true ), // phpcs:ignore
json_decode( file_get_contents( $cache ), true ), // phpcs:ignore WordPress.WP.AlternativeFunctions
$p['scheme'] . '://' . $p['host'],
$url,
$request
Expand Down Expand Up @@ -65,6 +98,13 @@ public static function pre_http_request( $preempt, $request, $url ) {
// Restore the old url.
update_option( 'home', $home_url );

/**
* Filters the return value of an HTTP request.
*
* @param array $response Array containing 'headers', 'body', 'response'.
* @param string $url The request URL.
* @param array $request Array of HTTP request arguments.
*/
return apply_filters(
'fake_http_response',
array(
Expand All @@ -82,12 +122,20 @@ public static function pre_http_request( $preempt, $request, $url ) {
);
}

/**
* Filters the HTTP response.
*
* @param array $response HTTP response.
* @param array $args HTTP request arguments.
* @param string $url The request URL.
* @return array HTTP response.
*/
public static function http_response( $response, $args, $url ) {
$p = wp_parse_url( $url );
$p = wp_parse_url( $url );
$cache = __DIR__ . '/fixtures/' . sanitize_title( $p['host'] . '-' . $p['path'] ) . '.json';
if ( ! file_exists( $cache ) ) {
$headers = wp_remote_retrieve_headers( $response );
file_put_contents( // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
file_put_contents( // phpcs:ignore WordPress.WP.AlternativeFunctions
$cache,
wp_json_encode(
array(
Expand Down
Loading

0 comments on commit 5ab5659

Please sign in to comment.