Skip to content

Commit ee6c675

Browse files
committed
Removes default exceptions, fixes interfaces
1 parent 0433afd commit ee6c675

4 files changed

+16
-319
lines changed

src/HttpAdapterException.php

Lines changed: 6 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111

1212
namespace Http\Adapter;
1313

14-
use Http\Adapter\Message\InternalRequestInterface;
15-
use Http\Adapter\Message\ResponseInterface;
14+
use Http\Adapter\Message\InternalRequest;
15+
use Psr\Http\Message\ResponseInterface;
1616

1717
/**
1818
* @author GeLo <[email protected]>
1919
*/
2020
class HttpAdapterException extends \Exception
2121
{
2222
/**
23-
* @var InternalRequestInterface|null
23+
* @var InternalRequest|null
2424
*/
2525
private $request;
2626

@@ -32,7 +32,7 @@ class HttpAdapterException extends \Exception
3232
/**
3333
* Returns the request
3434
*
35-
* @return InternalRequestInterface|null
35+
* @return InternalRequest|null
3636
*/
3737
public function getRequest()
3838
{
@@ -52,9 +52,9 @@ public function hasRequest()
5252
/**
5353
* Sets the request
5454
*
55-
* @param InternalRequestInterface|null $request
55+
* @param InternalRequest|null $request
5656
*/
57-
public function setRequest(InternalRequestInterface $request = null)
57+
public function setRequest(InternalRequest $request = null)
5858
{
5959
$this->request = $request;
6060
}
@@ -88,168 +88,4 @@ public function setResponse(ResponseInterface $response = null)
8888
{
8989
$this->response = $response;
9090
}
91-
92-
/**
93-
* Returns a "CANNOT FETCH URI" exception
94-
*
95-
* @param string $uri
96-
* @param string $adapter
97-
* @param string $error
98-
*
99-
* @return self
100-
*/
101-
public static function cannotFetchUri($uri, $adapter, $error)
102-
{
103-
return new self(sprintf(
104-
'An error occurred when fetching the URI "%s" with the adapter "%s" ("%s").',
105-
$uri,
106-
$adapter,
107-
$error
108-
));
109-
}
110-
111-
/**
112-
* Returns a "CANNOT LOAD COOKIE JAR" exception
113-
*
114-
* @param string $error
115-
*
116-
* @return self
117-
*/
118-
public static function cannotLoadCookieJar($error)
119-
{
120-
return new self(sprintf('An error occurred when loading the cookie jar ("%s").', $error));
121-
}
122-
123-
/**
124-
* Returns a "CANNOT SAVE COOKIE JAR" exception
125-
*
126-
* @param string $error
127-
*
128-
* @return self
129-
*/
130-
public static function cannotSaveCookieJar($error)
131-
{
132-
return new self(sprintf('An error occurred when saving the cookie jar ("%s").', $error));
133-
}
134-
135-
/**
136-
* Returns a "HTTP ADAPTER DOES NOT EXIST" exception
137-
*
138-
* @param string $name
139-
*
140-
* @return self
141-
*/
142-
public static function httpAdapterDoesNotExist($name)
143-
{
144-
return new self(sprintf('The http adapter "%s" does not exist.', $name));
145-
}
146-
147-
/**
148-
* Returns a "HTTP ADAPTER IS NOT USABLE" exception
149-
*
150-
* @param string $name
151-
*
152-
* @return self
153-
*/
154-
public static function httpAdapterIsNotUsable($name)
155-
{
156-
return new self(sprintf('The http adapter "%s" is not usable.', $name));
157-
}
158-
159-
/**
160-
* Returns a "HTTP ADAPTERS ARE NOT USABLE" exception
161-
*
162-
* @return self
163-
*/
164-
public static function httpAdaptersAreNotUsable()
165-
{
166-
return new self('No http adapters are usable.');
167-
}
168-
169-
/**
170-
* Returns a "HTTP ADAPTER MUST IMPLEMENT INTERFACE" exception
171-
*
172-
* @param string $class
173-
*
174-
* @return self
175-
*/
176-
public static function httpAdapterMustImplementInterface($class)
177-
{
178-
return new self(sprintf('The class "%s" must implement "Ivory\HttpAdapter\HttpAdapterInterface".', $class));
179-
}
180-
181-
/**
182-
* Returns a "DOES NOT SUPPORT SUB ADAPTER" exception
183-
*
184-
* @param string $adapter
185-
* @param string $subAdapter
186-
*
187-
* @return self
188-
*/
189-
public static function doesNotSupportSubAdapter($adapter, $subAdapter)
190-
{
191-
return new self(sprintf('The adapter "%s" does not support the sub-adapter "%s".', $adapter, $subAdapter));
192-
}
193-
194-
/**
195-
* Returns a "MAX REDIRECTS EXCEEDED" exception
196-
*
197-
* @param string $uri
198-
* @param integer $maxRedirects
199-
* @param string $adapter
200-
*
201-
* @return self
202-
*/
203-
public static function maxRedirectsExceeded($uri, $maxRedirects, $adapter)
204-
{
205-
return self::cannotFetchUri($uri, $adapter, sprintf('Max redirects exceeded (%d)', $maxRedirects));
206-
}
207-
208-
/**
209-
* Returns a "REQUEST IS NOT VALID" exception
210-
*
211-
* @param mixed $request
212-
*
213-
* @return self
214-
*/
215-
public static function requestIsNotValid($request)
216-
{
217-
return new self(sprintf(
218-
'The request must be a string, an array or implement "Psr\Http\Message\RequestInterface" ("%s" given).',
219-
is_object($request) ? get_class($request) : gettype($request)
220-
));
221-
}
222-
223-
/**
224-
* Returns a "STREAM IS NOT VALID" exception
225-
*
226-
* @param mixed $stream
227-
* @param string $wrapper
228-
* @param string $expected
229-
*
230-
* @return self
231-
*/
232-
public static function streamIsNotValid($stream, $wrapper, $expected)
233-
{
234-
return new self(sprintf(
235-
'The stream "%s" only accepts a "%s" (current: "%s").',
236-
$wrapper,
237-
$expected,
238-
is_object($stream) ? get_class($stream) : gettype($stream)
239-
));
240-
}
241-
242-
/**
243-
* Returns a "TIMEOUT EXCEEDED" exception
244-
*
245-
* @param string $uri
246-
* @param float $timeout
247-
* @param string $adapter
248-
*
249-
* @return self
250-
*/
251-
public static function timeoutExceeded($uri, $timeout, $adapter)
252-
{
253-
return self::cannotFetchUri($uri, $adapter, sprintf('Timeout exceeded (%.2f)', $timeout));
254-
}
25591
}

src/MultiHttpAdapterException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Http\Adapter;
1313

14-
use Http\Adapter\Message\ResponseInterface;
14+
use Psr\Http\Message\ResponseInterface;
1515

1616
/**
1717
* @author GeLo <[email protected]>

tests/HttpAdapterExceptionTest.php

Lines changed: 5 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Http\Adapter\Tests;
1313

1414
use Http\Adapter\HttpAdapterException;
15-
use Http\Adapter\Message\InternalRequestInterface;
16-
use Http\Adapter\Message\ResponseInterface;
15+
use Http\Adapter\Message\InternalRequest;
16+
use Psr\Http\Message\ResponseInterface;
1717

1818
/**
1919
* @author GeLo <[email protected]>
@@ -82,153 +82,14 @@ public function testResetResponse()
8282
$this->assertNull($this->exception->getRequest());
8383
}
8484

85-
public function testCannotFetchUri()
86-
{
87-
$exception = HttpAdapterException::cannotFetchUri('uri', 'adapter', 'error');
88-
89-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
90-
$this->assertSame(
91-
'An error occurred when fetching the URI "uri" with the adapter "adapter" ("error").',
92-
$exception->getMessage()
93-
);
94-
}
95-
96-
public function testCannotLoadCookieJar()
97-
{
98-
$exception = HttpAdapterException::cannotLoadCookieJar('error');
99-
100-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
101-
$this->assertSame('An error occurred when loading the cookie jar ("error").', $exception->getMessage());
102-
}
103-
104-
public function testCannotSaveCookieJar()
105-
{
106-
$exception = HttpAdapterException::cannotSaveCookieJar('error');
107-
108-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
109-
$this->assertSame('An error occurred when saving the cookie jar ("error").', $exception->getMessage());
110-
}
111-
112-
public function testHttpAdapterDoesNotExist()
113-
{
114-
$exception = HttpAdapterException::httpAdapterDoesNotExist('adapter');
115-
116-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
117-
$this->assertSame('The http adapter "adapter" does not exist.', $exception->getMessage());
118-
}
119-
120-
public function testHttpAdapterIsNotUsable()
121-
{
122-
$exception = HttpAdapterException::httpAdapterIsNotUsable('adapter');
123-
124-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
125-
$this->assertSame('The http adapter "adapter" is not usable.', $exception->getMessage());
126-
}
127-
128-
public function testHttpAdaptersAreNotUsable()
129-
{
130-
$exception = HttpAdapterException::httpAdaptersAreNotUsable();
131-
132-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
133-
$this->assertSame('No http adapters are usable.', $exception->getMessage());
134-
}
135-
136-
public function testHttpAdapterMustImplementInterface()
137-
{
138-
$exception = HttpAdapterException::httpAdapterMustImplementInterface('class');
139-
140-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
141-
$this->assertSame(
142-
'The class "class" must implement "Ivory\HttpAdapter\HttpAdapterInterface".',
143-
$exception->getMessage()
144-
);
145-
}
146-
147-
public function testDoesNotSupportSubAdapter()
148-
{
149-
$exception = HttpAdapterException::doesNotSupportSubAdapter('adapter', 'subAdapter');
150-
151-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
152-
$this->assertSame(
153-
'The adapter "adapter" does not support the sub-adapter "subAdapter".',
154-
$exception->getMessage()
155-
);
156-
}
157-
158-
public function testMaxRedirectsExceeded()
159-
{
160-
$exception = HttpAdapterException::maxRedirectsExceeded('uri', 5, 'adapter');
161-
162-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
163-
$this->assertSame(
164-
'An error occurred when fetching the URI "uri" with the adapter "adapter" ("Max redirects exceeded (5)").',
165-
$exception->getMessage()
166-
);
167-
}
168-
169-
public function testRequestIsNotValidWithObject()
170-
{
171-
$exception = HttpAdapterException::requestIsNotValid(new \stdClass());
172-
173-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
174-
$this->assertSame(
175-
'The request must be a string, an array or implement "Psr\Http\Message\RequestInterface" ("stdClass" given).',
176-
$exception->getMessage()
177-
);
178-
}
179-
180-
public function testRequestIsNotValidWithScalar()
181-
{
182-
$exception = HttpAdapterException::requestIsNotValid(true);
183-
184-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
185-
$this->assertSame(
186-
'The request must be a string, an array or implement "Psr\Http\Message\RequestInterface" ("boolean" given).',
187-
$exception->getMessage()
188-
);
189-
}
190-
191-
public function testStreamIsNotValidWithObject()
192-
{
193-
$exception = HttpAdapterException::streamIsNotValid(new \stdClass(), 'wrapper', 'expected');
194-
195-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
196-
$this->assertSame(
197-
'The stream "wrapper" only accepts a "expected" (current: "stdClass").',
198-
$exception->getMessage()
199-
);
200-
}
201-
202-
public function testStreamIsNotValidWithScalar()
203-
{
204-
$exception = HttpAdapterException::streamIsNotValid(true, 'wrapper', 'expected');
205-
206-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
207-
$this->assertSame(
208-
'The stream "wrapper" only accepts a "expected" (current: "boolean").',
209-
$exception->getMessage()
210-
);
211-
}
212-
213-
public function testTimeoutExceeded()
214-
{
215-
$exception = HttpAdapterException::timeoutExceeded('uri', 1.1, 'adapter');
216-
217-
$this->assertInstanceOf('Http\Adapter\HttpAdapterException', $exception);
218-
$this->assertSame(
219-
'An error occurred when fetching the URI "uri" with the adapter "adapter" ("Timeout exceeded (1.10)").',
220-
$exception->getMessage()
221-
);
222-
}
223-
22485
/**
22586
* Creates a request mock
22687
*
227-
* @return InternalRequestInterface|\PHPUnit_Framework_MockObject_MockObject
88+
* @return InternalRequest|\PHPUnit_Framework_MockObject_MockObject
22889
*/
22990
private function createRequestMock()
23091
{
231-
return $this->getMock('Http\Adapter\Message\InternalRequestInterface');
92+
return $this->getMock('Http\Adapter\Message\InternalRequest');
23293
}
23394

23495
/**
@@ -238,6 +99,6 @@ private function createRequestMock()
23899
*/
239100
private function createResponseMock()
240101
{
241-
return $this->getMock('Http\Adapter\Message\ResponseInterface');
102+
return $this->getMock('Psr\Http\Message\ResponseInterface');
242103
}
243104
}

0 commit comments

Comments
 (0)