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\ValidGigyaResponseSubscriber; |
20
|
|
|
use GuzzleHttp\Event\CompleteEvent; |
21
|
|
|
use GuzzleHttp\Event\RequestEvents; |
22
|
|
|
use GuzzleHttp\Event\SubscriberInterface; |
23
|
|
|
use Mockery as m; |
24
|
|
|
|
25
|
|
|
class ValidGigyaResponseSubscriberTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ValidGigyaResponseSubscriber |
29
|
|
|
*/ |
30
|
|
|
private $validator; |
31
|
|
|
|
32
|
|
|
public function setUp() |
33
|
|
|
{ |
34
|
|
|
$this->validator = new ValidGigyaResponseSubscriber(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function tearDown() |
38
|
|
|
{ |
39
|
|
|
$this->validator = null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testInstanceOf() |
43
|
|
|
{ |
44
|
|
|
static::assertInstanceOf(SubscriberInterface::class, $this->validator); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testGetEvents() |
48
|
|
|
{ |
49
|
|
|
static::assertEquals( |
50
|
|
|
['complete' => ['onComplete', RequestEvents::VERIFY_RESPONSE]], |
51
|
|
|
$this->validator->getEvents() |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws \Graze\Gigya\Exception\UnknownResponseException |
57
|
|
|
*/ |
58
|
|
|
public function testValidResponse() |
59
|
|
|
{ |
60
|
|
|
$completeEvent = m::mock(CompleteEvent::class); |
61
|
|
|
$response = m::mock('GuzzleHttp\Message\ResponseInterface'); |
62
|
|
|
$completeEvent->shouldReceive('getResponse') |
63
|
|
|
->andReturn($response); |
64
|
|
|
$response->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('accounts.search_simple')); |
65
|
|
|
|
66
|
|
|
$this->validator->onComplete($completeEvent, 'name'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testNullResponseWillThrowAnException() |
70
|
|
|
{ |
71
|
|
|
$completeEvent = m::mock(CompleteEvent::class); |
72
|
|
|
$completeEvent->shouldReceive('getResponse') |
73
|
|
|
->andReturn(null); |
74
|
|
|
|
75
|
|
|
static::setExpectedException( |
76
|
|
|
'Graze\Gigya\Exception\UnknownResponseException', |
77
|
|
|
'The contents of the response could not be determined. No response provided' |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$this->validator->onComplete($completeEvent, 'name'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function testMissingFieldWillThrowAnException() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$completeEvent = m::mock(CompleteEvent::class); |
86
|
|
|
$response = m::mock('GuzzleHttp\Message\ResponseInterface'); |
87
|
|
|
$completeEvent->shouldReceive('getResponse') |
88
|
|
|
->andReturn($response); |
89
|
|
|
$response->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('missing_field')); |
90
|
|
|
|
91
|
|
|
static::setExpectedException( |
92
|
|
|
'Graze\Gigya\Exception\UnknownResponseException', |
93
|
|
|
"The contents of the response could not be determined. Missing required field: 'statusReason'" |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$this->validator->onComplete($completeEvent, 'name'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testNoBodyWillFail() |
100
|
|
|
{ |
101
|
|
|
$completeEvent = m::mock(CompleteEvent::class); |
102
|
|
|
$response = m::mock('GuzzleHttp\Message\ResponseInterface'); |
103
|
|
|
$completeEvent->shouldReceive('getResponse') |
104
|
|
|
->andReturn($response); |
105
|
|
|
$response->shouldReceive('getBody')->andReturn(''); |
106
|
|
|
|
107
|
|
|
static::setExpectedException( |
108
|
|
|
'Graze\Gigya\Exception\UnknownResponseException', |
109
|
|
|
'The contents of the response could not be determined' |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$this->validator->onComplete($completeEvent, 'name'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
public function testInvalidBody() |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$completeEvent = m::mock(CompleteEvent::class); |
118
|
|
|
$response = m::mock('GuzzleHttp\Message\ResponseInterface'); |
119
|
|
|
$completeEvent->shouldReceive('getResponse') |
120
|
|
|
->andReturn($response); |
121
|
|
|
$response->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('invalid_json')); |
122
|
|
|
|
123
|
|
|
static::setExpectedException( |
124
|
|
|
'Graze\Gigya\Exception\UnknownResponseException', |
125
|
|
|
'The contents of the response could not be determined. Could not decode the body' |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$this->validator->onComplete($completeEvent, 'name'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testUnknownResponseContainsTheOriginalResponse() |
132
|
|
|
{ |
133
|
|
|
$completeEvent = m::mock(CompleteEvent::class); |
134
|
|
|
$response = m::mock('GuzzleHttp\Message\ResponseInterface'); |
135
|
|
|
$completeEvent->shouldReceive('getResponse') |
136
|
|
|
->andReturn($response); |
137
|
|
|
$response->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('invalid_json')); |
138
|
|
|
|
139
|
|
|
try { |
140
|
|
|
$this->validator->onComplete($completeEvent, 'name'); |
141
|
|
|
} catch (UnknownResponseException $e) { |
142
|
|
|
static::assertSame($response, $e->getResponse()); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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.