DumperTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 80
c 1
b 0
f 0
dl 0
loc 143
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testDumperThrowsAnExceptionIfUnableToCreateTheDirectory() 0 23 1
A testDumperIgnoresDuplicateTables() 0 26 1
A dataTables() 0 5 1
A testDumperCallsDumpForAllTables() 0 28 2
A testDumperDoesNothingWithAnEmptyListOfTables() 0 17 1
1
<?php
2
/**
3
 * This file is part of graze/sprout.
4
 *
5
 * Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/sprout/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/sprout
12
 */
13
14
namespace Graze\Sprout\Test\Unit\Dump;
15
16
use Graze\Sprout\Config\ConnectionConfigInterface;
17
use Graze\Sprout\Config\SchemaConfigInterface;
18
use Graze\Sprout\Dump\Dumper;
19
use Graze\Sprout\Dump\TableDumperFactory;
20
use Graze\Sprout\Dump\TableDumperInterface;
21
use Graze\Sprout\Test\TestCase;
22
use League\Flysystem\AdapterInterface;
23
use League\Flysystem\Config;
24
use Mockery;
25
use Symfony\Component\Console\Output\OutputInterface;
26
27
class DumperTest extends TestCase
28
{
29
    /** @var Dumper */
30
    private $dumper;
31
    /** @var mixed */
32
    private $config;
33
    /** @var mixed */
34
    private $outputter;
35
    /** @var mixed */
36
    private $factory;
37
    /** @var mixed */
38
    private $filesystem;
39
40
    public function setUp()
41
    {
42
        $this->config = Mockery::mock(SchemaConfigInterface::class);
43
        $this->outputter = Mockery::mock(OutputInterface::class);
44
        $this->factory = Mockery::mock(TableDumperFactory::class);
45
        $this->filesystem = Mockery::mock(AdapterInterface::class);
46
47
        $this->dumper = new Dumper($this->config, $this->outputter, $this->factory, $this->filesystem);
0 ignored issues
show
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

47
        $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

47
        $this->dumper = new Dumper($this->config, /** @scrutinizer ignore-type */ $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

47
        $this->dumper = new Dumper($this->config, $this->outputter, /** @scrutinizer ignore-type */ $this->factory, $this->filesystem);
Loading history...
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

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