TestBase.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Archive;
  9. abstract class TestBase extends \Test\TestCase {
  10. /**
  11. * @var \OC\Archive\Archive
  12. */
  13. protected $instance;
  14. /**
  15. * get the existing test archive
  16. * @return \OC\Archive\Archive
  17. */
  18. abstract protected function getExisting();
  19. /**
  20. * get a new archive for write testing
  21. * @return \OC\Archive\Archive
  22. */
  23. abstract protected function getNew();
  24. public function testGetFiles() {
  25. $this->instance=$this->getExisting();
  26. $allFiles=$this->instance->getFiles();
  27. $expected=array('lorem.txt','logo-wide.png','dir/', 'dir/lorem.txt');
  28. $this->assertEquals(4, count($allFiles), 'only found '.count($allFiles).' out of 4 expected files');
  29. foreach($expected as $file) {
  30. $this->assertContains($file, $allFiles, 'cant find '. $file . ' in archive');
  31. $this->assertTrue($this->instance->fileExists($file), 'file '.$file.' does not exist in archive');
  32. }
  33. $this->assertFalse($this->instance->fileExists('non/existing/file'));
  34. $rootContent=$this->instance->getFolder('');
  35. $expected=array('lorem.txt','logo-wide.png', 'dir/');
  36. $this->assertEquals(3, count($rootContent));
  37. foreach($expected as $file) {
  38. $this->assertContains($file, $rootContent, 'cant find '. $file . ' in archive');
  39. }
  40. $dirContent=$this->instance->getFolder('dir/');
  41. $expected=array('lorem.txt');
  42. $this->assertEquals(1, count($dirContent));
  43. foreach($expected as $file) {
  44. $this->assertContains($file, $dirContent, 'cant find '. $file . ' in archive');
  45. }
  46. }
  47. public function testContent() {
  48. $this->instance=$this->getExisting();
  49. $dir=\OC::$SERVERROOT.'/tests/data';
  50. $textFile=$dir.'/lorem.txt';
  51. $this->assertEquals(file_get_contents($textFile), $this->instance->getFile('lorem.txt'));
  52. $tmpFile=\OCP\Files::tmpFile('.txt');
  53. $this->instance->extractFile('lorem.txt', $tmpFile);
  54. $this->assertEquals(file_get_contents($textFile), file_get_contents($tmpFile));
  55. }
  56. public function testWrite() {
  57. $dir=\OC::$SERVERROOT.'/tests/data';
  58. $textFile=$dir.'/lorem.txt';
  59. $this->instance=$this->getNew();
  60. $this->assertEquals(0, count($this->instance->getFiles()));
  61. $this->instance->addFile('lorem.txt', $textFile);
  62. $this->assertEquals(1, count($this->instance->getFiles()));
  63. $this->assertTrue($this->instance->fileExists('lorem.txt'));
  64. $this->assertFalse($this->instance->fileExists('lorem.txt/'));
  65. $this->assertEquals(file_get_contents($textFile), $this->instance->getFile('lorem.txt'));
  66. $this->instance->addFile('lorem.txt', 'foobar');
  67. $this->assertEquals('foobar', $this->instance->getFile('lorem.txt'));
  68. }
  69. public function testReadStream() {
  70. $dir=\OC::$SERVERROOT.'/tests/data';
  71. $this->instance=$this->getExisting();
  72. $fh=$this->instance->getStream('lorem.txt', 'r');
  73. $this->assertTrue((bool)$fh);
  74. $content=fread($fh, $this->instance->filesize('lorem.txt'));
  75. fclose($fh);
  76. $this->assertEquals(file_get_contents($dir.'/lorem.txt'), $content);
  77. }
  78. public function testWriteStream() {
  79. $dir=\OC::$SERVERROOT.'/tests/data';
  80. $this->instance=$this->getNew();
  81. $fh=$this->instance->getStream('lorem.txt', 'w');
  82. $source=fopen($dir.'/lorem.txt', 'r');
  83. \OCP\Files::streamCopy($source, $fh);
  84. fclose($source);
  85. fclose($fh);
  86. $this->assertTrue($this->instance->fileExists('lorem.txt'));
  87. $this->assertEquals(file_get_contents($dir.'/lorem.txt'), $this->instance->getFile('lorem.txt'));
  88. }
  89. public function testFolder() {
  90. $this->instance=$this->getNew();
  91. $this->assertFalse($this->instance->fileExists('/test'));
  92. $this->assertFalse($this->instance->fileExists('/test/'));
  93. $this->instance->addFolder('/test');
  94. $this->assertTrue($this->instance->fileExists('/test'));
  95. $this->assertTrue($this->instance->fileExists('/test/'));
  96. $this->instance->remove('/test');
  97. $this->assertFalse($this->instance->fileExists('/test'));
  98. $this->assertFalse($this->instance->fileExists('/test/'));
  99. }
  100. public function testExtract() {
  101. $dir=\OC::$SERVERROOT.'/tests/data';
  102. $this->instance=$this->getExisting();
  103. $tmpDir=\OCP\Files::tmpFolder();
  104. $this->instance->extract($tmpDir);
  105. $this->assertEquals(true, file_exists($tmpDir.'lorem.txt'));
  106. $this->assertEquals(true, file_exists($tmpDir.'dir/lorem.txt'));
  107. $this->assertEquals(true, file_exists($tmpDir.'logo-wide.png'));
  108. $this->assertEquals(file_get_contents($dir.'/lorem.txt'), file_get_contents($tmpDir.'lorem.txt'));
  109. \OCP\Files::rmdirr($tmpDir);
  110. }
  111. public function testMoveRemove() {
  112. $dir=\OC::$SERVERROOT.'/tests/data';
  113. $textFile=$dir.'/lorem.txt';
  114. $this->instance=$this->getNew();
  115. $this->instance->addFile('lorem.txt', $textFile);
  116. $this->assertFalse($this->instance->fileExists('target.txt'));
  117. $this->instance->rename('lorem.txt', 'target.txt');
  118. $this->assertTrue($this->instance->fileExists('target.txt'));
  119. $this->assertFalse($this->instance->fileExists('lorem.txt'));
  120. $this->assertEquals(file_get_contents($textFile), $this->instance->getFile('target.txt'));
  121. $this->instance->remove('target.txt');
  122. $this->assertFalse($this->instance->fileExists('target.txt'));
  123. }
  124. public function testRecursive() {
  125. $dir=\OC::$SERVERROOT.'/tests/data';
  126. $this->instance=$this->getNew();
  127. $this->instance->addRecursive('/dir', $dir);
  128. $this->assertTrue($this->instance->fileExists('/dir/lorem.txt'));
  129. $this->assertTrue($this->instance->fileExists('/dir/data.zip'));
  130. $this->assertTrue($this->instance->fileExists('/dir/data.tar.gz'));
  131. }
  132. }