FileCacheTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Doctrine\Tests\Common\Cache;
  3. /**
  4. * @group DCOM-101
  5. */
  6. class FileCacheTest extends \Doctrine\Tests\DoctrineTestCase
  7. {
  8. /**
  9. * @var \Doctrine\Common\Cache\FileCache
  10. */
  11. private $driver;
  12. protected function setUp()
  13. {
  14. $this->driver = $this->getMock(
  15. 'Doctrine\Common\Cache\FileCache',
  16. array('doFetch', 'doContains', 'doSave'),
  17. array(), '', false
  18. );
  19. }
  20. public function getProviderFileName()
  21. {
  22. return array(
  23. //The characters :\/<>"*?| are not valid in Windows filenames.
  24. array('key:1', 'key1'),
  25. array('key\2', 'key2'),
  26. array('key/3', 'key3'),
  27. array('key<4', 'key4'),
  28. array('key>5', 'key5'),
  29. array('key"6', 'key6'),
  30. array('key*7', 'key7'),
  31. array('key?8', 'key8'),
  32. array('key|9', 'key9'),
  33. array('key[0]','key[0]'),
  34. );
  35. }
  36. /**
  37. * @dataProvider getProviderFileName
  38. */
  39. public function testInvalidFilename($key, $expected)
  40. {
  41. $cache = $this->driver;
  42. $method = new \ReflectionMethod($cache, 'getFilename');
  43. $method->setAccessible(true);
  44. $value = $method->invoke($cache, $key);
  45. $actual = pathinfo($value, PATHINFO_FILENAME);
  46. $this->assertEquals($expected, $actual);
  47. }
  48. public function testFilenameCollision()
  49. {
  50. $data['key:0'] = 'key0';
  51. $data['key\0'] = 'key0';
  52. $data['key/0'] = 'key0';
  53. $data['key<0'] = 'key0';
  54. $data['key>0'] = 'key0';
  55. $data['key"0'] = 'key0';
  56. $data['key*0'] = 'key0';
  57. $data['key?0'] = 'key0';
  58. $data['key|0'] = 'key0';
  59. $paths = array();
  60. $cache = $this->driver;
  61. $method = new \ReflectionMethod($cache, 'getFilename');
  62. $method->setAccessible(true);
  63. foreach ($data as $key => $expected) {
  64. $path = $method->invoke($cache, $key);
  65. $actual = pathinfo($path, PATHINFO_FILENAME);
  66. $this->assertNotContains($path, $paths);
  67. $this->assertEquals($expected, $actual);
  68. $paths[] = $path;
  69. }
  70. }
  71. public function testFilenameShouldCreateThePathWithFourSubDirectories()
  72. {
  73. $cache = $this->driver;
  74. $method = new \ReflectionMethod($cache, 'getFilename');
  75. $key = 'item-key';
  76. $expectedDir[] = '84e0e2e893febb73';
  77. $expectedDir[] = '7a0fee0c89d53f4b';
  78. $expectedDir[] = 'b7fcb44c57cdf3d3';
  79. $expectedDir[] = '2ce7363f5d597760';
  80. $expectedDir = implode(DIRECTORY_SEPARATOR, $expectedDir);
  81. $method->setAccessible(true);
  82. $path = $method->invoke($cache, $key);
  83. $filename = pathinfo($path, PATHINFO_FILENAME);
  84. $dirname = pathinfo($path, PATHINFO_DIRNAME);
  85. $this->assertEquals('item-key', $filename);
  86. $this->assertEquals(DIRECTORY_SEPARATOR . $expectedDir, $dirname);
  87. $this->assertEquals(DIRECTORY_SEPARATOR . $expectedDir . DIRECTORY_SEPARATOR . $key, $path);
  88. }
  89. }