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\Message; |
14
|
|
|
|
15
|
|
|
use Graze\GuzzleHttp\JsonRpc\ClientInterface; |
16
|
|
|
use Graze\GuzzleHttp\JsonRpc\Test\UnitTestCase; |
17
|
|
|
|
18
|
|
|
class MessageFactoryTest extends UnitTestCase |
19
|
|
|
{ |
20
|
|
|
public function setup() |
21
|
|
|
{ |
22
|
|
|
$this->factory = new MessageFactory(); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testInterface() |
26
|
|
|
{ |
27
|
|
|
$this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\Message\MessageFactoryInterface', $this->factory); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testCreateRequest() |
31
|
|
|
{ |
32
|
|
|
$method = RequestInterface::REQUEST; |
33
|
|
|
$uri = 'http://bar'; |
34
|
|
|
$options = [ |
35
|
|
|
'method' => 'baz' |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
$request = $this->factory->createRequest($method, $uri, [], $options); |
39
|
|
|
$this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\Message\RequestInterface', $request); |
40
|
|
|
$this->assertEquals('POST', $request->getMethod()); |
41
|
|
|
$this->assertEquals('http://bar', (string) $request->getUri()); |
42
|
|
|
$this->assertEquals('baz', $request->getRpcMethod()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testCreateResponse() |
46
|
|
|
{ |
47
|
|
|
$status = 200; |
48
|
|
|
$headers = ['Content-Type'=>'application/json']; |
49
|
|
|
|
50
|
|
|
$response = $this->factory->createResponse($status, $headers); |
51
|
|
|
$this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\Message\ResponseInterface', $response); |
52
|
|
|
$this->assertEquals($status, $response->getStatusCode()); |
53
|
|
|
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type')); |
54
|
|
|
$this->assertEquals('[]', (string) $response->getBody()); |
55
|
|
|
$this->assertEquals(null, $response->getRpcVersion()); |
56
|
|
|
$this->assertEquals(null, $response->getRpcResult()); |
57
|
|
|
$this->assertEquals(null, $response->getRpcId()); |
58
|
|
|
$this->assertEquals(null, $response->getRpcErrorCode()); |
59
|
|
|
$this->assertEquals(null, $response->getRpcErrorMessage()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testCreateResponseWithOptions() |
63
|
|
|
{ |
64
|
|
|
$status = 200; |
65
|
|
|
$headers = ['Content-Type'=>'application/json']; |
66
|
|
|
$options = [ |
67
|
|
|
'jsonrpc' => ClientInterface::SPEC, |
68
|
|
|
'result' => 'foo', |
69
|
|
|
'id' => 123 |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$response = $this->factory->createResponse($status, $headers, $options); |
73
|
|
|
$this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\Message\ResponseInterface', $response); |
74
|
|
|
$this->assertEquals($status, $response->getStatusCode()); |
75
|
|
|
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type')); |
76
|
|
|
$this->assertEquals(json_encode($options), (string) $response->getBody()); |
77
|
|
|
$this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion()); |
78
|
|
|
$this->assertEquals('foo', $response->getRpcResult()); |
79
|
|
|
$this->assertEquals(123, $response->getRpcId()); |
80
|
|
|
$this->assertEquals(null, $response->getRpcErrorCode()); |
81
|
|
|
$this->assertEquals(null, $response->getRpcErrorMessage()); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
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: