ZipLibTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. App::uses('ZipLib', 'Tools.Lib');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class ZipLibTest extends MyCakeTestCase {
  5. public $ZipLib;
  6. public function setUp() {
  7. parent::setUp();
  8. if (isset($this->ZipLib)) {
  9. return;
  10. }
  11. $this->ZipLib = new ZipLib();
  12. //$x = base64_encode(file_get_contents(TMP.'test4.zip')); die(returns($x));
  13. foreach ($this->testFiles as $file => $content) {
  14. $this->_createTestFile($file, $content);
  15. }
  16. }
  17. public function tearDown() {
  18. $this->ZipLib->close();
  19. foreach ($this->testFiles as $file => $content) {
  20. unlink(TMP . $file);
  21. }
  22. $this->_rrmdir(TMP . 'xyz');
  23. $this->_rrmdir(TMP . 'xyz2');
  24. parent::tearDown();
  25. }
  26. public function testOpen() {
  27. $is = $this->ZipLib->open(TMP . 'test_one_folder.zip');
  28. $this->assertTrue($is);
  29. $is = $this->ZipLib->getError();
  30. $this->assertTrue(empty($is));
  31. $is = $this->ZipLib->open(TMP . 'test_invalid.zip');
  32. $this->assertFalse($is);
  33. $is = $this->ZipLib->getError();
  34. $this->out($is);
  35. $this->assertTrue(!empty($is));
  36. $is = $this->ZipLib->getError(true);
  37. $this->out($is);
  38. $this->assertTrue(!empty($is));
  39. }
  40. public function testFilename() {
  41. $is = $this->ZipLib->open(TMP . 'test_one_folder.zip');
  42. $this->assertEquals($this->ZipLib->filename(), 'test_one_folder.zip');
  43. }
  44. public function testSize() {
  45. $this->ZipLib->open(TMP.'test_folder_and_file.zip');
  46. $is = $this->ZipLib->size();
  47. $this->out($is);
  48. $this->assertEquals(5, $is);
  49. }
  50. public function testNum() {
  51. $this->ZipLib->open(TMP . 'test_one_folder.zip');
  52. $res = $this->ZipLib->numFiles();
  53. $this->assertEquals(1, $res);
  54. $this->ZipLib->open(TMP . 'test_folder_and_file.zip');
  55. $res = $this->ZipLib->numFiles();
  56. $this->assertEquals(2, $res);
  57. }
  58. public function testUnzip() {
  59. $this->ZipLib->open(TMP . 'test_folder_and_file.zip');
  60. $res = $this->ZipLib->unzip(TMP . 'xyz');
  61. $this->assertTrue($res);
  62. $this->assertTrue(file_exists(TMP . 'xyz' . DS . 'folder' . DS . 'file.txt'));
  63. $this->assertSame('test', file_get_contents(TMP.'xyz' . DS . 'folder' . DS . 'file.txt'));
  64. $this->ZipLib->open(TMP . 'test_folder_and_file.zip');
  65. $res = $this->ZipLib->unzip(TMP . 'xyz2', true);
  66. $this->assertTrue($res);
  67. $this->assertTrue(file_exists(TMP . 'xyz2' . DS . 'e.txt'));
  68. $this->assertTrue(file_exists(TMP . 'xyz2' . DS . 'file.txt'));
  69. $this->assertSame('test', file_get_contents(TMP . 'xyz2' . DS . 'file.txt'));
  70. }
  71. /**
  72. * Helper method to recursively remove a directory
  73. */
  74. protected function _rrmdir($dir) {
  75. if (!is_dir($dir)) {
  76. return;
  77. }
  78. foreach (glob($dir . '/*') as $file) {
  79. if (is_dir($file)) {
  80. $this->_rrmdir($file);
  81. } else {
  82. unlink($file);
  83. }
  84. }
  85. rmdir($dir);
  86. }
  87. /**
  88. * Helper method to create zip test files
  89. */
  90. public function _createTestFile($file, $content = null) {
  91. if ($content === null) {
  92. $content = $this->testFiles[$file];
  93. }
  94. file_put_contents(TMP . $file, base64_decode($content));
  95. }
  96. public $testFiles = array(
  97. 'test_one_folder.zip' => 'UEsDBBQAAAAAABIdFz2DFtyMAQAAAAEAAAAFAAAAZS50eHR4UEsBAhQAFAAAAAAAEh0XPYMW3IwBAAAAAQAAAAUAAAAAAAAAAQAgAAAAAAAAAGUudHh0UEsFBgAAAAABAAEAMwAAACQAAAAAAA==',
  98. 'test_folder_and_file.zip' => 'UEsDBBQAAAAAABIdFz2DFtyMAQAAAAEAAAAFAAAAZS50eHR4UEsDBBQAAAAAAEsjFz0Mfn/YBAAAAAQAAAAPAAAAZm9sZGVyL2ZpbGUudHh0dGVzdFBLAQIUABQAAAAAABIdFz2DFtyMAQAAAAEAAAAFAAAAAAAAAAEAIAAAAAAAAABlLnR4dFBLAQIUABQAAAAAAEsjFz0Mfn/YBAAAAAQAAAAPAAAAAAAAAAEAIAAAACQAAABmb2xkZXIvZmlsZS50eHRQSwUGAAAAAAIAAgBwAAAAVQAAAAAA',
  99. 'test_invalid.zip' => 'UEsDBBQAAAAAABIdFz2DFtyMAQAAAAEAAAAFAAAsS50eHR4UEsDBBQAAAAAAEsjFz0Mfn/YBAAAAAQAAAAPAAAAZm9sZGVyL2ZpbGUudHh0dGVzdFBLAQIUABQAAAAAABIdFz2DFtyMAQAAAAEAAAAFAAAAAAAAAAEAIAAAAAAAAABlLnR4dFBLAQIUABQAAAAAAEsjFz0Mfn/YBAAAAAQAAAAPAAAAAAAAAAEAIAAAACQAAABmb2xkZXIvZmlsZS50eHRQSwUGAAAAAAIAAgBwAAAAVQAAAAAA',
  100. );
  101. }