RulesTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Authorization\Test\Unit\Model\ResourceModel;
  7. /**
  8. * Unit test for Rules resource model.
  9. *
  10. * Covers control flow logic.
  11. * The resource saving is covered with integration test in \Magento\Authorization\Model\RulesTest.
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class RulesTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * Test constants
  19. */
  20. const TEST_ROLE_ID = 13;
  21. /**
  22. * @var \Magento\Authorization\Model\ResourceModel\Rules
  23. */
  24. private $model;
  25. /**
  26. * @var \Magento\Framework\Model\ResourceModel\Db\Context|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $contextMock;
  29. /**
  30. * @var \Magento\Framework\Acl\Builder|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $aclBuilderMock;
  33. /**
  34. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $loggerMock;
  37. /**
  38. * @var \Magento\Framework\Acl\RootResource|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $rootResourceMock;
  41. /**
  42. * @var \Magento\Framework\Acl\Data\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $aclDataCacheMock;
  45. /**
  46. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $resourceConnectionMock;
  49. /**
  50. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $connectionMock;
  53. /**
  54. * @var \Magento\Authorization\Model\Rules|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $ruleMock;
  57. protected function setUp()
  58. {
  59. $this->contextMock = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\Context::class)
  60. ->disableOriginalConstructor()
  61. ->setMethods(['getResources'])
  62. ->getMock();
  63. $this->resourceConnectionMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  64. ->disableOriginalConstructor()
  65. ->setMethods(['getConnection', 'getTableName'])
  66. ->getMock();
  67. $this->contextMock->expects($this->once())
  68. ->method('getResources')
  69. ->will($this->returnValue($this->resourceConnectionMock));
  70. $this->connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  71. ->disableOriginalConstructor()
  72. ->setMethods([])
  73. ->getMock();
  74. $this->resourceConnectionMock->expects($this->once())
  75. ->method('getConnection')
  76. ->with('connection')
  77. ->will($this->returnValue($this->connectionMock));
  78. $this->resourceConnectionMock->expects($this->any())
  79. ->method('getTableName')
  80. ->with('authorization_rule', 'connection')
  81. ->will($this->returnArgument(0));
  82. $this->aclBuilderMock = $this->getMockBuilder(\Magento\Framework\Acl\Builder::class)
  83. ->disableOriginalConstructor()
  84. ->setMethods(['getConfigCache'])
  85. ->getMock();
  86. $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  87. ->disableOriginalConstructor()
  88. ->setMethods([])
  89. ->getMock();
  90. $this->rootResourceMock = $this->getMockBuilder(\Magento\Framework\Acl\RootResource::class)
  91. ->disableOriginalConstructor()
  92. ->setMethods([])
  93. ->getMock();
  94. $this->aclDataCacheMock = $this->getMockBuilder(\Magento\Framework\Acl\Data\CacheInterface::class)
  95. ->disableOriginalConstructor()
  96. ->setMethods([])
  97. ->getMock();
  98. $this->aclBuilderMock->expects($this->any())
  99. ->method('getConfigCache')
  100. ->will($this->returnValue($this->aclDataCacheMock));
  101. $this->ruleMock = $this->getMockBuilder(\Magento\Authorization\Model\Rules::class)
  102. ->disableOriginalConstructor()
  103. ->setMethods(['getRoleId'])
  104. ->getMock();
  105. $this->ruleMock->expects($this->any())
  106. ->method('getRoleId')
  107. ->will($this->returnValue(self::TEST_ROLE_ID));
  108. $this->model = new \Magento\Authorization\Model\ResourceModel\Rules(
  109. $this->contextMock,
  110. $this->aclBuilderMock,
  111. $this->loggerMock,
  112. $this->rootResourceMock,
  113. 'connection',
  114. $this->aclDataCacheMock
  115. );
  116. }
  117. /**
  118. * Test save with no resources posted.
  119. */
  120. public function testSaveRelNoResources()
  121. {
  122. $this->connectionMock->expects($this->once())
  123. ->method('beginTransaction');
  124. $this->connectionMock->expects($this->once())
  125. ->method('delete')
  126. ->with('authorization_rule', ['role_id = ?' => self::TEST_ROLE_ID]);
  127. $this->connectionMock->expects($this->once())
  128. ->method('commit');
  129. $this->aclDataCacheMock->expects($this->once())
  130. ->method('clean');
  131. $this->model->saveRel($this->ruleMock);
  132. }
  133. /**
  134. * Test LocalizedException throw case.
  135. *
  136. * @expectedException \Magento\Framework\Exception\LocalizedException
  137. * @expectedExceptionMessage TestException
  138. */
  139. public function testLocalizedExceptionOccurance()
  140. {
  141. $exceptionPhrase = $this->getMockBuilder(\Magento\Framework\Phrase::class)
  142. ->disableOriginalConstructor()
  143. ->setMethods(['render'])
  144. ->getMock();
  145. $exceptionPhrase->expects($this->any())->method('render')->will($this->returnValue('TestException'));
  146. $exception = new \Magento\Framework\Exception\LocalizedException($exceptionPhrase);
  147. $this->connectionMock->expects($this->once())
  148. ->method('beginTransaction');
  149. $this->connectionMock->expects($this->once())
  150. ->method('delete')
  151. ->with('authorization_rule', ['role_id = ?' => self::TEST_ROLE_ID])
  152. ->will($this->throwException($exception));
  153. $this->connectionMock->expects($this->once())->method('rollBack');
  154. $this->model->saveRel($this->ruleMock);
  155. }
  156. /**
  157. * Test generic exception throw case.
  158. */
  159. public function testGenericExceptionOccurance()
  160. {
  161. $exception = new \Exception('GenericException');
  162. $this->connectionMock->expects($this->once())
  163. ->method('beginTransaction');
  164. $this->connectionMock->expects($this->once())
  165. ->method('delete')
  166. ->with('authorization_rule', ['role_id = ?' => self::TEST_ROLE_ID])
  167. ->will($this->throwException($exception));
  168. $this->connectionMock->expects($this->once())->method('rollBack');
  169. $this->loggerMock->expects($this->once())->method('critical')->with($exception);
  170. $this->model->saveRel($this->ruleMock);
  171. }
  172. }