Passed
Push — amo/scrutinizer-new-engine ( 927449 )
by Andreas
10:16
created

TypeConversionTestCase   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
eloc 78
c 1
b 0
f 1
dl 0
loc 144
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testArrayType() 0 10 1
A testMapTypeNullValue() 0 7 1
A testArrayTypeInvalid() 0 8 1
A testMapTypeInvalid() 0 8 1
A testTimestampType() 0 53 1
A testTimestampTypeNull() 0 16 2
A setUp() 0 3 1
A testArrayTypeNullValue() 0 7 1
A testMapType() 0 10 1
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
23
namespace Crate\Test\DBAL\Functional;
24
25
use Crate\DBAL\Platforms\CratePlatform;
26
use Crate\DBAL\Types\TimestampType;
27
use Crate\Test\DBAL\DBALFunctionalTestCase;
28
use Crate\DBAL\Types\ArrayType;
29
use Crate\DBAL\Types\MapType;
30
use Doctrine\DBAL\Types\Type;
31
use PHPUnit\Framework\TestCase;
32
33
class TypeConversionTestCase extends TestCase {
34
35
    private $platform;
36
37
    public function setUp() : void
38
    {
39
        $this->platform = new CratePlatform();
40
    }
41
42
    public function testTimestampType()
43
    {
44
        $input = new \DateTime("2014-10-21 15:23:38");
45
46
        // datetimetz
47
        $type = Type::getType(Type::DATETIMETZ);
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::DATETIMETZ has been deprecated: Use {@see Types::DATETIMETZ_MUTABLE} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

47
        $type = Type::getType(/** @scrutinizer ignore-deprecated */ Type::DATETIMETZ);

This class constant 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 constant will be removed from the class and what other constant to use instead.

Loading history...
48
        $expected = $input->format('Y-m-d\TH:i:sO');
49
        $output = $type->convertToDatabaseValue($input, $this->platform);
50
        $this->assertEquals($output, $expected);
51
        $inputRestored = $type->convertToPHPValue($output, $this->platform);
52
        $this->assertEquals($inputRestored, $input);
53
        $inputRestored = $type->convertToPHPValue($input, $this->platform);
54
        $this->assertEquals($inputRestored, $input);
55
56
        // datetime
57
        $type = Type::getType(Type::DATETIME);
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::DATETIME has been deprecated: Use {@see Types::DATETIME_MUTABLE} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

57
        $type = Type::getType(/** @scrutinizer ignore-deprecated */ Type::DATETIME);

This class constant 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 constant will be removed from the class and what other constant to use instead.

Loading history...
58
        $expected = $input->format('Y-m-d\TH:i:s');
59
        $output = $type->convertToDatabaseValue($input, $this->platform);
60
        $this->assertEquals($output, $expected);
61
        $inputRestored = $type->convertToPHPValue($output, $this->platform);
62
        $this->assertEquals($inputRestored, $input);
63
        $inputRestored = $type->convertToPHPValue($input, $this->platform);
64
        $this->assertEquals($inputRestored, $input);
65
66
        // date
67
        $type = Type::getType(Type::DATE);
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::DATE has been deprecated: Use {@see Types::DATE_MUTABLE} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

67
        $type = Type::getType(/** @scrutinizer ignore-deprecated */ Type::DATE);

This class constant 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 constant will be removed from the class and what other constant to use instead.

Loading history...
68
        $expected = $input->format('Y-m-d\TH:i:s');
69
        $output = $type->convertToDatabaseValue($input, $this->platform);
70
        $this->assertEquals($output, $expected);
71
        $inputRestored = $type->convertToPHPValue($output, $this->platform);
72
        $this->assertEquals($inputRestored, $input);
73
        $inputRestored = $type->convertToPHPValue($input, $this->platform);
74
        $this->assertEquals($inputRestored, $input);
75
76
        // time
77
        $type = Type::getType(Type::TIME);
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::TIME has been deprecated: Use {@see Types::TIME_MUTABLE} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
        $type = Type::getType(/** @scrutinizer ignore-deprecated */ Type::TIME);

This class constant 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 constant will be removed from the class and what other constant to use instead.

Loading history...
78
        $expected = $input->format('Y-m-d\TH:i:s');
79
        $output = $type->convertToDatabaseValue($input, $this->platform);
80
        $this->assertEquals($output, $expected);
81
        $inputRestored = $type->convertToPHPValue($output, $this->platform);
82
        $this->assertEquals($inputRestored, $input);
83
        $inputRestored = $type->convertToPHPValue($input, $this->platform);
84
        $this->assertEquals($inputRestored, $input);
85
86
        // timestamp
87
        $type = Type::getType(TimestampType::NAME);
88
        $expected = $input->format('U')*TimestampType::S_TO_MS;
89
        $output = $type->convertToDatabaseValue($input, $this->platform);
90
        $this->assertEquals($output, $expected);
91
        $inputRestored = $type->convertToPHPValue($output, $this->platform);
92
        $this->assertEquals($inputRestored, $input);
93
        $inputRestored = $type->convertToPHPValue($input, $this->platform);
94
        $this->assertEquals($inputRestored, $input);
95
    }
96
97
    public function testTimestampTypeNull()
98
    {
99
        $types = array(Type::getType(Type::DATETIMETZ),
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::DATETIMETZ has been deprecated: Use {@see Types::DATETIMETZ_MUTABLE} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

99
        $types = array(Type::getType(/** @scrutinizer ignore-deprecated */ Type::DATETIMETZ),

This class constant 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 constant will be removed from the class and what other constant to use instead.

Loading history...
100
            Type::getType(Type::DATETIME),
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::DATETIME has been deprecated: Use {@see Types::DATETIME_MUTABLE} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

100
            Type::getType(/** @scrutinizer ignore-deprecated */ Type::DATETIME),

This class constant 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 constant will be removed from the class and what other constant to use instead.

Loading history...
101
            Type::getType(Type::DATE),
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::DATE has been deprecated: Use {@see Types::DATE_MUTABLE} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

101
            Type::getType(/** @scrutinizer ignore-deprecated */ Type::DATE),

This class constant 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 constant will be removed from the class and what other constant to use instead.

Loading history...
102
            Type::getType(Type::TIME),
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::TIME has been deprecated: Use {@see Types::TIME_MUTABLE} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

102
            Type::getType(/** @scrutinizer ignore-deprecated */ Type::TIME),

This class constant 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 constant will be removed from the class and what other constant to use instead.

Loading history...
103
            Type::getType(TimestampType::NAME)
104
        );
105
        foreach ($types as $type) {
106
            // to DB value
107
            $value = $type->convertToDatabaseValue(null, $this->platform);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as $type->convertToDatabase...(null, $this->platform) targeting Doctrine\DBAL\Types\Type::convertToDatabaseValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
108
            $this->assertEquals($value, null);
109
110
            // to PHP value
111
            $value = $type->convertToPHPValue(null, $this->platform);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as $type->convertToPHPValue(null, $this->platform) targeting Doctrine\DBAL\Types\Type::convertToPHPValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
112
            $this->assertEquals($value, null);
113
        }
114
    }
115
116
    public function testMapType()
117
    {
118
        $type = Type::getType(MapType::NAME);
119
120
        // to DB value
121
        $output = $type->convertToDatabaseValue(array('foo'=>'bar'), $this->platform);
122
        $this->assertEquals($output, array('foo'=>'bar'));
123
124
        $output = $type->convertToDatabaseValue(array(), $this->platform);
125
        $this->assertEquals($output, array());
126
    }
127
128
    public function testMapTypeNullValue()
129
    {
130
        $type = Type::getType(MapType::NAME);
131
132
        // to DB value
133
        $output = $type->convertToDatabaseValue(null, $this->platform);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $output is correct as $type->convertToDatabase...(null, $this->platform) targeting Doctrine\DBAL\Types\Type::convertToDatabaseValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
134
        $this->assertEquals($output, null);
135
    }
136
137
    public function testMapTypeInvalid()
138
    {
139
        $type = Type::getType(MapType::NAME);
140
141
        // to DB value
142
        $notAMap = array('foo', 'bar');
143
        $output = $type->convertToDatabaseValue($notAMap, $this->platform);
144
        $this->assertEquals($output, null);
145
146
    }
147
148
    public function testArrayType()
149
    {
150
        $type = Type::getType(ArrayType::NAME);
151
152
        // to DB value
153
        $output = $type->convertToDatabaseValue(array('foo', 'bar'), $this->platform);
154
        $this->assertEquals($output, array('foo', 'bar'));
155
156
        $output = $type->convertToDatabaseValue(array(), $this->platform);
157
        $this->assertEquals($output, array());
158
    }
159
160
    public function testArrayTypeNullValue()
161
    {
162
        $type = Type::getType(ArrayType::NAME);
163
164
        // to DB value
165
        $output = $type->convertToDatabaseValue(null, $this->platform);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $output is correct as $type->convertToDatabase...(null, $this->platform) targeting Doctrine\DBAL\Types\Type::convertToDatabaseValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
166
        $this->assertEquals($output, null);
167
    }
168
169
    public function testArrayTypeInvalid()
170
    {
171
        $type = Type::getType(ArrayType::NAME);
172
173
        // to DB value
174
        $notAnArray = array('foo'=>'bar');
175
        $output = $type->convertToDatabaseValue($notAnArray, $this->platform);
176
        $this->assertEquals($output, null);
177
    }
178
179
}
180