Open Journal Systems  3.3.0
MockPlugin.php
1 <?php
2 
4 
13 
17 class MockPlugin extends AbstractHasDispatcher implements EventSubscriberInterface, \Countable
18 {
20  protected $queue = array();
21 
23  protected $temporary = false;
24 
26  protected $received = array();
27 
29  protected $readBodies;
30 
36  public function __construct(array $items = null, $temporary = false, $readBodies = false)
37  {
38  $this->readBodies = $readBodies;
39  $this->temporary = $temporary;
40  if ($items) {
41  foreach ($items as $item) {
42  if ($item instanceof \Exception) {
43  $this->addException($item);
44  } else {
45  $this->addResponse($item);
46  }
47  }
48  }
49  }
50 
51  public static function getSubscribedEvents()
52  {
53  // Use a number lower than the CachePlugin
54  return array('request.before_send' => array('onRequestBeforeSend', -999));
55  }
56 
57  public static function getAllEvents()
58  {
59  return array('mock.request');
60  }
61 
70  public static function getMockFile($path)
71  {
72  if (!file_exists($path)) {
73  throw new InvalidArgumentException('Unable to open mock file: ' . $path);
74  }
75 
76  return Response::fromMessage(file_get_contents($path));
77  }
78 
87  public function readBodies($readBodies)
88  {
89  $this->readBodies = $readBodies;
90 
91  return $this;
92  }
93 
99  public function count()
100  {
101  return count($this->queue);
102  }
103 
112  public function addResponse($response)
113  {
114  if (!($response instanceof Response)) {
115  if (!is_string($response)) {
116  throw new InvalidArgumentException('Invalid response');
117  }
118  $response = self::getMockFile($response);
119  }
120 
121  $this->queue[] = $response;
122 
123  return $this;
124  }
125 
133  public function addException(CurlException $e)
134  {
135  $this->queue[] = $e;
136 
137  return $this;
138  }
139 
145  public function clearQueue()
146  {
147  $this->queue = array();
148 
149  return $this;
150  }
151 
157  public function getQueue()
158  {
159  return $this->queue;
160  }
161 
167  public function isTemporary()
168  {
170  }
171 
180  public function dequeue(RequestInterface $request)
181  {
182  $this->dispatch('mock.request', array('plugin' => $this, 'request' => $request));
183 
184  $item = array_shift($this->queue);
185  if ($item instanceof Response) {
186  if ($this->readBodies && $request instanceof EntityEnclosingRequestInterface) {
187  $request->getEventDispatcher()->addListener('request.sent', $f = function (Event $event) use (&$f) {
188  while ($data = $event['request']->getBody()->read(8096));
189  // Remove the listener after one-time use
190  $event['request']->getEventDispatcher()->removeListener('request.sent', $f);
191  });
192  }
193  $request->setResponse($item);
194  } elseif ($item instanceof CurlException) {
195  // Emulates exceptions encountered while transferring requests
196  $item->setRequest($request);
197  $state = $request->setState(RequestInterface::STATE_ERROR, array('exception' => $item));
198  // Only throw if the exception wasn't handled
199  if ($state == RequestInterface::STATE_ERROR) {
200  throw $item;
201  }
202  }
203 
204  return $this;
205  }
206 
210  public function flush()
211  {
212  $this->received = array();
213  }
214 
220  public function getReceivedRequests()
221  {
223  }
224 
231  public function onRequestBeforeSend(Event $event)
232  {
233  if (!$this->queue) {
234  throw new \OutOfBoundsException('Mock queue is empty');
235  }
236 
237  $request = $event['request'];
238  $this->received[] = $request;
239  // Detach the filter from the client so it's a one-time use
240  if ($this->temporary && count($this->queue) == 1 && $request->getClient()) {
241  $request->getClient()->getEventDispatcher()->removeSubscriber($this);
242  }
243  $this->dequeue($request);
244  }
245 }
Guzzle\Plugin\Mock\MockPlugin\$readBodies
$readBodies
Definition: MockPlugin.php:41
Guzzle\Http\Exception\CurlException
Definition: CurlException.php:10
Guzzle\Http\Message\RequestInterface
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestInterface.php:16
Guzzle\Http\Message\RequestInterface\setResponse
setResponse(Response $response, $queued=false)
Guzzle\Plugin\Mock\MockPlugin\getSubscribedEvents
static getSubscribedEvents()
Definition: MockPlugin.php:63
Guzzle\Plugin\Mock\MockPlugin\isTemporary
isTemporary()
Definition: MockPlugin.php:179
Guzzle\Plugin\Mock\MockPlugin\dequeue
dequeue(RequestInterface $request)
Definition: MockPlugin.php:192
Guzzle\Plugin\Mock\MockPlugin\readBodies
readBodies($readBodies)
Definition: MockPlugin.php:99
Guzzle\Plugin\Mock\MockPlugin\$received
$received
Definition: MockPlugin.php:35
Symfony\Component\EventDispatcher\EventSubscriberInterface
Definition: lib/vendor/symfony/event-dispatcher/EventSubscriberInterface.php:25
Guzzle\Http\Message\EntityEnclosingRequestInterface
Definition: EntityEnclosingRequestInterface.php:12
Guzzle\Http\Message\Response
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:17
Guzzle\Plugin\Mock\MockPlugin\onRequestBeforeSend
onRequestBeforeSend(Event $event)
Definition: MockPlugin.php:243
Guzzle\Plugin\Mock\MockPlugin\getQueue
getQueue()
Definition: MockPlugin.php:169
Guzzle\Http\Message\Response\fromMessage
static fromMessage($message)
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:123
Guzzle\Plugin\Mock\MockPlugin\__construct
__construct(array $items=null, $temporary=false, $readBodies=false)
Definition: MockPlugin.php:48
Guzzle\Plugin\Mock\MockPlugin\flush
flush()
Definition: MockPlugin.php:222
Guzzle\Plugin\Mock\MockPlugin\getMockFile
static getMockFile($path)
Definition: MockPlugin.php:82
Guzzle\Plugin\Mock\MockPlugin\getAllEvents
static getAllEvents()
Definition: MockPlugin.php:69
Guzzle\Plugin\Mock\MockPlugin\clearQueue
clearQueue()
Definition: MockPlugin.php:157
Guzzle\Plugin\Mock\MockPlugin\addResponse
addResponse($response)
Definition: MockPlugin.php:124
Guzzle\Http\Message\RequestInterface\getClient
getClient()
Guzzle\Common\Event
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Event.php:10
Guzzle\Common\Exception\InvalidArgumentException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/InvalidArgumentException.php:5
Guzzle\Plugin\Mock
Definition: MockPlugin.php:3
Guzzle\Common\HasDispatcherInterface\getEventDispatcher
getEventDispatcher()
Guzzle\Plugin\Mock\MockPlugin\addException
addException(CurlException $e)
Definition: MockPlugin.php:145
Guzzle\Plugin\Mock\MockPlugin\getReceivedRequests
getReceivedRequests()
Definition: MockPlugin.php:232
Guzzle\Plugin\Mock\MockPlugin\count
count()
Definition: MockPlugin.php:111
Guzzle\Http\Message\RequestInterface\setState
setState($state, array $context=array())
Guzzle\Plugin\Mock\MockPlugin
Definition: MockPlugin.php:17
Guzzle\Common\AbstractHasDispatcher\dispatch
dispatch($eventName, array $context=array())
Definition: AbstractHasDispatcher.php:41
Guzzle\Http\Message\RequestInterface\STATE_ERROR
const STATE_ERROR
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestInterface.php:21
Guzzle\Common\AbstractHasDispatcher
Definition: AbstractHasDispatcher.php:12
Guzzle\Plugin\Mock\MockPlugin\$queue
$queue
Definition: MockPlugin.php:23
Guzzle\Plugin\Mock\MockPlugin\$temporary
$temporary
Definition: MockPlugin.php:29