ZipLibTest.php 3.6 KB

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