|
| 1 | +--TEST-- |
| 2 | +registerPHPFunctions() with callables |
| 3 | +--EXTENSIONS-- |
| 4 | +dom |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | + |
| 8 | +class MyClass { |
| 9 | + public static function dump(string $var) { |
| 10 | + var_dump($var); |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +class MyDOMXPath extends DOMXPath { |
| 15 | + public function registerCycle() { |
| 16 | + $this->registerPhpFunctions(["cycle" => array($this, "dummy")]); |
| 17 | + } |
| 18 | + |
| 19 | + public function dummy(string $var) { |
| 20 | + echo "dummy: $var\n"; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +$doc = new DOMDocument(); |
| 25 | +$doc->loadHTML('<a href="https://php.net">hello</a>'); |
| 26 | + |
| 27 | +echo "--- Legit cases: none ---\n"; |
| 28 | + |
| 29 | +$xpath = new DOMXPath($doc); |
| 30 | +$xpath->registerNamespace("php", "http://php.net/xpath"); |
| 31 | +try { |
| 32 | + $xpath->evaluate("//a[php:function('var_dump', string(@href))]"); |
| 33 | +} catch (Error $e) { |
| 34 | + echo $e->getMessage(), "\n"; |
| 35 | +} |
| 36 | + |
| 37 | +echo "--- Legit cases: all ---\n"; |
| 38 | + |
| 39 | +$xpath->registerPHPFunctions(null); |
| 40 | +$xpath->evaluate("//a[php:function('var_dump', string(@href))]"); |
| 41 | +$xpath->evaluate("//a[php:function('MyClass::dump', string(@href))]"); |
| 42 | + |
| 43 | +echo "--- Legit cases: set ---\n"; |
| 44 | + |
| 45 | +$xpath = new DOMXPath($doc); |
| 46 | +$xpath->registerNamespace("php", "http://php.net/xpath"); |
| 47 | +$xpath->registerPhpFunctions([]); |
| 48 | +$xpath->registerPHPFunctions(["xyz" => MyClass::dump(...), "mydump" => function (string $x) { |
| 49 | + var_dump($x); |
| 50 | +}]); |
| 51 | +$xpath->registerPhpFunctions(str_repeat("var_dump", mt_rand(1, 1) /* defeat SCCP */)); |
| 52 | +$xpath->evaluate("//a[php:function('mydump', string(@href))]"); |
| 53 | +$xpath->evaluate("//a[php:function('xyz', string(@href))]"); |
| 54 | +$xpath->evaluate("//a[php:function('var_dump', string(@href))]"); |
| 55 | +try { |
| 56 | + $xpath->evaluate("//a[php:function('notinset', string(@href))]"); |
| 57 | +} catch (Throwable $e) { |
| 58 | + echo $e->getMessage(), "\n"; |
| 59 | +} |
| 60 | + |
| 61 | +echo "--- Legit cases: set with cycle ---\n"; |
| 62 | + |
| 63 | +$xpath = new MyDOMXPath($doc); |
| 64 | +$xpath->registerNamespace("php", "http://php.net/xpath"); |
| 65 | +$xpath->registerCycle(); |
| 66 | +$xpath->evaluate("//a[php:function('cycle', string(@href))]"); |
| 67 | + |
| 68 | +echo "--- Error cases ---\n"; |
| 69 | + |
| 70 | +$xpath = new DOMXPath($doc); |
| 71 | +try { |
| 72 | + $xpath->registerPhpFunctions("nonexistent"); |
| 73 | +} catch (TypeError $e) { |
| 74 | + echo $e->getMessage(), "\n"; |
| 75 | +} |
| 76 | + |
| 77 | +try { |
| 78 | + $xpath->registerPhpFunctions(function () {}); |
| 79 | +} catch (TypeError $e) { |
| 80 | + echo $e->getMessage(), "\n"; |
| 81 | +} |
| 82 | + |
| 83 | +try { |
| 84 | + $xpath->registerPhpFunctions([function () {}]); |
| 85 | +} catch (Throwable $e) { |
| 86 | + echo $e->getMessage(), "\n"; |
| 87 | +} |
| 88 | + |
| 89 | +try { |
| 90 | + $xpath->registerPhpFunctions([var_dump(...)]); |
| 91 | +} catch (Throwable $e) { |
| 92 | + echo $e->getMessage(), "\n"; |
| 93 | +} |
| 94 | + |
| 95 | +try { |
| 96 | + $xpath->registerPhpFunctions(["nonexistent"]); |
| 97 | +} catch (Throwable $e) { |
| 98 | + echo $e->getMessage(), "\n"; |
| 99 | +} |
| 100 | + |
| 101 | +try { |
| 102 | + $xpath->registerPhpFunctions(["" => var_dump(...)]); |
| 103 | +} catch (Throwable $e) { |
| 104 | + echo $e->getMessage(), "\n"; |
| 105 | +} |
| 106 | + |
| 107 | +?> |
| 108 | +--EXPECT-- |
| 109 | +--- Legit cases: none --- |
| 110 | +No callbacks were registered |
| 111 | +--- Legit cases: all --- |
| 112 | +string(15) "https://php.net" |
| 113 | +string(15) "https://php.net" |
| 114 | +--- Legit cases: set --- |
| 115 | +string(15) "https://php.net" |
| 116 | +string(15) "https://php.net" |
| 117 | +string(15) "https://php.net" |
| 118 | +No callback handler "notinset" registered |
| 119 | +--- Legit cases: set with cycle --- |
| 120 | +dummy: https://php.net |
| 121 | +--- Error cases --- |
| 122 | +DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be a callable, function "nonexistent" not found or invalid function name |
| 123 | +DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be of type array|string|null, Closure given |
| 124 | +Object of class Closure could not be converted to string |
| 125 | +Object of class Closure could not be converted to string |
| 126 | +DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be an array with valid callbacks as values, function "nonexistent" not found or invalid function name |
| 127 | +DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) array key must not be empty |
0 commit comments