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

RequestExceptionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 37.93 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 22
loc 58
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A dataCreateClientException() 0 4 1
A dataCreateServerException() 0 4 1
A testCreateClientException() 11 11 1
A testCreateServerException() 11 11 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
14
namespace Graze\GuzzleHttp\JsonRpc\Exception;
15
16
use Graze\GuzzleHttp\JsonRpc\Test\UnitTestCase;
17
18
class RequestExceptionTest extends UnitTestCase
19
{
20
    public function setUp()
21
    {
22
        $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...
23
        $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...
24
    }
25
26
    /**
27
     * @return array
28
     */
29
    public function dataCreateClientException()
30
    {
31
        return [[-32600], [-32601], [-32602], [-32700]];
32
    }
33
34
    /**
35
     * @return array
36
     */
37
    public function dataCreateServerException()
38
    {
39
        return [[-32603], [-32000], [-32099], [-10000]];
40
    }
41
42
    /**
43
     * @dataProvider dataCreateClientException
44
     *
45
     * @param int $code
46
     */
47 View Code Duplication
    public function testCreateClientException($code)
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...
48
    {
49
        $this->request->shouldReceive('getRequestTarget')->once()->withNoArgs()->andReturn('http://foo');
50
        $this->request->shouldReceive('getRpcMethod')->once()->withNoArgs()->andReturn('foo');
51
        $this->response->shouldReceive('getRpcErrorCode')->once()->withNoArgs()->andReturn($code);
52
        $this->response->shouldReceive('getRpcErrorMessage')->once()->withNoArgs()->andReturn('bar');
53
        $this->response->shouldReceive('getStatusCode')->once()->withNoArgs()->andReturn(200);
54
55
        $exception = RequestException::create($this->request, $this->response);
56
        $this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\Exception\ClientException', $exception);
57
    }
58
59
    /**
60
     * @dataProvider dataCreateServerException
61
     *
62
     * @param int $code
63
     */
64 View Code Duplication
    public function testCreateServerException($code)
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
        $this->request->shouldReceive('getRequestTarget')->once()->withNoArgs()->andReturn('http://foo');
67
        $this->request->shouldReceive('getRpcMethod')->once()->withNoArgs()->andReturn('foo');
68
        $this->response->shouldReceive('getRpcErrorCode')->once()->withNoArgs()->andReturn($code);
69
        $this->response->shouldReceive('getRpcErrorMessage')->once()->withNoArgs()->andReturn('bar');
70
        $this->response->shouldReceive('getStatusCode')->once()->withNoArgs()->andReturn(200);
71
72
        $exception = RequestException::create($this->request, $this->response);
73
        $this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\Exception\ServerException', $exception);
74
    }
75
}
76