ValidGigyaResponseMiddlewareTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 83
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidResponse() 0 12 1
A testUnknownResponseContainsTheOriginalResponse() 0 12 2
A testInvalidBody() 0 12 1
A testNoBodyWillFail() 0 12 1
A testMissingFieldWillThrowAnException() 0 12 1
1
<?php
2
/**
3
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Test\Unit\Validation;
15
16
use Graze\Gigya\Exception\UnknownResponseException;
17
use Graze\Gigya\Test\TestCase;
18
use Graze\Gigya\Test\TestFixtures;
19
use Graze\Gigya\Validation\ValidGigyaResponseMiddleware;
20
use GuzzleHttp\Handler\MockHandler;
21
use GuzzleHttp\Psr7\Request;
22
use GuzzleHttp\Psr7\Response;
23
24
class ValidGigyaResponseMiddlewareTest extends TestCase
25
{
26
    public function testValidResponse()
27
    {
28
        $handler = new MockHandler(
29
            [
30
                new Response(200, [], TestFixtures::getFixture('accounts.search_simple')),
31
            ]
32
        );
33
34
        $middleware = ValidGigyaResponseMiddleware::middleware();
35
36
        $func = $middleware($handler);
37
        $func(new Request('GET', 'https://foo.com'), [])->wait();
38
    }
39
40
    /**
41
     * @expectedException \Graze\Gigya\Exception\UnknownResponseException
42
     * @expectedExceptionMessage The contents of the response could not be determined. Missing required field:
43
     *                           'statusReason'
44
     */
45
    public function testMissingFieldWillThrowAnException()
46
    {
47
        $handler = new MockHandler(
48
            [
49
                new Response(200, [], TestFixtures::getFixture('missing_field')),
50
            ]
51
        );
52
53
        $middleware = ValidGigyaResponseMiddleware::middleware();
54
55
        $func = $middleware($handler);
56
        $func(new Request('GET', 'https://foo.com'), [])->wait();
57
    }
58
59
    /**
60
     * @expectedException \Graze\Gigya\Exception\UnknownResponseException
61
     * @expectedExceptionMessage The contents of the response could not be determined
62
     */
63
    public function testNoBodyWillFail()
64
    {
65
        $handler = new MockHandler(
66
            [
67
                new Response(200),
68
            ]
69
        );
70
71
        $middleware = ValidGigyaResponseMiddleware::middleware();
72
73
        $func = $middleware($handler);
74
        $func(new Request('GET', 'https://foo.com'), [])->wait();
75
    }
76
77
    /**
78
     * @expectedException \Graze\Gigya\Exception\UnknownResponseException
79
     * @expectedExceptionMessage The contents of the response could not be determined. Could not decode the body
80
     */
81
    public function testInvalidBody()
82
    {
83
        $handler = new MockHandler(
84
            [
85
                new Response(200, [], TestFixtures::getFixture('invalid_json')),
86
            ]
87
        );
88
89
        $middleware = ValidGigyaResponseMiddleware::middleware();
90
91
        $func = $middleware($handler);
92
        $func(new Request('GET', 'https://foo.com'), [])->wait();
93
    }
94
95
    public function testUnknownResponseContainsTheOriginalResponse()
96
    {
97
        $response = new Response(200, [], TestFixtures::getFixture('invalid_json'));
98
        $handler = new MockHandler([$response]);
99
100
        $middleware = ValidGigyaResponseMiddleware::middleware();
101
102
        $func = $middleware($handler);
103
        try {
104
            $func(new Request('GET', 'https://foo.com'), [])->wait();
105
        } catch (UnknownResponseException $e) {
106
            $this->assertSame($response, $e->getResponse());
107
        }
108
    }
109
}
110