TransactionTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB;
  7. use Magento\Framework\Flag;
  8. class TransactionTest extends \PHPUnit\Framework\TestCase
  9. {
  10. protected $objectManager;
  11. /**
  12. * @var \Magento\Framework\DB\Transaction
  13. */
  14. protected $_model;
  15. protected function setUp()
  16. {
  17. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  18. $this->_model = $this->objectManager
  19. ->create(\Magento\Framework\DB\Transaction::class);
  20. }
  21. /**
  22. * @magentoAppArea adminhtml
  23. */
  24. public function testSaveDelete()
  25. {
  26. /** @var Flag $first */
  27. $first = $this->objectManager->create(Flag::class, ['data' => ['flag_code' => 'test1']]);
  28. $first->setFlagData('test1data');
  29. $second = $this->objectManager->create(Flag::class, ['data' => ['flag_code' => 'test2']]);
  30. $second->setFlagData('test2data');
  31. $first->save();
  32. $this->_model->addObject($first)->addObject($second, 'second');
  33. $this->_model->save();
  34. $this->assertNotEmpty($first->getId());
  35. $this->assertNotEmpty($second->getId());
  36. $this->_model->delete();
  37. $test = $this->objectManager->create(Flag::class);
  38. $test->load($first->getId());
  39. $this->assertEmpty($test->getId());
  40. }
  41. /**
  42. * @magentoDbIsolation disabled
  43. */
  44. public function testTransactionLevelDbIsolationDisable()
  45. {
  46. $resourceConnection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  47. ->get(\Magento\Framework\App\ResourceConnection::class);
  48. $this->assertEquals(0, $resourceConnection->getConnection('default')->getTransactionLevel());
  49. }
  50. /**
  51. * @magentoDbIsolation enabled
  52. */
  53. public function testTransactionLevelDbIsolationEnabled()
  54. {
  55. $resourceConnection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  56. ->get(\Magento\Framework\App\ResourceConnection::class);
  57. $this->assertEquals(1, $resourceConnection->getConnection('default')->getTransactionLevel());
  58. }
  59. public function testTransactionLevelDbIsolationDefault()
  60. {
  61. $resourceConnection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  62. ->get(\Magento\Framework\App\ResourceConnection::class);
  63. $this->assertEquals(0, $resourceConnection->getConnection('default')->getTransactionLevel());
  64. }
  65. }