FileCacheTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Doctrine\Tests\Common\Cache;
  3. use Doctrine\Common\Cache\Cache;
  4. /**
  5. * @group DCOM-101
  6. */
  7. class FileCacheTest extends \Doctrine\Tests\DoctrineTestCase
  8. {
  9. /**
  10. * @var \Doctrine\Common\Cache\FileCache
  11. */
  12. private $driver;
  13. protected function setUp()
  14. {
  15. $this->driver = $this->getMock(
  16. 'Doctrine\Common\Cache\FileCache',
  17. array('doFetch', 'doContains', 'doSave'),
  18. array(), '', false
  19. );
  20. }
  21. public function getProviderFileName()
  22. {
  23. return array(
  24. //The characters :\/<>"*?| are not valid in Windows filenames.
  25. array('key:1', 'key-1'),
  26. array('key\2', 'key-2'),
  27. array('key/3', 'key-3'),
  28. array('key<4', 'key-4'),
  29. array('key>5', 'key-5'),
  30. array('key"6', 'key-6'),
  31. array('key*7', 'key-7'),
  32. array('key?8', 'key-8'),
  33. array('key|9', 'key-9'),
  34. array('key[10]', 'key[10]'),
  35. array('keyä11', 'key--11'),
  36. array('../key12', '---key12'),
  37. array('key-13', 'key__13'),
  38. );
  39. }
  40. /**
  41. * @dataProvider getProviderFileName
  42. */
  43. public function testInvalidFilename($key, $expected)
  44. {
  45. $cache = $this->driver;
  46. $method = new \ReflectionMethod($cache, 'getFilename');
  47. $method->setAccessible(true);
  48. $value = $method->invoke($cache, $key);
  49. $actual = pathinfo($value, PATHINFO_FILENAME);
  50. $this->assertEquals($expected, $actual);
  51. }
  52. public function testFilenameCollision()
  53. {
  54. $data = array(
  55. 'key:0' => 'key-0',
  56. 'key\0' => 'key-0',
  57. 'key/0' => 'key-0',
  58. 'key<0' => 'key-0',
  59. 'key>0' => 'key-0',
  60. 'key"0' => 'key-0',
  61. 'key*0' => 'key-0',
  62. 'key?0' => 'key-0',
  63. 'key|0' => 'key-0',
  64. 'key-0' => 'key__0',
  65. 'keyä0' => 'key--0',
  66. );
  67. $paths = array();
  68. $cache = $this->driver;
  69. $method = new \ReflectionMethod($cache, 'getFilename');
  70. $method->setAccessible(true);
  71. foreach ($data as $key => $expected) {
  72. $path = $method->invoke($cache, $key);
  73. $actual = pathinfo($path, PATHINFO_FILENAME);
  74. $this->assertNotContains($path, $paths);
  75. $this->assertEquals($expected, $actual);
  76. $paths[] = $path;
  77. }
  78. }
  79. public function testFilenameShouldCreateThePathWithFourSubDirectories()
  80. {
  81. $cache = $this->driver;
  82. $method = new \ReflectionMethod($cache, 'getFilename');
  83. $key = 'item-key';
  84. $expectedDir = array(
  85. '84', 'e0', 'e2', 'e8', '93', 'fe', 'bb', '73', '7a', '0f', 'ee',
  86. '0c', '89', 'd5', '3f', '4b', 'b7', 'fc', 'b4', '4c', '57', 'cd',
  87. 'f3', 'd3', '2c', 'e7', '36', '3f', '5d', '59', '77', '60'
  88. );
  89. $expectedDir = implode(DIRECTORY_SEPARATOR, $expectedDir);
  90. $method->setAccessible(true);
  91. $path = $method->invoke($cache, $key);
  92. $filename = pathinfo($path, PATHINFO_FILENAME);
  93. $dirname = pathinfo($path, PATHINFO_DIRNAME);
  94. $this->assertEquals('item__key', $filename);
  95. $this->assertEquals(DIRECTORY_SEPARATOR . $expectedDir, $dirname);
  96. $this->assertEquals(DIRECTORY_SEPARATOR . $expectedDir . DIRECTORY_SEPARATOR . 'item__key', $path);
  97. }
  98. public function testFileExtensionCorrectlyEscaped()
  99. {
  100. $driver1 = $this->getMock(
  101. 'Doctrine\Common\Cache\FileCache',
  102. array('doFetch', 'doContains', 'doSave'),
  103. array(__DIR__, '.*')
  104. );
  105. $driver2 = $this->getMock(
  106. 'Doctrine\Common\Cache\FileCache',
  107. array('doFetch', 'doContains', 'doSave'),
  108. array(__DIR__, '.php')
  109. );
  110. $doGetStats = new \ReflectionMethod($driver1, 'doGetStats');
  111. $doGetStats->setAccessible(true);
  112. $stats1 = $doGetStats->invoke($driver1);
  113. $stats2 = $doGetStats->invoke($driver2);
  114. $this->assertSame(0, $stats1[Cache::STATS_MEMORY_USAGE]);
  115. $this->assertGreaterThan(0, $stats2[Cache::STATS_MEMORY_USAGE]);
  116. }
  117. /**
  118. * @group DCOM-266
  119. */
  120. public function testFileExtensionSlashCorrectlyEscaped()
  121. {
  122. $driver = $this->getMock(
  123. 'Doctrine\Common\Cache\FileCache',
  124. array('doFetch', 'doContains', 'doSave'),
  125. array(__DIR__ . '/../', DIRECTORY_SEPARATOR . basename(__FILE__))
  126. );
  127. $doGetStats = new \ReflectionMethod($driver, 'doGetStats');
  128. $doGetStats->setAccessible(true);
  129. $stats = $doGetStats->invoke($driver);
  130. $this->assertGreaterThan(0, $stats[Cache::STATS_MEMORY_USAGE]);
  131. }
  132. }