Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent external requests during unit testing #293

Merged
merged 1 commit into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/mantle/testing/concerns/trait-interacts-with-requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Mantle\Support\Str;
use Mantle\Testing\Mock_Http_Response;
use PHPUnit\Framework\Assert as PHPUnit;
use RuntimeException;

use function Mantle\Support\Helpers\collect;
use function Mantle\Support\Helpers\value;

/**
* Allow Mock HTTP Requests
Expand All @@ -37,6 +39,13 @@ trait Interacts_With_Requests {
*/
protected $recorded_requests;

/**
* Flag to prevent external requests from being made.
*
* @var Mock_Http_Response|\Closure|bool
*/
protected $preventing_stray_requests = false;

/**
* Setup the trait.
*/
Expand Down Expand Up @@ -65,6 +74,8 @@ public function interacts_with_requests_tear_down() {
* @param string $url The request URL.
* @return mixed Array if the request has been preempted, any value that's
* not false otherwise.
*
* @throws RuntimeException If the request was made without a matching faked request.
*/
public function pre_http_request( $preempt, $request_args, $url ) {
$this->recorded_requests[] = new Request( $request_args, $url );
Expand All @@ -82,6 +93,16 @@ public function pre_http_request( $preempt, $request_args, $url ) {
}
}

if ( false !== $this->preventing_stray_requests ) {
$prevent = value( $this->preventing_stray_requests );

if ( $prevent instanceof Mock_Http_Response ) {
return $prevent->to_array();
}

throw new RuntimeException( "Attempted request to [{$url}] without a matching fake." );
}

// To aid in debugging, print a message to the console that this test is making an actual HTTP request
// which it probably shouldn't be.
printf(
Expand Down Expand Up @@ -151,6 +172,22 @@ function( $response, $url_or_callback ) {
return $response;
}

/**
* Prevent stray external requests.
*
* @param Mock_Http_Response|\Closure|bool $response A default response or callback to use, boolean otherwise.
*/
public function prevent_stray_requests( $response = true ) {
$this->preventing_stray_requests = $response;
}

/**
* Allow stray external requests.
*/
public function allow_stray_requests() {
$this->preventing_stray_requests = false;
}

/**
* Retrieve a callback for the stubbed response.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/testing/concerns/test-interacts-with-external-requests.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Mantle\Tests\Testing\Concerns;

use Mantle\Facade\Http;
use Mantle\Http_Client\Http_Client;
use Mantle\Testing\Mock_Http_Response;
use Mantle\Testing\Framework_Test_Case;
Expand Down Expand Up @@ -174,4 +175,31 @@ public function test_sequence_fallback_empty() {
$this->assertEquals( 202, $http->get( 'https://example.com/sequence/' )->status() );
$this->assertEquals( 202, $http->get( 'https://example.com/sequence/' )->status() );
}

public function test_prevent_stray_requests() {
$this->prevent_stray_requests(
Mock_Http_Response::create()->with_status( 201 ),
);

$this->assertEquals( 201, Http::get( 'https://example.com/' )->status() );
$this->assertRequestSent();
}

public function test_prevent_stray_requests_callback() {
$this->prevent_stray_requests(
fn () => Mock_Http_Response::create()->with_status( 400 ),
);

$this->assertEquals( 400, Http::get( 'https://example.com/' )->status() );
$this->assertRequestSent();
}

public function test_prevent_stray_requests_no_fallback() {
$this->expectException( RuntimeException::class );
$this->expectExceptionMessage( 'Attempted request to [https://example.org/path/] without a matching fake.' );

$this->prevent_stray_requests();

Http::get( 'https://example.org/path/' );
}
}