Completed
Push — develop ( 4097f8...d118ae )
by Tom
04:32
created

DumpCommandTest::realDump()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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