master

laravel/framework

Last updated at: 29/12/2023 09:23

ResponseSequence.php

TLDR

The ResponseSequence class in the Illuminate\Http\Client namespace is a class that allows you to create a sequence of responses. It is used in the Laravel framework for testing HTTP clients.

Methods

There are several methods available in the ResponseSequence class. These include:

push

This method allows you to push a response to the sequence. You can specify the body, status code, and headers of the response. The default values for the status code is 200, and for the headers is an empty array.

pushStatus

This method allows you to push a response with a given status code to the sequence. You can also specify the headers of the response. The default value for the headers is an empty array.

pushFile

This method allows you to push a response with the contents of a file as the body to the sequence. You need to provide the file path, and you can also specify the status code and headers of the response. The default value for the status code is 200, and for the headers is an empty array.

pushResponse

This method allows you to push a response object to the sequence. The response object can be of any type.

whenEmpty

This method allows you to set a default response to be returned when the sequence is empty. You can provide a response object or a callback function that returns a response object.

dontFailWhenEmpty

This method allows you to set a default response to be returned when the sequence is empty without throwing an exception. It uses an empty response object as the default response.

isEmpty

This method checks if the sequence is empty and returns a boolean value.

__invoke

This method is invoked when the ResponseSequence object is called as a function. It returns the next response in the sequence. If the sequence is empty, an exception is thrown unless a default response has been set using the whenEmpty method.

Classes

There are no additional classes in the file.

<?php

namespace Illuminate\Http\Client;

use Illuminate\Support\Traits\Macroable;
use OutOfBoundsException;

class ResponseSequence
{
    use Macroable;

    /**
     * The responses in the sequence.
     *
     * @var array
     */
    protected $responses;

    /**
     * Indicates that invoking this sequence when it is empty should throw an exception.
     *
     * @var bool
     */
    protected $failWhenEmpty = true;

    /**
     * The response that should be returned when the sequence is empty.
     *
     * @var \GuzzleHttp\Promise\PromiseInterface
     */
    protected $emptyResponse;

    /**
     * Create a new response sequence.
     *
     * @param  array  $responses
     * @return void
     */
    public function __construct(array $responses)
    {
        $this->responses = $responses;
    }

    /**
     * Push a response to the sequence.
     *
     * @param  string|array|null  $body
     * @param  int  $status
     * @param  array  $headers
     * @return $this
     */
    public function push($body = null, int $status = 200, array $headers = [])
    {
        return $this->pushResponse(
            Factory::response($body, $status, $headers)
        );
    }

    /**
     * Push a response with the given status code to the sequence.
     *
     * @param  int  $status
     * @param  array  $headers
     * @return $this
     */
    public function pushStatus(int $status, array $headers = [])
    {
        return $this->pushResponse(
            Factory::response('', $status, $headers)
        );
    }

    /**
     * Push response with the contents of a file as the body to the sequence.
     *
     * @param  string  $filePath
     * @param  int  $status
     * @param  array  $headers
     * @return $this
     */
    public function pushFile(string $filePath, int $status = 200, array $headers = [])
    {
        $string = file_get_contents($filePath);

        return $this->pushResponse(
            Factory::response($string, $status, $headers)
        );
    }

    /**
     * Push a response to the sequence.
     *
     * @param  mixed  $response
     * @return $this
     */
    public function pushResponse($response)
    {
        $this->responses[] = $response;

        return $this;
    }

    /**
     * Make the sequence return a default response when it is empty.
     *
     * @param  \GuzzleHttp\Promise\PromiseInterface|\Closure  $response
     * @return $this
     */
    public function whenEmpty($response)
    {
        $this->failWhenEmpty = false;
        $this->emptyResponse = $response;

        return $this;
    }

    /**
     * Make the sequence return a default response when it is empty.
     *
     * @return $this
     */
    public function dontFailWhenEmpty()
    {
        return $this->whenEmpty(Factory::response());
    }

    /**
     * Indicate that this sequence has depleted all of its responses.
     *
     * @return bool
     */
    public function isEmpty()
    {
        return count($this->responses) === 0;
    }

    /**
     * Get the next response in the sequence.
     *
     * @return mixed
     *
     * @throws \OutOfBoundsException
     */
    public function __invoke()
    {
        if ($this->failWhenEmpty && $this->isEmpty()) {
            throw new OutOfBoundsException('A request was made, but the response sequence is empty.');
        }

        if (! $this->failWhenEmpty && $this->isEmpty()) {
            return value($this->emptyResponse ?? Factory::response());
        }

        return array_shift($this->responses);
    }
}