/dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/Db/Adapter/TransactionInterfaceTest.php

https://bitbucket.org/jokusafet/magento2 · PHP · 106 lines · 48 code · 5 blank · 53 comment · 1 complexity · 71c9d37038e16a39efaa28a9d6c0a646 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Magento
  22. * @package Magento_Test
  23. * @subpackage integration_tests
  24. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  25. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  26. */
  27. /**
  28. * Test DB "transparent transaction" features in DB adapter substitutes of integration tests
  29. *
  30. * Test behavior of all methods assumed by this interface
  31. * Due to current architecture of DB adapters, they are copy-pasted.
  32. * So we need to make sure all these classes have exactly the same behavior.
  33. */
  34. class Magento_Test_Db_Adapter_TransactionInterfaceTest extends PHPUnit_Framework_TestCase
  35. {
  36. /**
  37. * @param string $class
  38. * @dataProvider transparentTransactionDataProvider
  39. */
  40. public function testBeginTransparentTransaction($class)
  41. {
  42. $adapter = $this->_getAdapterMock($class);
  43. $uniqid = uniqid();
  44. $adapter->expects($this->once())->method('beginTransaction')->will($this->returnValue($uniqid));
  45. $this->assertSame(0, $adapter->getTransactionLevel());
  46. $this->assertEquals($uniqid, $adapter->beginTransparentTransaction());
  47. $this->assertSame(-1, $adapter->getTransactionLevel());
  48. }
  49. /**
  50. * @param string $class
  51. * @dataProvider transparentTransactionDataProvider
  52. */
  53. public function testRollbackTransparentTransaction($class)
  54. {
  55. $adapter = $this->_getAdapterMock($class);
  56. $uniqid = uniqid();
  57. $adapter->expects($this->once())->method('rollback')->will($this->returnValue($uniqid));
  58. $adapter->beginTransparentTransaction();
  59. $this->assertEquals($uniqid, $adapter->rollbackTransparentTransaction());
  60. $this->assertSame(0, $adapter->getTransactionLevel());
  61. }
  62. /**
  63. * @param string $class
  64. * @dataProvider transparentTransactionDataProvider
  65. */
  66. public function testCommitTransparentTransaction($class)
  67. {
  68. $adapter = $this->_getAdapterMock($class);
  69. $uniqid = uniqid();
  70. $adapter->expects($this->once())->method('commit')->will($this->returnValue($uniqid));
  71. $adapter->beginTransparentTransaction();
  72. $this->assertEquals($uniqid, $adapter->commitTransparentTransaction());
  73. $this->assertSame(0, $adapter->getTransactionLevel());
  74. }
  75. /**
  76. * @return array
  77. */
  78. public function transparentTransactionDataProvider()
  79. {
  80. $result = array();
  81. foreach (glob(realpath(__DIR__ . '/../../../../../../../Magento/Test/Db/Adapter') . '/*.php') as $file) {
  82. $suffix = basename($file, '.php');
  83. if (false === strpos($suffix, 'Interface')) {
  84. $result[] = array("Magento_Test_Db_Adapter_{$suffix}");
  85. }
  86. }
  87. return $result;
  88. }
  89. /**
  90. * Instantiate specified adapter class and block all methods that would try to execute real queries
  91. *
  92. * @param string $class
  93. * @return Magento_Test_Db_Adapter_TransactionInterface|PHPUnit_Framework_MockObject_MockObject
  94. */
  95. protected function _getAdapterMock($class)
  96. {
  97. $adapter = $this->getMock($class, array('beginTransaction', 'rollback', 'commit'), array(), '', false);
  98. $this->assertInstanceOf('Magento_Test_Db_Adapter_TransactionInterface', $adapter);
  99. return $adapter;
  100. }
  101. }