1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GuillermoandraeTest\Highrise\Http; |
4
|
|
|
|
5
|
|
|
use Guillermoandrae\Highrise\Http\AdapterInterface; |
6
|
|
|
use Guillermoandrae\Highrise\Http\GuzzleAdapter; |
7
|
|
|
use Guillermoandrae\Highrise\Http\RequestException; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\Handler\MockHandler; |
10
|
|
|
use GuzzleHttp\HandlerStack; |
11
|
|
|
use GuzzleHttp\Psr7\Response; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class GuzzleAdapterTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var AdapterInterface |
18
|
|
|
*/ |
19
|
|
|
private $adapter; |
20
|
|
|
|
21
|
|
|
public function testRequestWithBody() |
22
|
|
|
{ |
23
|
|
|
$body = '<test><name>test</name></test>'; |
24
|
|
|
$this->assertRequest(200, 'GET', [], $body); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testRequestWithoutBody() |
28
|
|
|
{ |
29
|
|
|
$this->assertRequest(201, 'DELETE'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testInvalidRequestWithoutBody() |
33
|
|
|
{ |
34
|
|
|
$this->expectException(RequestException::class); |
35
|
|
|
$this->assertRequest(503, 'GET'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetClient() |
39
|
|
|
{ |
40
|
|
|
$this->assertInstanceOf(Client::class, $this->adapter->getClient()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function setUp() |
44
|
|
|
{ |
45
|
|
|
$this->adapter = new GuzzleAdapter('test', '123456'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function assertRequest($httpStatusCode, string $method, array $options = [], $body = '') |
49
|
|
|
{ |
50
|
|
|
$uri = '/test.xml'; |
51
|
|
|
$mock = new MockHandler([ |
52
|
|
|
new Response($httpStatusCode, [], $body) |
53
|
|
|
]); |
54
|
|
|
$handler = HandlerStack::create($mock); |
55
|
|
|
$this->adapter->setClient(new Client(['handler' => $handler])); |
56
|
|
|
$this->adapter->request($method, $uri, $options); |
57
|
|
|
$this->assertSame($uri, $this->adapter->getLastRequest()->getUri()->getPath()); |
58
|
|
|
$this->assertSame($body, (string) $this->adapter->getLastResponse()->getBody()); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|