Completed
Pull Request — master (#1132)
by Guillaume
02:06
created

DropDatabaseDoctrineTest::provideForceOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
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 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' => [['--force' => true]];
94
        yield 'short name' => [['-f' => true]];
95
    }
96
97
    public function provideIncompatibleDriverOptions(): Generator
98
    {
99
        yield 'full name' => [
100
            [
101
                '--force' => true,
102
                '--if-exists' => true,
103
            ],
104
        ];
105
        yield 'short name' => [
106
            [
107
                '-f' => true,
108
                '-e' => true,
109
            ],
110
        ];
111
    }
112
113
    /**
114
     * @param array|null $params Connection parameters
115
     */
116 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...
117
    {
118
        // Mock the container and everything you'll need here
119
        $mockDoctrine = $this->getMockBuilder('Doctrine\Persistence\ManagerRegistry')
120
            ->getMock();
121
122
        $mockDoctrine->expects($this->any())
123
            ->method('getDefaultConnectionName')
124
            ->withAnyParameters()
125
            ->willReturn($connectionName);
126
127
        $mockConnection = $this->getMockBuilder('Doctrine\DBAL\Connection')
128
            ->disableOriginalConstructor()
129
            ->setMethods(['getParams'])
130
            ->getMockForAbstractClass();
131
132
        $mockConnection->expects($this->any())
133
            ->method('getParams')
134
            ->withAnyParameters()
135
            ->willReturn($params);
136
137
        $mockDoctrine->expects($this->any())
138
            ->method('getConnection')
139
            ->withAnyParameters()
140
            ->willReturn($mockConnection);
141
142
        $mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container')
143
            ->setMethods(['get'])
144
            ->getMock();
145
146
        $mockContainer->expects($this->any())
147
            ->method('get')
148
            ->with('doctrine')
149
            ->willReturn($mockDoctrine);
150
151
        return $mockContainer;
152
    }
153
}
154