ToRespondWithMimeTypeExpectationTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 64
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testExpectation() 0 8 1
A testExpectationWithAllowedHttpErrors() 0 8 1
A testExpectationFails() 0 5 1
A testExpectationWithInvalidUrl() 0 5 1
A testExpectationWithBadResponse() 0 5 1
A createExpectationWithMockedResponse() 0 13 1
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