DatabaseTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Lock\Test\Unit\Backend;
  8. use Magento\Framework\App\DeploymentConfig;
  9. use Magento\Framework\Lock\Backend\Database;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. /**
  12. * @inheritdoc
  13. */
  14. class DatabaseTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ResourceConnection
  18. */
  19. private $resource;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Adapter\AdapterInterface
  22. */
  23. private $connection;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject|\Zend_Db_Statement_Interface
  26. */
  27. private $statement;
  28. /**
  29. * @var ObjectManager
  30. */
  31. private $objectManager;
  32. /**
  33. * @var Database
  34. */
  35. private $database;
  36. /**
  37. * @var DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $deploymentConfig;
  40. /**
  41. * @inheritdoc
  42. */
  43. protected function setUp()
  44. {
  45. $this->connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  46. ->disableOriginalConstructor()
  47. ->getMockForAbstractClass();
  48. $this->resource = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->statement = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)
  52. ->disableOriginalConstructor()
  53. ->getMockForAbstractClass();
  54. $this->resource->expects($this->any())
  55. ->method('getConnection')
  56. ->willReturn($this->connection);
  57. $this->connection->expects($this->any())
  58. ->method('query')
  59. ->willReturn($this->statement);
  60. $this->objectManager = new ObjectManager($this);
  61. $this->deploymentConfig = $this->getMockBuilder(DeploymentConfig::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. /** @var Database $database */
  65. $this->database = $this->objectManager->getObject(
  66. Database::class,
  67. [
  68. 'resource' => $this->resource,
  69. 'deploymentConfig' => $this->deploymentConfig,
  70. ]
  71. );
  72. }
  73. /**
  74. * @throws \Magento\Framework\Exception\AlreadyExistsException
  75. * @throws \Magento\Framework\Exception\InputException
  76. * @throws \Zend_Db_Statement_Exception
  77. */
  78. public function testLock()
  79. {
  80. $this->deploymentConfig
  81. ->method('isDbAvailable')
  82. ->with()
  83. ->willReturn(true);
  84. $this->statement->expects($this->once())
  85. ->method('fetchColumn')
  86. ->willReturn(true);
  87. $this->assertTrue($this->database->lock('testLock'));
  88. }
  89. /**
  90. * @throws \Magento\Framework\Exception\AlreadyExistsException
  91. * @throws \Magento\Framework\Exception\InputException
  92. * @throws \Zend_Db_Statement_Exception
  93. * @expectedException \Magento\Framework\Exception\InputException
  94. */
  95. public function testlockWithTooLongName()
  96. {
  97. $this->deploymentConfig
  98. ->method('isDbAvailable')
  99. ->with()
  100. ->willReturn(true);
  101. $this->database->lock('BbXbyf9rIY5xuAVdviQJmh76FyoeeVHTDpcjmcImNtgpO4Hnz4xk76ZGEyYALvrQu');
  102. }
  103. /**
  104. * @throws \Magento\Framework\Exception\AlreadyExistsException
  105. * @throws \Magento\Framework\Exception\InputException
  106. * @throws \Zend_Db_Statement_Exception
  107. * @expectedException \Magento\Framework\Exception\AlreadyExistsException
  108. */
  109. public function testlockWithAlreadyAcquiredLockInSameSession()
  110. {
  111. $this->deploymentConfig
  112. ->method('isDbAvailable')
  113. ->with()
  114. ->willReturn(true);
  115. $this->statement->expects($this->any())
  116. ->method('fetchColumn')
  117. ->willReturn(true);
  118. $this->database->lock('testLock');
  119. $this->database->lock('differentLock');
  120. }
  121. /**
  122. * @throws \Magento\Framework\Exception\AlreadyExistsException
  123. * @throws \Magento\Framework\Exception\InputException
  124. * @throws \Zend_Db_Statement_Exception
  125. */
  126. public function testLockWithUnavailableDeploymentConfig()
  127. {
  128. $this->deploymentConfig
  129. ->expects($this->atLeast(1))
  130. ->method('isDbAvailable')
  131. ->with()
  132. ->willReturn(false);
  133. $this->assertTrue($this->database->lock('testLock'));
  134. }
  135. /**
  136. * @throws \Magento\Framework\Exception\InputException
  137. * @throws \Zend_Db_Statement_Exception
  138. */
  139. public function testUnlockWithUnavailableDeploymentConfig()
  140. {
  141. $this->deploymentConfig
  142. ->expects($this->atLeast(1))
  143. ->method('isDbAvailable')
  144. ->with()
  145. ->willReturn(false);
  146. $this->assertTrue($this->database->unlock('testLock'));
  147. }
  148. /**
  149. * @throws \Magento\Framework\Exception\InputException
  150. * @throws \Zend_Db_Statement_Exception
  151. */
  152. public function testIsLockedWithUnavailableDB()
  153. {
  154. $this->deploymentConfig
  155. ->expects($this->atLeast(1))
  156. ->method('isDbAvailable')
  157. ->with()
  158. ->willReturn(false);
  159. $this->assertFalse($this->database->isLocked('testLock'));
  160. }
  161. }