Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ResourceConnectionTest.php
Go to the documentation of this file.
1 <?php
7 
14 
15 class ResourceConnectionTest extends \PHPUnit\Framework\TestCase
16 {
20  private $unit;
21 
25  private $deploymentConfigMock;
26 
30  private $connectionFactoryMock;
31 
35  private $objectManager;
36 
40  private $configMock;
41 
42  protected function setUp()
43  {
44  $this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
45  ->disableOriginalConstructor()
46  ->getMock();
47 
48  $this->connectionFactoryMock = $this->getMockBuilder(ConnectionFactoryInterface::class)
49  ->getMock();
50 
51  $this->configMock = $this->getMockBuilder(ConfigInterface::class)->getMock();
52 
53  $this->objectManager = (new ObjectManager($this));
54  $this->unit = $this->objectManager->getObject(
55  ResourceConnection::class,
56  [
57  'deploymentConfig' => $this->deploymentConfigMock,
58  'connectionFactory' => $this->connectionFactoryMock,
59  'config' => $this->configMock,
60  ]
61  );
62  }
63 
64  public function testGetTablePrefixWithInjectedPrefix()
65  {
67  $resourceConnection = $this->objectManager->getObject(
68  ResourceConnection::class,
69  [
70  'deploymentConfig' => $this->deploymentConfigMock,
71  'connectionFactory' => $this->connectionFactoryMock,
72  'config' => $this->configMock,
73  'tablePrefix' => 'some_prefix'
74  ]
75  );
76 
77  self::assertEquals($resourceConnection->getTablePrefix(), 'some_prefix');
78  }
79 
80  public function testGetTablePrefix()
81  {
82  $this->deploymentConfigMock->expects(self::once())
83  ->method('get')
85  ->willReturn('pref_');
86  self::assertEquals('pref_', $this->unit->getTablePrefix());
87  }
88 
89  public function testGetConnectionByName()
90  {
91  $this->deploymentConfigMock->expects(self::once())->method('get')
93  ->willReturn(['config']);
94  $this->connectionFactoryMock->expects(self::once())->method('create')
95  ->with(['config'])
96  ->willReturn('connection');
97 
98  self::assertEquals('connection', $this->unit->getConnectionByName('default'));
99  }
100 
102  {
103  $unit = $this->objectManager->getObject(
104  ResourceConnection::class,
105  [
106  'deploymentConfig' => $this->deploymentConfigMock,
107  'connections' => ['default_process_' . getmypid() => 'existing_connection']
108  ]
109  );
110  $this->deploymentConfigMock->expects(self::never())->method('get');
111 
112  self::assertEquals('existing_connection', $unit->getConnectionByName('default'));
113  }
114 
115  public function testCloseConnection()
116  {
117  $this->configMock->expects(self::once())->method('getConnectionName')->with('default');
118 
119  $this->unit->closeConnection('default');
120  }
121 }