Exemplo n.º 1
0
 /**
  * @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('TYPO3Fluid\\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);
 }
 /**
  * @test
  */
 public function testEvaluatedUsesVariableProviderGetByPath()
 {
     $node = new ObjectAccessorNode('foo.bar');
     $renderingContext = $this->getMock(RenderingContextInterface::class);
     $variableContainer = $this->getMock(StandardVariableProvider::class, array());
     $variableContainer->expects($this->once())->method('getByPath')->with('foo.bar', array())->will($this->returnValue('foo'));
     $renderingContext->expects($this->any())->method('getVariableProvider')->will($this->returnValue($variableContainer));
     $value = $node->evaluate($renderingContext);
     $this->assertEquals('foo', $value);
 }
Exemplo n.º 3
0
 /**
  * @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('isset(%s[\'%s\']) ? %s[\'%s\'] : NULL', $providerReference, str_replace('.', '\'][\'', $path), $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));
 }