File: AbstractColumnTestCase.php

package info (click to toggle)
php-doctrine-dbal 4.3.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,092 kB
  • sloc: php: 60,293; xml: 618; makefile: 23
file content (157 lines) | stat: -rw-r--r-- 4,261 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
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional\Platform;

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnEditor;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\Attributes\DataProvider;

use function sprintf;

abstract class AbstractColumnTestCase extends FunctionalTestCase
{
    public function testVariableLengthStringNoLength(): void
    {
        $this->assertColumn(
            Column::editor()
                ->setTypeName(Types::STRING),
            'Test',
            ParameterType::STRING,
        );
    }

    #[DataProvider('string8Provider')]
    public function testVariableLengthStringWithLength(string $value): void
    {
        $this->assertColumn(
            Column::editor()
                ->setTypeName(Types::STRING)
                ->setLength(8),
            $value,
            ParameterType::STRING,
        );
    }

    #[DataProvider('string1Provider')]
    public function testFixedLengthStringNoLength(string $value): void
    {
        $this->assertColumn(
            Column::editor()
                ->setTypeName(Types::STRING)
                ->setFixed(true),
            $value,
            ParameterType::STRING,
        );
    }

    #[DataProvider('string8Provider')]
    public function testFixedLengthStringWithLength(string $value): void
    {
        $this->assertColumn(
            Column::editor()
                ->setTypeName(Types::STRING)
                ->setFixed(true)
                ->setLength(8),
            $value,
            ParameterType::STRING,
        );
    }

    /** @return iterable<string, array<int, mixed>> */
    public static function string1Provider(): iterable
    {
        return [
            'ansi' => ['Z'],
            'unicode' => ['Я'],
        ];
    }

    /** @return iterable<string, array<int, mixed>> */
    public static function string8Provider(): iterable
    {
        return [
            'ansi' => ['Doctrine'],
            'unicode' => ['Доктрина'],
        ];
    }

    public function testVariableLengthBinaryNoLength(): void
    {
        $this->assertColumn(
            Column::editor()
                ->setTypeName(Types::BINARY),
            "\x00\x01\x02\x03",
            ParameterType::BINARY,
        );
    }

    public function testVariableLengthBinaryWithLength(): void
    {
        $this->assertColumn(
            Column::editor()
                ->setTypeName(Types::BINARY)
                ->setLength(8),
            "\xCE\xC6\x6B\xDD\x9F\xD8\x07\xB4",
            ParameterType::BINARY,
        );
    }

    public function testFixedLengthBinaryNoLength(): void
    {
        $this->assertColumn(
            Column::editor()
                ->setTypeName(Types::BINARY)
                ->setFixed(true),
            "\xFF",
            ParameterType::BINARY,
        );
    }

    public function testFixedLengthBinaryWithLength(): void
    {
        $this->assertColumn(
            Column::editor()
                ->setTypeName(Types::BINARY)
                ->setFixed(true)
                ->setLength(8),
            "\xA0\x0A\x7B\x0E\xA4\x60\x78\xD8",
            ParameterType::BINARY,
        );
    }

    protected function requirePlatform(string $class): void
    {
        if ($this->connection->getDatabasePlatform() instanceof $class) {
            return;
        }

        self::markTestSkipped(sprintf('The test requires %s', $class));
    }

    protected function assertColumn(ColumnEditor $editor, string $value, ParameterType $bindType): void
    {
        $column = $editor
            ->setUnquotedName('val')
            ->create();

        $table = Table::editor()
            ->setUnquotedName('column_test')
            ->setColumns($column)
            ->create();

        $this->dropAndCreateTable($table);

        self::assertSame(1, $this->connection->insert('column_test', ['val' => $value], [$bindType]));

        self::assertSame($value, $column->getType()->convertToPHPValue(
            $this->connection->fetchOne('SELECT val FROM column_test'),
            $this->connection->getDatabasePlatform(),
        ));
    }
}