1+ <?php
2+
3+ namespace Sprain \NewsParser \Test \NewsParser \Cache ;
4+
5+ use Doctrine \Common \Cache \FilesystemCache ;
6+ use Sprain \NewsParser \Cache \Cache ;
7+
8+ class CacheTest extends \PHPUnit_Framework_TestCase
9+ {
10+ protected $ cacheDir = '../../testcache ' ;
11+
12+ public function setUp ()
13+ {
14+ $ this ->cache = new Cache ();
15+ $ this ->cache ->setCacheDir (__DIR__ . DIRECTORY_SEPARATOR . $ this ->cacheDir );
16+ }
17+
18+ public function tearDown ()
19+ {
20+ $ this ->rrmdir (__DIR__ . DIRECTORY_SEPARATOR . $ this ->cacheDir );
21+ }
22+
23+ public function testGetCache ()
24+ {
25+ $ this ->assertTrue ($ this ->cache ->getCache () instanceof FilesystemCache);
26+ }
27+
28+ public function testGetCacheFailsWithoutCacheDir ()
29+ {
30+ $ this ->cache ->setCacheDir (null );
31+ $ this ->assertFalse ($ this ->cache ->getCache () instanceof FilesystemCache);
32+ }
33+
34+ public function testDisableCache ()
35+ {
36+ $ this ->cache ->disable ();
37+ $ this ->assertFalse ($ this ->cache ->getCache () instanceof FilesystemCache);
38+ }
39+
40+ public function testSaveAndFetch ()
41+ {
42+ $ this ->cache ->save ('foo ' , 'bar ' );
43+ $ this ->assertSame ('bar ' , $ this ->cache ->fetch ('foo ' ));
44+ }
45+
46+ public function testClearCache ()
47+ {
48+ $ this ->cache ->save ('foo ' , 'bar ' );
49+ $ this ->cache ->clear ();
50+ $ this ->assertFalse ($ this ->cache ->fetch ('foo ' ));
51+ }
52+
53+ public function testSetLifetime ()
54+ {
55+ $ this ->cache ->setCacheLifetime (1 );
56+ $ this ->assertSame (1 , $ this ->cache ->getCacheLifetime ());
57+ $ this ->cache ->save ('foo ' , 'bar ' );
58+ $ this ->assertSame ('bar ' , $ this ->cache ->fetch ('foo ' ));
59+ sleep (2 );
60+ $ this ->assertFalse ($ this ->cache ->fetch ('foo ' ));
61+ }
62+
63+ protected function rrmdir ($ dir ) {
64+ if (is_dir ($ dir )) {
65+ $ objects = scandir ($ dir );
66+ foreach ($ objects as $ object ) {
67+ if ($ object != ". " && $ object != ".. " ) {
68+ if (filetype ($ dir ."/ " .$ object ) == "dir " ) $ this ->rrmdir ($ dir ."/ " .$ object ); else unlink ($ dir ."/ " .$ object );
69+ }
70+ }
71+ reset ($ objects );
72+ rmdir ($ dir );
73+ }
74+ }
75+ }
0 commit comments