TYPO3 CMS  TYPO3_6-2
VariableFrontendTest.php
Go to the documentation of this file.
1 <?php
3 
25 
30  public function setChecksIfTheIdentifierIsValid() {
31  $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\StringFrontend', array('isValidEntryIdentifier'), array(), '', FALSE);
32  $cache->expects($this->once())->method('isValidEntryIdentifier')->with('foo')->will($this->returnValue(FALSE));
33  $cache->set('foo', 'bar');
34  }
35 
40  $theString = 'Just some value';
41  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
42  $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theString)));
43 
44  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
45  $cache->set('VariableCacheTest', $theString);
46  }
47 
52  $theArray = array('Just some value', 'and another one.');
53  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
54  $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theArray)));
55 
56  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
57  $cache->set('VariableCacheTest', $theArray);
58  }
59 
63  public function setPassesLifetimeToBackend() {
64  $theString = 'Just some value';
65  $theLifetime = 1234;
66  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
67  $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theString)), $this->equalTo(array()), $this->equalTo($theLifetime));
68 
69  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
70  $cache->set('VariableCacheTest', $theString, array(), $theLifetime);
71  }
72 
77  if (!extension_loaded('igbinary')) {
78  $this->markTestSkipped('Cannot test igbinary support, because igbinary is not installed.');
79  }
80 
81  $theString = 'Just some value';
82  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
83  $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(igbinary_serialize($theString)));
84 
85  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
86  $cache->initializeObject();
87  $cache->set('VariableCacheTest', $theString);
88  }
89 
94  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
95  $backend->expects($this->once())->method('get')->will($this->returnValue(serialize('Just some value')));
96 
97  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
98  $this->assertEquals('Just some value', $cache->get('VariableCacheTest'), 'The returned value was not the expected string.');
99  }
100 
105  $theArray = array('Just some value', 'and another one.');
106  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
107  $backend->expects($this->once())->method('get')->will($this->returnValue(serialize($theArray)));
108 
109  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
110  $this->assertEquals($theArray, $cache->get('VariableCacheTest'), 'The returned value was not the expected unserialized array.');
111  }
112 
117  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
118  $backend->expects($this->once())->method('get')->will($this->returnValue(serialize(FALSE)));
119 
120  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
121  $this->assertFalse($cache->get('VariableCacheTest'), 'The returned value was not the FALSE.');
122  }
123 
127  public function getUsesIgBinaryIfAvailable() {
128  if (!extension_loaded('igbinary')) {
129  $this->markTestSkipped('Cannot test igbinary support, because igbinary is not installed.');
130  }
131 
132  $theArray = array('Just some value', 'and another one.');
133  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
134  $backend->expects($this->once())->method('get')->will($this->returnValue(igbinary_serialize($theArray)));
135 
136  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
137  $cache->initializeObject();
138 
139  $this->assertEquals($theArray, $cache->get('VariableCacheTest'), 'The returned value was not the expected unserialized array.');
140  }
141 
145  public function hasReturnsResultFromBackend() {
146  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
147  $backend->expects($this->once())->method('has')->with($this->equalTo('VariableCacheTest'))->will($this->returnValue(TRUE));
148 
149  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
150  $this->assertTrue($cache->has('VariableCacheTest'), 'has() did not return TRUE.');
151  }
152 
156  public function removeCallsBackend() {
157  $cacheIdentifier = 'someCacheIdentifier';
158  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
159 
160  $backend->expects($this->once())->method('remove')->with($this->equalTo($cacheIdentifier))->will($this->returnValue(TRUE));
161 
162  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
163  $this->assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE');
164  }
165 
170  public function getByTagRejectsInvalidTags() {
171  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\BackendInterface', array(), array(), '', FALSE);
172  $backend->expects($this->never())->method('getByTag');
173 
174  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
175  $cache->getByTag('SomeInvalid\Tag');
176  }
177 
181  public function getByTagCallsBackend() {
182  $tag = 'sometag';
183  $identifiers = array('one', 'two');
184  $entries = array('one value', 'two value');
185  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
186 
187  $backend->expects($this->once())->method('findIdentifiersByTag')->with($this->equalTo($tag))->will($this->returnValue($identifiers));
188  $backend->expects($this->exactly(2))->method('get')->will($this->onConsecutiveCalls(serialize('one value'), serialize('two value')));
189 
190  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
191  $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries');
192  }
193 
198  if (!extension_loaded('igbinary')) {
199  $this->markTestSkipped('Cannot test igbinary support, because igbinary is not installed.');
200  }
201 
202  $tag = 'sometag';
203  $identifiers = array('one', 'two');
204  $entries = array('one value', 'two value');
205  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
206 
207  $backend->expects($this->once())->method('findIdentifiersByTag')->with($this->equalTo($tag))->will($this->returnValue($identifiers));
208  $backend->expects($this->exactly(2))->method('get')->will($this->onConsecutiveCalls(igbinary_serialize('one value'), igbinary_serialize('two value')));
209 
210  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
211  $cache->initializeObject();
212  $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries');
213  }
214 }