|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand; |
|
6
|
|
|
use Generator; |
|
7
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Symfony\Component\Console\Application; |
|
10
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
11
|
|
|
|
|
12
|
|
|
class CreateDatabaseDoctrineTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
public function tearDown() : void |
|
15
|
|
|
{ |
|
16
|
|
|
@unlink(sys_get_temp_dir() . '/test'); |
|
|
|
|
|
|
17
|
|
|
@unlink(sys_get_temp_dir() . '/shard_1'); |
|
|
|
|
|
|
18
|
|
|
@unlink(sys_get_temp_dir() . '/shard_2'); |
|
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function testExecute() : void |
|
22
|
|
|
{ |
|
23
|
|
|
$connectionName = 'default'; |
|
24
|
|
|
$dbName = 'test'; |
|
25
|
|
|
$params = [ |
|
26
|
|
|
'path' => sys_get_temp_dir() . '/' . $dbName, |
|
27
|
|
|
'driver' => 'pdo_sqlite', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
$container = $this->getMockContainer($connectionName, $params); |
|
31
|
|
|
|
|
32
|
|
|
$application = new Application(); |
|
33
|
|
|
$application->add(new CreateDatabaseDoctrineCommand($container->get('doctrine'))); |
|
34
|
|
|
|
|
35
|
|
|
$command = $application->find('doctrine:database:create'); |
|
36
|
|
|
|
|
37
|
|
|
$commandTester = new CommandTester($command); |
|
38
|
|
|
$commandTester->execute( |
|
39
|
|
|
array_merge(['command' => $command->getName()]) |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertContains('Created database ' . sys_get_temp_dir() . '/' . $dbName . ' for connection named ' . $connectionName, $commandTester->getDisplay()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @dataProvider provideShardOption |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testExecuteWithShardAlias(string $shardOption) : void |
|
49
|
|
|
{ |
|
50
|
|
|
$connectionName = 'default'; |
|
51
|
|
|
$params = [ |
|
52
|
|
|
'dbname' => 'test', |
|
53
|
|
|
'memory' => true, |
|
54
|
|
|
'driver' => 'pdo_sqlite', |
|
55
|
|
|
'global' => [ |
|
56
|
|
|
'driver' => 'pdo_sqlite', |
|
57
|
|
|
'dbname' => 'test', |
|
58
|
|
|
'path' => sys_get_temp_dir() . '/global', |
|
59
|
|
|
], |
|
60
|
|
|
'shards' => [ |
|
61
|
|
|
'foo' => [ |
|
62
|
|
|
'id' => 1, |
|
63
|
|
|
'path' => sys_get_temp_dir() . '/shard_1', |
|
64
|
|
|
'driver' => 'pdo_sqlite', |
|
65
|
|
|
], |
|
66
|
|
|
'bar' => [ |
|
67
|
|
|
'id' => 2, |
|
68
|
|
|
'path' => sys_get_temp_dir() . '/shard_2', |
|
69
|
|
|
'driver' => 'pdo_sqlite', |
|
70
|
|
|
], |
|
71
|
|
|
], |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
$container = $this->getMockContainer($connectionName, $params); |
|
75
|
|
|
|
|
76
|
|
|
$application = new Application(); |
|
77
|
|
|
$application->add(new CreateDatabaseDoctrineCommand($container->get('doctrine'))); |
|
78
|
|
|
|
|
79
|
|
|
$command = $application->find('doctrine:database:create'); |
|
80
|
|
|
|
|
81
|
|
|
$commandTester = new CommandTester($command); |
|
82
|
|
|
$commandTester->execute(['command' => $command->getName(), $shardOption => 1]); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertContains('Created database ' . sys_get_temp_dir() . '/shard_1 for connection named ' . $connectionName, $commandTester->getDisplay()); |
|
85
|
|
|
|
|
86
|
|
|
$commandTester = new CommandTester($command); |
|
87
|
|
|
$commandTester->execute(['command' => $command->getName(), $shardOption => 2]); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertContains('Created database ' . sys_get_temp_dir() . '/shard_2 for connection named ' . $connectionName, $commandTester->getDisplay()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function provideShardOption() : Generator |
|
93
|
|
|
{ |
|
94
|
|
|
yield 'full name' => ['--shard']; |
|
95
|
|
|
yield 'short name' => ['-s']; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param mixed[]|null $params Connection parameters |
|
100
|
|
|
*/ |
|
101
|
|
View Code Duplication |
private function getMockContainer(string $connectionName, array $params = null) : MockObject |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
// Mock the container and everything you'll need here |
|
104
|
|
|
$mockDoctrine = $this->getMockBuilder('Doctrine\Persistence\ManagerRegistry') |
|
105
|
|
|
->getMock(); |
|
106
|
|
|
|
|
107
|
|
|
$mockDoctrine->expects($this->any()) |
|
108
|
|
|
->method('getDefaultConnectionName') |
|
109
|
|
|
->withAnyParameters() |
|
110
|
|
|
->willReturn($connectionName); |
|
111
|
|
|
|
|
112
|
|
|
$mockConnection = $this->getMockBuilder('Doctrine\DBAL\Connection') |
|
113
|
|
|
->disableOriginalConstructor() |
|
114
|
|
|
->setMethods(['getParams']) |
|
115
|
|
|
->getMockForAbstractClass(); |
|
116
|
|
|
|
|
117
|
|
|
$mockConnection->expects($this->any()) |
|
118
|
|
|
->method('getParams') |
|
119
|
|
|
->withAnyParameters() |
|
120
|
|
|
->willReturn($params); |
|
121
|
|
|
|
|
122
|
|
|
$mockDoctrine->expects($this->any()) |
|
123
|
|
|
->method('getConnection') |
|
124
|
|
|
->withAnyParameters() |
|
125
|
|
|
->willReturn($mockConnection); |
|
126
|
|
|
|
|
127
|
|
|
$mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container') |
|
128
|
|
|
->setMethods(['get']) |
|
129
|
|
|
->getMock(); |
|
130
|
|
|
|
|
131
|
|
|
$mockContainer->expects($this->any()) |
|
132
|
|
|
->method('get') |
|
133
|
|
|
->with('doctrine') |
|
134
|
|
|
->willReturn($mockDoctrine); |
|
135
|
|
|
|
|
136
|
|
|
return $mockContainer; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: