1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Util\Console\Helper; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use N98\Magento\Command\TestCase; |
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class DatabaseHelperTest |
11
|
|
|
* |
12
|
|
|
* @covers \N98\Util\Console\Helper\DatabaseHelper |
13
|
|
|
*/ |
14
|
|
|
class DatabaseHelperTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @return DatabaseHelper |
18
|
|
|
*/ |
19
|
|
|
protected function getHelper() |
20
|
|
|
{ |
21
|
|
|
$command = $this->getApplication()->find('db:info'); |
|
|
|
|
22
|
|
|
$command->getHelperSet()->setCommand($command); |
23
|
|
|
|
24
|
|
|
return $command->getHelper('database'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @test |
29
|
|
|
*/ |
30
|
|
|
public function testHelperInstance() |
31
|
|
|
{ |
32
|
|
|
$this->assertInstanceOf('\N98\Util\Console\Helper\DatabaseHelper', $this->getHelper()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @test |
37
|
|
|
*/ |
38
|
|
|
public function getConnection() |
39
|
|
|
{ |
40
|
|
|
$this->assertInstanceOf('\PDO', $this->getHelper()->getConnection()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @test |
45
|
|
|
*/ |
46
|
|
|
public function dsn() |
47
|
|
|
{ |
48
|
|
|
$this->assertStringStartsWith('mysql:', $this->getHelper()->dsn()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
*/ |
54
|
|
|
public function mysqlUserHasPrivilege() |
55
|
|
|
{ |
56
|
|
|
$this->assertTrue($this->getHelper()->mysqlUserHasPrivilege('SELECT')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @test |
61
|
|
|
*/ |
62
|
|
|
public function getMysqlVariableValue() |
63
|
|
|
{ |
64
|
|
|
$helper = $this->getHelper(); |
65
|
|
|
|
66
|
|
|
// verify (complex) return value with existing global variable |
67
|
|
|
$actual = $helper->getMysqlVariableValue('version'); |
68
|
|
|
|
69
|
|
|
$this->assertInternalType('array', $actual); |
70
|
|
|
$this->assertCount(1, $actual); |
71
|
|
|
$key = '@@version'; |
72
|
|
|
$this->assertArrayHasKey($key, $actual); |
73
|
|
|
$this->assertInternalType('string', $actual[$key]); |
74
|
|
|
|
75
|
|
|
// quoted |
76
|
|
|
$actual = $helper->getMysqlVariableValue('`version`'); |
77
|
|
|
$this->assertEquals('@@`version`', key($actual)); |
78
|
|
|
|
79
|
|
|
// non-existent global variable |
80
|
|
|
try { |
81
|
|
|
$helper->getMysqlVariableValue('nonexistent'); |
82
|
|
|
$this->fail('An expected exception has not been thrown'); |
83
|
|
|
} catch (RuntimeException $e) { |
84
|
|
|
$this->assertEquals("Failed to query mysql variable 'nonexistent'", $e->getMessage()); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @test |
90
|
|
|
*/ |
91
|
|
|
public function getMysqlVariable() |
92
|
|
|
{ |
93
|
|
|
$helper = $this->getHelper(); |
94
|
|
|
|
95
|
|
|
// behaviour with existing global variable |
96
|
|
|
$actual = $helper->getMysqlVariable('version'); |
97
|
|
|
$this->assertInternalType('string', $actual); |
98
|
|
|
|
99
|
|
|
// behavior with existent session variable (INTEGER) |
100
|
|
|
$helper->getConnection()->query('SET @existent = 14;'); |
101
|
|
|
$actual = $helper->getMysqlVariable('existent', '@'); |
102
|
|
|
$this->assertSame("14", $actual); |
103
|
|
|
|
104
|
|
|
// behavior with non-existent session variable |
105
|
|
|
$actual = $helper->getMysqlVariable('nonexistent', '@'); |
106
|
|
|
$this->assertNull($actual); |
107
|
|
|
|
108
|
|
|
// behavior with non-existent global variable |
109
|
|
|
try { |
110
|
|
|
$helper->getMysqlVariable('nonexistent'); |
111
|
|
|
$this->fail('An expected Exception has not been thrown'); |
112
|
|
|
} catch (RuntimeException $e) { |
113
|
|
|
// test against the mysql error message |
114
|
|
|
$this->assertStringEndsWith( |
115
|
|
|
"SQLSTATE[HY000]: 1193: Unknown system variable 'nonexistent'", |
116
|
|
|
$e->getMessage() |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// invalid variable type |
121
|
|
|
try { |
122
|
|
|
$helper->getMysqlVariable('nonexistent', '@@@'); |
123
|
|
|
$this->fail('An expected Exception has not been thrown'); |
124
|
|
|
} catch (InvalidArgumentException $e) { |
125
|
|
|
// test against the mysql error message |
126
|
|
|
$this->assertEquals( |
127
|
|
|
'Invalid mysql variable type "@@@", must be "@@" (system) or "@" (session)', |
128
|
|
|
$e->getMessage() |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @test |
135
|
|
|
*/ |
136
|
|
|
public function getTables() |
137
|
|
|
{ |
138
|
|
|
$helper = $this->getHelper(); |
139
|
|
|
|
140
|
|
|
$tables = $helper->getTables(); |
141
|
|
|
$this->assertInternalType('array', $tables); |
142
|
|
|
$this->assertContains('admin_user', $tables); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @test |
147
|
|
|
*/ |
148
|
|
|
public function resolveTables() |
149
|
|
|
{ |
150
|
|
|
$tables = $this->getHelper()->resolveTables(array('catalog\_*')); |
151
|
|
|
$this->assertContains('catalog_product_entity', $tables); |
152
|
|
|
$this->assertNotContains('catalogrule', $tables); |
153
|
|
|
|
154
|
|
|
$definitions = array( |
155
|
|
|
'catalog_glob' => array('tables' => array('catalog\_*')), |
156
|
|
|
'directory' => array('tables' => array('directory_country directory_country_format')), |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
$tables = $this->getHelper()->resolveTables( |
160
|
|
|
array('@catalog_glob', '@directory'), |
161
|
|
|
$definitions |
162
|
|
|
); |
163
|
|
|
$this->assertContains('catalog_product_entity', $tables); |
164
|
|
|
$this->assertContains('directory_country', $tables); |
165
|
|
|
$this->assertNotContains('catalogrule', $tables); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: