Completed
Pull Request — master (#30)
by Harry
02:53 queued 01:19
created

RpcMiddlewareMiddlewareTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 43.14 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 22
loc 51
rs 10
c 0
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() 11 11 1
A testApplyResponseThrowsServerException() 11 11 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 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...
33
    {
34
        $this->response->shouldReceive('getRpcErrorCode')->times(2)->withNoArgs()->andReturn(-32600);
35
        $this->request->shouldReceive('getRequestTarget')->once()->withNoArgs()->andReturn('http://foo');
36
        $this->request->shouldReceive('getRpcMethod')->once()->withNoArgs()->andReturn('foo');
37
        $this->response->shouldReceive('getRpcErrorMessage')->once()->withNoArgs()->andReturn('bar');
38
        $this->response->shouldReceive('getStatusCode')->once()->withNoArgs()->andReturn(200);
39
40
        $this->setExpectedException('Graze\GuzzleHttp\JsonRpc\Exception\ClientException');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
41
        $this->middleware->applyResponse($this->request, $this->response, ['rpc_error' => true]);
42
    }
43
44 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...
45
    {
46
        $this->response->shouldReceive('getRpcErrorCode')->times(2)->withNoArgs()->andReturn(-32000);
47
        $this->request->shouldReceive('getRequestTarget')->once()->withNoArgs()->andReturn('http://foo');
48
        $this->request->shouldReceive('getRpcMethod')->once()->withNoArgs()->andReturn('foo');
49
        $this->response->shouldReceive('getRpcErrorMessage')->once()->withNoArgs()->andReturn('bar');
50
        $this->response->shouldReceive('getStatusCode')->once()->withNoArgs()->andReturn(200);
51
52
        $this->setExpectedException('Graze\GuzzleHttp\JsonRpc\Exception\ServerException');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
53
        $this->middleware->applyResponse($this->request, $this->response, ['rpc_error' => true]);
54
    }
55
56
    public function testApplyResponseNoError()
57
    {
58
        $this->response->shouldReceive('getRpcErrorCode')->once()->withNoArgs()->andReturn(null);
59
60
        $this->assertSame($this->response, $this->middleware->applyResponse($this->request, $this->response, ['rpc_error' => true]));
61
    }
62
63
    public function testApplyResponseNoOption()
64
    {
65
        $this->assertSame($this->response, $this->middleware->applyResponse($this->request, $this->response, []));
66
    }
67
}
68