PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/TestOfBackupMySQLDAO.php

https://github.com/devsatish/ThinkUp
PHP | 157 lines | 98 code | 12 blank | 47 comment | 6 complexity | 01a87c36dab2402ec08b80386e19d2b9 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/tests/TestOfBackupMySQLDAO.php
  5. *
  6. * Copyright (c) 2009-2011 Mark Wilkie
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkupapp.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. *
  24. * @author Mark Wilkie <mark[at]bitterpill[dot]org>
  25. * @license http://www.gnu.org/licenses/gpl.html
  26. * @copyright 2009-2011 Mark Wilkie
  27. */
  28. require_once dirname(__FILE__).'/init.tests.php';
  29. require_once THINKUP_ROOT_PATH.'webapp/_lib/extlib/simpletest/autorun.php';
  30. require_once THINKUP_ROOT_PATH.'webapp/config.inc.php';
  31. class TestOfBackupMySQLDAO extends ThinkUpUnitTestCase {
  32. public function setUp() {
  33. parent::setUp();
  34. new BackupMySQLDAO();
  35. $this->pdo = BackupMySQLDAO::$PDO;
  36. }
  37. public function tearDown() {
  38. parent::tearDown();
  39. $zipfile = THINKUP_WEBAPP_PATH . BackupDAO::CACHE_DIR . '/.htthinkup_db_backup.zip';
  40. $backup_dir = THINKUP_WEBAPP_PATH . BackupDAO::CACHE_DIR . '/backup';
  41. if (file_exists($zipfile)) {
  42. unlink($zipfile);
  43. }
  44. if (file_exists($backup_dir)) {
  45. $this->recursiveDelete($backup_dir);
  46. }
  47. }
  48. /**
  49. * Test constructor
  50. */
  51. public function testConstructor() {
  52. $dao = new BackupMySQLDAO();
  53. $this->assertTrue(isset($dao));
  54. }
  55. /**
  56. * test export data
  57. */
  58. public function testExportData() {
  59. $dao = new BackupMySQLDAO();
  60. $export_file = $dao->export();
  61. $this->assertTrue( file_exists($export_file) );
  62. $zip_stats = stat($export_file);
  63. $this->assertTrue($zip_stats['size'] > 0);
  64. $za = new ZipArchive();
  65. $za->open($export_file);
  66. $zip_files = array();
  67. for ($i=0; $i<$za->numFiles;$i++) {
  68. $zfile = $za->statIndex($i);
  69. $zip_files[$zfile['name']] = $zfile['name'];
  70. }
  71. //verify we have create table file
  72. $this->assertTrue($zip_files["create_tables.sql"]);
  73. $za->close();
  74. $q = "show tables";
  75. $q2 = "show create table ";
  76. $stmt = $this->pdo->query($q);
  77. // verify we have all table files
  78. while($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
  79. foreach($data as $key => $value) {
  80. $zfile = '/' . $value .'.txt';
  81. $this->assertTrue($zip_files[$zfile]);
  82. }
  83. }
  84. }
  85. /**
  86. * test import data, no such file
  87. */
  88. public function testImportDataNoFile() {
  89. $dao = new BackupMySQLDAO();
  90. $export_file = $dao->export();
  91. $this->expectException('Exception', 'Unable to open import file: jshshsgsgs-badfile.nofile');
  92. $dao->import('jshshsgsgs-badfile.nofile');
  93. }
  94. /**
  95. * test import data, bad zip file
  96. */
  97. public function testImportDataBadFile() {
  98. $dao = new BackupMySQLDAO();
  99. $zipfile = 'tests/data/backup/bad-zip-archive.zip';
  100. $this->expectException('Exception', 'Unable to open import file, corrupted zip file?: ' . $zipfile);
  101. $dao->import($zipfile);
  102. }
  103. /**
  104. * test import data, good zip file, but data missing
  105. */
  106. public function testImportDataBadFile2() {
  107. $dao = new BackupMySQLDAO();
  108. $zipfile = 'tests/data/backup/bad-zip-archive2.zip';
  109. $this->expectException('Exception', 'Unable to open import file, corrupted zip file?: ' . $zipfile);
  110. $dao->import($zipfile);
  111. }
  112. /**
  113. * test import data
  114. */
  115. public function testImportData() {
  116. $dao = new BackupMySQLDAO();
  117. $export_file = $dao->export();
  118. //$this->pdo->query("drop table tu_plugin_options");
  119. $this->assertTrue( $dao->import($export_file) );
  120. $stmt = $this->pdo->query("show create table tu_plugins");
  121. $data = $stmt->fetch();
  122. $stmt->closeCursor();
  123. $this->assertEqual($data['Table'], 'tu_plugins');
  124. $stmt = $this->pdo->query("select * from tu_plugins");
  125. $data = $stmt->fetch();
  126. $this->assertEqual($data['id'], 1);
  127. $this->assertEqual($data['name'], 'Twitter');
  128. }
  129. public function recursiveDelete($str){
  130. if (is_file($str)){
  131. if (! preg_match("MAKETHISDIRWRITABLE", $str)) {
  132. return @unlink($str);
  133. } else {
  134. return true;
  135. }
  136. }
  137. elseif (is_dir($str)){
  138. $scan = glob(rtrim($str,'/').'/*');
  139. foreach($scan as $index=>$path){
  140. $this->recursiveDelete($path);
  141. }
  142. return @rmdir($str);
  143. }
  144. }
  145. }