Completed
Pull Request — master (#767)
by
unknown
02:27
created

DropDatabaseDoctrineTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 120
Duplicated Lines 50.83 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
lcom 1
cbo 8
dl 61
loc 120
rs 10
c 1
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testExecute() 0 23 1
A testExecuteWithoutOptionForceWillFailWithAttentionMessage() 23 23 1
B 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
4
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command;
5
6
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
7
use PHPUnit\Framework\TestCase;
8
use Symfony\Component\Console\Application;
9
use Symfony\Component\Console\Tester\CommandTester;
10
11
class DropDatabaseDoctrineTest extends TestCase
12
{
13
14
    public function testExecute()
15
    {
16
        $connectionName = 'default';
17
        $dbName = 'test';
18
        $params = array(
19
            'url' => "sqlite:///". sys_get_temp_dir() ."/test.db",
20
            'path' => sys_get_temp_dir() . "/" . $dbName,
21
            'driver' => 'pdo_sqlite',
22
        );
23
24
        $application = new Application();
25
        $application->add(new DropDatabaseDoctrineCommand());
26
27
        $command = $application->find('doctrine:database:drop');
28
        $command->setContainer($this->getMockContainer($connectionName, $params));
29
30
        $commandTester = new CommandTester($command);
31
        $commandTester->execute(
32
            array_merge(array('command' => $command->getName(), '--force' => true))
33
        );
34
35
        $this->assertContains("Dropped database for connection named " . sys_get_temp_dir() . "/" . $dbName . "" , $commandTester->getDisplay());
36
    }
37
38
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
    public function testExecuteWithConnectionUrlParam()
40
    {
41
        $connectionName = 'default';
42
        $dbName = 'test';
43
        $params = array(
44
            'url' => "mysql://[email protected]:3306/" . $dbName,
45
            'user' => 'root',
46
            'dbname' => $dbName,
47
            'driver' => "pdo_mysql"
48
        );
49
50
        $application = new Application();
51
        $application->add(new DropDatabaseDoctrineCommand());
52
53
        $command = $application->find('doctrine:database:drop');
54
        $command->setContainer($this->getMockContainer($connectionName, $params));
55
56
        $commandTester = new CommandTester($command);
57
        $commandTester->execute(
58
            array_merge(array('command' => $command->getName(), '--force' => true, '--if-exists' => true))
59
        );
60
61
        $this->assertContains("Database for connection named `". $dbName ."` doesn't exist. Skipped.", $commandTester->getDisplay());
62
    }*/
63
64 View Code Duplication
    public function testExecuteWithoutOptionForceWillFailWithAttentionMessage()
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...
65
    {
66
        $connectionName = 'default';
67
        $dbName = 'test';
68
        $params = array(
69
            'path' => sys_get_temp_dir() . "/" . $dbName,
70
            'driver' => 'pdo_sqlite',
71
        );
72
73
        $application = new Application();
74
        $application->add(new DropDatabaseDoctrineCommand());
75
76
        $command = $application->find('doctrine:database:drop');
77
        $command->setContainer($this->getMockContainer($connectionName, $params));
78
79
        $commandTester = new CommandTester($command);
80
        $commandTester->execute(
81
            array_merge(array('command' => $command->getName()))
82
        );
83
84
        $this->assertContains("Would drop the database named " . sys_get_temp_dir() . "/" . $dbName . ".", $commandTester->getDisplay());
85
        $this->assertContains("Please run the operation with --force to execute", $commandTester->getDisplay());
86
    }
87
88
    /**
89
     * @param string     $connectionName Connection name
90
     * @param array|null $params         Connection parameters
91
     * @return \PHPUnit_Framework_MockObject_MockObject
92
     */
93 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...
94
    {
95
        // Mock the container and everything you'll need here
96
        $mockDoctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ConnectionRegistry')
97
            ->getMock();
98
99
        $mockDoctrine->expects($this->any())
100
            ->method('getDefaultConnectionName')
101
            ->withAnyParameters()
102
            ->willReturn($connectionName);
103
104
        $mockConnection = $this->getMockBuilder('Doctrine\DBAL\Connection')
105
            ->disableOriginalConstructor()
106
            ->setMethods(array('getParams'))
107
            ->getMockForAbstractClass();
108
109
        $mockConnection->expects($this->any())
110
            ->method('getParams')
111
            ->withAnyParameters()
112
            ->willReturn($params);
113
114
        $mockDoctrine->expects($this->any())
115
            ->method('getConnection')
116
            ->withAnyParameters()
117
            ->willReturn($mockConnection);
118
119
        $mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container')
120
            ->setMethods(array('get'))
121
            ->getMock();
122
123
        $mockContainer->expects($this->any())
124
            ->method('get')
125
            ->with('doctrine')
126
            ->willReturn($mockDoctrine);
127
128
        return $mockContainer;
129
    }
130
}