7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
26 require_once 'Zend/Cache.php';
27 require_once 'Zend/Cache/Frontend/Function.php';
28 require_once 'Zend/Cache/Backend/Test.php';
33 require_once 'PHPUnit/Framework/TestCase.php';
35 function foobar($param1, $param2) {
36 echo "foobar_output($param1, $param2)";
37 return "foobar_return($param1, $param2)";
41 private static $_instanceCounter = 0;
43 public function __construct()
45 self
::$_instanceCounter++
;
48 public function foobar($param1, $param2)
50 return foobar($param1, $param2)
51 . ':' . self
::$_instanceCounter;
58 * @subpackage UnitTests
59 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
60 * @license http://framework.zend.com/license/new-bsd New BSD License
63 class Zend_Cache_FunctionFrontendTest
extends PHPUnit_Framework_TestCase
{
67 public function setUp()
69 if (!$this->_instance
) {
70 $this->_instance
= new Zend_Cache_Frontend_Function(array());
71 $this->_backend
= new Zend_Cache_Backend_Test();
72 $this->_instance
->setBackend($this->_backend
);
76 public function tearDown()
78 unset($this->_instance
);
81 public function testConstructorCorrectCall()
84 'cache_by_default' => false,
85 'cached_functions' => array('foo', 'bar')
87 $test = new Zend_Cache_Frontend_Function($options);
90 public function testConstructorBadCall()
93 'cache_by_default' => false,
94 0 => array('foo', 'bar')
97 $test = new Zend_Cache_Frontend_Function($options);
98 } catch (Zend_Cache_Exception
$e) {
101 $this->fail('Zend_Cache_Exception was expected but not thrown');
104 public function testCallCorrectCall1()
107 ob_implicit_flush(false);
108 $return = $this->_instance
->call('foobar', array('param1', 'param2'));
109 $data = ob_get_contents();
111 ob_implicit_flush(true);
112 $this->assertEquals('bar', $return);
113 $this->assertEquals('foo', $data);
116 public function testCallCorrectCall2()
119 ob_implicit_flush(false);
120 $return = $this->_instance
->call('foobar', array('param3', 'param4'));
121 $data = ob_get_contents();
123 ob_implicit_flush(true);
124 $this->assertEquals('foobar_return(param3, param4)', $return);
125 $this->assertEquals('foobar_output(param3, param4)', $data);
128 public function testCallCorrectCall3()
130 // cacheByDefault = false
131 $this->_instance
->setOption('cache_by_default', false);
133 ob_implicit_flush(false);
134 $return = $this->_instance
->call('foobar', array('param1', 'param2'));
135 $data = ob_get_contents();
137 ob_implicit_flush(true);
138 $this->assertEquals('foobar_return(param1, param2)', $return);
139 $this->assertEquals('foobar_output(param1, param2)', $data);
142 public function testCallCorrectCall4()
144 // cacheByDefault = false
145 // cachedFunctions = array('foobar')
146 $this->_instance
->setOption('cache_by_default', false);
147 $this->_instance
->setOption('cached_functions', array('foobar'));
149 ob_implicit_flush(false);
150 $return = $this->_instance
->call('foobar', array('param1', 'param2'));
151 $data = ob_get_contents();
153 ob_implicit_flush(true);
154 $this->assertEquals('bar', $return);
155 $this->assertEquals('foo', $data);
158 public function testCallCorrectCall5()
160 // cacheByDefault = true
161 // nonCachedFunctions = array('foobar')
162 $this->_instance
->setOption('cache_by_default', true);
163 $this->_instance
->setOption('non_cached_functions', array('foobar'));
165 ob_implicit_flush(false);
166 $return = $this->_instance
->call('foobar', array('param1', 'param2'));
167 $data = ob_get_contents();
169 ob_implicit_flush(true);
170 $this->assertEquals('foobar_return(param1, param2)', $return);
171 $this->assertEquals('foobar_output(param1, param2)', $data);
174 public function testCallObjectMethodCorrectCall1()
176 // cacheByDefault = true
177 // nonCachedFunctions = array('foobar')
178 $this->_instance
->setOption('cache_by_default', true);
179 $this->_instance
->setOption('non_cached_functions', array('foobar'));
181 ob_implicit_flush(false);
182 $object = new fooclass();
183 $return = $this->_instance
->call(array($object, 'foobar'), array('param1', 'param2'));
184 $data = ob_get_contents();
186 ob_implicit_flush(true);
187 $this->assertEquals('foobar_return(param1, param2):1', $return);
188 $this->assertEquals('foobar_output(param1, param2)', $data);
191 public function testCallObjectMethodCorrectCall2()
193 // cacheByDefault = true
194 // nonCachedFunctions = array('foobar')
195 $this->_instance
->setOption('cache_by_default', true);
196 $this->_instance
->setOption('non_cached_functions', array('foobar'));
198 ob_implicit_flush(false);
199 $object = new fooclass();
200 $return = $this->_instance
->call(array($object, 'foobar'), array('param1', 'param2'));
201 $data = ob_get_contents();
203 ob_implicit_flush(true);
204 $this->assertEquals('foobar_return(param1, param2):2', $return);
205 $this->assertEquals('foobar_output(param1, param2)', $data);
208 public function testCallClosureThrowsException()
210 if (version_compare(PHP_VERSION
, '5.3', '<')) {
211 $this->markTestSkipped();
214 $this->setExpectedException('Zend_Cache_Exception');
215 eval('$closure = function () {};'); // no parse error on php < 5.3
216 $this->_instance
->call($closure);
219 public function testCallWithABadSyntax1()
222 $this->_instance
->call(1, array());
223 } catch (Zend_Cache_Exception
$e) {
226 $this->fail('Zend_Cache_Exception was expected but not thrown');