ClientTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 118
Duplicated Lines 47.46 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 56
loc 118
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getStub() 0 4 1
A jsonAcceptationProvider() 0 7 1
A testApiSecret() 0 7 1
A testApiKey() 0 7 1
A testValidPublicationId() 0 7 1
A testInvalidPublicationId() 0 6 1
A testSignRequest() 12 12 1
A testSignRequestWithEmptyApiKey() 12 12 1
A testBuildRequest() 17 17 2
A testApiGet() 15 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Stp\SndApi\Common\Test;
4
5
use GuzzleHttp\Client as GuzzleHttpClient;
6
use GuzzleHttp\Message\Response;
7
use PHPUnit_Framework_MockObject_MockObject;
8
use Stp\SndApi\Common\Client;
9
10
class ClientTest extends \PHPUnit_Framework_TestCase
11
{
12
    use InvokeInaccessibleMethodTrait;
13
14
    /**
15
     * @return Client|PHPUnit_Framework_MockObject_MockObject
16
     */
17
    private function getStub()
18
    {
19
        return $this->getMockForAbstractClass(Client::class, ['apikey', 'apisecret', 'sa']);
20
    }
21
22
    public function jsonAcceptationProvider()
23
    {
24
        return [
25
            [true],
26
            [false]
27
        ];
28
    }
29
30
    public function testApiSecret()
31
    {
32
        $stub = $this->getStub();
33
34
        $stub->setApiSecret('test');
0 ignored issues
show
Bug introduced by
The method setApiSecret does only exist in Stp\SndApi\Common\Client, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
35
        $this->assertEquals('test', $stub->getApiSecret());
0 ignored issues
show
Bug introduced by
The method getApiSecret does only exist in Stp\SndApi\Common\Client, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
36
    }
37
38
    public function testApiKey()
39
    {
40
        $stub = $this->getStub();
41
42
        $stub->setApiKey('test');
0 ignored issues
show
Bug introduced by
The method setApiKey does only exist in Stp\SndApi\Common\Client, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
43
        $this->assertEquals('test', $stub->getApiKey());
0 ignored issues
show
Bug introduced by
The method getApiKey does only exist in Stp\SndApi\Common\Client, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
44
    }
45
46
    public function testValidPublicationId()
47
    {
48
        $stub = $this->getStub();
49
50
        $stub->setPublicationId('sa');
0 ignored issues
show
Bug introduced by
The method setPublicationId does only exist in Stp\SndApi\Common\Client, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
51
        $this->assertEquals('sa', $stub->getPublicationId());
0 ignored issues
show
Bug introduced by
The method getPublicationId does only exist in Stp\SndApi\Common\Client, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
52
    }
53
54
    /**
55
     * @expectedException \Stp\SndApi\Common\Exception\InvalidPublicationIdException
56
     */
57
    public function testInvalidPublicationId()
58
    {
59
        $stub = $this->getStub();
60
61
        $stub->setPublicationId('invalidpublicationid');
0 ignored issues
show
Bug introduced by
The method setPublicationId does only exist in Stp\SndApi\Common\Client, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
62
    }
63
64 View Code Duplication
    public function testSignRequest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
65
    {
66
        $stub = $this->getStub();
67
68
        $client = new GuzzleHttpClient();
69
        $request = $client->createRequest('GET', 'http://www.example.org/');
70
71
        $this->invokeMethod($stub, 'signRequest', [$request]);
72
73
        $this->assertNotNull($request->getHeader('X-Snd-ApiSignature'));
74
        $this->assertNotNull($request->getHeader('X-Snd-ApiKey'));
75
    }
76
77 View Code Duplication
    public function testSignRequestWithEmptyApiKey()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
78
    {
79
        $stub = $this->getStub();
80
        $stub->setApiKey('');
0 ignored issues
show
Bug introduced by
The method setApiKey does only exist in Stp\SndApi\Common\Client, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
81
82
        $client = new GuzzleHttpClient();
83
        $request = $client->createRequest('GET', 'http://www.example.org/');
84
85
        $this->invokeMethod($stub, 'signRequest', [$request]);
86
87
        $this->assertEmpty($request->getHeader('X-Snd-ApiKey'));
88
    }
89
90
    /**
91
     * @dataProvider jsonAcceptationProvider
92
     * @param bool $isJsonAccepted
93
     */
94 View Code Duplication
    public function testBuildRequest($isJsonAccepted)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
95
    {
96
        $stub = $this->getStub();
97
98
        $client = new GuzzleHttpClient();
99
        $request = $client->createRequest('GET', 'http://www.example.org/');
100
101
        $this->invokeMethod($stub, 'buildRequest', [$request, $isJsonAccepted]);
102
103
        if ($isJsonAccepted) {
104
            $this->assertNotNull($request->getHeader('Accept'));
105
        } else {
106
            $this->assertEmpty($request->getHeader('Accept'));
107
        }
108
        $this->assertNotNull($request->getHeader('Accept-Charset'));
109
110
    }
111
112 View Code Duplication
    public function testApiGet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
113
    {
114
        $validResponse = new Response(200);
115
116
        $client = $this->getMock(GuzzleHttpClient::class, ['send']);
117
        $client->expects($this->once())
118
            ->method('send')
119
            ->willReturn($validResponse);
120
121
        $stub = $this->getStub();
122
        $stub->setClient($client);
0 ignored issues
show
Bug introduced by
The method setClient does only exist in Stp\SndApi\Common\Client, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
123
124
        $response = $this->invokeMethod($stub, 'apiGet', ['']);
125
        $this->assertEquals($validResponse, $response);
126
    }
127
}
128