Passed
Pull Request — master (#3)
by Harry
02:24
created

testDumperThrowsAnExceptionIfUnableToCreateTheDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 23
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Graze\Sprout\Test\Unit\Dump;
4
5
use Graze\Sprout\Config\ConnectionConfigInterface;
6
use Graze\Sprout\Config\SchemaConfigInterface;
7
use Graze\Sprout\Dump\Dumper;
8
use Graze\Sprout\Dump\TableDumperFactory;
9
use Graze\Sprout\Dump\TableDumperInterface;
10
use Graze\Sprout\Test\TestCase;
11
use League\Flysystem\AdapterInterface;
12
use League\Flysystem\Config;
13
use Mockery;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
class DumperTest extends TestCase
17
{
18
    /** @var Dumper */
19
    private $dumper;
20
    /** @var mixed */
21
    private $config;
22
    /** @var mixed */
23
    private $outputter;
24
    /** @var mixed */
25
    private $factory;
26
    /** @var mixed */
27
    private $filesystem;
28
29
    public function setUp()
30
    {
31
        $this->config = Mockery::mock(SchemaConfigInterface::class);
32
        $this->outputter = Mockery::mock(OutputInterface::class);
33
        $this->factory = Mockery::mock(TableDumperFactory::class);
34
        $this->filesystem = Mockery::mock(AdapterInterface::class);
35
36
        $this->dumper = new Dumper($this->config, $this->outputter, $this->factory, $this->filesystem);
0 ignored issues
show
Bug introduced by
$this->config of type Mockery\MockInterface is incompatible with the type Graze\Sprout\Config\SchemaConfigInterface expected by parameter $schemaConfig of Graze\Sprout\Dump\Dumper::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        $this->dumper = new Dumper(/** @scrutinizer ignore-type */ $this->config, $this->outputter, $this->factory, $this->filesystem);
Loading history...
Bug introduced by
$this->factory of type Mockery\MockInterface is incompatible with the type Graze\Sprout\Dump\TableDumperFactory expected by parameter $factory of Graze\Sprout\Dump\Dumper::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        $this->dumper = new Dumper($this->config, $this->outputter, /** @scrutinizer ignore-type */ $this->factory, $this->filesystem);
Loading history...
Bug introduced by
$this->filesystem of type Mockery\MockInterface is incompatible with the type League\Flysystem\AdapterInterface expected by parameter $filesystem of Graze\Sprout\Dump\Dumper::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        $this->dumper = new Dumper($this->config, $this->outputter, $this->factory, /** @scrutinizer ignore-type */ $this->filesystem);
Loading history...
Bug introduced by
$this->outputter of type Mockery\MockInterface is incompatible with the type Symfony\Component\Console\Output\OutputInterface expected by parameter $output of Graze\Sprout\Dump\Dumper::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        $this->dumper = new Dumper($this->config, /** @scrutinizer ignore-type */ $this->outputter, $this->factory, $this->filesystem);
Loading history...
37
    }
38
39
    /**
40
     * @dataProvider dataTables
41
     *
42
     * @param array $tables
43
     */
44
    public function testDumperCallsDumpForAllTables(array $tables = [])
45
    {
46
        $connConfig = Mockery::mock(ConnectionConfigInterface::class);
47
        $this->config->shouldReceive('getConnection')
48
                     ->andReturn($connConfig);
49
        $this->config->shouldReceive('getSchema')
50
                     ->andReturn('schema');
51
52
        $this->outputter->shouldReceive('writeln');
53
        $this->outputter->shouldReceive('write');
54
        $this->outputter->shouldReceive('isDecorated')->andReturn(false);
55
        $this->outputter->shouldReceive('getVerbosity')->andReturn(OutputInterface::VERBOSITY_QUIET);
56
57
        $tableDumper = Mockery::mock(TableDumperInterface::class);
58
59
        $this->factory->shouldReceive('getDumper')->with($connConfig)->andReturn($tableDumper);
60
61
        $this->filesystem->allows()
62
                         ->createDir('/some/path/schema', Mockery::type(Config::class))
63
                         ->andReturns([]);
64
65
        foreach ($tables as $table) {
66
            $tableDumper->shouldReceive('dump')
67
                        ->with('schema', $table, "/some/path/schema/{$table}.sql")
68
                        ->once();
69
        }
70
71
        $this->dumper->dump('/some/path/schema', $tables);
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function dataTables()
78
    {
79
        return [
80
            [['table1', 'table2', 'table3']],
81
            [['table1']],
82
        ];
83
    }
84
85
    public function testDumperIgnoresDuplicateTables()
86
    {
87
        $connConfig = Mockery::mock(ConnectionConfigInterface::class);
88
        $this->config->shouldReceive('getConnection')
89
                     ->andReturn($connConfig);
90
        $this->config->shouldReceive('getSchema')
91
                     ->andReturn('schema');
92
93
        $this->outputter->shouldReceive('writeln');
94
        $this->outputter->shouldReceive('write');
95
        $this->outputter->shouldReceive('isDecorated')->andReturn(false);
96
        $this->outputter->shouldReceive('getVerbosity')->andReturn(OutputInterface::VERBOSITY_QUIET);
97
98
        $tableDumper = Mockery::mock(TableDumperInterface::class);
99
100
        $this->factory->shouldReceive('getDumper')->with($connConfig)->andReturn($tableDumper);
101
102
        $this->filesystem->allows()
103
                         ->createDir('/some/path/schema', Mockery::type(Config::class))
104
                         ->andReturns([]);
105
106
        $tables = ['table1', 'table1'];
107
        $tableDumper->shouldReceive('dump')
108
                    ->with('schema', 'table1', "/some/path/schema/table1.sql")
109
                    ->once();
110
        $this->dumper->dump('/some/path/schema', $tables);
111
    }
112
113
    public function testDumperDoesNothingWithAnEmptyListOfTables()
114
    {
115
        $connConfig = Mockery::mock(ConnectionConfigInterface::class);
116
        $this->config->shouldReceive('getConnection')
117
                     ->andReturn($connConfig);
118
        $this->config->shouldReceive('getSchema')
119
                     ->andReturn('schema');
120
121
        $this->outputter->shouldReceive('writeln')
122
                        ->with('<warning>No tables specified, nothing to do</warning>')
123
                        ->once();
124
125
        $tableDumper = Mockery::mock(TableDumperInterface::class);
126
        $this->factory->shouldReceive('getDumper')->with($connConfig)->andReturn($tableDumper);
127
128
        $tables = [];
129
        $this->dumper->dump('/some/path/schema', $tables);
130
    }
131
132
    /**
133
     * @expectedException \RuntimeException
134
     * @expectedExceptionMessage dump: failed to create directory: /some/path/schema
135
     */
136
    public function testDumperThrowsAnExceptionIfUnableToCreateTheDirectory()
137
    {
138
        $connConfig = Mockery::mock(ConnectionConfigInterface::class);
139
        $this->config->shouldReceive('getConnection')
140
                     ->andReturn($connConfig);
141
        $this->config->shouldReceive('getSchema')
142
                     ->andReturn('schema');
143
144
        $this->outputter->shouldReceive('writeln');
145
        $this->outputter->shouldReceive('write');
146
        $this->outputter->shouldReceive('isDecorated')->andReturn(false);
147
        $this->outputter->shouldReceive('getVerbosity')->andReturn(OutputInterface::VERBOSITY_QUIET);
148
149
        $tableDumper = Mockery::mock(TableDumperInterface::class);
150
151
        $this->factory->shouldReceive('getDumper')->with($connConfig)->andReturn($tableDumper);
152
153
        $this->filesystem->allows()
154
                         ->createDir('/some/path/schema', Mockery::type(Config::class))
155
                         ->andReturns(false);
156
157
        $tables = ['table1', 'table1'];
158
        $this->dumper->dump('/some/path/schema', $tables);
159
    }
160
}
161