Completed
Pull Request — master (#523)
by
unknown
08:47
created

DropDatabaseDoctrineTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 99
Duplicated Lines 88.89 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 7
dl 88
loc 99
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testExecuteWithoutForce() 23 23 1
A testExecute() 23 23 1
B getMockContainer() 42 42 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
/*
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\DropDatabaseDoctrineCommand;
18
use Symfony\Component\Console\Application;
19
use Symfony\Component\Console\Tester\CommandTester;
20
21
class DropDatabaseDoctrineTest extends \PHPUnit_Framework_TestCase
22
{
23 View Code Duplication
    public function testExecuteWithoutForce()
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...
24
    {
25
        $connectionName = 'default';
26
        $dbName = 'test';
27
        $params = array(
28
            'dbname' => $dbName,
29
            'memory' => true,
30
            'driver' => 'pdo_sqlite'
31
        );
32
33
        $application = new Application();
34
        $application->add(new DropDatabaseDoctrineCommand());
35
36
        $command = $application->find('doctrine:database:drop');
37
        $command->setContainer($this->getMockContainer($connectionName, $params));
38
39
        $commandTester = new CommandTester($command);
40
        $commandTester->execute(
41
            array_merge(array('command' => $command->getName()))
42
        );
43
44
        $this->assertContains("Please run the operation with --force to execute", $commandTester->getDisplay());
45
    }
46
47 View Code Duplication
    public function testExecute()
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...
48
    {
49
        $connectionName = 'default';
50
        $dbName = 'test';
51
        $params = array(
52
            'dbname' => $dbName,
53
            'memory' => true,
54
            'driver' => 'pdo_sqlite'
55
        );
56
57
        $application = new Application();
58
        $application->add(new DropDatabaseDoctrineCommand());
59
60
        $command = $application->find('doctrine:database:drop');
61
        $command->setContainer($this->getMockContainer($connectionName, $params));
62
63
        $commandTester = new CommandTester($command);
64
        $commandTester->execute(
65
            array_merge(array('command' => $command->getName(), '--force' => true))
66
        );
67
68
        $this->assertContains("Dropped database for connection named \"$dbName\"", $commandTester->getDisplay());
69
    }
70
71
72
    /**
73
     * @param $connectionName
74
     * @param null $params Connection parameters
75
     * @return \PHPUnit_Framework_MockObject_MockObject
76
     */
77 View Code Duplication
    private function getMockContainer($connectionName, $params=null)
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...
78
    {
79
        // Mock the container and everything you'll need here
80
        $mockDoctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ConnectionRegistry')
81
            ->getMock();
82
83
        $mockDoctrine->expects($this->any())
84
            ->method('getDefaultConnectionName')
85
            ->withAnyParameters()
86
            ->willReturn($connectionName)
87
        ;
88
89
90
        $mockConnection = $this->getMockBuilder('Doctrine\DBAL\Connection')
91
            ->disableOriginalConstructor()
92
            ->setMethods(array('getParams'))
93
            ->getMockForAbstractClass();
94
95
        $mockConnection->expects($this->any())
96
            ->method('getParams')
97
            ->withAnyParameters()
98
            ->willReturn($params);
99
100
        
101
        $mockDoctrine->expects($this->any())
102
            ->method('getConnection')
103
            ->withAnyParameters()
104
            ->willReturn($mockConnection);
105
        ;
106
107
108
        $mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container')
109
            ->setMethods(array('get'))
110
            ->getMock();
111
112
        $mockContainer->expects($this->any())
113
            ->method('get')
114
            ->with('doctrine')
115
            ->willReturn($mockDoctrine);
116
117
        return $mockContainer;
118
    }
119
}
120