GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#110)
by Jan
06:42
created

RequestTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 120
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testPerform() 0 61 2
B testSchemeSwitch() 0 44 2
A testSchemeSetterWithInvalidValue() 0 7 1
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\Rest\Request;
23
use Prophecy\Argument;
24
use Psr\Http\Message\RequestInterface;
25
26
class RequestTest 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('Operation', $parts);
62
                $this->assertSame('ItemLookup', $parts['Operation']);
63
                $this->assertArrayHasKey('Service', $parts);
64
                $this->assertSame('AWSECommerceService', $parts['Service']);
65
                $this->assertArrayHasKey('Timestamp', $parts);
66
                $this->assertRegExp('#[0-9]{4}(-[0-9]{2}){2}T([0-9]{2}:){2}[0-9]{2}Z#', $parts['Timestamp']);
67
                $this->assertArrayHasKey('Version', $parts);
68
                $this->assertSame('2011-08-01', $parts['Version']);
69
                $this->assertArrayHasKey('Signature', $parts);
70
                return true;
71
            }))
72
            ->shouldBeCalledTimes(1)
73
            ->willReturn($response->reveal());
74
75
        $request = new Request($client->reveal());
76
77
        $operation  = new Lookup();
78
        $operation->setItemId('1');
79
80
        $config = new GenericConfiguration();
81
        $config->setAccessKey('abc');
82
        $config->setAssociateTag('def');
83
        $config->setCountry('DE');
84
        $config->setSecretKey('ghi');
85
        $config->setAccessKey('jkl');
86
87
        $request->perform($operation, $config);
88
    }
89
90
    public function testSchemeSwitch()
91
    {
92
        $body = $this->prophesize('\Psr\Http\Message\StreamInterface');
93
        $body
94
            ->getContents()
95
            ->shouldBeCalledTimes(1)
96
            ->willReturn('ABC');
97
98
        $response = $this->prophesize('\Psr\Http\Message\ResponseInterface');
99
        $response
100
            ->getBody()
101
            ->shouldBeCalledTimes(1)
102
            ->willReturn($body->reveal());
103
104
        $client = $this->prophesize('\GuzzleHttp\ClientInterface');
105
        $client
106
            ->send(Argument::that(function ($request) {
107
                if (!$request instanceof RequestInterface) {
108
                    return false;
109
                }
110
111
                $uri = $request->getUri();
112
113
                $this->assertSame('https', $uri->getScheme());
114
                return true;
115
            }))
116
            ->shouldBeCalledTimes(1)
117
            ->willReturn($response->reveal());
118
119
        $request = new Request($client->reveal());
120
        $request->setScheme('HTTPS');
121
122
        $operation  = new Lookup();
123
        $operation->setItemId('1');
124
125
        $config = new GenericConfiguration();
126
        $config->setAccessKey('abc');
127
        $config->setAssociateTag('def');
128
        $config->setCountry('DE');
129
        $config->setSecretKey('ghi');
130
        $config->setAccessKey('jkl');
131
132
        $request->perform($operation, $config);
133
    }
134
135
    /**
136
     * @expectedException \InvalidArgumentException
137
     */
138
    public function testSchemeSetterWithInvalidValue()
139
    {
140
        $client = $this->prophesize('\GuzzleHttp\ClientInterface');
141
142
        $request = new Request($client->reveal());
143
        $request->setScheme('ftp');
144
    }
145
}
146