MockPlugin
extends AbstractHasDispatcher
in package
implements
EventSubscriberInterface, Countable
Queues mock responses or exceptions and delivers mock responses or exceptions in a fifo order.
Table of Contents
Interfaces
- EventSubscriberInterface
- An EventSubscriber knows himself what events he is interested in.
- Countable
Properties
- $eventDispatcher : EventDispatcherInterface
- $queue : array<string|int, mixed>
- $readBodies : bool
- $received : array<string|int, mixed>
- $temporary : bool
Methods
- __construct() : mixed
- addException() : MockPlugin
- Add an exception to the end of the queue
- addResponse() : MockPlugin
- Add a response to the end of the queue
- addSubscriber() : self
- Add an event subscriber to the dispatcher
- clearQueue() : MockPlugin
- Clear the queue
- count() : int
- Returns the number of remaining mock responses
- dequeue() : self
- Get a response from the front of the list and add it to a request
- dispatch() : Event
- Helper to dispatch Guzzle events and set the event name on the event
- flush() : mixed
- Clear the array of received requests
- getAllEvents() : array<string|int, mixed>
- Get a list of all of the events emitted from the class
- getEventDispatcher() : EventDispatcherInterface
- Get the EventDispatcher of the request
- getMockFile() : Response
- Get a mock response from a file
- getQueue() : array<string|int, mixed>
- Returns an array of mock responses remaining in the queue
- getReceivedRequests() : array<string|int, mixed>
- Get an array of requests that were mocked by this plugin
- getSubscribedEvents() : array<string|int, mixed>
- Returns an array of event names this subscriber wants to listen to.
- isTemporary() : bool
- Check if this is a temporary plugin
- onRequestBeforeSend() : mixed
- Called when a request is about to be sent
- readBodies() : self
- Set whether or not to consume the entity body of a request when a mock response is used
- setEventDispatcher() : self
- Set the EventDispatcher of the request
Properties
$eventDispatcher
protected
EventDispatcherInterface
$eventDispatcher
$queue
protected
array<string|int, mixed>
$queue
= array()
Array of mock responses / exceptions
$readBodies
protected
bool
$readBodies
Whether or not to consume an entity body when a mock response is served
$received
protected
array<string|int, mixed>
$received
= array()
Array of requests that were mocked
$temporary
protected
bool
$temporary
= false
Whether or not to remove the plugin when the queue is empty
Methods
__construct()
public
__construct([array<string|int, mixed> $items = null ][, bool $temporary = false ][, bool $readBodies = false ]) : mixed
Parameters
- $items : array<string|int, mixed> = null
-
Array of responses or exceptions to queue
- $temporary : bool = false
-
Set to TRUE to remove the plugin when the queue is empty
- $readBodies : bool = false
-
Set to TRUE to consume the entity body when a mock is served
addException()
Add an exception to the end of the queue
public
addException(CurlException $e) : MockPlugin
Parameters
- $e : CurlException
-
Exception to throw when the request is executed
Return values
MockPluginaddResponse()
Add a response to the end of the queue
public
addResponse(string|Response $response) : MockPlugin
Parameters
- $response : string|Response
-
Response object or path to response file
Tags
Return values
MockPluginaddSubscriber()
Add an event subscriber to the dispatcher
public
addSubscriber(EventSubscriberInterface $subscriber) : self
Parameters
- $subscriber : EventSubscriberInterface
-
Event subscriber
Return values
selfclearQueue()
Clear the queue
public
clearQueue() : MockPlugin
Return values
MockPlugincount()
Returns the number of remaining mock responses
public
count() : int
Return values
intdequeue()
Get a response from the front of the list and add it to a request
public
dequeue(RequestInterface $request) : self
Parameters
- $request : RequestInterface
-
Request to mock
Tags
Return values
selfdispatch()
Helper to dispatch Guzzle events and set the event name on the event
public
dispatch(mixed $eventName[, array<string|int, mixed> $context = array() ]) : Event
Parameters
- $eventName : mixed
-
Name of the event to dispatch
- $context : array<string|int, mixed> = array()
-
Context of the event
Return values
Event —Returns the created event object
flush()
Clear the array of received requests
public
flush() : mixed
getAllEvents()
Get a list of all of the events emitted from the class
public
static getAllEvents() : array<string|int, mixed>
Return values
array<string|int, mixed>getEventDispatcher()
Get the EventDispatcher of the request
public
getEventDispatcher() : EventDispatcherInterface
Return values
EventDispatcherInterfacegetMockFile()
Get a mock response from a file
public
static getMockFile(string $path) : Response
Parameters
- $path : string
-
File to retrieve a mock response from
Tags
Return values
ResponsegetQueue()
Returns an array of mock responses remaining in the queue
public
getQueue() : array<string|int, mixed>
Return values
array<string|int, mixed>getReceivedRequests()
Get an array of requests that were mocked by this plugin
public
getReceivedRequests() : array<string|int, mixed>
Return values
array<string|int, mixed>getSubscribedEvents()
Returns an array of event names this subscriber wants to listen to.
public
static getSubscribedEvents() : array<string|int, mixed>
The array keys are event names and the value can be:
- The method name to call (priority defaults to 0)
- An array composed of the method name to call and the priority
- An array of arrays composed of the method names to call and respective priorities, or 0 if unset
For instance:
- array('eventName' => 'methodName')
- array('eventName' => array('methodName', $priority))
- array('eventName' => array(array('methodName1', $priority), array('methodName2')))
Return values
array<string|int, mixed> —The event names to listen to
isTemporary()
Check if this is a temporary plugin
public
isTemporary() : bool
Return values
boolonRequestBeforeSend()
Called when a request is about to be sent
public
onRequestBeforeSend(Event $event) : mixed
Parameters
- $event : Event
Tags
readBodies()
Set whether or not to consume the entity body of a request when a mock response is used
public
readBodies(bool $readBodies) : self
Parameters
- $readBodies : bool
-
Set to true to read and consume entity bodies
Return values
selfsetEventDispatcher()
Set the EventDispatcher of the request
public
setEventDispatcher(EventDispatcherInterface $eventDispatcher) : self
Parameters
- $eventDispatcher : EventDispatcherInterface