Skip to content

Instantly share code, notes, and snippets.

@joehoyle
Created April 6, 2015 16:34
Show Gist options
  • Save joehoyle/0028c6a8444ad0af2aee to your computer and use it in GitHub Desktop.
Save joehoyle/0028c6a8444ad0af2aee to your computer and use it in GitHub Desktop.
class Test_Restrict_Articles extends WP_UnitTestCase {
function test_mr_get_article_stats_for_yahoo_ticker() {
add_filter(
'pre_http_request',
$this->get_http_request_overide( 'https://finance.yahoo.com/q?s=spy',
file_get_contents( __DIR__ . '/data/yahoo-finance-spy.html' )
), 10, 3 );
// this is doing a http request internally
$stats = mr_get_article_stats_for_yahoo_ticker();
$this->assertEquals( 15, count( $stats ) );
$this->assertEquals( 6, count( wp_list_filter( $stats, array( 'is_mr' => true ) ) ) );
}
private function get_http_request_overide( $matched_url, $response_body ) {
$func = null;
return function( $return, $request, $url ) use ( $matched_url, $response_body, &$func ) {
remove_filter( 'pre_http_request', $func );
if ( $url !== $matched_url ) {
return $return;
}
$response = array(
'headers' => array(),
'body' => $response_body,
'response' => 200,
);
return $response;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment