Completed
Pull Request — master (#1132)
by Guillaume
01:51
created

testExecuteWithoutOptionForceWillFailWithAttentionMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 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 DropDatabaseDoctrineTest extends TestCase
13
{
14 View Code Duplication
    public function testExecute() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
15
    {
16
        $connectionName = 'default';
17
        $dbName         = 'test';
18
        $params         = [
19
            'url' => 'sqlite:///' . sys_get_temp_dir() . '/test.db',
20
            'path' => sys_get_temp_dir() . '/' . $dbName,
21
            'driver' => 'pdo_sqlite',
22
        ];
23
24
        $container = $this->getMockContainer($connectionName, $params);
25
26
        $application = new Application();
27
        $application->add(new DropDatabaseDoctrineCommand($container->get('doctrine')));
28
29
        $command = $application->find('doctrine:database:drop');
30
31
        $commandTester = new CommandTester($command);
32
        $commandTester->execute(
33
            array_merge(['command' => $command->getName(), '--force' => true])
34
        );
35
36
        $this->assertContains(
37
            sprintf(
38
                'Dropped database %s for connection named %s',
39
                sys_get_temp_dir() . '/' . $dbName,
40
                $connectionName
41
            ),
42
            $commandTester->getDisplay()
43
        );
44
    }
45
46 View Code Duplication
    public function testExecuteWithAlias(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
47
    {
48
        $connectionName = 'default';
49
        $dbName         = 'test';
50
        $params         = [
51
            'url' => 'sqlite:///' . sys_get_temp_dir() . '/test.db',
52
            'path' => sys_get_temp_dir() . '/' . $dbName,
53
            'driver' => 'pdo_sqlite',
54
        ];
55
56
        $container = $this->getMockContainer($connectionName, $params);
57
58
        $application = new Application();
59
        $application->add(new DropDatabaseDoctrineCommand($container->get('doctrine')));
60
61
        $command = $application->find('doctrine:database:drop');
62
63
        $commandTester = new CommandTester($command);
64
        $commandTester->execute(
65
            array_merge(['command' => $command->getName(), '-f' => true])
66
        );
67
68
        $this->assertContains(
69
            sprintf(
70
                'Dropped database %s for connection named %s',
71
                sys_get_temp_dir() . '/' . $dbName,
72
                $connectionName
73
            ),
74
            $commandTester->getDisplay()
75
        );
76
    }
77
78
    public function testItThrowsWhenUsingIfExistsWithAnIncompatibleDriver(): void
79
    {
80
        $connectionName = 'default';
81
        $dbName         = 'test';
82
        $params         = [
83
            'url' => 'sqlite:///' . sys_get_temp_dir() . '/test.db',
84
            'path' => sys_get_temp_dir() . '/' . $dbName,
85
            'driver' => 'pdo_sqlite',
86
        ];
87
88
        $container = $this->getMockContainer($connectionName, $params);
89
90
        $application = new Application();
91
        $application->add(new DropDatabaseDoctrineCommand($container->get('doctrine')));
92
93
        $command = $application->find('doctrine:database:drop');
94
95
        $commandTester = new CommandTester($command);
96
97
        static::expectException(DBALException::class);
98
        $commandTester->execute(
99
            array_merge(['command' => $command->getName(), '-f' => true, '-e' => true])
100
        );
101
102
        $this->assertContains(
103
            sprintf(
104
                'Dropped database %s for connection named %s',
105
                sys_get_temp_dir() . '/' . $dbName,
106
                $connectionName
107
            ),
108
            $commandTester->getDisplay()
109
        );
110
    }
111
112
    public function testExecuteWithoutOptionForceWillFailWithAttentionMessage() : void
113
    {
114
        $connectionName = 'default';
115
        $dbName         = 'test';
116
        $params         = [
117
            'path' => sys_get_temp_dir() . '/' . $dbName,
118
            'driver' => 'pdo_sqlite',
119
        ];
120
121
        $container = $this->getMockContainer($connectionName, $params);
122
123
        $application = new Application();
124
        $application->add(new DropDatabaseDoctrineCommand($container->get('doctrine')));
125
126
        $command = $application->find('doctrine:database:drop');
127
128
        $commandTester = new CommandTester($command);
129
        $commandTester->execute(
130
            array_merge(['command' => $command->getName()])
131
        );
132
133
        $this->assertContains(
134
            sprintf(
135
                'Would drop the database %s for connection named %s.',
136
                sys_get_temp_dir() . '/' . $dbName,
137
                $connectionName
138
            ),
139
            $commandTester->getDisplay()
140
        );
141
        $this->assertContains('Please run the operation with --force to execute', $commandTester->getDisplay());
142
    }
143
144
    /**
145
     * @param array|null $params Connection parameters
146
     */
147 View Code Duplication
    private function getMockContainer(string $connectionName, array $params = null) : MockObject
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
148
    {
149
        // Mock the container and everything you'll need here
150
        $mockDoctrine = $this->getMockBuilder('Doctrine\Persistence\ManagerRegistry')
151
            ->getMock();
152
153
        $mockDoctrine->expects($this->any())
154
            ->method('getDefaultConnectionName')
155
            ->withAnyParameters()
156
            ->willReturn($connectionName);
157
158
        $mockConnection = $this->getMockBuilder('Doctrine\DBAL\Connection')
159
            ->disableOriginalConstructor()
160
            ->setMethods(['getParams'])
161
            ->getMockForAbstractClass();
162
163
        $mockConnection->expects($this->any())
164
            ->method('getParams')
165
            ->withAnyParameters()
166
            ->willReturn($params);
167
168
        $mockDoctrine->expects($this->any())
169
            ->method('getConnection')
170
            ->withAnyParameters()
171
            ->willReturn($mockConnection);
172
173
        $mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container')
174
            ->setMethods(['get'])
175
            ->getMock();
176
177
        $mockContainer->expects($this->any())
178
            ->method('get')
179
            ->with('doctrine')
180
            ->willReturn($mockDoctrine);
181
182
        return $mockContainer;
183
    }
184
}
185