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 Mockery; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class DumperTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @dataProvider dataTables |
18
|
|
|
* |
19
|
|
|
* @param array $tables |
20
|
|
|
*/ |
21
|
|
|
public function testDumperCallsDumpForAllTables(array $tables = []) |
22
|
|
|
{ |
23
|
|
|
$config = Mockery::mock(SchemaConfigInterface::class); |
24
|
|
|
$connConfig = Mockery::mock(ConnectionConfigInterface::class); |
25
|
|
|
$config->shouldReceive('getConnection') |
26
|
|
|
->andReturn($connConfig); |
27
|
|
|
$config->shouldReceive('getSchema') |
28
|
|
|
->andReturn('schema'); |
29
|
|
|
|
30
|
|
|
$output = Mockery::mock(OutputInterface::class); |
31
|
|
|
$output->shouldReceive('writeln'); |
32
|
|
|
$output->shouldReceive('write'); |
33
|
|
|
$output->shouldReceive('isDecorated')->andReturn(false); |
34
|
|
|
$output->shouldReceive('getVerbosity')->andReturn(OutputInterface::VERBOSITY_QUIET); |
35
|
|
|
|
36
|
|
|
$factory = Mockery::mock(TableDumperFactory::class); |
37
|
|
|
|
38
|
|
|
$dumper = new Dumper($config, $output, $factory); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$tableDumper = Mockery::mock(TableDumperInterface::class); |
41
|
|
|
|
42
|
|
|
$factory->shouldReceive('getDumper')->with($connConfig)->andReturn($tableDumper); |
43
|
|
|
|
44
|
|
|
foreach ($tables as $table) { |
45
|
|
|
$tableDumper->shouldReceive('dump') |
46
|
|
|
->with('schema', $table, "/some/path/schema/{$table}.sql") |
47
|
|
|
->once(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$dumper->dump('/some/path/schema', $tables); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function dataTables() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
|
|
[['table1', 'table2', 'table3']], |
60
|
|
|
[['table1']], |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testDumperIgnoresDuplicateTables() |
65
|
|
|
{ |
66
|
|
|
$config = Mockery::mock(SchemaConfigInterface::class); |
67
|
|
|
$connConfig = Mockery::mock(ConnectionConfigInterface::class); |
68
|
|
|
$config->shouldReceive('getConnection') |
69
|
|
|
->andReturn($connConfig); |
70
|
|
|
$config->shouldReceive('getSchema') |
71
|
|
|
->andReturn('schema'); |
72
|
|
|
|
73
|
|
|
$output = Mockery::mock(OutputInterface::class); |
74
|
|
|
$output->shouldReceive('writeln'); |
75
|
|
|
$output->shouldReceive('write'); |
76
|
|
|
$output->shouldReceive('isDecorated')->andReturn(false); |
77
|
|
|
$output->shouldReceive('getVerbosity')->andReturn(OutputInterface::VERBOSITY_QUIET); |
78
|
|
|
|
79
|
|
|
$factory = Mockery::mock(TableDumperFactory::class); |
80
|
|
|
|
81
|
|
|
$dumper = new Dumper($config, $output, $factory); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$tableDumper = Mockery::mock(TableDumperInterface::class); |
84
|
|
|
|
85
|
|
|
$factory->shouldReceive('getDumper')->with($connConfig)->andReturn($tableDumper); |
86
|
|
|
|
87
|
|
|
$tables = ['table1', 'table1']; |
88
|
|
|
$tableDumper->shouldReceive('dump') |
89
|
|
|
->with('schema', 'table1', "/some/path/schema/table1.sql") |
90
|
|
|
->once(); |
91
|
|
|
$dumper->dump('/some/path/schema', $tables); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testDumperDoesNothingWithAnEmptyListOfTables() |
95
|
|
|
{ |
96
|
|
|
$config = Mockery::mock(SchemaConfigInterface::class); |
97
|
|
|
$connConfig = Mockery::mock(ConnectionConfigInterface::class); |
98
|
|
|
$config->shouldReceive('getConnection') |
99
|
|
|
->andReturn($connConfig); |
100
|
|
|
$config->shouldReceive('getSchema') |
101
|
|
|
->andReturn('schema'); |
102
|
|
|
|
103
|
|
|
$output = Mockery::mock(OutputInterface::class); |
104
|
|
|
$output->shouldReceive('writeln')->with('<warning>No tables specified, nothing to do</warning>')->once(); |
105
|
|
|
|
106
|
|
|
$factory = Mockery::mock(TableDumperFactory::class); |
107
|
|
|
|
108
|
|
|
$dumper = new Dumper($config, $output, $factory); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$tableDumper = Mockery::mock(TableDumperInterface::class); |
111
|
|
|
$factory->shouldReceive('getDumper')->with($connConfig)->andReturn($tableDumper); |
112
|
|
|
|
113
|
|
|
$tables = []; |
114
|
|
|
$dumper->dump('/some/path/schema', $tables); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|