From c74006005187e35c01f5f3900c9a33ab281e02ef Mon Sep 17 00:00:00 2001 From: Rhodri Pugh Date: Mon, 8 Sep 2014 19:35:00 +0100 Subject: [PATCH] fix inconsistent response from twig_slice --- lib/Twig/Extension/Core.php | 2 +- test/Twig/Tests/Extension/CoreTest.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletions(-) diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index ef36576..876d5c6 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -705,7 +705,7 @@ function twig_slice(Twig_Environment $env, $item, $start, $length = null, $prese return (string) mb_substr($item, $start, null === $length ? mb_strlen($item, $charset) - $start : $length, $charset); } - return null === $length ? substr($item, $start) : substr($item, $start, $length); + return (string) (null === $length ? substr($item, $start) : substr($item, $start, $length)); } /** diff --git a/test/Twig/Tests/Extension/CoreTest.php b/test/Twig/Tests/Extension/CoreTest.php index 5f3da18..15a5de4 100644 --- a/test/Twig/Tests/Extension/CoreTest.php +++ b/test/Twig/Tests/Extension/CoreTest.php @@ -130,6 +130,24 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase { twig_escape_filter(new Twig_Environment(), 'foo', 'bar'); } + + public function testTwigFirst() + { + $twig = new Twig_Environment(); + $this->assertEquals('a', twig_first($twig, 'abc')); + $this->assertEquals(1, twig_first($twig, array(1, 2, 3))); + $this->assertEquals('', twig_first($twig, null)); + $this->assertEquals('', twig_first($twig, '')); + } + + public function testTwigLast() + { + $twig = new Twig_Environment(); + $this->assertEquals('c', twig_last($twig, 'abc')); + $this->assertEquals(3, twig_last($twig, array(1, 2, 3))); + $this->assertEquals('', twig_last($twig, null)); + $this->assertEquals('', twig_last($twig, '')); + } } function foo_escaper_for_test(Twig_Environment $env, $string, $charset) -- 1.7.2.5