Completed
Push — develop ( 18b2a7...266658 )
by Tom
07:22
created

DumpCommandTest::testWithExcludeOption()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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