Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AclResourceTest.php
Go to the documentation of this file.
1 <?php
8 
12 
13 class AclResourceTest extends \PHPUnit\Framework\TestCase
14 {
16  const CONNECTION_NAME = 'connection-name';
17  const TABLE_PREFIX = 'prefix_';
18 
22  protected $config;
23 
27  protected $connectionFactory;
28 
32  private $deploymentConfig;
33 
37  protected $resource;
38 
42  private $connection;
43 
44  protected function setUp()
45  {
46  $this->connectionFactory = $this->getMockBuilder(ConnectionFactoryInterface::class)
47  ->setMethods(['create'])
48  ->getMockForAbstractClass();
49  $this->config = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection\ConfigInterface::class)
50  ->disableOriginalConstructor()
51  ->setMethods(['getConnectionName'])
52  ->getMock();
53  $this->config->expects($this->any())
54  ->method('getConnectionName')
55  ->with(self::RESOURCE_NAME)
56  ->will($this->returnValue(self::CONNECTION_NAME));
57 
58  $this->deploymentConfig = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
59  $this->deploymentConfig
60  ->expects($this->any())
61  ->method('get')
62  ->willReturnMap(
63  [
64  [
66  null,
67  [
68  'host' => 'localhost',
69  'dbname' => 'magento',
70  'username' => 'username',
71  ]
72  ],
73  [
75  null,
76  self::TABLE_PREFIX
77  ]
78  ]
79  );
80 
81  $this->connection = $this->getMockForAbstractClass(\Magento\Framework\DB\Adapter\AdapterInterface::class);
82  $this->connection->expects($this->any())
83  ->method('getTableName')
84  ->will($this->returnArgument(0));
85 
86  $this->resource = new ResourceConnection(
87  $this->config,
88  $this->connectionFactory,
89  $this->deploymentConfig
90  );
91  }
92 
97  public function testGetConnectionFail()
98  {
99  $this->resource->getConnectionByName('invalid');
100  }
101 
103  {
104  $this->connectionFactory->expects($this->once())
105  ->method('create')
106  ->will($this->returnValue($this->connection));
107  $this->assertSame($this->connection, $this->resource->getConnection(self::RESOURCE_NAME));
108  $this->assertSame($this->connection, $this->resource->getConnection(self::RESOURCE_NAME));
109  }
110 
117  public function testGetTableName($modelEntity, $expected)
118  {
119  $this->connectionFactory->expects($this->once())
120  ->method('create')
121  ->will($this->returnValue($this->connection));
122  $this->assertSame($expected, $this->resource->getTableName($modelEntity));
123  }
124 
128  public function getTableNameDataProvider()
129  {
130  return [
131  ['tableName', self::TABLE_PREFIX . 'tableName'],
132  [['tableName', 'tableSuffix'], self::TABLE_PREFIX . 'tableName_tableSuffix'],
133  ];
134  }
135 
144  public function testGetTableNameMapped($modelEntity, $tableName, $mappedName, $expected)
145  {
146  $this->connectionFactory->expects($this->once())
147  ->method('create')
148  ->will($this->returnValue($this->connection));
149  $this->resource->setMappedTableName($tableName, $mappedName);
150  $this->assertSame($expected, $this->resource->getTableName($modelEntity));
151  }
152 
157  {
158  return [
159  ['tableName', 'tableName', 'mappedTableName', 'mappedTableName'],
160  [['tableName', 'tableSuffix'], 'tableName', 'mappedTableName', 'mappedTableName_tableSuffix'],
161  ];
162  }
163 
164  public function testGetIdxName()
165  {
166  $table = 'table';
167  $calculatedTableName = self::TABLE_PREFIX . 'table';
168  $fields = ['field'];
169  $indexType = 'index_type';
170  $expectedIdxName = 'idxName';
171 
172  $this->connection->expects($this->once())
173  ->method('getIndexName')
174  ->with($calculatedTableName, $fields, $indexType)
175  ->will($this->returnValue($expectedIdxName));
176  $this->connectionFactory->expects($this->once())
177  ->method('create')
178  ->will($this->returnValue($this->connection));
179 
180  $this->assertEquals('idxName', $this->resource->getIdxName($table, $fields, $indexType));
181  }
182 
183  public function testGetFkName()
184  {
185  $table = 'table';
186  $calculatedTableName = self::TABLE_PREFIX . 'table';
187  $refTable = 'ref_table';
188  $calculatedRefTableName = self::TABLE_PREFIX . 'ref_table';
189  $columnName = 'columnName';
190  $refColumnName = 'refColumnName';
191 
192  $this->connection->expects($this->once())
193  ->method('getForeignKeyName')
194  ->with($calculatedTableName, $columnName, $calculatedRefTableName, $refColumnName)
195  ->will($this->returnValue('fkName'));
196  $this->connectionFactory->expects($this->once())
197  ->method('create')
198  ->will($this->returnValue($this->connection));
199 
200  $this->assertEquals('fkName', $this->resource->getFkName($table, $columnName, $refTable, $refColumnName));
201  }
202 
203  public function testGetTriggerName()
204  {
205  $tableName = 'subject_table';
206  $time = 'before';
207  $event = 'insert';
208  $triggerName = 'trg_subject_table_before_insert';
209 
210  $this->connectionFactory->expects($this->once())
211  ->method('create')
212  ->will($this->returnValue($this->connection));
213  $this->connection->expects($this->once())
214  ->method('getTriggerName')
215  ->with($tableName, $time, $event)
216  ->willReturn($triggerName);
217  $this->assertSame($triggerName, $this->resource->getTriggerName($tableName, $time, $event));
218  }
219 }
$tableName
Definition: trigger.php:13
testGetTableNameMapped($modelEntity, $tableName, $mappedName, $expected)
$fields
Definition: details.phtml:14
$table
Definition: trigger.php:14