From 11f798cf4125cc6bf8f81ab964e0c9258abb3c3e Mon Sep 17 00:00:00 2001 From: Abdellatif Ait boudad Date: Fri, 10 Apr 2015 14:43:55 +0100 Subject: [PATCH] [Config][cache factory] check type of callback argument. --- .../Component/Config/ConfigCacheFactory.php | 5 +++- .../Config/Tests/ConfigCacheFactoryTest.php | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/Config/Tests/ConfigCacheFactoryTest.php diff --git a/src/Symfony/Component/Config/ConfigCacheFactory.php b/src/Symfony/Component/Config/ConfigCacheFactory.php index 3c76087a64..5a8f456238 100644 --- a/src/Symfony/Component/Config/ConfigCacheFactory.php +++ b/src/Symfony/Component/Config/ConfigCacheFactory.php @@ -37,8 +37,11 @@ class ConfigCacheFactory implements ConfigCacheFactoryInterface */ public function cache($file, $callback) { - $cache = new ConfigCache($file, $this->debug); + if (!is_callable($callback)) { + throw new \InvalidArgumentException(sprintf('Invalid type for callback argument. Expected callable, but got "%s".', gettype($callback))); + } + $cache = new ConfigCache($file, $this->debug); if (!$cache->isFresh()) { call_user_func($callback, $cache); } diff --git a/src/Symfony/Component/Config/Tests/ConfigCacheFactoryTest.php b/src/Symfony/Component/Config/Tests/ConfigCacheFactoryTest.php new file mode 100644 index 0000000000..291243dead --- /dev/null +++ b/src/Symfony/Component/Config/Tests/ConfigCacheFactoryTest.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Config\Tests; + +use Symfony\Component\Config\ConfigCacheFactory; + +class ConfigCacheFactoryTest extends \PHPUnit_Framework_TestCase +{ + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Invalid type for callback argument. Expected callable, but got "object". + */ + public function testCachWithInvalidCallback() + { + $cacheFactory = new ConfigCacheFactory(true); + + $cacheFactory->cache('file', new \stdClass()); + } +}