MigrationQueryExecutorTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 144
Duplicated Lines 51.39 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 5
dl 74
loc 144
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A testGetConnection() 0 4 1
A testExecuteSql() 13 13 1
A testExecuteSqlDryRun() 12 12 1
A testExecuteMigrationQuery() 12 12 1
A testExecuteConnectionAwareMigrationQuery() 0 15 1
A testExecuteMigrationQueryDryRun() 19 19 1
A testExecuteMigrationQueryDryRunArrayDescription() 0 22 1
A testExecuteMigrationQueryDryRunEmptyDescription() 18 18 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 RDV\Bundle\MigrationBundle\Tests\Unit\Migration;
4
5
use RDV\Bundle\MigrationBundle\Migration\MigrationQueryExecutor;
6
7
class MigrationQueryExecutorTest extends \PHPUnit_Framework_TestCase
8
{
9
    /** @var \PHPUnit_Framework_MockObject_MockObject */
10
    protected $connection;
11
12
    /** @var \PHPUnit_Framework_MockObject_MockObject */
13
    protected $logger;
14
15
    /** @var MigrationQueryExecutor */
16
    protected $executor;
17
18
    protected function setUp()
19
    {
20
        $this->connection = $this->getMockBuilder('Doctrine\DBAL\Connection')
21
            ->disableOriginalConstructor()
22
            ->getMock();
23
        $this->logger     = $this->getMock('Psr\Log\LoggerInterface');
24
        $this->executor   = new MigrationQueryExecutor($this->connection);
25
        $this->executor->setLogger($this->logger);
26
    }
27
28
    public function testGetConnection()
29
    {
30
        $this->assertSame($this->connection, $this->executor->getConnection());
31
    }
32
33 View Code Duplication
    public function testExecuteSql()
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...
34
    {
35
        $query = 'DELETE FROM some_table';
36
37
        $this->logger->expects($this->once())
38
            ->method('notice')
39
            ->with($query);
40
        $this->connection->expects($this->once())
41
            ->method('executeQuery')
42
            ->with($query);
43
44
        $this->executor->execute($query, false);
45
    }
46
47 View Code Duplication
    public function testExecuteSqlDryRun()
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
        $query = 'DELETE FROM some_table';
50
51
        $this->logger->expects($this->once())
52
            ->method('notice')
53
            ->with($query);
54
        $this->connection->expects($this->never())
55
            ->method('executeQuery');
56
57
        $this->executor->execute($query, true);
58
    }
59
60 View Code Duplication
    public function testExecuteMigrationQuery()
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...
61
    {
62
        $query = $this->getMock('RDV\Bundle\MigrationBundle\Migration\MigrationQuery');
63
64
        $query->expects($this->never())
65
            ->method('setConnection');
66
        $query->expects($this->once())
67
            ->method('execute')
68
            ->with($this->identicalTo($this->logger));
69
70
        $this->executor->execute($query, false);
71
    }
72
73
    public function testExecuteConnectionAwareMigrationQuery()
74
    {
75
        $query = $this->getMockBuilder('RDV\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery')
76
            ->disableOriginalConstructor()
77
            ->getMock();
78
79
        $query->expects($this->once())
80
            ->method('setConnection')
81
            ->with($this->identicalTo($this->connection));
82
        $query->expects($this->once())
83
            ->method('execute')
84
            ->with($this->identicalTo($this->logger));
85
86
        $this->executor->execute($query, false);
87
    }
88
89 View Code Duplication
    public function testExecuteMigrationQueryDryRun()
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...
90
    {
91
        $queryDescription = 'test query';
92
93
        $query = $this->getMock('RDV\Bundle\MigrationBundle\Migration\MigrationQuery');
94
95
        $query->expects($this->once())
96
            ->method('getDescription')
97
            ->will($this->returnValue($queryDescription));
98
99
        $this->logger->expects($this->once())
100
            ->method('notice')
101
            ->with($queryDescription);
102
103
        $query->expects($this->never())
104
            ->method('execute');
105
106
        $this->executor->execute($query, true);
107
    }
108
109
    public function testExecuteMigrationQueryDryRunArrayDescription()
110
    {
111
        $queryDescription = ['test query 1', 'test query 2'];
112
113
        $query = $this->getMock('RDV\Bundle\MigrationBundle\Migration\MigrationQuery');
114
115
        $query->expects($this->once())
116
            ->method('getDescription')
117
            ->will($this->returnValue($queryDescription));
118
119
        $this->logger->expects($this->at(0))
120
            ->method('notice')
121
            ->with($queryDescription[0]);
122
        $this->logger->expects($this->at(1))
123
            ->method('notice')
124
            ->with($queryDescription[1]);
125
126
        $query->expects($this->never())
127
            ->method('execute');
128
129
        $this->executor->execute($query, true);
130
    }
131
132 View Code Duplication
    public function testExecuteMigrationQueryDryRunEmptyDescription()
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...
133
    {
134
        $queryDescription = null;
135
136
        $query = $this->getMock('RDV\Bundle\MigrationBundle\Migration\MigrationQuery');
137
138
        $query->expects($this->once())
139
            ->method('getDescription')
140
            ->will($this->returnValue($queryDescription));
141
142
        $this->logger->expects($this->never())
143
            ->method('notice');
144
145
        $query->expects($this->never())
146
            ->method('execute');
147
148
        $this->executor->execute($query, true);
149
    }
150
}
151