summaryrefslogtreecommitdiff
path: root/libs/jsonrpc/tests/Request/RequestBuilderTest.php
blob: ce9cf67426e06d39c0d723c90ca6e20318da8a06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

use JsonRPC\Request\RequestBuilder;

require_once __DIR__.'/../../../../vendor/autoload.php';

class RequestBuilderTest extends PHPUnit_Framework_TestCase
{
    public function testBuilder()
    {
        $payload = RequestBuilder::create()
            ->withId(123)
            ->withProcedure('foobar')
            ->withParams(array(1, 2, 3))
            ->build();

        $this->assertEquals('{"jsonrpc":"2.0","method":"foobar","id":123,"params":[1,2,3]}', $payload);
    }

    public function testBuilderWithoutParams()
    {
        $payload = RequestBuilder::create()
            ->withId(123)
            ->withProcedure('foobar')
            ->build();

        $this->assertEquals('{"jsonrpc":"2.0","method":"foobar","id":123}', $payload);
    }

    public function testBuilderWithoutId()
    {
        $payload = RequestBuilder::create()
            ->withProcedure('foobar')
            ->withParams(array(1, 2, 3))
            ->build();

        $result = json_decode($payload, true);
        $this->assertNotNull($result['id']);
    }

    public function testBuilderWithAdditionalRequestAttributes()
    {
        $payload = RequestBuilder::create()
            ->withProcedure('foobar')
            ->withParams(array(1, 2, 3))
            ->withRequestAttributes(array("some-attr" => 42))
            ->build();

        $result = json_decode($payload, true);
        $this->assertNotNull($result['some-attr']);
    }

}