public function testAddsNonRequestExceptions()
 {
     $e = new MultiTransferException();
     $e->add(new \Exception('bar'));
     $e->addFailedRequestWithException(new Request('GET', 'http://www.foo.com'), new \Exception('foo'));
     $ce = CommandTransferException::fromMultiTransferException($e);
     $this->assertEquals(2, count($ce));
 }
 public function testAssociatesExceptionsWithRequests()
 {
     $r1 = new Request('GET', 'http://www.foo.com');
     $re1 = new \Exception('foo');
     $re2 = new \Exception('bar');
     $e = new MultiTransferException();
     $e->add($re2);
     $e->addFailedRequestWithException($r1, $re1);
     $this->assertSame($re1, $e->getExceptionForFailedRequest($r1));
     $this->assertNull($e->getExceptionForFailedRequest(new Request('POST', '/foo')));
 }
 public function testConvertsMultiExceptionIntoCommandTransfer()
 {
     $r1 = new Request('GET', 'http://foo.com');
     $r2 = new Request('GET', 'http://foobaz.com');
     $e = new MultiTransferException('Test', 123);
     $e->addSuccessfulRequest($r1)->addFailedRequest($r2);
     $ce = CommandTransferException::fromMultiTransferException($e);
     $this->assertInstanceOf('Guzzle\\Service\\Exception\\CommandTransferException', $ce);
     $this->assertEquals('Test', $ce->getMessage());
     $this->assertEquals(123, $ce->getCode());
     $this->assertSame(array($r1), $ce->getSuccessfulRequests());
     $this->assertSame(array($r2), $ce->getFailedRequests());
 }
Example #4
0
 protected function throwMultiException(array $exceptions, array $successful)
 {
     $multiException = new MultiTransferException('Errors during multi transfer');
     while ($e = array_shift($exceptions)) {
         $multiException->addFailedRequestWithException($e['request'], $e['exception']);
     }
     foreach ($successful as $request) {
         if (!$multiException->containsRequest($request)) {
             $multiException->addSuccessfulRequest($request);
         }
     }
     throw $multiException;
 }
 public static function fromMultiTransferException(MultiTransferException $e)
 {
     $ce = new self($e->getMessage(), $e->getCode(), $e->getPrevious());
     $ce->setSuccessfulRequests($e->getSuccessfulRequests());
     $alreadyAddedExceptions = array();
     foreach ($e->getFailedRequests() as $request) {
         if ($re = $e->getExceptionForFailedRequest($request)) {
             $alreadyAddedExceptions[] = $re;
             $ce->addFailedRequestWithException($request, $re);
         } else {
             $ce->addFailedRequest($request);
         }
     }
     if (count($alreadyAddedExceptions) < count($e)) {
         foreach ($e as $ex) {
             if (!in_array($ex, $alreadyAddedExceptions)) {
                 $ce->add($ex);
             }
         }
     }
     return $ce;
 }
Example #6
0
    /**
     * @dataProvider curlExceptionProvider
     *
     * @param \Exception $exception The exception that curl should throw.
     * @param string     $type      The returned exception class to be expected.
     */
    public function testExceptions(\Exception $exception, $type)
    {
        // the guzzle mock plugin does not allow arbitrary exceptions
        // mockery does not provide all methods of the interface
        $collection = new MultiTransferException();
        $collection->setExceptions(array($exception));
        $client = $this->getMock('\Guzzle\Http\ClientInterface');
        $client->expects($this->any())
            ->method('createRequest')
            ->willReturn(new Request('GET', '/'))
        ;
        $client->expects($this->once())
            ->method('send')
            ->willThrowException($collection)
        ;

        $varnish = new Varnish(array('http://127.0.0.1:123'), 'my_hostname.dev', $client);

        $varnish->ban(array());
        try {
            $varnish->flush();
            $this->fail('Should have aborted with an exception');
        } catch (ExceptionCollection $exceptions) {
            $this->assertCount(1, $exceptions);
            $this->assertInstanceOf($type, $exceptions->getFirst());
        }
    }
Example #7
0
 /**
  * Build a MultiTransferException if needed
  *
  * @param array $requestsInScope All requests in the previous scope
  *
  * @return MultiTransferException|null
  */
 protected function buildMultiTransferException(array $requestsInScope)
 {
     if (empty($this->exceptions)) {
         return null;
     }
     // Keep a list of all requests, and remove errored requests from the list
     $store = new \SplObjectStorage();
     foreach ($requestsInScope as $request) {
         $store->attach($request);
     }
     $multiException = new MultiTransferException('Errors during multi transfer');
     while ($e = array_shift($this->exceptions)) {
         $multiException->add($e['exception']);
         if (isset($e['request'])) {
             $multiException->addFailedRequest($e['request']);
             // Remove from the total list so that it becomes a list of successful requests
             unset($store[$e['request']]);
         }
     }
     // Add successful requests
     foreach ($store as $request) {
         $multiException->addSuccessfulRequest($request);
     }
     return $multiException;
 }
 /**
  * Creates a new CommandTransferException from a MultiTransferException
  *
  * @param MultiTransferException $e Exception to base a new exception on
  *
  * @return self
  */
 public static function fromMultiTransferException(MultiTransferException $e)
 {
     $ce = new self($e->getMessage(), $e->getCode(), $e->getPrevious());
     return $ce->setExceptions($e->getIterator()->getArrayCopy())->setSuccessfulRequests($e->getSuccessfulRequests())->setFailedRequests($e->getFailedRequests());
 }