This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
CallbackCacheTest.php
189 lines (160 loc) · 5.11 KB
/
CallbackCacheTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Cache
*/
namespace ZendTest\Cache\Pattern;
use Zend\Cache;
/**
* Test class
*/
class TestCallbackCache
{
/**
* A counter how oftern the method "foo" was called
*/
public static $fooCounter = 0;
public static function bar()
{
++self::$fooCounter;
$args = func_get_args();
echo 'foobar_output('.implode(', ', $args) . ') : ' . self::$fooCounter;
return 'foobar_return('.implode(', ', $args) . ') : ' . self::$fooCounter;
}
public static function emptyMethod() {}
}
class FailableCallback
{
public function __invoke()
{
throw new \Exception('This callback should either fail or never be invoked');
}
}
/**
* Test function
* @see ZendTest\Cache\Pattern\Foo::bar
*/
function bar ()
{
return call_user_func_array(__NAMESPACE__ . '\TestCallbackCache::bar', func_get_args());
}
/**
* @category Zend
* @package Zend_Cache
* @subpackage UnitTests
* @group Zend_Cache
*/
class CallbackCacheTest extends CommonPatternTest
{
/**
* @var Zend\Cache\Storage\StorageInterface
*/
protected $_storage;
public function setUp()
{
$this->_storage = new Cache\Storage\Adapter\Memory(array(
'memory_limit' => 0
));
$this->_options = new Cache\Pattern\PatternOptions(array(
'storage' => $this->_storage,
));
$this->_pattern = new Cache\Pattern\CallbackCache();
$this->_pattern->setOptions($this->_options);
parent::setUp();
}
public function tearDown()
{
parent::tearDown();
}
public function testCallEnabledCacheOutputByDefault()
{
$this->_testCall(
__NAMESPACE__ . '\TestCallbackCache::bar',
array('testCallEnabledCacheOutputByDefault', 'arg2')
);
}
public function testCallDisabledCacheOutput()
{
$options = $this->_pattern->getOptions();
$options->setCacheOutput(false);
$this->_testCall(
__NAMESPACE__ . '\TestCallbackCache::bar',
array('testCallDisabledCacheOutput', 'arg2')
);
}
public function testMagicFunctionCall()
{
$this->_testCall(
__NAMESPACE__ . '\bar',
array('testMagicFunctionCall', 'arg2')
);
}
public function testGenerateKey()
{
$callback = __NAMESPACE__ . '\TestCallbackCache::emptyMethod';
$args = array('arg1', 2, 3.33, null);
$generatedKey = $this->_pattern->generateKey($callback, $args);
$usedKey = null;
$this->_options->getStorage()->getEventManager()->attach('setItem.pre', function ($event) use (&$usedKey) {
$params = $event->getParams();
$usedKey = $params['key'];
});
$this->_pattern->call($callback, $args);
$this->assertEquals($generatedKey, $usedKey);
}
public function testCallInvalidCallbackException()
{
$this->setExpectedException('Zend\Cache\Exception\InvalidArgumentException');
$this->_pattern->call(1);
}
public function testCallUnknownCallbackException()
{
$this->setExpectedException('Zend\Cache\Exception\InvalidArgumentException');
$this->_pattern->call('notExiststingFunction');
}
/**
* Running tests calling ZendTest\Cache\Pattern\TestCallbackCache::bar
* using different callbacks resulting in this method call
*/
protected function _testCall($callback, array $args)
{
$returnSpec = 'foobar_return(' . implode(', ', $args) . ') : ';
$outputSpec = 'foobar_output(' . implode(', ', $args) . ') : ';
// first call - not cached
$firstCounter = TestCallbackCache::$fooCounter + 1;
ob_start();
ob_implicit_flush(false);
$return = $this->_pattern->call($callback, $args);
$data = ob_get_clean();
$this->assertEquals($returnSpec . $firstCounter, $return);
$this->assertEquals($outputSpec . $firstCounter, $data);
// second call - cached
ob_start();
ob_implicit_flush(false);
$return = $this->_pattern->call($callback, $args);
$data = ob_get_clean();
$this->assertEquals($returnSpec . $firstCounter, $return);
$options = $this->_pattern->getOptions();
if ($options->getCacheOutput()) {
$this->assertEquals($outputSpec . $firstCounter, $data);
} else {
$this->assertEquals('', $data);
}
}
/**
* @group 4629
* @return void
*/
public function testCallCanReturnCachedNullValues()
{
$callback = new FailableCallback();
$key = $this->_pattern->generateKey($callback, array());
$this->_storage->setItem($key, array(null));
$value = $this->_pattern->call($callback);
$this->assertNull($value);
}
}