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
|
|
View Code Duplication |
public function testApplyResponseThrowsClientException() |
|
|
|
|
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'); |
|
|
|
|
41
|
|
|
$this->middleware->applyResponse($this->request, $this->response, ['rpc_error' => true]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function testApplyResponseThrowsServerException() |
|
|
|
|
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'); |
|
|
|
|
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
|
|
|
|
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: