Completed
Pull Request — master (#2579)
by Luís
04:31
created

DateIntervalTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 112
Duplicated Lines 45.54 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 51
loc 112
rs 10
c 1
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testDateIntervalConvertsToDatabaseValue() 9 9 1
A testDateIntervalConvertsToPHPValue() 7 7 1
A testNegativeDateIntervalConvertsToDatabaseValue() 9 9 1
A testNegativeDateIntervalConvertsToPHPValue() 7 7 1
A testInvalidDateIntervalFormatConversion() 0 6 1
A testDateIntervalNullConversion() 0 4 1
A testRequiresSQLCommentHint() 0 4 1
A testInvalidTypeConversionToDatabaseValue() 0 6 1
A invalidPHPValuesProvider() 19 19 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\Tests\DBAL\Types;
4
5
use Doctrine\DBAL\Types\ConversionException;
6
use Doctrine\DBAL\Types\DateIntervalType;
7
use Doctrine\DBAL\Types\Type;
8
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
9
10
final class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
11
{
12
    /**
13
     * @var MockPlatform
14
     */
15
    private $platform;
16
17
    /**
18
     * @var \Doctrine\DBAL\Types\DateIntervalType
19
     */
20
    private $type;
21
22
    /**
23
     * {@inheritDoc}
24
     */
25
    protected function setUp() : void
26
    {
27
        $this->platform = new MockPlatform();
28
        $this->type     = Type::getType('dateinterval');
29
30
        self::assertInstanceOf(DateIntervalType::class, $this->type);
31
    }
32
33 View Code Duplication
    public function testDateIntervalConvertsToDatabaseValue() : 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
        $interval = new \DateInterval('P2Y1DT1H2M3S');
36
37
        $expected = '+P02Y00M01DT01H02M03S';
38
        $actual = $this->type->convertToDatabaseValue($interval, $this->platform);
39
40
        self::assertEquals($expected, $actual);
41
    }
42
43 View Code Duplication
    public function testDateIntervalConvertsToPHPValue() : 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...
44
    {
45
        $interval = $this->type->convertToPHPValue('+P02Y00M01DT01H02M03S', $this->platform);
46
47
        self::assertInstanceOf(\DateInterval::class, $interval);
48
        self::assertEquals('+P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT));
49
    }
50
51 View Code Duplication
    public function testNegativeDateIntervalConvertsToDatabaseValue() : 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...
52
    {
53
        $interval = new \DateInterval('P2Y1DT1H2M3S');
54
        $interval->invert = 1;
55
56
        $actual = $this->type->convertToDatabaseValue($interval, $this->platform);
57
58
        self::assertEquals('-P02Y00M01DT01H02M03S', $actual);
59
    }
60
61 View Code Duplication
    public function testNegativeDateIntervalConvertsToPHPValue() : 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...
62
    {
63
        $interval = $this->type->convertToPHPValue('-P02Y00M01DT01H02M03S', $this->platform);
64
65
        self::assertInstanceOf(\DateInterval::class, $interval);
66
        self::assertEquals('-P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT));
67
    }
68
69
    public function testInvalidDateIntervalFormatConversion() : void
70
    {
71
        $this->expectException(ConversionException::class);
72
73
        $this->type->convertToPHPValue('abcdefg', $this->platform);
74
    }
75
76
    public function testDateIntervalNullConversion() : void
77
    {
78
        self::assertNull($this->type->convertToPHPValue(null, $this->platform));
79
    }
80
81
    /**
82
     * @group DBAL-1288
83
     */
84
    public function testRequiresSQLCommentHint() : void
85
    {
86
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
87
    }
88
89
    /**
90
     * @dataProvider invalidPHPValuesProvider
91
     */
92
    public function testInvalidTypeConversionToDatabaseValue($value) : void
93
    {
94
        $this->expectException(ConversionException::class);
95
96
        $this->type->convertToDatabaseValue($value, $this->platform);
97
    }
98
99
    /**
100
     * @return mixed[][]
101
     */
102 View Code Duplication
    public function invalidPHPValuesProvider() : array
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...
103
    {
104
        return [
105
            [0],
106
            [''],
107
            ['foo'],
108
            ['10:11:12'],
109
            ['2015-01-31'],
110
            ['2015-01-31 10:11:12'],
111
            [new \stdClass()],
112
            [$this],
113
            [27],
114
            [-1],
115
            [1.2],
116
            [[]],
117
            [['an array']],
118
            [new \DateTime()],
119
        ];
120
    }
121
}
122