Completed
Push — develop ( 7623eb...611a18 )
by Tom
03:37
created

DumpCommandTest::filenamePatterns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
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' => false, ]],
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
    /**
114
     * @test
115
     * @link https://github.com/netz98/n98-magerun2/issues/200
116
     */
117
    public function realDump()
118
    {
119
        $dumpFile = new SplFileInfo($this->getTestMagentoRoot() . '/test-dump.sql');
120
        if ($dumpFile->isReadable()) {
121
            $this->assertTrue(unlink($dumpFile), 'Precondition to unlink that the file does not exists');
122
        }
123
        $this->assertFalse(is_readable($dumpFile), 'Precondition that the file does not exists');
124
125
        $this->assertExecute(
126
            array(
127
                'command'  => 'db:dump',
128
                '--strip'  => '@stripped',
129
                'filename' => $dumpFile,
130
            )
131
        );
132
133
        $this->assertTrue($dumpFile->isReadable(), 'File was created');
134
        // dump should be larger than quarter a megabyte
135
        $this->assertGreaterThan(250000, $dumpFile->getSize());
136
    }
137
}
138