1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; |
6
|
|
|
use Doctrine\DBAL\DBALException; |
7
|
|
|
use Generator; |
8
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Symfony\Component\Console\Application; |
11
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
12
|
|
|
|
13
|
|
|
class DropDatabaseDoctrineTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @dataProvider provideForceOption |
17
|
|
|
*/ |
18
|
|
|
public function testExecute(array $options) : void |
19
|
|
|
{ |
20
|
|
|
$connectionName = 'default'; |
21
|
|
|
$dbName = 'test'; |
22
|
|
|
$params = [ |
23
|
|
|
'url' => 'sqlite:///' . sys_get_temp_dir() . '/test.db', |
24
|
|
|
'path' => sys_get_temp_dir() . '/' . $dbName, |
25
|
|
|
'driver' => 'pdo_sqlite', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
$container = $this->getMockContainer($connectionName, $params); |
29
|
|
|
|
30
|
|
|
$application = new Application(); |
31
|
|
|
$application->add(new DropDatabaseDoctrineCommand($container->get('doctrine'))); |
32
|
|
|
|
33
|
|
|
$command = $application->find('doctrine:database:drop'); |
34
|
|
|
|
35
|
|
|
$commandTester = new CommandTester($command); |
36
|
|
|
$commandTester->execute( |
37
|
|
|
array_merge(['command' => $command->getName()], $options) |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$this->assertContains( |
41
|
|
|
sprintf( |
42
|
|
|
'Dropped database %s for connection named %s', |
43
|
|
|
sys_get_temp_dir() . '/' . $dbName, |
44
|
|
|
$connectionName |
45
|
|
|
), |
46
|
|
|
$commandTester->getDisplay() |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @dataProvider provideIncompatibleDriverOptions |
52
|
|
|
*/ |
53
|
|
|
public function testItThrowsWhenUsingIfExistsWithAnIncompatibleDriver(array $options) : void |
54
|
|
|
{ |
55
|
|
|
static::expectException(DBALException::class); |
56
|
|
|
$this->testExecute($options); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testExecuteWithoutOptionForceWillFailWithAttentionMessage() : void |
60
|
|
|
{ |
61
|
|
|
$connectionName = 'default'; |
62
|
|
|
$dbName = 'test'; |
63
|
|
|
$params = [ |
64
|
|
|
'path' => sys_get_temp_dir() . '/' . $dbName, |
65
|
|
|
'driver' => 'pdo_sqlite', |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
$container = $this->getMockContainer($connectionName, $params); |
69
|
|
|
|
70
|
|
|
$application = new Application(); |
71
|
|
|
$application->add(new DropDatabaseDoctrineCommand($container->get('doctrine'))); |
72
|
|
|
|
73
|
|
|
$command = $application->find('doctrine:database:drop'); |
74
|
|
|
|
75
|
|
|
$commandTester = new CommandTester($command); |
76
|
|
|
$commandTester->execute( |
77
|
|
|
array_merge(['command' => $command->getName()]) |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$this->assertContains( |
81
|
|
|
sprintf( |
82
|
|
|
'Would drop the database %s for connection named %s.', |
83
|
|
|
sys_get_temp_dir() . '/' . $dbName, |
84
|
|
|
$connectionName |
85
|
|
|
), |
86
|
|
|
$commandTester->getDisplay() |
87
|
|
|
); |
88
|
|
|
$this->assertContains('Please run the operation with --force to execute', $commandTester->getDisplay()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function provideForceOption() : Generator |
92
|
|
|
{ |
93
|
|
|
yield 'full name' => [ |
94
|
|
|
['--force' => true], |
95
|
|
|
]; |
96
|
|
|
yield 'short name' => [ |
97
|
|
|
['-f' => true], |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function provideIncompatibleDriverOptions() : Generator |
102
|
|
|
{ |
103
|
|
|
yield 'full name' => [ |
104
|
|
|
[ |
105
|
|
|
'--force' => true, |
106
|
|
|
'--if-exists' => true, |
107
|
|
|
], |
108
|
|
|
]; |
109
|
|
|
yield 'short name' => [ |
110
|
|
|
[ |
111
|
|
|
'-f' => true, |
112
|
|
|
'--if-exists' => true, |
113
|
|
|
], |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param array|null $params Connection parameters |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
private function getMockContainer(string $connectionName, array $params = null) : MockObject |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
// Mock the container and everything you'll need here |
123
|
|
|
$mockDoctrine = $this->getMockBuilder('Doctrine\Persistence\ManagerRegistry') |
124
|
|
|
->getMock(); |
125
|
|
|
|
126
|
|
|
$mockDoctrine->expects($this->any()) |
127
|
|
|
->method('getDefaultConnectionName') |
128
|
|
|
->withAnyParameters() |
129
|
|
|
->willReturn($connectionName); |
130
|
|
|
|
131
|
|
|
$mockConnection = $this->getMockBuilder('Doctrine\DBAL\Connection') |
132
|
|
|
->disableOriginalConstructor() |
133
|
|
|
->setMethods(['getParams']) |
134
|
|
|
->getMockForAbstractClass(); |
135
|
|
|
|
136
|
|
|
$mockConnection->expects($this->any()) |
137
|
|
|
->method('getParams') |
138
|
|
|
->withAnyParameters() |
139
|
|
|
->willReturn($params); |
140
|
|
|
|
141
|
|
|
$mockDoctrine->expects($this->any()) |
142
|
|
|
->method('getConnection') |
143
|
|
|
->withAnyParameters() |
144
|
|
|
->willReturn($mockConnection); |
145
|
|
|
|
146
|
|
|
$mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container') |
147
|
|
|
->setMethods(['get']) |
148
|
|
|
->getMock(); |
149
|
|
|
|
150
|
|
|
$mockContainer->expects($this->any()) |
151
|
|
|
->method('get') |
152
|
|
|
->with('doctrine') |
153
|
|
|
->willReturn($mockDoctrine); |
154
|
|
|
|
155
|
|
|
return $mockContainer; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.