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
|
|
|
|
25
|
|
|
class ValidGigyaResponseMiddlewareTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
public function testValidResponse() |
28
|
|
|
{ |
29
|
|
|
$handler = new MockHandler([ |
30
|
|
|
new Response(200, [], TestFixtures::getFixture('accounts.search_simple')), |
31
|
|
|
]); |
32
|
|
|
|
33
|
|
|
$middleware = ValidGigyaResponseMiddleware::middleware(); |
34
|
|
|
|
35
|
|
|
$func = $middleware($handler); |
36
|
|
|
$func(new Request('GET', 'https://foo.com'), [])->wait(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
public function testMissingFieldWillThrowAnException() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$handler = new MockHandler([ |
42
|
|
|
new Response(200, [], TestFixtures::getFixture('missing_field')), |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
$this->expectException(UnknownResponseException::class); |
46
|
|
|
$this->expectExceptionMessage("The contents of the response could not be determined. Missing required field: 'statusReason'"); |
47
|
|
|
|
48
|
|
|
$middleware = ValidGigyaResponseMiddleware::middleware(); |
49
|
|
|
|
50
|
|
|
$func = $middleware($handler); |
51
|
|
|
$func(new Request('GET', 'https://foo.com'), [])->wait(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function testNoBodyWillFail() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$handler = new MockHandler([ |
57
|
|
|
new Response(200), |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
$this->expectException(UnknownResponseException::class); |
61
|
|
|
$this->expectExceptionMessage('The contents of the response could not be determined'); |
62
|
|
|
|
63
|
|
|
$middleware = ValidGigyaResponseMiddleware::middleware(); |
64
|
|
|
|
65
|
|
|
$func = $middleware($handler); |
66
|
|
|
$func(new Request('GET', 'https://foo.com'), [])->wait(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function testInvalidBody() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$handler = new MockHandler([ |
72
|
|
|
new Response(200, [], TestFixtures::getFixture('invalid_json')), |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
$this->expectException(UnknownResponseException::class); |
76
|
|
|
$this->expectExceptionMessage('The contents of the response could not be determined. Could not decode the body'); |
77
|
|
|
|
78
|
|
|
$middleware = ValidGigyaResponseMiddleware::middleware(); |
79
|
|
|
|
80
|
|
|
$func = $middleware($handler); |
81
|
|
|
$func(new Request('GET', 'https://foo.com'), [])->wait(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testUnknownResponseContainsTheOriginalResponse() |
85
|
|
|
{ |
86
|
|
|
$response = new Response(200, [], TestFixtures::getFixture('invalid_json')); |
87
|
|
|
$handler = new MockHandler([$response]); |
88
|
|
|
|
89
|
|
|
$middleware = ValidGigyaResponseMiddleware::middleware(); |
90
|
|
|
|
91
|
|
|
$func = $middleware($handler); |
92
|
|
|
try { |
93
|
|
|
$func(new Request('GET', 'https://foo.com'), [])->wait(); |
94
|
|
|
} catch (UnknownResponseException $e) { |
95
|
|
|
$this->assertSame($response, $e->getResponse()); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.