Passed
Pull Request — master (#45)
by Harry
12:00
created

MessageFactoryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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