DumpCommandTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 8
c 5
b 1
f 2
lcom 1
cbo 3
dl 0
loc 164
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommand() 0 13 2
A testExecute() 0 19 1
A provideFilenamePatternsAndOptions() 0 19 1
A filenamePatterns() 0 19 1
B testWithStripOption() 0 46 1
A realDump() 0 22 2
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);
0 ignored issues
show
Bug introduced by
The method add does only exist in N98\Magento\Application, but not in PHPUnit_Framework_MockObject_MockObject.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
27
        $command = $this->getApplication()->find('db:dump');
0 ignored issues
show
Bug introduced by
The method find does only exist in N98\Magento\Application, but not in PHPUnit_Framework_MockObject_MockObject.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method initMagento does only exist in N98\Magento\Application, but not in PHPUnit_Framework_MockObject_MockObject.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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