/** * @test * @dataProvider getEvaluateTestValues * @param array $variables * @param string $path * @param mixed $expected */ public function testEvaluateGetsExpectedValue(array $variables, $path, $expected) { $node = new ObjectAccessorNode($path); $renderingContext = $this->getMock('NamelessCoder\\Fluid\\Core\\Rendering\\RenderingContextInterface'); $variableContainer = new StandardVariableProvider($variables); $renderingContext->expects($this->any())->method('getVariableProvider')->will($this->returnValue($variableContainer)); $value = $node->evaluate($renderingContext); $this->assertEquals($expected, $value); }
/** * @param ObjectAccessorNode $node * @return array * @see convert() */ protected function convertObjectAccessorNode(ObjectAccessorNode $node) { $arrayVariableName = $this->variableName('array'); $accessors = $node->getAccessors(); $providerReference = '$renderingContext->getVariableProvider()'; $path = $node->getObjectPath(); $pathSegments = explode('.', $path); if ($path === '_all') { return array('initialization' => '', 'execution' => sprintf('%s->getAll()', $providerReference)); } elseif (1 === count(array_unique($accessors)) && reset($accessors) === VariableExtractor::ACCESSOR_ARRAY && count($accessors) === count($pathSegments) && FALSE === strpos($path, '{')) { // every extractor used in this path is a straight-forward arrayaccess. // Create the compiled code as a plain old variable assignment: return array('initialization' => '', 'execution' => sprintf('%s[\'%s\']', $providerReference, str_replace('.', '\'][\'', $path))); } $accessorsVariable = var_export($accessors, TRUE); $initialization = sprintf('%s = %s;', $arrayVariableName, $accessorsVariable); return array('initialization' => $initialization, 'execution' => sprintf('%s->getByPath(\'%s\', %s)', $providerReference, $path, $arrayVariableName)); }