Skip to content

Commit

Permalink
Merge pull request #303 from alleyinteractive/http-sequence-response
Browse files Browse the repository at this point in the history
Adding support for Mock_Http_Sequence inside an array
  • Loading branch information
srtfisher authored Jul 29, 2022
2 parents 5bb8dd7 + 2c9ea29 commit bb41da0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/mantle/testing/concerns/trait-interacts-with-requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Mantle\Support\Collection;
use Mantle\Support\Str;
use Mantle\Testing\Mock_Http_Response;
use Mantle\Testing\Mock_Http_Sequence;
use PHPUnit\Framework\Assert as PHPUnit;
use RuntimeException;

Expand Down Expand Up @@ -124,12 +125,12 @@ public function pre_http_request( $preempt, $request_args, $url ) {
*
* @throws \InvalidArgumentException Thrown on invalid argument.
*
* @param Closure|string|array $url_or_callback URL to fake, array of URL and response pairs, or a closure
* that will return a faked response.
* @param Mock_Http_Response $response Optional response object, defaults to creating a 200 response.
* @param Closure|string|array $url_or_callback URL to fake, array of URL and response pairs, or a closure
* that will return a faked response.
* @param Mock_Http_Response|Mock_Http_Sequence $response Optional response object, defaults to creating a 200 response.
* @return static|Mock_Http_Response
*/
public function fake_request( $url_or_callback = null, Mock_Http_Response $response = null ) {
public function fake_request( $url_or_callback = null, $response = null ) {
if ( is_array( $url_or_callback ) ) {
$this->stub_callbacks = $this->stub_callbacks->merge(
collect( $url_or_callback )
Expand Down Expand Up @@ -191,17 +192,17 @@ public function allow_stray_requests() {
/**
* Retrieve a callback for the stubbed response.
*
* @param string $url URL to stub.
* @param Mock_Http_Response $response Response to send.
* @param string $url URL to stub.
* @param Mock_Http_Response|Mock_Http_Sequence $response Response to send.
* @return Closure
*/
protected function create_stub_request_callback( string $url, Mock_Http_Response $response ): Closure {
protected function create_stub_request_callback( string $url, $response ): Closure {
return function( string $request_url, array $request_args ) use ( $url, $response ) {
if ( ! Str::is( Str::start( $url, '*' ), $request_url ) ) {
return;
}

return $response instanceof Closure
return $response instanceof Closure || $response instanceof Mock_Http_Sequence
? $response( $request_url, $request_args )
: $response;
};
Expand Down
22 changes: 22 additions & 0 deletions tests/testing/concerns/test-interacts-with-external-requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ public function test_sequence() {
$this->assertEquals( 500, $http->get( 'https://example.com/sequence/' )->status() );
}

public function test_sequence_array() {
$this->fake_request(
[
'github.com/*' => Mock_Http_Sequence::create()
->push_status( 200 )
->push_status( 400 )
->push_status( 500 ),
'alley.co/*' => Mock_Http_Sequence::create()
->push_status( 200 )
->push_status( 403 ),
]
);

$http = new Http_Client();

$this->assertEquals( 200, $http->get( 'https://github.com/request/' )->status() );
$this->assertEquals( 400, $http->get( 'https://github.com/request/' )->status() );

$this->assertEquals( 200, $http->get( 'https://alley.co/test/' )->status() );
$this->assertEquals( 403, $http->get( 'https://alley.co/test/' )->status() );
}

public function test_sequence_exception_empty() {
$this->expectException( RuntimeException::class );
$this->expectExceptionMessage( 'No more responses in sequence.' );
Expand Down

0 comments on commit bb41da0

Please sign in to comment.