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

Adding support for Mock_Http_Sequence inside an array #303

Merged
merged 1 commit into from
Jul 29, 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
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