AclResourceTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\Config\ConfigOptionsListConstants;
  9. use Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactoryInterface;
  10. class AclResourceTest extends \PHPUnit\Framework\TestCase
  11. {
  12. const RESOURCE_NAME = \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION;
  13. const CONNECTION_NAME = 'connection-name';
  14. const TABLE_PREFIX = 'prefix_';
  15. /**
  16. * @var \Magento\Framework\App\ResourceConnection\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $config;
  19. /**
  20. * @var ConnectionFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $connectionFactory;
  23. /**
  24. * @var \Magento\Framework\App\DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $deploymentConfig;
  27. /**
  28. * @var \Magento\Framework\App\ResourceConnection
  29. */
  30. protected $resource;
  31. /**
  32. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $connection;
  35. protected function setUp()
  36. {
  37. $this->connectionFactory = $this->getMockBuilder(ConnectionFactoryInterface::class)
  38. ->setMethods(['create'])
  39. ->getMockForAbstractClass();
  40. $this->config = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection\ConfigInterface::class)
  41. ->disableOriginalConstructor()
  42. ->setMethods(['getConnectionName'])
  43. ->getMock();
  44. $this->config->expects($this->any())
  45. ->method('getConnectionName')
  46. ->with(self::RESOURCE_NAME)
  47. ->will($this->returnValue(self::CONNECTION_NAME));
  48. $this->deploymentConfig = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
  49. $this->deploymentConfig
  50. ->expects($this->any())
  51. ->method('get')
  52. ->willReturnMap(
  53. [
  54. [
  55. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTIONS . '/connection-name',
  56. null,
  57. [
  58. 'host' => 'localhost',
  59. 'dbname' => 'magento',
  60. 'username' => 'username',
  61. ]
  62. ],
  63. [
  64. ConfigOptionsListConstants::CONFIG_PATH_DB_PREFIX,
  65. null,
  66. self::TABLE_PREFIX
  67. ]
  68. ]
  69. );
  70. $this->connection = $this->getMockForAbstractClass(\Magento\Framework\DB\Adapter\AdapterInterface::class);
  71. $this->connection->expects($this->any())
  72. ->method('getTableName')
  73. ->will($this->returnArgument(0));
  74. $this->resource = new ResourceConnection(
  75. $this->config,
  76. $this->connectionFactory,
  77. $this->deploymentConfig
  78. );
  79. }
  80. /**
  81. * @expectedException \DomainException
  82. * @expectedExceptionMessage Connection "invalid" is not defined
  83. */
  84. public function testGetConnectionFail()
  85. {
  86. $this->resource->getConnectionByName('invalid');
  87. }
  88. public function testGetConnectionInitConnection()
  89. {
  90. $this->connectionFactory->expects($this->once())
  91. ->method('create')
  92. ->will($this->returnValue($this->connection));
  93. $this->assertSame($this->connection, $this->resource->getConnection(self::RESOURCE_NAME));
  94. $this->assertSame($this->connection, $this->resource->getConnection(self::RESOURCE_NAME));
  95. }
  96. /**
  97. * @param array|string $modelEntity
  98. * @param string $expected
  99. *
  100. * @dataProvider getTableNameDataProvider
  101. */
  102. public function testGetTableName($modelEntity, $expected)
  103. {
  104. $this->connectionFactory->expects($this->once())
  105. ->method('create')
  106. ->will($this->returnValue($this->connection));
  107. $this->assertSame($expected, $this->resource->getTableName($modelEntity));
  108. }
  109. /**
  110. * @return array
  111. */
  112. public function getTableNameDataProvider()
  113. {
  114. return [
  115. ['tableName', self::TABLE_PREFIX . 'tableName'],
  116. [['tableName', 'tableSuffix'], self::TABLE_PREFIX . 'tableName_tableSuffix'],
  117. ];
  118. }
  119. /**
  120. * @param array|string $modelEntity
  121. * @param string $tableName
  122. * @param string $mappedName
  123. * @param string $expected
  124. *
  125. * @dataProvider getTableNameMappedDataProvider
  126. */
  127. public function testGetTableNameMapped($modelEntity, $tableName, $mappedName, $expected)
  128. {
  129. $this->connectionFactory->expects($this->once())
  130. ->method('create')
  131. ->will($this->returnValue($this->connection));
  132. $this->resource->setMappedTableName($tableName, $mappedName);
  133. $this->assertSame($expected, $this->resource->getTableName($modelEntity));
  134. }
  135. /**
  136. * @return array
  137. */
  138. public function getTableNameMappedDataProvider()
  139. {
  140. return [
  141. ['tableName', 'tableName', 'mappedTableName', 'mappedTableName'],
  142. [['tableName', 'tableSuffix'], 'tableName', 'mappedTableName', 'mappedTableName_tableSuffix'],
  143. ];
  144. }
  145. public function testGetIdxName()
  146. {
  147. $table = 'table';
  148. $calculatedTableName = self::TABLE_PREFIX . 'table';
  149. $fields = ['field'];
  150. $indexType = 'index_type';
  151. $expectedIdxName = 'idxName';
  152. $this->connection->expects($this->once())
  153. ->method('getIndexName')
  154. ->with($calculatedTableName, $fields, $indexType)
  155. ->will($this->returnValue($expectedIdxName));
  156. $this->connectionFactory->expects($this->once())
  157. ->method('create')
  158. ->will($this->returnValue($this->connection));
  159. $this->assertEquals('idxName', $this->resource->getIdxName($table, $fields, $indexType));
  160. }
  161. public function testGetFkName()
  162. {
  163. $table = 'table';
  164. $calculatedTableName = self::TABLE_PREFIX . 'table';
  165. $refTable = 'ref_table';
  166. $calculatedRefTableName = self::TABLE_PREFIX . 'ref_table';
  167. $columnName = 'columnName';
  168. $refColumnName = 'refColumnName';
  169. $this->connection->expects($this->once())
  170. ->method('getForeignKeyName')
  171. ->with($calculatedTableName, $columnName, $calculatedRefTableName, $refColumnName)
  172. ->will($this->returnValue('fkName'));
  173. $this->connectionFactory->expects($this->once())
  174. ->method('create')
  175. ->will($this->returnValue($this->connection));
  176. $this->assertEquals('fkName', $this->resource->getFkName($table, $columnName, $refTable, $refColumnName));
  177. }
  178. public function testGetTriggerName()
  179. {
  180. $tableName = 'subject_table';
  181. $time = 'before';
  182. $event = 'insert';
  183. $triggerName = 'trg_subject_table_before_insert';
  184. $this->connectionFactory->expects($this->once())
  185. ->method('create')
  186. ->will($this->returnValue($this->connection));
  187. $this->connection->expects($this->once())
  188. ->method('getTriggerName')
  189. ->with($tableName, $time, $event)
  190. ->willReturn($triggerName);
  191. $this->assertSame($triggerName, $this->resource->getTriggerName($tableName, $time, $event));
  192. }
  193. }