Completed
Push — master ( c9f8cc...1aa2ac )
by Benjamin
01:50 queued 18s
created

CreateDatabaseDoctrineTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 117
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 6 1
A testExecute() 0 22 1
B testExecuteWithShardOption() 0 41 1
B getMockContainer() 0 37 1
1
<?php
2
3
/*
4
 * This file is part of the Doctrine Bundle
5
 *
6
 * The code was originally distributed inside the Symfony framework.
7
 *
8
 * (c) Fabien Potencier <[email protected]>
9
 * (c) Doctrine Project, Benjamin Eberlei <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command;
16
17
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
18
use PHPUnit\Framework\TestCase;
19
use Symfony\Component\Console\Application;
20
use Symfony\Component\Console\Tester\CommandTester;
21
22
class CreateDatabaseDoctrineTest extends TestCase
23
{
24
    public function tearDown()
25
    {
26
        @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...
27
        @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...
28
        @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...
29
    }
30
31
    public function testExecute()
32
    {
33
        $connectionName = 'default';
34
        $dbName = 'test';
35
        $params = array(
36
            'path' => sys_get_temp_dir() . "/" . $dbName,
37
            'driver' => 'pdo_sqlite',
38
        );
39
40
        $application = new Application();
41
        $application->add(new CreateDatabaseDoctrineCommand());
42
43
        $command = $application->find('doctrine:database:create');
44
        $command->setContainer($this->getMockContainer($connectionName, $params));
45
46
        $commandTester = new CommandTester($command);
47
        $commandTester->execute(
48
            array_merge(array('command' => $command->getName()))
49
        );
50
51
        $this->assertContains("Created database " . sys_get_temp_dir() . "/" . $dbName . " for connection named $connectionName", $commandTester->getDisplay());
52
    }
53
54
    public function testExecuteWithShardOption()
55
    {
56
        $connectionName = 'default';
57
        $params = array(
58
            'dbname' => 'test',
59
            'memory' => true,
60
            'driver' => 'pdo_sqlite',
61
            'global' => array(
62
                'driver' => 'pdo_sqlite',
63
                'dbname' => 'test',
64
            ),
65
            'shards' => array(
66
                'foo' => array(
67
                    'id' => 1,
68
                    'path' => sys_get_temp_dir() . '/shard_1',
69
                    'driver' => 'pdo_sqlite',
70
                ),
71
                'bar' => array(
72
                    'id' => 2,
73
                    'path' => sys_get_temp_dir() . '/shard_2',
74
                    'driver' => 'pdo_sqlite',
75
                ),
76
            )
77
        );
78
79
        $application = new Application();
80
        $application->add(new CreateDatabaseDoctrineCommand());
81
82
        $command = $application->find('doctrine:database:create');
83
        $command->setContainer($this->getMockContainer($connectionName, $params));
84
85
        $commandTester = new CommandTester($command);
86
        $commandTester->execute(array('command' => $command->getName(), '--shard' => 1));
87
88
        $this->assertContains("Created database " . sys_get_temp_dir() ."/shard_1 for connection named $connectionName", $commandTester->getDisplay());
89
90
        $commandTester = new CommandTester($command);
91
        $commandTester->execute(array('command' => $command->getName(), '--shard' => 2));
92
93
        $this->assertContains("Created database " . sys_get_temp_dir() ."/shard_2 for connection named $connectionName", $commandTester->getDisplay());
94
    }
95
96
    /**
97
     * @param string     $connectionName Connection name
98
     * @param array|null $params         Connection parameters
99
     * @return \PHPUnit_Framework_MockObject_MockObject
100
     */
101
    private function getMockContainer($connectionName, $params = null)
102
    {
103
        // Mock the container and everything you'll need here
104
        $mockDoctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ConnectionRegistry')
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(array('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(array('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