|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Symfony\Component\Console\Application; |
|
9
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
10
|
|
|
|
|
11
|
|
|
class DropDatabaseDoctrineTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
public function testExecute() |
|
15
|
|
|
{ |
|
16
|
|
|
$connectionName = 'default'; |
|
17
|
|
|
$dbName = 'test'; |
|
18
|
|
|
$params = array( |
|
19
|
|
|
'url' => "sqlite:///". sys_get_temp_dir() ."/test.db", |
|
20
|
|
|
'path' => sys_get_temp_dir() . "/" . $dbName, |
|
21
|
|
|
'driver' => 'pdo_sqlite', |
|
22
|
|
|
); |
|
23
|
|
|
|
|
24
|
|
|
$application = new Application(); |
|
25
|
|
|
$application->add(new DropDatabaseDoctrineCommand()); |
|
26
|
|
|
|
|
27
|
|
|
$command = $application->find('doctrine:database:drop'); |
|
28
|
|
|
$command->setContainer($this->getMockContainer($connectionName, $params)); |
|
29
|
|
|
|
|
30
|
|
|
$commandTester = new CommandTester($command); |
|
31
|
|
|
$commandTester->execute( |
|
32
|
|
|
array_merge(array('command' => $command->getName(), '--force' => true)) |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertContains("Dropped database for connection named " . sys_get_temp_dir() . "/" . $dbName . "" , $commandTester->getDisplay()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/* |
|
|
|
|
|
|
39
|
|
|
public function testExecuteWithConnectionUrlParam() |
|
40
|
|
|
{ |
|
41
|
|
|
$connectionName = 'default'; |
|
42
|
|
|
$dbName = 'test'; |
|
43
|
|
|
$params = array( |
|
44
|
|
|
'url' => "mysql://[email protected]:3306/" . $dbName, |
|
45
|
|
|
'user' => 'root', |
|
46
|
|
|
'dbname' => $dbName, |
|
47
|
|
|
'driver' => "pdo_mysql" |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$application = new Application(); |
|
51
|
|
|
$application->add(new DropDatabaseDoctrineCommand()); |
|
52
|
|
|
|
|
53
|
|
|
$command = $application->find('doctrine:database:drop'); |
|
54
|
|
|
$command->setContainer($this->getMockContainer($connectionName, $params)); |
|
55
|
|
|
|
|
56
|
|
|
$commandTester = new CommandTester($command); |
|
57
|
|
|
$commandTester->execute( |
|
58
|
|
|
array_merge(array('command' => $command->getName(), '--force' => true, '--if-exists' => true)) |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertContains("Database for connection named `". $dbName ."` doesn't exist. Skipped.", $commandTester->getDisplay()); |
|
62
|
|
|
}*/ |
|
63
|
|
|
|
|
64
|
|
View Code Duplication |
public function testExecuteWithoutOptionForceWillFailWithAttentionMessage() |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
$connectionName = 'default'; |
|
67
|
|
|
$dbName = 'test'; |
|
68
|
|
|
$params = array( |
|
69
|
|
|
'path' => sys_get_temp_dir() . "/" . $dbName, |
|
70
|
|
|
'driver' => 'pdo_sqlite', |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$application = new Application(); |
|
74
|
|
|
$application->add(new DropDatabaseDoctrineCommand()); |
|
75
|
|
|
|
|
76
|
|
|
$command = $application->find('doctrine:database:drop'); |
|
77
|
|
|
$command->setContainer($this->getMockContainer($connectionName, $params)); |
|
78
|
|
|
|
|
79
|
|
|
$commandTester = new CommandTester($command); |
|
80
|
|
|
$commandTester->execute( |
|
81
|
|
|
array_merge(array('command' => $command->getName())) |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertContains("Would drop the database named " . sys_get_temp_dir() . "/" . $dbName . ".", $commandTester->getDisplay()); |
|
85
|
|
|
$this->assertContains("Please run the operation with --force to execute", $commandTester->getDisplay()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $connectionName Connection name |
|
90
|
|
|
* @param array|null $params Connection parameters |
|
91
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
private function getMockContainer($connectionName, $params = null) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
// Mock the container and everything you'll need here |
|
96
|
|
|
$mockDoctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ConnectionRegistry') |
|
97
|
|
|
->getMock(); |
|
98
|
|
|
|
|
99
|
|
|
$mockDoctrine->expects($this->any()) |
|
100
|
|
|
->method('getDefaultConnectionName') |
|
101
|
|
|
->withAnyParameters() |
|
102
|
|
|
->willReturn($connectionName); |
|
103
|
|
|
|
|
104
|
|
|
$mockConnection = $this->getMockBuilder('Doctrine\DBAL\Connection') |
|
105
|
|
|
->disableOriginalConstructor() |
|
106
|
|
|
->setMethods(array('getParams')) |
|
107
|
|
|
->getMockForAbstractClass(); |
|
108
|
|
|
|
|
109
|
|
|
$mockConnection->expects($this->any()) |
|
110
|
|
|
->method('getParams') |
|
111
|
|
|
->withAnyParameters() |
|
112
|
|
|
->willReturn($params); |
|
113
|
|
|
|
|
114
|
|
|
$mockDoctrine->expects($this->any()) |
|
115
|
|
|
->method('getConnection') |
|
116
|
|
|
->withAnyParameters() |
|
117
|
|
|
->willReturn($mockConnection); |
|
118
|
|
|
|
|
119
|
|
|
$mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container') |
|
120
|
|
|
->setMethods(array('get')) |
|
121
|
|
|
->getMock(); |
|
122
|
|
|
|
|
123
|
|
|
$mockContainer->expects($this->any()) |
|
124
|
|
|
->method('get') |
|
125
|
|
|
->with('doctrine') |
|
126
|
|
|
->willReturn($mockDoctrine); |
|
127
|
|
|
|
|
128
|
|
|
return $mockContainer; |
|
129
|
|
|
} |
|
130
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.