File size: 4,795 Bytes
e4f4821 |
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
<?php
use JsonRPC\ProcedureHandler;
require_once __DIR__.'/../../../vendor/autoload.php';
class A
{
public function getAll($p1, $p2, $p3 = 4)
{
return $p1 + $p2 + $p3;
}
}
class B
{
public function getAll($p1)
{
return $p1 + 2;
}
}
class ClassWithBeforeMethod
{
private $foobar = '';
public function before($procedure)
{
$this->foobar = $procedure;
}
public function myProcedure()
{
return $this->foobar;
}
}
class ProcedureHandlerTest extends PHPUnit_Framework_TestCase
{
public function testProcedureNotFound()
{
$this->expectException('BadFunctionCallException');
$handler = new ProcedureHandler;
$handler->executeProcedure('a');
}
public function testCallbackNotFound()
{
$this->expectException('BadFunctionCallException');
$handler = new ProcedureHandler;
$handler->withCallback('b', function() {});
$handler->executeProcedure('a');
}
public function testClassNotFound()
{
$this->expectException('BadFunctionCallException');
$handler = new ProcedureHandler;
$handler->withClassAndMethod('getAllTasks', 'c', 'getAll');
$handler->executeProcedure('getAllTasks');
}
public function testMethodNotFound()
{
$this->expectException('BadFunctionCallException');
$handler = new ProcedureHandler;
$handler->withClassAndMethod('getAllTasks', 'A', 'getNothing');
$handler->executeProcedure('getAllTasks');
}
public function testIsPositionalArguments()
{
$handler = new ProcedureHandler;
$this->assertFalse($handler->isPositionalArguments(
array('a' => 'b', 'c' => 'd')
));
$handler = new ProcedureHandler;
$this->assertTrue($handler->isPositionalArguments(
array('a', 'b', 'c')
));
}
public function testBindNamedArguments()
{
$handler = new ProcedureHandler;
$handler->withClassAndMethod('getAllA', 'A', 'getAll');
$handler->withClassAndMethod('getAllB', 'B', 'getAll');
$handler->withClassAndMethod('getAllC', new B, 'getAll');
$this->assertEquals(6, $handler->executeProcedure('getAllA', array('p2' => 4, 'p1' => -2)));
$this->assertEquals(10, $handler->executeProcedure('getAllA', array('p2' => 4, 'p3' => 8, 'p1' => -2)));
$this->assertEquals(6, $handler->executeProcedure('getAllB', array('p1' => 4)));
$this->assertEquals(5, $handler->executeProcedure('getAllC', array('p1' => 3)));
}
public function testBindPositionalArguments()
{
$handler = new ProcedureHandler;
$handler->withClassAndMethod('getAllA', 'A', 'getAll');
$handler->withClassAndMethod('getAllB', 'B', 'getAll');
$this->assertEquals(6, $handler->executeProcedure('getAllA', array(4, -2)));
$this->assertEquals(2, $handler->executeProcedure('getAllA', array(4, 0, -2)));
$this->assertEquals(4, $handler->executeProcedure('getAllB', array(2)));
}
public function testRegisterNamedArguments()
{
$handler = new ProcedureHandler;
$handler->withCallback('getAllA', function($p1, $p2, $p3 = 4) {
return $p1 + $p2 + $p3;
});
$this->assertEquals(6, $handler->executeProcedure('getAllA', array('p2' => 4, 'p1' => -2)));
$this->assertEquals(10, $handler->executeProcedure('getAllA', array('p2' => 4, 'p3' => 8, 'p1' => -2)));
}
public function testRegisterPositionalArguments()
{
$handler = new ProcedureHandler;
$handler->withCallback('getAllA', function($p1, $p2, $p3 = 4) {
return $p1 + $p2 + $p3;
});
$this->assertEquals(6, $handler->executeProcedure('getAllA', array(4, -2)));
$this->assertEquals(2, $handler->executeProcedure('getAllA', array(4, 0, -2)));
}
public function testTooManyArguments()
{
$this->expectException('InvalidArgumentException');
$handler = new ProcedureHandler;
$handler->withClassAndMethod('getAllC', new B, 'getAll');
$handler->executeProcedure('getAllC', array('p1' => 3, 'p2' => 5));
}
public function testNotEnoughArguments()
{
$this->expectException('InvalidArgumentException');
$handler = new ProcedureHandler;
$handler->withClassAndMethod('getAllC', new B, 'getAll');
$handler->executeProcedure('getAllC');
}
public function testBeforeMethod()
{
$handler = new ProcedureHandler;
$handler->withObject(new ClassWithBeforeMethod);
$handler->withBeforeMethod('before');
$this->assertEquals('myProcedure', $handler->executeProcedure('myProcedure'));
}
}
|