1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overwatch\ServiceBundle\Tests\Expectation; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Handler\MockHandler; |
6
|
|
|
use GuzzleHttp\HandlerStack; |
7
|
|
|
use GuzzleHttp\Psr7\Response; |
8
|
|
|
use Overwatch\ServiceBundle\Expectation\ToRespondWithMimeTypeExpectation; |
9
|
|
|
|
10
|
|
|
class ToRespondWithMimeTypeExpectationTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
public function testExpectation() |
13
|
|
|
{ |
14
|
|
|
$expectation = $this->createExpectationWithMockedResponse('application/json'); |
15
|
|
|
|
16
|
|
|
$actualResult = $expectation->run('http://someapi.test/endpoint.json', 'application/json'); |
17
|
|
|
|
18
|
|
|
$this->assertEquals('Responded with mime type: "application/json"', $actualResult); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testExpectationWithAllowedHttpErrors() |
22
|
|
|
{ |
23
|
|
|
$expectation = $this->createExpectationWithMockedResponse('application/json', 500, true); |
24
|
|
|
|
25
|
|
|
$actualResult = $expectation->run('http://someapi.test/endpoint.json', 'application/json'); |
26
|
|
|
|
27
|
|
|
$this->assertEquals('Responded with mime type: "application/json"', $actualResult); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @expectedException Overwatch\ExpectationBundle\Exception\ExpectationFailedException |
33
|
|
|
* @expectedExceptionMessage Expected http://someapi.test/endpoint.json to respond with mime type "application/json", actually responded "text/html" |
34
|
|
|
*/ |
35
|
|
|
public function testExpectationFails() |
36
|
|
|
{ |
37
|
|
|
$this->createExpectationWithMockedResponse('text/html') |
38
|
|
|
->run('http://someapi.test/endpoint.json', 'application/json'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @expectedException \InvalidArgumentException |
43
|
|
|
* @expectedExceptionMessage The actual value provided is not a valid URL |
44
|
|
|
*/ |
45
|
|
|
public function testExpectationWithInvalidUrl() |
46
|
|
|
{ |
47
|
|
|
$this->createExpectationWithMockedResponse('text/html') |
48
|
|
|
->run('invalid', 'text/html'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @expectedException \GuzzleHttp\Exception\RequestException |
53
|
|
|
*/ |
54
|
|
|
public function testExpectationWithBadResponse() |
55
|
|
|
{ |
56
|
|
|
$this->createExpectationWithMockedResponse('text/html', 500) |
57
|
|
|
->run('http://someapi.test/endpoint.json', 'text/html'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function createExpectationWithMockedResponse($resultMimeType, $httpCode = 200, $httpErrors = false) |
61
|
|
|
{ |
62
|
|
|
$mock = new MockHandler([ |
63
|
|
|
new Response($httpCode, ['Content-Type' => $resultMimeType]), |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
$handler = HandlerStack::create($mock); |
67
|
|
|
|
68
|
|
|
return new ToRespondWithMimeTypeExpectation([ |
69
|
|
|
'allow_errors' => $httpErrors, |
70
|
|
|
'timeout' => 5 |
71
|
|
|
], ['handler' => $handler]); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|