|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Database; |
|
4
|
|
|
|
|
5
|
|
|
use N98\Magento\Command\PHPUnit\TestCase; |
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
8
|
|
|
use SplFileInfo; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @see \N98\Magento\Command\Database\DumpCommand |
|
12
|
|
|
*/ |
|
13
|
|
|
class DumpCommandTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @return Command |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function getCommand() |
|
19
|
|
|
{ |
|
20
|
|
|
$dumpCommand = new DumpCommand(); |
|
21
|
|
|
if (!$dumpCommand->isEnabled()) { |
|
22
|
|
|
$this->markTestSkipped('DumpCommand is not enabled.'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$application = $this->getApplication(); |
|
26
|
|
|
$application->add($dumpCommand); |
|
|
|
|
|
|
27
|
|
|
$command = $this->getApplication()->find('db:dump'); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
return $command; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testExecute() |
|
33
|
|
|
{ |
|
34
|
|
|
$command = $this->getCommand(); |
|
35
|
|
|
|
|
36
|
|
|
$commandTester = new CommandTester($command); |
|
37
|
|
|
$commandTester->execute( |
|
38
|
|
|
array( |
|
39
|
|
|
'command' => $command->getName(), |
|
40
|
|
|
'--add-time' => true, |
|
41
|
|
|
'--only-command' => true, |
|
42
|
|
|
'--force' => true, |
|
43
|
|
|
'--compression' => 'gz' |
|
44
|
|
|
) |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertRegExp('/mysqldump/', $commandTester->getDisplay()); |
|
48
|
|
|
$this->assertRegExp('/\.sql/', $commandTester->getDisplay()); |
|
49
|
|
|
$this->assertContains(".sql.gz", $commandTester->getDisplay()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @see filenamePatterns |
|
54
|
|
|
*/ |
|
55
|
|
|
public function provideFilenamePatternsAndOptions() |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
# testAddTimeAutogenerated |
|
59
|
|
|
['/^.*[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}\.sql$/', []], |
|
60
|
|
|
# testAddTimePrefixAutogenerated |
|
61
|
|
|
['/^[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}.*\.sql$/', ['--add-time' => 'prefix', ]], |
|
62
|
|
|
# testAddTimeFilenameSpecified |
|
63
|
|
|
['/^.*[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}\.sql.gz$/', ['--compression' => 'gzip', ]], |
|
64
|
|
|
# testAddTimeFilenameSpecified |
|
65
|
|
|
['/^foo_[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}\.sql$/', ['filename' => 'foo.sql', ]], |
|
66
|
|
|
# testAddTimePrefixFilenameSpecified |
|
67
|
|
|
['/^[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}_foo\.sql$/', ['filename' => 'foo.sql', '--add-time' => 'prefix', ]], |
|
68
|
|
|
# testAddTimeOffFilenameSpecified |
|
69
|
|
|
['/^foo.sql$/', ['filename' => 'foo.sql', '--add-time' => false, ]], |
|
70
|
|
|
# testAddTimeFilenameSpecifiedRelative |
|
71
|
|
|
['/^..\/foo_[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}\.sql$/', ['filename' => '../foo.sql', ]], |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @test |
|
77
|
|
|
* @dataProvider provideFilenamePatternsAndOptions |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $regex |
|
80
|
|
|
* @param array $options |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function filenamePatterns($regex, array $options) |
|
84
|
|
|
{ |
|
85
|
|
|
$command = $this->getCommand(); |
|
86
|
|
|
|
|
87
|
|
|
$mandatory = array( |
|
88
|
|
|
'command' => $command->getName(), |
|
89
|
|
|
'--force' => true, |
|
90
|
|
|
'--print-only-filename' => true, |
|
91
|
|
|
'--dry-run' => null, |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
$defaults = array( |
|
95
|
|
|
'--add-time' => true, |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
$commandTester = new CommandTester($command); |
|
99
|
|
|
$commandTester->execute($mandatory + $options + $defaults); |
|
100
|
|
|
$this->assertRegExp($regex, $commandTester->getDisplay()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testWithStripOption() |
|
104
|
|
|
{ |
|
105
|
|
|
$command = $this->getCommand(); |
|
106
|
|
|
|
|
107
|
|
|
$this->getApplication()->initMagento(); |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
$commandTester = new CommandTester($command); |
|
110
|
|
|
$commandTester->execute( |
|
111
|
|
|
array( |
|
112
|
|
|
'command' => $command->getName(), |
|
113
|
|
|
'--add-time' => true, |
|
114
|
|
|
'--only-command' => true, |
|
115
|
|
|
'--force' => true, |
|
116
|
|
|
'--strip' => '@development not_existing_table_1', |
|
117
|
|
|
'--compression' => 'gzip' |
|
118
|
|
|
) |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
$dbConfig = $this->getDatabaseConnection()->getConfig(); |
|
122
|
|
|
$db = $dbConfig['dbname']; |
|
123
|
|
|
|
|
124
|
|
|
$this->assertRegExp("/--ignore-table=$db.customer_entity/", $commandTester->getDisplay()); |
|
125
|
|
|
$this->assertRegExp("/--ignore-table=$db.customer_address_entity/", $commandTester->getDisplay()); |
|
126
|
|
|
$this->assertRegExp("/--ignore-table=$db.sales_order/", $commandTester->getDisplay()); |
|
127
|
|
|
$this->assertRegExp("/--ignore-table=$db.sales_order_item/", $commandTester->getDisplay()); |
|
128
|
|
|
$this->assertRegExp("/--ignore-table=$db.sales_order_item/", $commandTester->getDisplay()); |
|
129
|
|
|
$this->assertNotContains("not_existing_table_1", $commandTester->getDisplay()); |
|
130
|
|
|
$this->assertContains(".sql.gz", $commandTester->getDisplay()); |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Uncompressed |
|
135
|
|
|
*/ |
|
136
|
|
|
$commandTester = new CommandTester($command); |
|
137
|
|
|
$commandTester->execute( |
|
138
|
|
|
array( |
|
139
|
|
|
'command' => $command->getName(), |
|
140
|
|
|
'--add-time' => true, |
|
141
|
|
|
'--only-command' => true, |
|
142
|
|
|
'--force' => true, |
|
143
|
|
|
'--strip' => '@development', |
|
144
|
|
|
) |
|
145
|
|
|
); |
|
146
|
|
|
$this->assertNotContains(".sql.gz", $commandTester->getDisplay()); |
|
147
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @test |
|
152
|
|
|
* @link https://github.com/netz98/n98-magerun2/issues/200 |
|
153
|
|
|
*/ |
|
154
|
|
|
public function realDump() |
|
155
|
|
|
{ |
|
156
|
|
|
$dumpFile = new SplFileInfo($this->getTestMagentoRoot() . '/test-dump.sql'); |
|
157
|
|
|
if ($dumpFile->isReadable()) { |
|
158
|
|
|
$this->assertTrue(unlink($dumpFile), 'Precondition to unlink that the file does not exists'); |
|
159
|
|
|
} |
|
160
|
|
|
$this->assertFalse(is_readable($dumpFile), 'Precondition that the file does not exists'); |
|
161
|
|
|
|
|
162
|
|
|
$command = $this->getCommand(); |
|
163
|
|
|
$commandTester = new CommandTester($command); |
|
164
|
|
|
$commandTester->execute( |
|
165
|
|
|
array( |
|
166
|
|
|
'command' => $command->getName(), |
|
167
|
|
|
'--strip' => '@stripped', |
|
168
|
|
|
'filename' => $dumpFile, |
|
169
|
|
|
) |
|
170
|
|
|
); |
|
171
|
|
|
|
|
172
|
|
|
$this->assertTrue($dumpFile->isReadable(), 'File was created'); |
|
173
|
|
|
// dump should be larger than quarter a megabyte |
|
174
|
|
|
$this->assertGreaterThan(250000, $dumpFile->getSize()); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
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: