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