]>
Commit | Line | Data |
---|---|---|
01e48f26 | 1 | <?php |
c4ad3d4f | 2 | |
01e48f26 V |
3 | /** |
4 | * Cache tests | |
5 | */ | |
c4ad3d4f | 6 | |
b0428aa9 A |
7 | namespace Shaarli\Render; |
8 | ||
9 | use PHPUnit\Framework\TestCase; | |
10 | use Shaarli\Security\SessionManager; | |
01e48f26 | 11 | |
01e48f26 V |
12 | /** |
13 | * Unitary tests for cached pages | |
14 | */ | |
b0428aa9 | 15 | class PageCacheManagerTest extends TestCase |
01e48f26 V |
16 | { |
17 | // test cache directory | |
4bf35ba5 | 18 | protected static $testCacheDir = 'sandbox/dummycache'; |
01e48f26 V |
19 | |
20 | // dummy cached file names / content | |
21 | protected static $pages = array('a', 'toto', 'd7b59c'); | |
22 | ||
b0428aa9 A |
23 | /** @var PageCacheManager */ |
24 | protected $cacheManager; | |
25 | ||
26 | /** @var SessionManager */ | |
27 | protected $sessionManager; | |
01e48f26 V |
28 | |
29 | /** | |
30 | * Populate the cache with dummy files | |
31 | */ | |
32 | public function setUp() | |
33 | { | |
c4d5be53 | 34 | $this->cacheManager = new PageCacheManager(static::$testCacheDir, true); |
b0428aa9 | 35 | |
dfc650aa | 36 | if (!is_dir(self::$testCacheDir)) { |
01e48f26 | 37 | mkdir(self::$testCacheDir); |
aedd62e2 | 38 | } else { |
dfc650aa | 39 | array_map('unlink', glob(self::$testCacheDir . '/*')); |
01e48f26 | 40 | } |
b6a54537 | 41 | |
01e48f26 | 42 | foreach (self::$pages as $page) { |
dfc650aa | 43 | file_put_contents(self::$testCacheDir . '/' . $page . '.cache', $page); |
01e48f26 | 44 | } |
dfc650aa | 45 | file_put_contents(self::$testCacheDir . '/intru.der', 'ShouldNotBeThere'); |
01e48f26 V |
46 | } |
47 | ||
b6a54537 A |
48 | /** |
49 | * Remove dummycache folder after each tests. | |
50 | */ | |
51 | public function tearDown() | |
52 | { | |
dfc650aa | 53 | array_map('unlink', glob(self::$testCacheDir . '/*')); |
b6a54537 A |
54 | rmdir(self::$testCacheDir); |
55 | } | |
56 | ||
01e48f26 V |
57 | /** |
58 | * Purge cached pages | |
59 | */ | |
60 | public function testPurgeCachedPages() | |
61 | { | |
b0428aa9 | 62 | $this->cacheManager->purgeCachedPages(); |
01e48f26 | 63 | foreach (self::$pages as $page) { |
dfc650aa | 64 | $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache'); |
aedd62e2 V |
65 | } |
66 | ||
dfc650aa | 67 | $this->assertFileExists(self::$testCacheDir . '/intru.der'); |
aedd62e2 V |
68 | } |
69 | ||
70 | /** | |
71 | * Purge cached pages - missing directory | |
72 | */ | |
73 | public function testPurgeCachedPagesMissingDir() | |
74 | { | |
c4d5be53 | 75 | $this->cacheManager = new PageCacheManager(self::$testCacheDir . '_missing', true); |
b0428aa9 | 76 | |
87f9f4f9 A |
77 | $oldlog = ini_get('error_log'); |
78 | ini_set('error_log', '/dev/null'); | |
aedd62e2 | 79 | $this->assertEquals( |
4bf35ba5 | 80 | 'Cannot purge sandbox/dummycache_missing: no directory', |
b0428aa9 | 81 | $this->cacheManager->purgeCachedPages() |
aedd62e2 | 82 | ); |
87f9f4f9 | 83 | ini_set('error_log', $oldlog); |
01e48f26 | 84 | } |
01e48f26 | 85 | } |