Completed
Pull Request — master (#30)
by Harry
24:34
created

RpcMiddlewareMiddlewareTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 36.36 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 20
loc 55
rs 10
c 1
b 0
f 0
wmc 6
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testApplyRequest() 0 4 1
A testApplyResponseThrowsClientException() 10 10 1
A testApplyResponseThrowsServerException() 10 10 1
A testApplyResponseNoError() 0 6 1
A testApplyResponseNoOption() 0 4 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
 * This file is part of Guzzle HTTP JSON-RPC
4
 *
5
 * Copyright (c) 2014 Nature Delivered Ltd. <http://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
 * @see  http://github.com/graze/guzzle-jsonrpc/blob/master/LICENSE
11
 * @link http://github.com/graze/guzzle-jsonrpc
12
 */
13
namespace Graze\GuzzleHttp\JsonRpc\Middleware;
14
15
use Graze\GuzzleHttp\JsonRpc\Test\UnitTestCase;
16
17
class RpcMiddlewareMiddlewareTest extends UnitTestCase
18
{
19
    public function setUp()
20
    {
21
        $this->request = $this->mockRequest();
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
        $this->response = $this->mockResponse();
0 ignored issues
show
Bug introduced by
The property response does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
24
        $this->middleware = new RpcErrorMiddleware();
0 ignored issues
show
Bug introduced by
The property middleware does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
    }
26
27
    public function testApplyRequest()
28
    {
29
        $this->assertSame($this->request, $this->middleware->applyRequest($this->request, []));
30
    }
31
32
    /**
33
     * @expectedException \Graze\GuzzleHttp\JsonRpc\Exception\ClientException
34
     */
35 View Code Duplication
    public function testApplyResponseThrowsClientException()
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...
36
    {
37
        $this->response->shouldReceive('getRpcErrorCode')->times(2)->withNoArgs()->andReturn(-32600);
38
        $this->request->shouldReceive('getRequestTarget')->once()->withNoArgs()->andReturn('http://foo');
39
        $this->request->shouldReceive('getRpcMethod')->once()->withNoArgs()->andReturn('foo');
40
        $this->response->shouldReceive('getRpcErrorMessage')->once()->withNoArgs()->andReturn('bar');
41
        $this->response->shouldReceive('getStatusCode')->once()->withNoArgs()->andReturn(200);
42
43
        $this->middleware->applyResponse($this->request, $this->response, ['rpc_error' => true]);
44
    }
45
46
    /**
47
     * @expectedException \Graze\GuzzleHttp\JsonRpc\Exception\ServerException
48
     */
49 View Code Duplication
    public function testApplyResponseThrowsServerException()
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...
50
    {
51
        $this->response->shouldReceive('getRpcErrorCode')->times(2)->withNoArgs()->andReturn(-32000);
52
        $this->request->shouldReceive('getRequestTarget')->once()->withNoArgs()->andReturn('http://foo');
53
        $this->request->shouldReceive('getRpcMethod')->once()->withNoArgs()->andReturn('foo');
54
        $this->response->shouldReceive('getRpcErrorMessage')->once()->withNoArgs()->andReturn('bar');
55
        $this->response->shouldReceive('getStatusCode')->once()->withNoArgs()->andReturn(200);
56
57
        $this->middleware->applyResponse($this->request, $this->response, ['rpc_error' => true]);
58
    }
59
60
    public function testApplyResponseNoError()
61
    {
62
        $this->response->shouldReceive('getRpcErrorCode')->once()->withNoArgs()->andReturn(null);
63
64
        $this->assertSame($this->response, $this->middleware->applyResponse($this->request, $this->response, ['rpc_error' => true]));
65
    }
66
67
    public function testApplyResponseNoOption()
68
    {
69
        $this->assertSame($this->response, $this->middleware->applyResponse($this->request, $this->response, []));
70
    }
71
}
72