CreateDatabaseDoctrineTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 127
Duplicated Lines 29.13 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 37
loc 127
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 6 1
A testExecute() 0 23 1
A testExecuteWithShardAlias() 0 43 1
A provideShardOption() 0 5 1
A getMockContainer() 37 37 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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