File: TypeConversionTest.php

package info (click to toggle)
php-doctrine-dbal 4.2.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 4,644 kB
  • sloc: php: 46,471; xml: 460; makefile: 22
file content (177 lines) | stat: -rw-r--r-- 6,266 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional;

use DateTime;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhpunit;

use function str_repeat;

#[RequiresPhpunit('< 11.5.18')]
class TypeConversionTest extends FunctionalTestCase
{
    private static int $typeCounter = 0;

    protected function setUp(): void
    {
        $table = new Table('type_conversion');
        $table->addColumn('id', Types::INTEGER, ['notnull' => false]);
        $table->addColumn('test_string', Types::STRING, [
            'length' => 16,
            'notnull' => false,
        ]);
        $table->addColumn('test_boolean', Types::BOOLEAN, ['notnull' => false]);
        $table->addColumn('test_bigint', Types::BIGINT, ['notnull' => false]);
        $table->addColumn('test_smallint', Types::BIGINT, ['notnull' => false]);
        $table->addColumn('test_datetime', Types::DATETIME_MUTABLE, ['notnull' => false]);
        $table->addColumn('test_datetimetz', Types::DATETIMETZ_MUTABLE, ['notnull' => false]);
        $table->addColumn('test_date', Types::DATE_MUTABLE, ['notnull' => false]);
        $table->addColumn('test_time', Types::TIME_MUTABLE, ['notnull' => false]);
        $table->addColumn('test_text', Types::TEXT, ['notnull' => false]);
        $table->addColumn('test_json', Types::JSON, ['notnull' => false]);
        $table->addColumn('test_float', Types::FLOAT, ['notnull' => false]);
        $table->addColumn('test_smallfloat', Types::SMALLFLOAT, ['notnull' => false]);
        $table->addColumn('test_decimal', Types::DECIMAL, ['notnull' => false, 'scale' => 2, 'precision' => 10]);
        $table->setPrimaryKey(['id']);

        $this->dropAndCreateTable($table);
    }

    #[DataProvider('booleanProvider')]
    public function testIdempotentConversionToBoolean(string $type, mixed $originalValue): void
    {
        $dbValue = $this->processValue($type, $originalValue);

        self::assertIsBool($dbValue);
        self::assertEquals($originalValue, $dbValue);
    }

    /** @return mixed[][] */
    public static function booleanProvider(): iterable
    {
        return [
            'true' => [Types::BOOLEAN, true],
            'false' => [Types::BOOLEAN, false],
        ];
    }

    #[DataProvider('integerProvider')]
    public function testIdempotentConversionToInteger(string $type, mixed $originalValue): void
    {
        $dbValue = $this->processValue($type, $originalValue);

        self::assertIsInt($dbValue);
        self::assertEquals($originalValue, $dbValue);
    }

    /** @return mixed[][] */
    public static function integerProvider(): iterable
    {
        return [
            'smallint' => [Types::SMALLINT, 123],
        ];
    }

    #[DataProvider('floatProvider')]
    public function testIdempotentConversionToFloat(string $type, mixed $originalValue): void
    {
        $dbValue = $this->processValue($type, $originalValue);

        self::assertIsFloat($dbValue);
        self::assertEquals($originalValue, $dbValue);
    }

    /** @return mixed[][] */
    public static function floatProvider(): iterable
    {
        return [
            'float' => [Types::FLOAT, 1.5],
            'smallfloat' => [Types::SMALLFLOAT, 1.5],
        ];
    }

    #[DataProvider('toStringProvider')]
    public function testIdempotentConversionToString(string $type, mixed $originalValue): void
    {
        if ($type === Types::TEXT && TestUtil::isDriverOneOf('pdo_oci')) {
            // inserting BLOBs as streams on Oracle requires Oracle-specific SQL syntax which is currently not supported
            // see http://php.net/manual/en/pdo.lobs.php#example-1035
            self::markTestSkipped("DBAL doesn't support storing LOBs represented as streams using PDO_OCI");
        }

        $dbValue = $this->processValue($type, $originalValue);

        self::assertIsString($dbValue);
        self::assertEquals($originalValue, $dbValue);
    }

    /** @return mixed[][] */
    public static function toStringProvider(): iterable
    {
        return [
            'string' => [Types::STRING, 'ABCDEFGabcdefg'],
            'text' => [Types::TEXT, str_repeat('foo ', 1000)],
        ];
    }

    public function testIdempotentConversionToArray(): void
    {
        self::assertEquals(['foo' => 'bar'], $this->processValue('json', ['foo' => 'bar']));
    }

    #[DataProvider('toDateTimeProvider')]
    public function testIdempotentConversionToDateTime(string $type, DateTime $originalValue): void
    {
        $dbValue = $this->processValue($type, $originalValue);

        self::assertInstanceOf(DateTime::class, $dbValue);

        if ($type === Types::DATETIMETZ_MUTABLE) {
            return;
        }

        self::assertEquals($originalValue, $dbValue);
        self::assertEquals(
            $originalValue->getTimezone(),
            $dbValue->getTimezone(),
        );
    }

    /** @return mixed[][] */
    public static function toDateTimeProvider(): iterable
    {
        return [
            'datetime' => [Types::DATETIME_MUTABLE, new DateTime('2010-04-05 10:10:10')],
            'datetimetz' => [Types::DATETIMETZ_MUTABLE, new DateTime('2010-04-05 10:10:10')],
            'date' => [Types::DATE_MUTABLE, new DateTime('2010-04-05')],
            'time' => [Types::TIME_MUTABLE, new DateTime('1970-01-01 10:10:10')],
        ];
    }

    private function processValue(string $type, mixed $originalValue): mixed
    {
        $columnName     = 'test_' . $type;
        $typeInstance   = Type::getType($type);
        $insertionValue = $typeInstance->convertToDatabaseValue(
            $originalValue,
            $this->connection->getDatabasePlatform(),
        );

        $this->connection->insert('type_conversion', ['id' => ++self::$typeCounter, $columnName => $insertionValue]);

        $sql = 'SELECT ' . $columnName . ' FROM type_conversion WHERE id = ' . self::$typeCounter;

        return $typeInstance->convertToPHPValue(
            $this->connection->fetchOne($sql),
            $this->connection->getDatabasePlatform(),
        );
    }
}