LocalTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Gaufrette\Functional\Adapter;
  3. use Gaufrette\Filesystem;
  4. use Gaufrette\Adapter\Local;
  5. class LocalTest extends FunctionalTestCase
  6. {
  7. private $directory;
  8. public function setUp()
  9. {
  10. $this->directory = sprintf('%s/filesystem', str_replace('\\', '/', __DIR__));
  11. if (!file_exists($this->directory)) {
  12. mkdir($this->directory);
  13. }
  14. $this->filesystem = new Filesystem(new Local($this->directory));
  15. }
  16. public function tearDown()
  17. {
  18. $this->filesystem = null;
  19. if (file_exists($this->directory)) {
  20. $iterator = new \RecursiveIteratorIterator(
  21. new \RecursiveDirectoryIterator(
  22. $this->directory,
  23. \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS
  24. )
  25. );
  26. foreach ($iterator as $item) {
  27. if ($item->isDir()) {
  28. rmdir(strval($item));
  29. } else {
  30. unlink(strval($item));
  31. }
  32. }
  33. }
  34. }
  35. /**
  36. * @test
  37. * @group functional
  38. */
  39. public function shouldWorkWithSyslink()
  40. {
  41. $dirname = sprintf(
  42. '%s/adapters/aaa',
  43. dirname(__DIR__)
  44. );
  45. $linkname = sprintf(
  46. '%s/adapters/../../../../link',
  47. dirname(__DIR__)
  48. );
  49. @mkdir($dirname);
  50. @unlink($linkname);
  51. symlink($dirname, $linkname);
  52. $this->filesystem = new Filesystem(new Local($linkname));
  53. $this->filesystem->write('test.txt', 'abc 123');
  54. $this->assertSame('abc 123', $this->filesystem->read('test.txt'));
  55. $this->filesystem->delete('test.txt');
  56. @unlink($linkname);
  57. @rmdir($dirname);
  58. }
  59. /**
  60. * @test
  61. * @covers Gaufrette\Adapter\Local
  62. */
  63. public function shouldListingOnlyGivenDirectory()
  64. {
  65. $dirname = sprintf(
  66. '%s/localDir',
  67. $this->directory
  68. );
  69. @mkdir($dirname);
  70. $this->filesystem = new Filesystem(new Local($this->directory));
  71. $this->filesystem->write('aaa.txt', 'some content');
  72. $this->filesystem->write('localDir/test.txt', 'some content');
  73. $dirs = $this->filesystem->listKeys('localDir/test');
  74. $this->assertEmpty($dirs['dirs']);
  75. $this->assertCount(1, $dirs['keys']);
  76. $this->assertEquals('localDir/test.txt', $dirs['keys'][0]);
  77. $dirs = $this->filesystem->listKeys();
  78. $this->assertCount(1, $dirs['dirs']);
  79. $this->assertEquals('localDir', $dirs['dirs'][0]);
  80. $this->assertCount(2, $dirs['keys']);
  81. $this->assertEquals('aaa.txt', $dirs['keys'][0]);
  82. $this->assertEquals('localDir/test.txt', $dirs['keys'][1]);
  83. @unlink($dirname.DIRECTORY_SEPARATOR.'test.txt');
  84. @unlink($this->directory.DIRECTORY_SEPARATOR.'aaa.txt');
  85. @rmdir($dirname);
  86. }
  87. }