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
|
|
|
use Mockery as m; |
24
|
|
|
use Psr\Http\Message\RequestInterface; |
25
|
|
|
|
26
|
|
|
class ValidGigyaResponseMiddlewareTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
public function testValidResponse() |
29
|
|
|
{ |
30
|
|
|
$h = new MockHandler([ |
|
|
|
|
31
|
|
|
function (RequestInterface $request, array $options) { |
|
|
|
|
32
|
|
|
return new Response(200, [], TestFixtures::getFixture('accounts.search_simple')); |
33
|
|
|
}, |
34
|
|
|
]); |
35
|
|
|
|
36
|
|
|
$m = ValidGigyaResponseMiddleware::middleware(); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$f = $m($h); |
|
|
|
|
39
|
|
|
$f(new Request('GET', 'https://foo.com'), [])->wait(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function testMissingFieldWillThrowAnException() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$h = new MockHandler([ |
|
|
|
|
45
|
|
|
function (RequestInterface $request, array $options) { |
|
|
|
|
46
|
|
|
return new Response(200, [], TestFixtures::getFixture('missing_field')); |
47
|
|
|
}, |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
$this->expectException(UnknownResponseException::class); |
51
|
|
|
$this->expectExceptionMessage("The contents of the response could not be determined. Missing required field: 'statusReason'"); |
52
|
|
|
|
53
|
|
|
$m = ValidGigyaResponseMiddleware::middleware(); |
|
|
|
|
54
|
|
|
|
55
|
|
|
$f = $m($h); |
|
|
|
|
56
|
|
|
$f(new Request('GET', 'https://foo.com'), [])->wait(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function testNoBodyWillFail() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$h = new MockHandler([ |
|
|
|
|
62
|
|
|
function (RequestInterface $request, array $options) { |
|
|
|
|
63
|
|
|
return new Response(200); |
64
|
|
|
}, |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
$this->expectException(UnknownResponseException::class); |
68
|
|
|
$this->expectExceptionMessage('The contents of the response could not be determined'); |
69
|
|
|
|
70
|
|
|
$m = ValidGigyaResponseMiddleware::middleware(); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$f = $m($h); |
|
|
|
|
73
|
|
|
$f(new Request('GET', 'https://foo.com'), [])->wait(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function testInvalidBody() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$h = new MockHandler([ |
|
|
|
|
79
|
|
|
function (RequestInterface $request, array $options) { |
|
|
|
|
80
|
|
|
return new Response(200, [], TestFixtures::getFixture('invalid_json')); |
81
|
|
|
}, |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$this->expectException(UnknownResponseException::class); |
85
|
|
|
$this->expectExceptionMessage('The contents of the response could not be determined. Could not decode the body'); |
86
|
|
|
|
87
|
|
|
$m = ValidGigyaResponseMiddleware::middleware(); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$f = $m($h); |
|
|
|
|
90
|
|
|
$f(new Request('GET', 'https://foo.com'), [])->wait(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testUnknownResponseContainsTheOriginalResponse() |
94
|
|
|
{ |
95
|
|
|
$response = new Response(200, [], TestFixtures::getFixture('invalid_json')); |
96
|
|
|
$h = new MockHandler([ |
|
|
|
|
97
|
|
|
function (RequestInterface $request, array $options) use ($response) { |
|
|
|
|
98
|
|
|
return $response; |
99
|
|
|
}, |
100
|
|
|
]); |
101
|
|
|
|
102
|
|
|
$m = ValidGigyaResponseMiddleware::middleware(); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$f = $m($h); |
|
|
|
|
105
|
|
|
try { |
106
|
|
|
$f(new Request('GET', 'https://foo.com'), [])->wait(); |
107
|
|
|
} catch (UnknownResponseException $e) { |
108
|
|
|
$this->assertSame($response, $e->getResponse()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.