|
1
|
|
|
<?php |
|
2
|
|
|
namespace HtImgModuleTest\Service; |
|
3
|
|
|
|
|
4
|
|
|
use HtImgModule\Options\ModuleOptions; |
|
5
|
|
|
use HtImgModule\Service\CacheManager; |
|
6
|
|
|
use Imagine\Gd\Imagine; |
|
7
|
|
|
|
|
8
|
|
|
class CacheManagerTest extends \PHPUnit_Framework_TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @covers HtImgModule\Service\CacheManager::getCacheUrl |
|
12
|
|
|
*/ |
|
13
|
|
|
public function testGetCacheUrl() |
|
14
|
|
|
{ |
|
15
|
|
|
$options = new ModuleOptions; |
|
16
|
|
|
$cacheManager = new CacheManager($options); |
|
17
|
|
|
$options->setCachePath(RESOURCES_DIR); |
|
18
|
|
|
$this->assertEquals(RESOURCES_DIR . '/hello/world', $cacheManager->getCacheUrl('world', 'hello')); |
|
19
|
|
|
$this->assertEquals(RESOURCES_DIR . '/hello/world.jpg', $cacheManager->getCacheUrl('world', 'hello', RESOURCES_DIR . '/Archos.jpg')); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @covers HtImgModule\Service\CacheManager::getCachePath |
|
24
|
|
|
*/ |
|
25
|
|
|
public function testGetCachePath() |
|
26
|
|
|
{ |
|
27
|
|
|
$options = new ModuleOptions; |
|
28
|
|
|
$cacheManager = new CacheManager($options); |
|
29
|
|
|
$options->setCachePath(RESOURCES_DIR); |
|
30
|
|
|
$options->setWebRoot('public_html'); |
|
31
|
|
|
$this->assertEquals('public_html/'. RESOURCES_DIR . '/hello/world', $cacheManager->getCachePath('world', 'hello')); |
|
32
|
|
|
$this->assertEquals(RESOURCES_DIR . '/hello/Archos.jpg.jpg', $cacheManager->getCacheUrl('Archos.jpg', 'hello', RESOURCES_DIR . '/Archos.jpg')); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @covers HtImgModule\Service\CacheManager::createCache |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testCreateCache() |
|
39
|
|
|
{ |
|
40
|
|
|
$options = new ModuleOptions; |
|
41
|
|
|
$cacheManager = new CacheManager($options); |
|
42
|
|
|
$imagine = new Imagine; |
|
43
|
|
|
$image = $imagine->open(RESOURCES_DIR . '/Archos.jpg'); |
|
44
|
|
|
$options->setWebRoot(RESOURCES_DIR); |
|
45
|
|
|
$options->setCachePath('.'); |
|
46
|
|
|
$cacheManager->createCache('awesome/archos-crop.jpg', 'crop', $image); |
|
47
|
|
|
$cacheFile = RESOURCES_DIR . '/crop/awesome/archos-crop.jpg'; |
|
48
|
|
|
$this->assertTrue(is_readable($cacheFile)); |
|
49
|
|
|
|
|
50
|
|
|
return $cacheManager; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @covers HtImgModule\Service\CacheManager::deleteCache |
|
55
|
|
|
* @depends testCreateCache |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testDeleteCache(CacheManager $cacheManager) |
|
58
|
|
|
{ |
|
59
|
|
|
$cacheManager->deleteCache('awesome/archos-crop.jpg', 'crop'); |
|
60
|
|
|
$this->assertFalse(is_readable(RESOURCES_DIR . '/crop/awesome/archos-crop.jpg')); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @covers HtImgModule\Service\CacheManager::cacheExists |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testCacheExists() |
|
67
|
|
|
{ |
|
68
|
|
|
$options = new ModuleOptions; |
|
69
|
|
|
$cacheManager = new CacheManager($options); |
|
70
|
|
|
$imagine = new Imagine; |
|
71
|
|
|
$image = $imagine->open(RESOURCES_DIR . '/Archos.jpg'); |
|
72
|
|
|
$options->setWebRoot(RESOURCES_DIR); |
|
73
|
|
|
$options->setCachePath('.'); |
|
74
|
|
|
$cacheManager->createCache('awesome/archos-crop.jpg', 'crop', $image); |
|
75
|
|
|
$cacheFile = RESOURCES_DIR . '/crop/awesome/archos-crop.jpg'; |
|
|
|
|
|
|
76
|
|
|
$this->assertTrue($cacheManager->cacheExists('awesome/archos-crop.jpg', 'crop')); |
|
77
|
|
|
$this->assertFalse($cacheManager->cacheExists('awesome/random-crop.jpg', 'crop')); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testCachingEnabledChecking() |
|
81
|
|
|
{ |
|
82
|
|
|
$options = $this->createMock('HtImgModule\Options\CacheOptionsInterface'); |
|
83
|
|
|
$cacheManager = new CacheManager($options); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertTrue($cacheManager->isCachingEnabled('abcd', ['enable_cache' => true])); |
|
86
|
|
|
$this->assertFalse($cacheManager->isCachingEnabled('abcd', ['enable_cache' => false])); |
|
87
|
|
|
|
|
88
|
|
|
$options->expects($this->once()) |
|
89
|
|
|
->method('getEnableCache') |
|
90
|
|
|
->will($this->returnValue(true)); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertTrue($cacheManager->isCachingEnabled('abcd', [])); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.