testCreateResponseWithOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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