Completed
Push — develop ( 9739cc...72819a )
by Christian
02:34
created

DumpCommandTest::testWithIncludeOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Database;
4
5
use N98\Magento\Command\TestCase;
6
use SplFileInfo;
7
8
/**
9
 * @see \N98\Magento\Command\Database\DumpCommand
10
 */
11
class DumpCommandTest extends TestCase
12
{
13
    public function testExecute()
14
    {
15
        $input = [
16
            'command'        => 'db:dump',
17
            '--add-time'     => true,
18
            '--only-command' => true,
19
            '--force'        => true,
20
            '--compression'  => 'gz',
21
        ];
22
23
        $this->assertDisplayContains($input, 'mysqldump');
24
        $this->assertDisplayContains($input, '.sql');
25
        $this->assertDisplayContains($input, ".sql.gz");
26
    }
27
28
    /**
29
     * @see filenamePatterns
30
     */
31
    public function provideFilenamePatternsAndOptions()
32
    {
33
        return [
34
            # testAddTimeAutogenerated
35
            ['/^.*[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}\.sql$/', []],
36
            # testAddTimePrefixAutogenerated
37
            ['/^[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}.*\.sql$/', ['--add-time' => 'prefix']],
38
            # testAddTimeFilenameSpecified
39
            ['/^.*[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}\.sql.gz$/', ['--compression' => 'gzip']],
40
            # testAddTimeFilenameSpecified
41
            ['/^foo_[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}\.sql$/', ['filename' => 'foo.sql']],
42
            # testAddTimePrefixFilenameSpecified
43
            ['/^[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}_foo\.sql$/', ['filename' => 'foo.sql', '--add-time' => 'prefix']],
44
            # testAddTimeOffFilenameSpecified
45
            ['/^foo.sql$/', ['filename' => 'foo.sql', '--add-time' => 'no']],
46
            # testAddTimeFilenameSpecifiedRelative
47
            ['/^..\/foo_[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}\.sql$/', ['filename' => '../foo.sql']],
48
        ];
49
    }
50
51
    /**
52
     * @test
53
     * @dataProvider provideFilenamePatternsAndOptions
54
     *
55
     * @param string $regex
56
     * @param array $options
57
     * @return void
58
     */
59
    public function filenamePatterns($regex, array $options)
60
    {
61
        $mandatory = [
62
            'command'               => 'db:dump',
63
            '--force'               => true,
64
            '--print-only-filename' => true,
65
            '--dry-run'             => null,
66
        ];
67
68
        $defaults = [
69
            '--add-time' => true,
70
        ];
71
72
        $this->assertDisplayRegExp($mandatory + $options + $defaults, $regex);
73
    }
74
75
    public function testWithStripOption()
76
    {
77
        $input = [
78
            'command'        => 'db:dump',
79
            '--add-time'     => true,
80
            '--only-command' => true,
81
            '--force'        => true,
82
            '--strip'        => '@development not_existing_table_1',
83
            '--compression'  => 'gzip',
84
        ];
85
86
        $dbConfig = $this->getDatabaseConnection()->getConfig();
87
        $db = $dbConfig['dbname'];
88
89
        $this->assertDisplayRegExp($input, "/--ignore-table=$db.customer_entity/");
90
        $this->assertDisplayRegExp($input, "/--ignore-table=$db.customer_address_entity/");
91
        $this->assertDisplayRegExp($input, "/--ignore-table=$db.sales_order/");
92
        $this->assertDisplayRegExp($input, "/--ignore-table=$db.sales_order_item/");
93
        $this->assertDisplayRegExp($input, "/--ignore-table=$db.sales_order_item/");
94
        $this->assertDisplayNotContains($input, "not_existing_table_1");
95
        $this->assertDisplayContains($input, ".sql.gz");
96
97
        /**
98
         * Uncompressed
99
         */
100
        $this->assertDisplayNotContains(
101
            [
102
                'command'        => 'db:dump',
103
                '--add-time'     => true,
104
                '--only-command' => true,
105
                '--force'        => true,
106
                '--strip'        => '@development',
107
            ],
108
            ".sql.gz"
109
        );
110
    }
111
112
    public function testWithExcludeOption()
113
    {
114
        $input = [
115
            'command'        => 'db:dump',
116
            '--add-time'     => true,
117
            '--only-command' => true,
118
            '--force'        => true,
119
            '--exclude'      => 'core_config_data',
120
            '--compression'  => 'gzip',
121
        ];
122
123
        $dbConfig = $this->getDatabaseConnection()->getConfig();
124
        $db = $dbConfig['dbname'];
125
126
        $this->assertDisplayRegExp($input, "/--ignore-table=$db.core_config_data/");
127
        $this->assertDisplayNotContains($input, "not_existing_table_1");
128
        $this->assertDisplayContains($input, ".sql.gz");
129
130
        /**
131
         * Uncompressed
132
         */
133
        $this->assertDisplayNotContains(
134
            [
135
                'command'        => 'db:dump',
136
                '--add-time'     => true,
137
                '--only-command' => true,
138
                '--force'        => true,
139
                '--exclude'      => 'core_config_data',
140
            ],
141
            ".sql.gz"
142
        );
143
    }
144
145
    public function testWithIncludeOptions()
146
    {
147
        $input = [
148
            'command'        => 'db:dump',
149
            '--add-time'     => true,
150
            '--only-command' => true,
151
            '--force'        => true,
152
            '--include'      => 'core_config_data',
153
            '--compression'  => 'gzip',
154
        ];
155
156
        $dbConfig = $this->getDatabaseConnection()->getConfig();
157
        $db = $dbConfig['dbname'];
158
159
        $this->assertDisplayNotContains($input, "--ignore-table=$db.core_config_data");
160
        $this->assertDisplayContains($input, "--ignore-table=$db.catalog_product_entity");
161
        $this->assertDisplayContains($input, ".sql.gz");
162
    }
163
164
    public function testWithIncludeExcludeOptions()
165
    {
166
        $input = [
167
            'command'        => 'db:dump',
168
            '--add-time'     => true,
169
            '--only-command' => true,
170
            '--force'        => true,
171
            '--include'      => '@development',
172
        ];
173
174
        $dbConfig = $this->getDatabaseConnection()->getConfig();
175
        $db = $dbConfig['dbname'];
176
177
        $this->assertDisplayNotContains($input, "--ignore-table=$db.customer_entity");
178
        $this->assertDisplayNotContains($input, "--ignore-table=$db.customer_address_entity");
179
        $this->assertDisplayNotContains($input, "--ignore-table=$db.admin_user");
180
        $this->assertDisplayContains($input, "--ignore-table=$db.catalog_product_entity");
181
        $this->assertDisplayNotContains($input, ".sql.gz");
182
183
        $input['--exclude'] = '@admin';
184
        $this->assertDisplayNotContains($input, "--ignore-table=$db.customer_entity");
185
        $this->assertDisplayNotContains($input, "--ignore-table=$db.customer_address_entity");
186
        $this->assertDisplayContains($input, "--ignore-table=$db.admin_user");
187
        $this->assertDisplayContains($input, "--ignore-table=$db.catalog_product_entity");
188
    }
189
190
    /**
191
     * @test
192
     * @link https://github.com/netz98/n98-magerun2/issues/200
193
     */
194
    public function realDump()
195
    {
196
        $dumpFile = new SplFileInfo($this->getTestMagentoRoot() . '/test-dump.sql');
197
        if ($dumpFile->isReadable()) {
198
            $this->assertTrue(unlink($dumpFile), 'Precondition to unlink that the file does not exists');
199
        }
200
        $this->assertFalse(is_readable($dumpFile), 'Precondition that the file does not exists');
201
202
        $this->assertExecute(
203
            [
204
                'command'  => 'db:dump',
205
                '--strip'  => '@stripped',
206
                'filename' => $dumpFile,
207
            ]
208
        );
209
210
        $this->assertTrue($dumpFile->isReadable(), 'File was created');
211
        // dump should be larger than quarter a megabyte
212
        $this->assertGreaterThan(250000, $dumpFile->getSize());
213
    }
214
}
215