Completed
Push — develop ( e0b418...317415 )
by Sergei
21s queued 13s
created

ColumnTest::string8Provider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Functional\Platform;
6
7
use Doctrine\DBAL\ParameterType;
8
use Doctrine\DBAL\Schema\Table;
9
use Doctrine\DBAL\Types\Type;
10
use Doctrine\DBAL\Types\Types;
11
use Doctrine\Tests\DbalFunctionalTestCase;
12
use function sprintf;
13
14
abstract class ColumnTest extends DbalFunctionalTestCase
15
{
16
    public function testVariableLengthStringNoLength() : void
17
    {
18
        $this->assertColumn(Types::STRING, [], 'Test', ParameterType::STRING);
19
    }
20
21
    /**
22
     * @dataProvider string8Provider
23
     */
24
    public function testVariableLengthStringWithLength(string $value) : void
25
    {
26
        $this->assertColumn(Types::STRING, ['length' => 8], $value, ParameterType::STRING);
27
    }
28
29
    /**
30
     * @dataProvider string1Provider
31
     */
32
    public function testFixedLengthStringNoLength(string $value) : void
33
    {
34
        $this->assertColumn(Types::STRING, ['fixed' => true], $value, ParameterType::STRING);
35
    }
36
37
    /**
38
     * @dataProvider string8Provider
39
     */
40
    public function testFixedLengthStringWithLength(string $value) : void
41
    {
42
        $this->assertColumn(Types::STRING, [
43
            'fixed' => true,
44
            'length' => 8,
45
        ], $value, ParameterType::STRING);
46
    }
47
48
    /**
49
     * @return iterable<string, array<int, mixed>>
50
     */
51
    public static function string1Provider() : iterable
52
    {
53
        return [
54
            'ansi' => ['Z'],
55
            'unicode' => ['Я'],
56
        ];
57
    }
58
59
    /**
60
     * @return iterable<string, array<int, mixed>>
61
     */
62
    public static function string8Provider() : iterable
63
    {
64
        return [
65
            'ansi' => ['Doctrine'],
66
            'unicode' => ['Доктрина'],
67
        ];
68
    }
69
70
    public function testVariableLengthBinaryNoLength() : void
71
    {
72
        $this->assertColumn(Types::BINARY, [], "\x00\x01\x02\x03", ParameterType::BINARY);
73
    }
74
75
    public function testVariableLengthBinaryWithLength() : void
76
    {
77
        $this->assertColumn(Types::BINARY, ['length' => 8], "\xCE\xC6\x6B\xDD\x9F\xD8\x07\xB4", ParameterType::BINARY);
78
    }
79
80
    public function testFixedLengthBinaryNoLength() : void
81
    {
82
        $this->assertColumn(Types::BINARY, ['fixed' => true], "\xFF", ParameterType::BINARY);
83
    }
84
85
    public function testFixedLengthBinaryWithLength() : void
86
    {
87
        $this->assertColumn(Types::BINARY, [
88
            'fixed' => true,
89
            'length' => 8,
90
        ], "\xA0\x0A\x7B\x0E\xA4\x60\x78\xD8", ParameterType::BINARY);
91
    }
92
93
    protected function requirePlatform(string $class) : void
94
    {
95
        if ($this->connection->getDatabasePlatform() instanceof $class) {
96
            return;
97
        }
98
99
        self::markTestSkipped(sprintf('The test requires %s', $class));
100
    }
101
102
    /**
103
     * @param array<string, mixed> $column
104
     */
105
    protected function assertColumn(string $type, array $column, string $value, int $bindType) : void
106
    {
107
        $table = new Table('column_test');
108
        $table->addColumn('val', $type, $column);
109
110
        $sm = $this->connection->getSchemaManager();
111
        $sm->dropAndCreateTable($table);
112
113
        self::assertSame(1, $this->connection->insert('column_test', ['val' => $value], [$bindType]));
114
115
        self::assertSame($value, Type::getType($type)->convertToPHPValue(
116
            $this->connection->fetchColumn('SELECT val FROM column_test'),
117
            $this->connection->getDatabasePlatform()
118
        ));
119
    }
120
}
121