Passed
Pull Request — master (#3645)
by Matthew
13:01
created

DoubleTest::selectDouble()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Functional\Types;
6
7
use Doctrine\DBAL\ParameterType;
8
use Doctrine\DBAL\Schema\Table;
9
use Doctrine\Tests\DbalFunctionalTestCase;
10
use function localeconv;
11
use function microtime;
12
use function number_format;
13
use function strlen;
14
use function strpos;
15
use function substr;
16
17
class DoubleTest extends DbalFunctionalTestCase
18
{
19
    protected function setUp() : void
20
    {
21
        parent::setUp();
22
23
        $table = new Table('double_table');
24
        $table->addColumn('id', 'integer');
25
        $table->addColumn('val', 'float');
26
        $table->setPrimaryKey(['id']);
27
28
        $sm = $this->connection->getSchemaManager();
29
        $sm->dropAndCreateTable($table);
30
    }
31
32
    public function testInsertAndSelect() : void
33
    {
34
        $value1     = 1.1;
35
        $value2     = 77.99999999999;
36
        $value3     = microtime(true);
37
        $valStr     = (string) $value3;
38
        $localeInfo = localeconv();
39
        $len        = strlen(substr($valStr, strpos($valStr, $localeInfo['decimal_point'] ?? '.') + 1));
40
41
        $this->insert(1, $value1);
42
        $this->insert(2, $value2);
43
        $this->insert(3, $value3);
44
45
        $this->assertSame(number_format($value1, 1, '.', ''), $this->select(1));
46
        $this->assertSame(number_format($value2, 11, '.', ''), $this->select(2));
47
        $this->assertSame(number_format($value3, $len, '.', ''), $this->select(3));
48
        $this->assertSame('1', $this->selectDouble($value1));
49
        $this->assertSame('2', $this->selectDouble($value2));
50
        $this->assertSame('3', $this->selectDouble($value3));
51
    }
52
53
    private function insert(int $id, float $value) : void
54
    {
55
        $result = $this->connection->insert('double_table', [
56
            'id'  => $id,
57
            'val' => $value,
58
        ], [
59
            ParameterType::INTEGER,
60
            ParameterType::DOUBLE,
61
        ]);
62
63
        self::assertSame(1, $result);
64
    }
65
66
    /**
67
     * @return mixed
68
     */
69
    private function select(int $id)
70
    {
71
        return $this->connection->fetchColumn(
72
            'SELECT val FROM double_table WHERE id = ?',
73
            [$id],
74
            0,
75
            [ParameterType::INTEGER]
76
        );
77
    }
78
79
    /**
80
     * @return mixed
81
     */
82
    private function selectDouble(float $value)
83
    {
84
        return $this->connection->fetchColumn(
85
            'SELECT id FROM double_table WHERE val = ?',
86
            [$value],
87
            0,
88
            [ParameterType::DOUBLE]
89
        );
90
    }
91
}
92