Beispiel #1
0
 /**
  * Add a response to the end of the queue
  *
  * @param string|ResponseInterface $response Response or path to response file
  *
  * @return self
  * @throws \InvalidArgumentException if a string or Response is not passed
  */
 public function addResponse($response)
 {
     if (is_string($response)) {
         $response = file_exists($response) ? $this->factory->fromMessage(file_get_contents($response)) : $this->factory->fromMessage($response);
     } elseif (!$response instanceof ResponseInterface) {
         throw new \InvalidArgumentException('Response must a message ' . 'string, response object, or path to a file');
     }
     $this->queue[] = $response;
     return $this;
 }
Beispiel #2
0
 /**
  * Queue an array of responses or a single response on the server.
  *
  * Any currently queued responses will be overwritten.  Subsequent requests
  * on the server will return queued responses in FIFO order.
  *
  * @param array $responses Responses to queue.
  * @throws \Exception
  */
 public static function enqueue(array $responses)
 {
     static $factory;
     if (!$factory) {
         $factory = new MessageFactory();
     }
     $data = [];
     foreach ($responses as $response) {
         // Create the response object from a string
         if (is_string($response)) {
             $response = $factory->fromMessage($response);
         } elseif (!$response instanceof ResponseInterface) {
             throw new \Exception('Responses must be strings or Responses');
         }
         $data[] = self::convertResponse($response);
     }
     TestServer::enqueue($data);
 }
 public function testCanUseCustomRequestOptions()
 {
     $c = false;
     $f = new MessageFactory(['foo' => function (RequestInterface $request, $value) use(&$c) {
         $c = true;
         $this->assertEquals('bar', $value);
     }]);
     $f->createRequest('PUT', 'http://f.com', ['headers' => ['Content-Type' => 'foo'], 'foo' => 'bar']);
     $this->assertTrue($c);
 }