Completed
Pull Request — master (#2795)
by Marco
04:44
created

testInvalidTypeConversionToDatabaseValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Types;
4
5
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
6
use PHPUnit_Framework_TestCase;
7
8
abstract class BaseDateTypeTestCase extends PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @var MockPlatform
12
     */
13
    protected $platform;
14
15
    /**
16
     * @var \Doctrine\DBAL\Types\Type
17
     */
18
    protected $type;
19
20
    /**
21
     * @var string
22
     */
23
    private $currentTimezone;
24
25
    /**
26
     * {@inheritDoc}
27
     */
28
    protected function setUp()
29
    {
30
        $this->platform        = new MockPlatform();
31
        $this->currentTimezone = date_default_timezone_get();
32
33
        $this->assertInstanceOf('Doctrine\DBAL\Types\Type', $this->type);
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    protected function tearDown()
40
    {
41
        date_default_timezone_set($this->currentTimezone);
42
    }
43
44
    public function testDateConvertsToDatabaseValue()
45
    {
46
        $this->assertInternalType('string', $this->type->convertToDatabaseValue(new \DateTime(), $this->platform));
47
    }
48
49
    /**
50
     * @dataProvider invalidPHPValuesProvider
51
     *
52
     * @param mixed $value
53
     */
54
    public function testInvalidTypeConversionToDatabaseValue($value)
55
    {
56
        $this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
57
58
        $this->type->convertToDatabaseValue($value, $this->platform);
59
    }
60
61
    public function testNullConversion()
62
    {
63
        $this->assertNull($this->type->convertToPHPValue(null, $this->platform));
64
    }
65
66
    public function testConvertDateTimeToPHPValue()
67
    {
68
        $date = new \DateTime('now');
69
70
        $this->assertSame($date, $this->type->convertToPHPValue($date, $this->platform));
71
    }
72
73
    /**
74
     * @group #2794
75
     *
76
     * Note that while DateTimeImmutable is supposed to be handled by @see \Doctrine\DBAL\Types\DateTimeImmutableType,
77
     * previous DBAL versions handled it just fine. This test is just in place to prevent further regressions, even
78
     * if the type is being misused
79
     */
80
    public function testConvertDateTimeImmutableToPHPValue()
81
    {
82
        $date = new \DateTimeImmutable('now');
83
84
        self::assertSame($date, $this->type->convertToPHPValue($date, $this->platform));
85
    }
86
87
    /**
88
     * @group #2794
89
     *
90
     * Note that while DateTimeImmutable is supposed to be handled by @see \Doctrine\DBAL\Types\DateTimeImmutableType,
91
     * previous DBAL versions handled it just fine. This test is just in place to prevent further regressions, even
92
     * if the type is being misused
93
     */
94
    public function testDateTimeImmutableConvertsToDatabaseValue()
95
    {
96
        self::assertInternalType(
97
            'string',
98
            $this->type->convertToDatabaseValue(new \DateTimeImmutable(), $this->platform)
99
        );
100
    }
101
102
    /**
103
     * @return mixed[][]
104
     */
105 View Code Duplication
    public function invalidPHPValuesProvider()
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...
106
    {
107
        return [
108
            [0],
109
            [''],
110
            ['foo'],
111
            ['10:11:12'],
112
            ['2015-01-31'],
113
            ['2015-01-31 10:11:12'],
114
            [new \stdClass()],
115
            [$this],
116
            [27],
117
            [-1],
118
            [1.2],
119
            [[]],
120
            [['an array']],
121
        ];
122
    }
123
}
124