|
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\ToRespondHttpExpectation; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* ToRespondHttpExpectationTest |
|
12
|
|
|
*/ |
|
13
|
|
|
class ToRespondHttpExpectationTest extends \PHPUnit_Framework_TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
public function testExpectation() |
|
16
|
|
|
{ |
|
17
|
|
|
$expectation = $this->createExpectationWithMockedResponse(200); |
|
18
|
|
|
|
|
19
|
|
|
$this->assertEquals('Responded HTTP 200 OK', $expectation->run('https://github.com/zsturgess/overwatch')); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function testExpectationWithGivenStatus() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->assertEquals('Responded HTTP 200 OK', $this->createExpectationWithMockedResponse(200)->run('https://github.com/zsturgess/overwatch', 200)); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testExpectationWithInvalidGivenStatus() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->assertEquals('Responded HTTP 200 OK', $this->createExpectationWithMockedResponse(200)->run('https://github.com/zsturgess/overwatch', 'LS')); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @expectedException \InvalidArgumentException |
|
34
|
|
|
* @expectedExceptionMessage The actual value provided is not a valid URL |
|
35
|
|
|
*/ |
|
36
|
|
|
public function testExpectationInvalidUrl() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->createExpectationWithMockedResponse(200)->run('8.8.8.8'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @expectedException Overwatch\ExpectationBundle\Exception\ExpectationFailedException |
|
43
|
|
|
* @expectedExceptionMessage Expected https://github.com/zsturgess/overwatch to respond HTTP 200, actually responded HTTP 404 |
|
44
|
|
|
*/ |
|
45
|
|
|
public function testExpectationFailsWithGivenStatus() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->createExpectationWithMockedResponse(404)->run('https://github.com/zsturgess/overwatch', 200); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @expectedException Overwatch\ExpectationBundle\Exception\ExpectationFailedException |
|
52
|
|
|
* @expectedExceptionMessage https://github.com/zsturgess/overwatch responded HTTP 404, which is configured as a failed result |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testExpectationFails() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->createExpectationWithMockedResponse(404)->run('https://github.com/zsturgess/overwatch'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @covers Overwatch\ServiceBundle\Expectation\ToRespondHttpExpectation |
|
61
|
|
|
* @expectedException Overwatch\ExpectationBundle\Exception\ExpectationFailedException |
|
62
|
|
|
* @expectedExceptionMessage https://github.com/zsturgess/overwatch responded HTTP 404, which is configured as a failed result |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testExpectationFailsWithInvalidGivenStatus() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->createExpectationWithMockedResponse(404)->run('https://github.com/zsturgess/overwatch', 'LS'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @expectedException Overwatch\ExpectationBundle\Exception\ExpectationFailedException |
|
71
|
|
|
* @expectedExceptionMessage Expected https://void.example.com/ to respond HTTP , actually failed to respond |
|
72
|
|
|
* @todo Find a way of mocking a response failure that doesn't rely on a hostname not existing! |
|
|
|
|
|
|
73
|
|
|
*/ |
|
74
|
|
|
public function testExpectationFailsOnNoResponse() |
|
75
|
|
|
{ |
|
76
|
|
|
$expectation = new ToRespondHttpExpectation([ |
|
77
|
|
|
'allowable_codes' => [200, 201, 204, 206, 304], |
|
78
|
|
|
'unsatisfactory_codes' => [301, 302, 307, 308], |
|
79
|
|
|
'timeout' => 5 |
|
80
|
|
|
]); |
|
81
|
|
|
|
|
82
|
|
|
$expectation->run('https://void.example.com/'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @expectedException Overwatch\ExpectationBundle\Exception\ExpectationUnsatisfactoryException |
|
87
|
|
|
* @expectedExceptionMessage https://github.com/zsturgess/overwatch responded HTTP 301, which is configured as unsatisfactory |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testExpectationUnsatsifactory() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->createExpectationWithMockedResponse(301)->run('https://github.com/zsturgess/overwatch'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function createExpectationWithMockedResponse($result) |
|
95
|
|
|
{ |
|
96
|
|
|
$mock = new MockHandler([ |
|
97
|
|
|
new Response($result, ['Content-Length' => 0]), |
|
98
|
|
|
]); |
|
99
|
|
|
|
|
100
|
|
|
$handler = HandlerStack::create($mock); |
|
101
|
|
|
|
|
102
|
|
|
return new ToRespondHttpExpectation([ |
|
103
|
|
|
'allowable_codes' => [200, 201, 204, 206, 304], |
|
104
|
|
|
'unsatisfactory_codes' => [301, 302, 307, 308], |
|
105
|
|
|
'timeout' => 5 |
|
106
|
|
|
], ['handler' => $handler]); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check looks
TODOcomments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.