-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathFnDispatcherTest.php
More file actions
81 lines (68 loc) · 2.06 KB
/
FnDispatcherTest.php
File metadata and controls
81 lines (68 loc) · 2.06 KB
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
<?php
namespace JmesPath\Tests;
use JmesPath\FnDispatcher;
class FnDispatcherTest extends \PHPUnit_Framework_TestCase
{
public function testConvertsToString()
{
$fn = new FnDispatcher();
$this->assertEquals('foo', $fn('to_string', ['foo']));
$this->assertEquals('1', $fn('to_string', [1]));
$this->assertEquals('["foo"]', $fn('to_string', [['foo']]));
$std = new \stdClass();
$std->foo = 'bar';
$this->assertEquals('{"foo":"bar"}', $fn('to_string', [$std]));
$this->assertEquals('foo', $fn('to_string', [new _TestStringClass()]));
$this->assertEquals('"foo"', $fn('to_string', [new _TestJsonStringClass()]));
}
public function testCustomFunctions()
{
$callable = new _TestCustomFunctionCallable();
$fn = new FnDispatcher();
$fn->registerCustomFn('double', [$callable, 'double']);
$fn->registerCustomFn('testSuffix', [$callable, 'testSuffix']);
$fn->registerCustomFn('testTypeValidation', [$callable, 'testTypeValidation'], [['number'], ['number']]);
$this->assertEquals(4, $fn('double', [2]));
$this->assertEquals('someStringTest', $fn('testSuffix', ['someString']));
// check type validation
try {
$this->assertEquals(2, $fn('testTypeValidation', [1, '1']));
} catch (\Exception $e) {
$this->assertInstanceOf('\RuntimeException', $e);
}
$this->assertEquals(4, $fn('testTypeValidation', [2, 2]));
}
}
class _TestStringClass
{
public function __toString()
{
return 'foo';
}
}
class _TestJsonStringClass implements \JsonSerializable
{
public function __toString()
{
return 'no!';
}
public function jsonSerialize()
{
return 'foo';
}
}
class _TestCustomFunctionCallable
{
public function double($args)
{
return $args[0] * 2;
}
public function testSuffix($args)
{
return $args[0].'Test';
}
public function testTypeValidation($args)
{
return $args[0] + $args[1];
}
}