1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2016 Jan Eichhorn <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ApaiIO\Test\Request\Rest; |
19
|
|
|
|
20
|
|
|
use ApaiIO\Configuration\GenericConfiguration; |
21
|
|
|
use ApaiIO\Operations\Lookup; |
22
|
|
|
use ApaiIO\Request\GuzzleRequest; |
23
|
|
|
use Prophecy\Argument; |
24
|
|
|
use Psr\Http\Message\RequestInterface; |
25
|
|
|
use PHPUnit\Framework\TestCase; |
26
|
|
|
|
27
|
|
|
class GuzzleRequestTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
public function testPerform() |
30
|
|
|
{ |
31
|
|
|
$body = $this->prophesize('\Psr\Http\Message\StreamInterface'); |
32
|
|
|
$body |
33
|
|
|
->getContents() |
34
|
|
|
->shouldBeCalledTimes(1) |
35
|
|
|
->willReturn('ABC'); |
36
|
|
|
|
37
|
|
|
$response = $this->prophesize('\Psr\Http\Message\ResponseInterface'); |
38
|
|
|
$response |
39
|
|
|
->getBody() |
40
|
|
|
->shouldBeCalledTimes(1) |
41
|
|
|
->willReturn($body->reveal()); |
42
|
|
|
|
43
|
|
|
$client = $this->prophesize('\GuzzleHttp\ClientInterface'); |
44
|
|
|
$client |
45
|
|
|
->send(Argument::that(function ($request) { |
46
|
|
|
if (!$request instanceof RequestInterface) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$uri = $request->getUri(); |
51
|
|
|
$parts = []; |
52
|
|
|
parse_str($uri->getQuery(), $parts); |
53
|
|
|
|
54
|
|
|
$this->assertSame('webservices.amazon.de', $uri->getHost()); |
55
|
|
|
$this->assertSame('/onca/xml', $uri->getPath()); |
56
|
|
|
$this->assertArrayHasKey('AWSAccessKeyId', $parts); |
57
|
|
|
$this->assertSame('jkl', $parts['AWSAccessKeyId']); |
58
|
|
|
$this->assertArrayHasKey('AssociateTag', $parts); |
59
|
|
|
$this->assertSame('def', $parts['AssociateTag']); |
60
|
|
|
$this->assertArrayHasKey('ItemId', $parts); |
61
|
|
|
$this->assertSame('1', $parts['ItemId']); |
62
|
|
|
$this->assertArrayHasKey('Test', $parts); |
63
|
|
|
$this->assertSame('a,b', $parts['Test']); |
64
|
|
|
$this->assertArrayHasKey('Operation', $parts); |
65
|
|
|
$this->assertSame('ItemLookup', $parts['Operation']); |
66
|
|
|
$this->assertArrayHasKey('Service', $parts); |
67
|
|
|
$this->assertSame('AWSECommerceService', $parts['Service']); |
68
|
|
|
$this->assertArrayHasKey('Timestamp', $parts); |
69
|
|
|
$this->assertRegExp('#[0-9]{4}(-[0-9]{2}){2}T([0-9]{2}:){2}[0-9]{2}Z#', $parts['Timestamp']); |
70
|
|
|
$this->assertArrayHasKey('Version', $parts); |
71
|
|
|
$this->assertSame('2013-08-01', $parts['Version']); |
72
|
|
|
$this->assertArrayHasKey('Signature', $parts); |
73
|
|
|
return true; |
74
|
|
|
})) |
75
|
|
|
->shouldBeCalledTimes(1) |
76
|
|
|
->willReturn($response->reveal()); |
77
|
|
|
|
78
|
|
|
$request = new GuzzleRequest($client->reveal()); |
79
|
|
|
|
80
|
|
|
$operation = new Lookup(); |
81
|
|
|
$operation->setItemId('1'); |
82
|
|
|
$operation->setTest(['a', 'b']); |
83
|
|
|
|
84
|
|
|
$config = new GenericConfiguration(); |
85
|
|
|
$config->setAccessKey('abc'); |
86
|
|
|
$config->setAssociateTag('def'); |
87
|
|
|
$config->setCountry('DE'); |
88
|
|
|
$config->setSecretKey('ghi'); |
89
|
|
|
$config->setAccessKey('jkl'); |
90
|
|
|
|
91
|
|
|
$request->perform($operation, $config); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testSchemeSwitch() |
95
|
|
|
{ |
96
|
|
|
$body = $this->prophesize('\Psr\Http\Message\StreamInterface'); |
97
|
|
|
$body |
98
|
|
|
->getContents() |
99
|
|
|
->shouldBeCalledTimes(1) |
100
|
|
|
->willReturn('ABC'); |
101
|
|
|
|
102
|
|
|
$response = $this->prophesize('\Psr\Http\Message\ResponseInterface'); |
103
|
|
|
$response |
104
|
|
|
->getBody() |
105
|
|
|
->shouldBeCalledTimes(1) |
106
|
|
|
->willReturn($body->reveal()); |
107
|
|
|
|
108
|
|
|
$client = $this->prophesize('\GuzzleHttp\ClientInterface'); |
109
|
|
|
$client |
110
|
|
|
->send(Argument::that(function ($request) { |
111
|
|
|
if (!$request instanceof RequestInterface) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$uri = $request->getUri(); |
116
|
|
|
|
117
|
|
|
$this->assertSame('https', $uri->getScheme()); |
118
|
|
|
return true; |
119
|
|
|
})) |
120
|
|
|
->shouldBeCalledTimes(1) |
121
|
|
|
->willReturn($response->reveal()); |
122
|
|
|
|
123
|
|
|
$request = new GuzzleRequest($client->reveal()); |
124
|
|
|
$request->setScheme('HTTPS'); |
125
|
|
|
|
126
|
|
|
$operation = new Lookup(); |
127
|
|
|
$operation->setItemId('1'); |
128
|
|
|
|
129
|
|
|
$config = new GenericConfiguration(); |
130
|
|
|
$config->setAccessKey('abc'); |
131
|
|
|
$config->setAssociateTag('def'); |
132
|
|
|
$config->setCountry('DE'); |
133
|
|
|
$config->setSecretKey('ghi'); |
134
|
|
|
$config->setAccessKey('jkl'); |
135
|
|
|
|
136
|
|
|
$request->perform($operation, $config); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @expectedException \InvalidArgumentException |
141
|
|
|
*/ |
142
|
|
|
public function testSchemeSetterWithInvalidValue() |
143
|
|
|
{ |
144
|
|
|
$client = $this->prophesize('\GuzzleHttp\ClientInterface'); |
145
|
|
|
|
146
|
|
|
$request = new GuzzleRequest($client->reveal()); |
147
|
|
|
$request->setScheme('ftp'); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|