GuzzleRequestTest::testPerform()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 64
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 64
rs 9.3956
cc 2
eloc 52
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
26
class GuzzleRequestTest extends \PHPUnit_Framework_TestCase
27
{
28
    public function testPerform()
29
    {
30
        $body = $this->prophesize('\Psr\Http\Message\StreamInterface');
31
        $body
32
            ->getContents()
33
            ->shouldBeCalledTimes(1)
34
            ->willReturn('ABC');
35
36
        $response = $this->prophesize('\Psr\Http\Message\ResponseInterface');
37
        $response
38
            ->getBody()
39
            ->shouldBeCalledTimes(1)
40
            ->willReturn($body->reveal());
41
42
        $client = $this->prophesize('\GuzzleHttp\ClientInterface');
43
        $client
44
            ->send(Argument::that(function ($request) {
45
                if (!$request instanceof RequestInterface) {
46
                    return false;
47
                }
48
49
                $uri = $request->getUri();
50
                $parts = [];
51
                parse_str($uri->getQuery(), $parts);
52
53
                $this->assertSame('webservices.amazon.de', $uri->getHost());
54
                $this->assertSame('/onca/xml', $uri->getPath());
55
                $this->assertArrayHasKey('AWSAccessKeyId', $parts);
56
                $this->assertSame('jkl', $parts['AWSAccessKeyId']);
57
                $this->assertArrayHasKey('AssociateTag', $parts);
58
                $this->assertSame('def', $parts['AssociateTag']);
59
                $this->assertArrayHasKey('ItemId', $parts);
60
                $this->assertSame('1', $parts['ItemId']);
61
                $this->assertArrayHasKey('Test', $parts);
62
                $this->assertSame('a,b', $parts['Test']);
63
                $this->assertArrayHasKey('Operation', $parts);
64
                $this->assertSame('ItemLookup', $parts['Operation']);
65
                $this->assertArrayHasKey('Service', $parts);
66
                $this->assertSame('AWSECommerceService', $parts['Service']);
67
                $this->assertArrayHasKey('Timestamp', $parts);
68
                $this->assertRegExp('#[0-9]{4}(-[0-9]{2}){2}T([0-9]{2}:){2}[0-9]{2}Z#', $parts['Timestamp']);
69
                $this->assertArrayHasKey('Version', $parts);
70
                $this->assertSame('2013-08-01', $parts['Version']);
71
                $this->assertArrayHasKey('Signature', $parts);
72
                return true;
73
            }))
74
            ->shouldBeCalledTimes(1)
75
            ->willReturn($response->reveal());
76
77
        $request = new GuzzleRequest($client->reveal());
78
79
        $operation  = new Lookup();
80
        $operation->setItemId('1');
81
        $operation->setTest(['a', 'b']);
82
83
        $config = new GenericConfiguration();
84
        $config->setAccessKey('abc');
85
        $config->setAssociateTag('def');
86
        $config->setCountry('DE');
87
        $config->setSecretKey('ghi');
88
        $config->setAccessKey('jkl');
89
90
        $request->perform($operation, $config);
91
    }
92
93
    public function testSchemeSwitch()
94
    {
95
        $body = $this->prophesize('\Psr\Http\Message\StreamInterface');
96
        $body
97
            ->getContents()
98
            ->shouldBeCalledTimes(1)
99
            ->willReturn('ABC');
100
101
        $response = $this->prophesize('\Psr\Http\Message\ResponseInterface');
102
        $response
103
            ->getBody()
104
            ->shouldBeCalledTimes(1)
105
            ->willReturn($body->reveal());
106
107
        $client = $this->prophesize('\GuzzleHttp\ClientInterface');
108
        $client
109
            ->send(Argument::that(function ($request) {
110
                if (!$request instanceof RequestInterface) {
111
                    return false;
112
                }
113
114
                $uri = $request->getUri();
115
116
                $this->assertSame('https', $uri->getScheme());
117
                return true;
118
            }))
119
            ->shouldBeCalledTimes(1)
120
            ->willReturn($response->reveal());
121
122
        $request = new GuzzleRequest($client->reveal());
123
        $request->setScheme('HTTPS');
124
125
        $operation  = new Lookup();
126
        $operation->setItemId('1');
127
128
        $config = new GenericConfiguration();
129
        $config->setAccessKey('abc');
130
        $config->setAssociateTag('def');
131
        $config->setCountry('DE');
132
        $config->setSecretKey('ghi');
133
        $config->setAccessKey('jkl');
134
135
        $request->perform($operation, $config);
136
    }
137
138
    /**
139
     * @expectedException \InvalidArgumentException
140
     */
141
    public function testSchemeSetterWithInvalidValue()
142
    {
143
        $client = $this->prophesize('\GuzzleHttp\ClientInterface');
144
145
        $request = new GuzzleRequest($client->reveal());
146
        $request->setScheme('ftp');
147
    }
148
}
149