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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
<?php
/**
* Copyright (C) 2015 Datto, Inc.
*
* This file is part of PHP JSON-RPC.
*
* PHP JSON-RPC is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, version 3,
* as published by the Free Software Foundation.
*
* PHP JSON-RPC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with PHP JSON-RPC. If not, see <http://www.gnu.org/licenses/>.
*
* @author Spencer Mortensen <smortensen@datto.com>
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL-3.0
* @copyright 2015 Datto, Inc.
*/
namespace Datto\JsonRpc\Tests;
use PHPUnit\Framework\TestCase;
use Datto\JsonRpc\Client;
use Datto\JsonRpc\Responses\ResultResponse;
use Datto\JsonRpc\Responses\ErrorResponse;
class ClientTest extends TestCase
{
public function testNotification()
{
$client = new Client();
$client->notify('subtract', array(3, 2));
$this->compare($client, '{"jsonrpc":"2.0","method":"subtract","params":[3,2]}');
}
public function testQuery()
{
$client = new Client();
$client->query(1, 'subtract', array(3, 2));
$this->compare($client, '{"jsonrpc":"2.0","id":1,"method":"subtract","params":[3,2]}');
}
public function testBatch()
{
$client = new Client();
$client->query(1, 'subtract', array(3, 2));
$client->notify('subtract', array(4, 3));
$this->compare($client, '[{"jsonrpc":"2.0","id":1,"method":"subtract","params":[3,2]},{"jsonrpc":"2.0","method":"subtract","params":[4,3]}]');
}
public function testEmpty()
{
$client = new Client();
$this->compare($client, null);
}
public function testReset()
{
$client = new Client();
$client->notify('subtract', array(3, 2));
$client->encode();
$this->compare($client, null);
}
public function testDecodeResult()
{
$reply = '{"jsonrpc":"2.0","result":2,"id":1}';
$client = new Client();
$actualOutput = $client->decode($reply);
$expectedOutput = [new ResultResponse(1, 2)];
$this->assertSameValues($expectedOutput, $actualOutput);
}
public function testDecodeError()
{
$reply = '{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"Method not found"}}';
$client = new Client();
$actualOutput = $client->decode($reply);
$expectedOutput = [new ErrorResponse(1, 'Method not found', -32601)];
$this->assertSameValues($expectedOutput, $actualOutput);
}
private function assertSameValues($expected, $actual)
{
$expectedPhp = var_export($expected, true);
$actualPhp = var_export($actual, true);
$this->assertSame($expectedPhp, $actualPhp);
}
private function compare(Client $client, $expectedJsonOutput)
{
$actualJsonOutput = $client->encode();
$expectedOutput = @json_decode($expectedJsonOutput, true);
$actualOutput = @json_decode($actualJsonOutput, true);
$this->assertEquals($expectedOutput, $actualOutput);
}
}
|