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

CreateDatabaseDoctrineTest::provideShardOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
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\CreateDatabaseDoctrineCommand;
6
use PHPUnit\Framework\MockObject\MockObject;
7
use PHPUnit\Framework\TestCase;
8
use Symfony\Component\Console\Application;
9
use Symfony\Component\Console\Tester\CommandTester;
10
11
class CreateDatabaseDoctrineTest extends TestCase
12
{
13
    public function tearDown() : void
14
    {
15
        @unlink(sys_get_temp_dir() . '/test');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
16
        @unlink(sys_get_temp_dir() . '/shard_1');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
17
        @unlink(sys_get_temp_dir() . '/shard_2');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
18
    }
19
20
    public function testExecute() : void
21
    {
22
        $connectionName = 'default';
23
        $dbName         = 'test';
24
        $params         = [
25
            'path' => sys_get_temp_dir() . '/' . $dbName,
26
            'driver' => 'pdo_sqlite',
27
        ];
28
29
        $container = $this->getMockContainer($connectionName, $params);
30
31
        $application = new Application();
32
        $application->add(new CreateDatabaseDoctrineCommand($container->get('doctrine')));
33
34
        $command = $application->find('doctrine:database:create');
35
36
        $commandTester = new CommandTester($command);
37
        $commandTester->execute(
38
            array_merge(['command' => $command->getName()])
39
        );
40
41
        $this->assertContains('Created database ' . sys_get_temp_dir() . '/' . $dbName . ' for connection named ' . $connectionName, $commandTester->getDisplay());
42
    }
43
44
    /**
45
     * @dataProvider provideShardOption
46
     */
47
    public function testExecuteWithShardAlias(string $shardOption) : void
48
    {
49
        $connectionName = 'default';
50
        $params         = [
51
            'dbname' => 'test',
52
            'memory' => true,
53
            'driver' => 'pdo_sqlite',
54
            'global' => [
55
                'driver' => 'pdo_sqlite',
56
                'dbname' => 'test',
57
                'path' => sys_get_temp_dir() . '/global',
58
            ],
59
            'shards' => [
60
                'foo' => [
61
                    'id' => 1,
62
                    'path' => sys_get_temp_dir() . '/shard_1',
63
                    'driver' => 'pdo_sqlite',
64
                ],
65
                'bar' => [
66
                    'id' => 2,
67
                    'path' => sys_get_temp_dir() . '/shard_2',
68
                    'driver' => 'pdo_sqlite',
69
                ],
70
            ],
71
        ];
72
73
        $container = $this->getMockContainer($connectionName, $params);
74
75
        $application = new Application();
76
        $application->add(new CreateDatabaseDoctrineCommand($container->get('doctrine')));
77
78
        $command = $application->find('doctrine:database:create');
79
80
        $commandTester = new CommandTester($command);
81
        $commandTester->execute(['command' => $command->getName(), $shardOption => 1]);
82
83
        $this->assertContains('Created database ' . sys_get_temp_dir() . '/shard_1 for connection named ' . $connectionName, $commandTester->getDisplay());
84
85
        $commandTester = new CommandTester($command);
86
        $commandTester->execute(['command' => $command->getName(), $shardOption => 2]);
87
88
        $this->assertContains('Created database ' . sys_get_temp_dir() . '/shard_2 for connection named ' . $connectionName, $commandTester->getDisplay());
89
    }
90
91
    public function provideShardOption(): \Generator
92
    {
93
        yield 'full name' => [
94
            '--shard',
95
        ];
96
        yield 'short name' => [
97
            '-s',
98
        ];
99
    }
100
101
    /**
102
     * @param mixed[]|null $params Connection parameters
103
     */
104 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...
105
    {
106
        // Mock the container and everything you'll need here
107
        $mockDoctrine = $this->getMockBuilder('Doctrine\Persistence\ManagerRegistry')
108
            ->getMock();
109
110
        $mockDoctrine->expects($this->any())
111
            ->method('getDefaultConnectionName')
112
            ->withAnyParameters()
113
            ->willReturn($connectionName);
114
115
        $mockConnection = $this->getMockBuilder('Doctrine\DBAL\Connection')
116
            ->disableOriginalConstructor()
117
            ->setMethods(['getParams'])
118
            ->getMockForAbstractClass();
119
120
        $mockConnection->expects($this->any())
121
            ->method('getParams')
122
            ->withAnyParameters()
123
            ->willReturn($params);
124
125
        $mockDoctrine->expects($this->any())
126
            ->method('getConnection')
127
            ->withAnyParameters()
128
            ->willReturn($mockConnection);
129
130
        $mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container')
131
            ->setMethods(['get'])
132
            ->getMock();
133
134
        $mockContainer->expects($this->any())
135
            ->method('get')
136
            ->with('doctrine')
137
            ->willReturn($mockDoctrine);
138
139
        return $mockContainer;
140
    }
141
}
142