|
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(); |
|
|
|
|
|
|
22
|
|
|
$this->response = $this->mockResponse(); |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
$this->middleware = new RpcErrorMiddleware(); |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: