/** * Non-regression test */ public function testSuccessiveParsingWithFunctions() { $scope = new Scope(); $scope->bindFunction('test', function ($str) { return strtoupper($str); }); $twigParser = new TwigExecutor($scope->getFunctions()); $this->assertEquals('BAR', $twigParser->parse('{{ test("bar") }}', $scope)); $this->assertEquals('BAR', $twigParser->parse('{{ test("bar") }}', $scope)); }
public function testBindFunction() { $scope = new Scope(); $scope->bindFunction('foo', function () { return 'hello'; }); $function = $scope->getFunction('foo'); $this->assertEquals('hello', $function()); $functions = $scope->getFunctions(); $this->assertCount(1, $functions); $this->assertEquals('hello', $functions['foo']()); }
public function testWithFunctionMultipleParameters() { $global = new \stdClass(); $global->value = 5; $scope = new Scope(); $scope->bind('foo', ['0', '1']); $scope->bind('global', $global); $scope->bindFunction('blah', function ($parameters, $global) { $a = []; foreach ($parameters as $i) { $a['bimIndex' . $global . $i] = 'barValue' . $global . $i; } return $a; }); $parser = new ForEachExecutor(); $subScopes = $parser->execute('blah(foo, global.value) as bim => bar', $scope); $this->assertCount(2, $subScopes); foreach ($subScopes as $i => $subScope) { $this->assertInstanceOf('Xport\\Parser\\Scope', $subScope); $this->assertEquals('bimIndex' . $global->value . $i, $subScope->get('bim')); $this->assertEquals('barValue' . $global->value . $i, $subScope->get('bar')); } }
/** * Bind a function to a name. * * @param string $name * @param callable $callable */ public function bindFunction($name, $callable) { $this->scope->bindFunction($name, $callable); }