From 4c55be6ddace9ac79b78a11cb2c2d3284f3d0459 Mon Sep 17 00:00:00 2001 From: fabien Date: Sun, 13 Dec 2009 12:55:04 +0000 Subject: [PATCH] added tests for the in filter git-svn-id: http://svn.twig-project.org/trunk@169 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- doc/02-Twig-for-Template-Designers.markdown | 2 +- lib/Twig/runtime.php | 2 +- test/fixtures/filters/in.test | 27 +++++++++++++++++++++++++++ test/unit/integrationTest.php | 2 +- 4 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/filters/in.test diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index cac3278..6d2709a 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -844,7 +844,7 @@ Returns true if the value is contained within another one. {{ 'cd'|in('abcde') }} You can use this filter to perform a containment test on strings, arrays, or -objects implementing the `ArrayAccess` interface. +objects implementing the `Traversable` interface. ### `default` diff --git a/lib/Twig/runtime.php b/lib/Twig/runtime.php index 4d98244..16e617e 100644 --- a/lib/Twig/runtime.php +++ b/lib/Twig/runtime.php @@ -97,7 +97,7 @@ function twig_in_filter($value, $compare) { return false !== strpos($compare, (string) $value); } - elseif (is_object($compare) && $compare instanceof ArrayAccess) + elseif (is_object($compare) && $compare instanceof Traversable) { return in_array($value, iterator_to_array($compare)); } diff --git a/test/fixtures/filters/in.test b/test/fixtures/filters/in.test new file mode 100644 index 0000000..4d6e337 --- /dev/null +++ b/test/fixtures/filters/in.test @@ -0,0 +1,27 @@ +--TEST-- +"in" filter +--TEMPLATE-- +{{ 1|in([1, 2, 3]) }} +{{ 5|in([1, 2, 3]) }} +{{ 'cd'|in('abcde') }} +{{ 'ad'|in('abcde') }} +{{ 'foo'|in(foo) }} +{{ 'a'|in(foo|keys) }} +--DATA-- +class ItemsIteratorForInFilter implements Iterator +{ + protected $values = array('a' => 'foo', 'b' => 'bar'); + public function current() { return current($this->values); } + public function key() { return key($this->values); } + public function next() { return next($this->values); } + public function rewind() { return reset($this->values); } + public function valid() { return false !== current($this->values); } +} +return array('foo' => new ItemsIteratorForInFilter()) +--EXPECT-- +1 + +1 + +1 +1 diff --git a/test/unit/integrationTest.php b/test/unit/integrationTest.php index 9b56a86..ee05be2 100644 --- a/test/unit/integrationTest.php +++ b/test/unit/integrationTest.php @@ -33,7 +33,7 @@ class Foo } } -$t = new LimeTest(52); +$t = new LimeTest(53); $fixturesDir = realpath(dirname(__FILE__).'/../fixtures/'); foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) -- 1.7.2.5