MigrateTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. use PrivateBin\Data\Database;
  3. use PrivateBin\Data\Filesystem;
  4. class MigrateTest extends PHPUnit_Framework_TestCase
  5. {
  6. protected $_model_1;
  7. protected $_model_2;
  8. protected $_path;
  9. protected $_path_instance_1;
  10. protected $_path_instance_2;
  11. public function setUp()
  12. {
  13. /* Setup Routine */
  14. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
  15. $this->_path_instance_1 = $this->_path . DIRECTORY_SEPARATOR . 'instance_1';
  16. $this->_path_instance_2 = $this->_path . DIRECTORY_SEPARATOR . 'instance_2';
  17. if (!is_dir($this->_path)) {
  18. mkdir($this->_path);
  19. }
  20. mkdir($this->_path_instance_1);
  21. mkdir($this->_path_instance_1 . DIRECTORY_SEPARATOR . 'cfg');
  22. mkdir($this->_path_instance_2);
  23. mkdir($this->_path_instance_2 . DIRECTORY_SEPARATOR . 'cfg');
  24. $options = parse_ini_file(CONF_SAMPLE, true);
  25. $options['purge']['limit'] = 0;
  26. $options['model_options']['dir'] = $this->_path_instance_1 . DIRECTORY_SEPARATOR . 'data';
  27. $this->_model_1 = new Filesystem($options['model_options']);
  28. Helper::createIniFile($this->_path_instance_1 . DIRECTORY_SEPARATOR . 'cfg' . DIRECTORY_SEPARATOR . 'conf.php', $options);
  29. $options['model'] = array(
  30. 'class' => 'Database',
  31. );
  32. $options['model_options'] = array(
  33. 'dsn' => 'sqlite:' . $this->_path_instance_2 . DIRECTORY_SEPARATOR . 'test.sq3',
  34. 'usr' => null,
  35. 'pwd' => null,
  36. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  37. );
  38. $this->_model_2 = new Database($options['model_options']);
  39. Helper::createIniFile($this->_path_instance_2 . DIRECTORY_SEPARATOR . 'cfg' . DIRECTORY_SEPARATOR . 'conf.php', $options);
  40. }
  41. public function tearDown()
  42. {
  43. /* Tear Down Routine */
  44. Helper::rmDir($this->_path);
  45. }
  46. public function testMigrate()
  47. {
  48. $this->_model_1->delete(Helper::getPasteId());
  49. $this->_model_2->delete(Helper::getPasteId());
  50. // storing paste & comment
  51. $this->_model_1->create(Helper::getPasteId(), Helper::getPaste());
  52. $this->_model_1->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), Helper::getComment());
  53. // migrate files to database
  54. $output = null;
  55. $exit_code = 255;
  56. exec('php ' . PATH . "bin" . DIRECTORY_SEPARATOR . 'migrate --delete-after ' . $this->_path_instance_1 . DIRECTORY_SEPARATOR . 'cfg '. $this->_path_instance_2 . DIRECTORY_SEPARATOR . 'cfg', $output, $exit_code);
  57. $this->assertEquals(0, $exit_code, 'migrate script exits 0');
  58. $this->assertFalse($this->_model_1->exists(Helper::getPasteId()), 'paste removed after migrating it');
  59. $this->assertFalse($this->_model_1->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment removed after migrating it');
  60. $this->assertTrue($this->_model_2->exists(Helper::getPasteId()), 'paste migrated');
  61. $this->assertTrue($this->_model_2->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment migrated');
  62. // migrate back to files
  63. $exit_code = 255;
  64. exec('php ' . PATH . "bin" . DIRECTORY_SEPARATOR . 'migrate ' . $this->_path_instance_2 . DIRECTORY_SEPARATOR . 'cfg '. $this->_path_instance_1 . DIRECTORY_SEPARATOR . 'cfg', $output, $exit_code);
  65. $this->assertEquals(0, $exit_code, 'migrate script exits 0');
  66. $this->assertTrue($this->_model_1->exists(Helper::getPasteId()), 'paste migrated back');
  67. $this->assertTrue($this->_model_1->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment migrated back');
  68. }
  69. }