ZF-9979 & ZF-9422: allow function cache to use all types of callbacks
[zend/radio.git] / tests / Zend / Cache / FunctionFrontendTest.php
blob6ebcb757ac137defca45fb21c46890618537cc07
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Cache
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
20 * @version $Id$
23 /**
24 * Zend_Cache
26 require_once 'Zend/Cache.php';
27 require_once 'Zend/Cache/Frontend/Function.php';
28 require_once 'Zend/Cache/Backend/Test.php';
30 /**
31 * PHPUnit test case
33 require_once 'PHPUnit/Framework/TestCase.php';
35 function foobar($param1, $param2) {
36 echo "foobar_output($param1, $param2)";
37 return "foobar_return($param1, $param2)";
40 class fooclass {
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;
55 /**
56 * @category Zend
57 * @package Zend_Cache
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
61 * @group Zend_Cache
63 class Zend_Cache_FunctionFrontendTest extends PHPUnit_Framework_TestCase {
65 private $_instance;
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()
83 $options = array(
84 'cache_by_default' => false,
85 'cached_functions' => array('foo', 'bar')
87 $test = new Zend_Cache_Frontend_Function($options);
90 public function testConstructorBadCall()
92 $options = array(
93 'cache_by_default' => false,
94 0 => array('foo', 'bar')
96 try {
97 $test = new Zend_Cache_Frontend_Function($options);
98 } catch (Zend_Cache_Exception $e) {
99 return;
101 $this->fail('Zend_Cache_Exception was expected but not thrown');
104 public function testCallCorrectCall1()
106 ob_start();
107 ob_implicit_flush(false);
108 $return = $this->_instance->call('foobar', array('param1', 'param2'));
109 $data = ob_get_contents();
110 ob_end_clean();
111 ob_implicit_flush(true);
112 $this->assertEquals('bar', $return);
113 $this->assertEquals('foo', $data);
116 public function testCallCorrectCall2()
118 ob_start();
119 ob_implicit_flush(false);
120 $return = $this->_instance->call('foobar', array('param3', 'param4'));
121 $data = ob_get_contents();
122 ob_end_clean();
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);
132 ob_start();
133 ob_implicit_flush(false);
134 $return = $this->_instance->call('foobar', array('param1', 'param2'));
135 $data = ob_get_contents();
136 ob_end_clean();
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'));
148 ob_start();
149 ob_implicit_flush(false);
150 $return = $this->_instance->call('foobar', array('param1', 'param2'));
151 $data = ob_get_contents();
152 ob_end_clean();
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'));
164 ob_start();
165 ob_implicit_flush(false);
166 $return = $this->_instance->call('foobar', array('param1', 'param2'));
167 $data = ob_get_contents();
168 ob_end_clean();
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'));
180 ob_start();
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();
185 ob_end_clean();
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'));
197 ob_start();
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();
202 ob_end_clean();
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()
221 try {
222 $this->_instance->call(1, array());
223 } catch (Zend_Cache_Exception $e) {
224 return;
226 $this->fail('Zend_Cache_Exception was expected but not thrown');