Completed
Push — master ( 0ea57d...94aac2 )
by Grégoire
15s queued 12s
created

DoctrineExtensionTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 152
Duplicated Lines 44.74 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 3
dl 68
loc 152
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testReplaceQueryParametersWithPostgresCasting() 9 9 1
A testReplaceQueryParametersWithStartingIndexAtOne() 12 12 1
A testReplaceQueryParameters() 12 12 1
A testReplaceQueryParametersWithNamedIndex() 0 12 1
A testReplaceQueryParametersWithEmptyArray() 11 11 1
A testEscapeBinaryParameter() 0 5 1
A testEscapeStringParameter() 0 4 1
A testEscapeArrayParameter() 0 4 1
A testEscapeObjectParameter() 0 5 1
A testEscapeNullParameter() 0 4 1
A testEscapeBooleanParameter() 0 4 1
A testItHighlightsSqlQueriesUsingCssClasses() 0 12 1
A testItDoesNotOutputDuplicatePreTags() 12 12 1
A testItUsesCssOnTheDivTag() 12 12 1
A testItUsesCssOnThePreTag() 0 8 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 Doctrine\Bundle\DoctrineBundle\Tests\Twig;
4
5
use Doctrine\Bundle\DoctrineBundle\Twig\DoctrineExtension;
6
use PHPUnit\Framework\TestCase;
7
8
class DoctrineExtensionTest extends TestCase
9
{
10 View Code Duplication
    public function testReplaceQueryParametersWithPostgresCasting() : void
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...
11
    {
12
        $extension  = new DoctrineExtension();
13
        $query      = 'a=? OR (1)::string OR b=?';
14
        $parameters = [1, 2];
15
16
        $result = $extension->replaceQueryParameters($query, $parameters);
17
        $this->assertEquals('a=1 OR (1)::string OR b=2', $result);
18
    }
19
20 View Code Duplication
    public function testReplaceQueryParametersWithStartingIndexAtOne() : void
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...
21
    {
22
        $extension  = new DoctrineExtension();
23
        $query      = 'a=? OR b=?';
24
        $parameters = [
25
            1 => 1,
26
            2 => 2,
27
        ];
28
29
        $result = $extension->replaceQueryParameters($query, $parameters);
30
        $this->assertEquals('a=1 OR b=2', $result);
31
    }
32
33 View Code Duplication
    public function testReplaceQueryParameters() : void
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
        $extension  = new DoctrineExtension();
36
        $query      = 'a=? OR b=?';
37
        $parameters = [
38
            1,
39
            2,
40
        ];
41
42
        $result = $extension->replaceQueryParameters($query, $parameters);
43
        $this->assertEquals('a=1 OR b=2', $result);
44
    }
45
46
    public function testReplaceQueryParametersWithNamedIndex() : void
47
    {
48
        $extension  = new DoctrineExtension();
49
        $query      = 'a=:a OR b=:b';
50
        $parameters = [
51
            'a' => 1,
52
            'b' => 2,
53
        ];
54
55
        $result = $extension->replaceQueryParameters($query, $parameters);
56
        $this->assertEquals('a=1 OR b=2', $result);
57
    }
58
59 View Code Duplication
    public function testReplaceQueryParametersWithEmptyArray() : void
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...
60
    {
61
        $extension  = new DoctrineExtension();
62
        $query      = 'IN (?)';
63
        $parameters = [
64
            [],
65
        ];
66
67
        $result = $extension->replaceQueryParameters($query, $parameters);
68
        $this->assertEquals('IN (NULL)', $result);
69
    }
70
71
    public function testEscapeBinaryParameter() : void
72
    {
73
        $binaryString = pack('H*', '9d40b8c1417f42d099af4782ec4b20b6');
74
        $this->assertEquals('0x9D40B8C1417F42D099AF4782EC4B20B6', DoctrineExtension::escapeFunction($binaryString));
75
    }
76
77
    public function testEscapeStringParameter() : void
78
    {
79
        $this->assertEquals("'test string'", DoctrineExtension::escapeFunction('test string'));
80
    }
81
82
    public function testEscapeArrayParameter() : void
83
    {
84
        $this->assertEquals("1, NULL, 'test', foo, NULL", DoctrineExtension::escapeFunction([1, null, 'test', new DummyClass('foo'), []]));
85
    }
86
87
    public function testEscapeObjectParameter() : void
88
    {
89
        $object = new DummyClass('bar');
90
        $this->assertEquals('bar', DoctrineExtension::escapeFunction($object));
91
    }
92
93
    public function testEscapeNullParameter() : void
94
    {
95
        $this->assertEquals('NULL', DoctrineExtension::escapeFunction(null));
96
    }
97
98
    public function testEscapeBooleanParameter() : void
99
    {
100
        $this->assertEquals('1', DoctrineExtension::escapeFunction(true));
101
    }
102
103
    /**
104
     * @group legacy
105
     */
106
    public function testItHighlightsSqlQueriesUsingCssClasses() : void
107
    {
108
        $extension = new DoctrineExtension();
109
        self::assertStringContainsString(
110
            'class=',
111
            $extension->formatQuery('CREATE DATABASE 📚;')
112
        );
113
        self::assertStringContainsString(
114
            'class=',
115
            $extension->formatSql('CREATE DATABASE 📚;', true)
116
        );
117
    }
118
119
    /**
120
     * @group legacy
121
     */
122 View Code Duplication
    public function testItDoesNotOutputDuplicatePreTags() : void
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...
123
    {
124
        $extension = new DoctrineExtension();
125
        self::assertSame(
126
            1,
127
            substr_count($extension->formatQuery('CREATE DATABASE 📚;'), '<pre')
128
        );
129
        self::assertSame(
130
            1,
131
            substr_count($extension->formatSQL('CREATE DATABASE 📚;', true), '<pre')
132
        );
133
    }
134
135
    /**
136
     * @group legacy
137
     */
138 View Code Duplication
    public function testItUsesCssOnTheDivTag() : void
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...
139
    {
140
        $extension = new DoctrineExtension();
141
        self::assertSame(
142
            1,
143
            substr_count($extension->formatQuery('CREATE DATABASE 📚;'), '<div class=')
144
        );
145
        self::assertSame(
146
            1,
147
            substr_count($extension->formatQuery('CREATE DATABASE 📚;'), '<pre>')
148
        );
149
    }
150
151
    public function testItUsesCssOnThePreTag() : void
152
    {
153
        $extension = new DoctrineExtension();
154
        self::assertSame(
155
            1,
156
            substr_count($extension->formatSQL('CREATE DATABASE 📚;', true), '<pre class=')
157
        );
158
    }
159
}
160
161
class DummyClass
162
{
163
    /** @var string */
164
    protected $str;
165
166
    public function __construct(string $str)
167
    {
168
        $this->str = $str;
169
    }
170
171
    public function __toString() : string
172
    {
173
        return $this->str;
174
    }
175
}
176