mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-09 08:56:47 +00:00
Better type description on PHPDoc, better testing of returned value.
This commit is contained in:
+1
-1
@@ -596,7 +596,7 @@ An extension is a class that implements the following interface::
|
||||
/**
|
||||
* Returns a list of operators to add to the existing list.
|
||||
*
|
||||
* @return array An array of operators
|
||||
* @return array<array> First array of unary operators, second array of binary operators
|
||||
*/
|
||||
function getOperators();
|
||||
|
||||
|
||||
@@ -580,7 +580,7 @@ An extension is a class that implements the following interface::
|
||||
/**
|
||||
* Returns a list of operators to add to the existing list.
|
||||
*
|
||||
* @return array An array of operators
|
||||
* @return array<array> First array of unary operators, second array of binary operators
|
||||
*/
|
||||
function getOperators();
|
||||
|
||||
|
||||
@@ -1512,8 +1512,15 @@ class Twig_Environment
|
||||
|
||||
// operators
|
||||
if ($operators = $extension->getOperators()) {
|
||||
if (!is_array($operators)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'"%s::getOperators()" must return an array with operators, got "%s".',
|
||||
get_class($extension), is_object($operators) ? get_class($operators) : gettype($operators).(is_resource($operators) ? '' : '#'.$operators)
|
||||
));
|
||||
}
|
||||
|
||||
if (2 !== count($operators)) {
|
||||
throw new InvalidArgumentException(sprintf('"%s::getOperators()" does not return a valid operators array.', get_class($extension)));
|
||||
throw new InvalidArgumentException(sprintf('"%s::getOperators()" must return an array of 2 elements, got %d.', get_class($extension), count($operators)));
|
||||
}
|
||||
|
||||
$this->unaryOperators = array_merge($this->unaryOperators, $operators[0]);
|
||||
|
||||
@@ -63,7 +63,7 @@ interface Twig_ExtensionInterface
|
||||
/**
|
||||
* Returns a list of operators to add to the existing list.
|
||||
*
|
||||
* @return array An array of operators
|
||||
* @return array<array> First array of unary operators, second array of binary operators
|
||||
*/
|
||||
public function getOperators();
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
final class CustomExtensionTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @param Twig_ExtensionInterface $extension
|
||||
* @param string $expectedExceptionMessage
|
||||
*
|
||||
* @requires PHP 5.3
|
||||
* @dataProvider provideInvalidExtensions
|
||||
*/
|
||||
public function testGetInvalidOperators(\Twig_ExtensionInterface $extension, $expectedExceptionMessage)
|
||||
{
|
||||
$this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
|
||||
|
||||
$loader = new \Twig_Loader_Array(array('foo' => '{{ foo }}'));
|
||||
$env = new \Twig_Environment($loader);
|
||||
$env->addExtension($extension);
|
||||
|
||||
$method = new \ReflectionMethod($env, 'initExtensions');
|
||||
$method->setAccessible(true);
|
||||
$method->invoke($env);
|
||||
}
|
||||
|
||||
public function provideInvalidExtensions()
|
||||
{
|
||||
return array(
|
||||
array(new InvalidOperatorExtension(new \stdClass()), '"InvalidOperatorExtension::getOperators()" must return an array with operators, got "stdClass".'),
|
||||
array(new InvalidOperatorExtension(array(1, 2, 3)), '"InvalidOperatorExtension::getOperators()" must return an array of 2 elements, got 3.'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final class InvalidOperatorExtension implements \Twig_ExtensionInterface
|
||||
{
|
||||
private $operators;
|
||||
|
||||
public function __construct($operators)
|
||||
{
|
||||
$this->operators = $operators;
|
||||
}
|
||||
|
||||
public function initRuntime(Twig_Environment $environment)
|
||||
{
|
||||
}
|
||||
|
||||
public function getTokenParsers()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getNodeVisitors()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getFilters()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getTests()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getFunctions()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getGlobals()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getOperators()
|
||||
{
|
||||
return $this->operators;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return __CLASS__;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user