Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DbValidatorTest.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Setup\Validator\DbValidator;
12 
13 class DbValidatorTest extends \PHPUnit\Framework\TestCase
14 {
18  private $dbValidator;
19 
23  private $connectionFactory;
24 
28  private $connection;
29 
30  protected function setUp()
31  {
32  $this->connectionFactory = $this->createMock(\Magento\Setup\Module\ConnectionFactory::class);
33  $this->connection = $this->getMockForAbstractClass(\Magento\Framework\DB\Adapter\AdapterInterface::class);
34  $this->connectionFactory->expects($this->any())->method('create')->willReturn($this->connection);
35  $this->dbValidator = new DbValidator($this->connectionFactory);
36  }
37 
38  public function testCheckDatabaseConnection()
39  {
40  $this->connection
41  ->expects($this->exactly(2))
42  ->method('fetchOne')
43  ->with('SELECT version()')
44  ->willReturn('5.6.0-0ubuntu0.12.04.1');
45  $pdo = $this->getMockForAbstractClass(\Zend_Db_Statement_Interface::class, [], '', false);
46  $this->connection
47  ->expects($this->atLeastOnce())
48  ->method('query')
49  ->willReturn($pdo);
50 
51  $listOfPrivileges = [
52  ['SELECT'],
53  ['INSERT'],
54  ['UPDATE'],
55  ['DELETE'],
56  ['CREATE'],
57  ['DROP'],
58  ['INDEX'],
59  ['ALTER'],
60  ['CREATE TEMPORARY TABLES'],
61  ['LOCK TABLES'],
62  ['EXECUTE'],
63  ['CREATE VIEW'],
64  ['SHOW VIEW'],
65  ['CREATE ROUTINE'],
66  ['ALTER ROUTINE'],
67  ['TRIGGER'],
68  ];
69  $accessibleDbs = ['some_db', 'name', 'another_db'];
70 
71  $pdo->expects($this->atLeastOnce())
72  ->method('fetchAll')
73  ->willReturnMap(
74  [
75  [\PDO::FETCH_COLUMN, 0, $accessibleDbs],
76  [\PDO::FETCH_NUM, null, $listOfPrivileges]
77  ]
78  );
79  $this->assertEquals(true, $this->dbValidator->checkDatabaseConnection('name', 'host', 'user', 'password'));
80  $this->assertEquals(true, $this->dbValidator->checkDatabaseConnection('name', 'host:3339', 'user', 'password'));
81  }
82 
88  {
89  $this->connection
90  ->expects($this->once())
91  ->method('fetchOne')
92  ->with('SELECT version()')
93  ->willReturn('5.6.0-0ubuntu0.12.04.1');
94  $pdo = $this->getMockForAbstractClass(\Zend_Db_Statement_Interface::class, [], '', false);
95  $this->connection
96  ->expects($this->atLeastOnce())
97  ->method('query')
98  ->willReturn($pdo);
99  $listOfPrivileges = [['SELECT']];
100  $accessibleDbs = ['some_db', 'name', 'another_db'];
101 
102  $pdo->expects($this->atLeastOnce())
103  ->method('fetchAll')
104  ->willReturnMap(
105  [
106  [\PDO::FETCH_COLUMN, 0, $accessibleDbs],
107  [\PDO::FETCH_NUM, null, $listOfPrivileges]
108  ]
109  );
110  $this->dbValidator->checkDatabaseConnection('name', 'host', 'user', 'password');
111  }
112 
118  {
119  $this->connection
120  ->expects($this->once())
121  ->method('fetchOne')
122  ->with('SELECT version()')
123  ->willReturn('5.6.0-0ubuntu0.12.04.1');
124  $pdo = $this->getMockForAbstractClass(\Zend_Db_Statement_Interface::class, [], '', false);
125  $this->connection
126  ->expects($this->atLeastOnce())
127  ->method('query')
128  ->willReturn($pdo);
129  $accessibleDbs = ['some_db', 'another_db'];
130 
131  $pdo->expects($this->atLeastOnce())
132  ->method('fetchAll')
133  ->willReturn($accessibleDbs);
134  $this->dbValidator->checkDatabaseConnection('name', 'host', 'user', 'password');
135  }
136 
138  {
139  $this->assertEquals(true, $this->dbValidator->checkDatabaseTablePrefix('test'));
140  }
141 
147  {
148  $this->assertEquals(true, $this->dbValidator->checkDatabaseTablePrefix('_wrong_format'));
149  }
150 
156  {
157  $this->assertEquals(
158  true,
159  $this->dbValidator->checkDatabaseTablePrefix('mvbXzXzItSIr0wrZW3gqgV2UKrWiK1Mj7bkBlW72rZW3gqgV2UKrWiK1M')
160  );
161  }
162 
168  {
169  $connectionFactory = $this->createMock(\Magento\Setup\Module\ConnectionFactory::class);
170  $connectionFactory->expects($this->once())->method('create')->willReturn(false);
171  $this->dbValidator = new DbValidator($connectionFactory);
172  $this->dbValidator->checkDatabaseConnection('name', 'host', 'user', 'password');
173  }
174 
180  {
181  $this->connection
182  ->expects($this->once())
183  ->method('fetchOne')
184  ->with('SELECT version()')
185  ->willReturn('5.5.40-0ubuntu0.12.04.1');
186  $this->dbValidator->checkDatabaseConnection('name', 'host', 'user', 'password');
187  }
188 }