DbTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Backup;
  7. use Magento\Backup\Helper\Data;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\Filesystem;
  10. use Magento\Framework\Module\Setup;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. use PHPUnit\Framework\TestCase;
  13. /**
  14. * Provide tests for \Magento\Framework\Backup\Db.
  15. */
  16. class DbTest extends \Magento\TestFramework\Indexer\TestCase
  17. {
  18. public static function setUpBeforeClass()
  19. {
  20. $db = Bootstrap::getInstance()->getBootstrap()
  21. ->getApplication()
  22. ->getDbInstance();
  23. if (!$db->isDbDumpExists()) {
  24. throw new \LogicException('DB dump does not exist.');
  25. }
  26. $db->restoreFromDbDump();
  27. parent::setUpBeforeClass();
  28. }
  29. /**
  30. * Test db backup includes triggers.
  31. *
  32. * @magentoConfigFixture default/system/backup/functionality_enabled 1
  33. * @magentoDataFixture Magento/Framework/Backup/_files/trigger.php
  34. * @magentoDbIsolation disabled
  35. */
  36. public function testBackupIncludesCustomTriggers()
  37. {
  38. $helper = Bootstrap::getObjectManager()->get(Data::class);
  39. $time = time();
  40. $backupManager = Bootstrap::getObjectManager()->get(Factory::class)->create(
  41. Factory::TYPE_DB
  42. )->setBackupExtension(
  43. $helper->getExtensionByType(Factory::TYPE_DB)
  44. )->setTime(
  45. $time
  46. )->setBackupsDir(
  47. $helper->getBackupsDir()
  48. )->setName('test_backup');
  49. $backupManager->create();
  50. $write = Bootstrap::getObjectManager()->get(Filesystem::class)->getDirectoryWrite(DirectoryList::VAR_DIR);
  51. $content = $write->readFile('/backups/' . $time . '_db_testbackup.sql');
  52. $tableName = Bootstrap::getObjectManager()->get(Setup::class)
  53. ->getTable('test_table_with_custom_trigger');
  54. $this->assertRegExp(
  55. '/CREATE TRIGGER test_custom_trigger AFTER INSERT ON '. $tableName . ' FOR EACH ROW/',
  56. $content
  57. );
  58. //Clean up.
  59. $write->delete('/backups/' . $time . '_db_testbackup.sql');
  60. }
  61. /**
  62. * teardown
  63. */
  64. public function tearDown()
  65. {
  66. parent::tearDown();
  67. }
  68. }