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

testExecuteWithoutOptionForceWillFailWithAttentionMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 23
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 23
loc 23
rs 9.0856
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command;
4
5
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
6
use PHPUnit\Framework\TestCase;
7
use Symfony\Component\Console\Application;
8
use Symfony\Component\Console\Tester\CommandTester;
9
10
class DropDatabaseDoctrineTest extends TestCase
11
{
12
    public function testExecute()
13
    {
14
        $connectionName = 'default';
15
        $dbName = 'test';
16
        $params = array(
17
            'url' => "sqlite:///". sys_get_temp_dir() ."/test.db",
18
            'path' => sys_get_temp_dir() . "/" . $dbName,
19
            'driver' => 'pdo_sqlite',
20
        );
21
22
        $application = new Application();
23
        $application->add(new DropDatabaseDoctrineCommand());
24
25
        $command = $application->find('doctrine:database:drop');
26
        $command->setContainer($this->getMockContainer($connectionName, $params));
27
28
        $commandTester = new CommandTester($command);
29
        $commandTester->execute(
30
            array_merge(array('command' => $command->getName(), '--force' => true))
31
        );
32
33
        $this->assertContains("Dropped database for connection named " . sys_get_temp_dir() . "/" . $dbName . "" , $commandTester->getDisplay());
34
    }
35
36 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...
37
    {
38
        $connectionName = 'default';
39
        $dbName = 'test';
40
        $params = array(
41
            'path' => sys_get_temp_dir() . "/" . $dbName,
42
            'driver' => 'pdo_sqlite',
43
        );
44
45
        $application = new Application();
46
        $application->add(new DropDatabaseDoctrineCommand());
47
48
        $command = $application->find('doctrine:database:drop');
49
        $command->setContainer($this->getMockContainer($connectionName, $params));
50
51
        $commandTester = new CommandTester($command);
52
        $commandTester->execute(
53
            array_merge(array('command' => $command->getName()))
54
        );
55
56
        $this->assertContains("Would drop the database named " . sys_get_temp_dir() . "/" . $dbName . ".", $commandTester->getDisplay());
57
        $this->assertContains("Please run the operation with --force to execute", $commandTester->getDisplay());
58
    }
59
60
    /**
61
     * @param string     $connectionName Connection name
62
     * @param array|null $params         Connection parameters
63
     * @return \PHPUnit_Framework_MockObject_MockObject
64
     */
65 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...
66
    {
67
        // Mock the container and everything you'll need here
68
        $mockDoctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ConnectionRegistry')
69
            ->getMock();
70
71
        $mockDoctrine->expects($this->any())
72
            ->method('getDefaultConnectionName')
73
            ->withAnyParameters()
74
            ->willReturn($connectionName);
75
76
        $mockConnection = $this->getMockBuilder('Doctrine\DBAL\Connection')
77
            ->disableOriginalConstructor()
78
            ->setMethods(array('getParams'))
79
            ->getMockForAbstractClass();
80
81
        $mockConnection->expects($this->any())
82
            ->method('getParams')
83
            ->withAnyParameters()
84
            ->willReturn($params);
85
86
        $mockDoctrine->expects($this->any())
87
            ->method('getConnection')
88
            ->withAnyParameters()
89
            ->willReturn($mockConnection);
90
91
        $mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container')
92
            ->setMethods(array('get'))
93
            ->getMock();
94
95
        $mockContainer->expects($this->any())
96
            ->method('get')
97
            ->with('doctrine')
98
            ->willReturn($mockDoctrine);
99
100
        return $mockContainer;
101
    }
102
}
103