Completed
Pull Request — master (#3645)
by Matthew
62:43
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 abs;
11
use function floatval;
12
use function is_int;
13
use function is_string;
14
use function microtime;
15
use function sprintf;
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
38
        $this->insert(1, $value1);
39
        $this->insert(2, $value2);
40
        $this->insert(3, $value3);
41
42
        $result1 = $this->select(1);
43
        $result2 = $this->select(2);
44
        $result3 = $this->select(3);
45
46
        if (is_string($result1)) {
47
            $result1 = floatval($result1);
48
            $result2 = floatval($result2);
49
            $result3 = floatval($result3);
50
        }
51
52
        $diff1 = abs($result1 - $value1);
53
        $diff2 = abs($result2 - $value2);
54
        $diff3 = abs($result3 - $value3);
55
56
        $this->assertLessThanOrEqual(0.0001, $diff1, sprintf('%s, %s, %s', $diff2, $result2, $value2));
0 ignored issues
show
Bug introduced by
It seems like $result2 can also be of type false; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

56
        $this->assertLessThanOrEqual(0.0001, $diff1, sprintf('%s, %s, %s', $diff2, /** @scrutinizer ignore-type */ $result2, $value2));
Loading history...
57
        $this->assertLessThanOrEqual(0.0001, $diff2, sprintf('%s, %s, %s', $diff2, $result2, $value2));
58
        $this->assertLessThanOrEqual(0.0001, $diff3, sprintf('%s, %s, %s', $diff2, $result2, $value2));
59
60
        $result1 = $this->selectDouble($value1);
61
        $result2 = $this->selectDouble($value2);
62
        $result3 = $this->selectDouble($value3);
63
64
        $this->assertSame(is_int($result1) ? 1 : '1', $result1);
65
        $this->assertSame(is_int($result2) ? 2 : '2', $result2);
66
        $this->assertSame(is_int($result3) ? 3 : '3', $result3);
67
    }
68
69
    private function insert(int $id, float $value) : void
70
    {
71
        $result = $this->connection->insert('double_table', [
72
            'id'  => $id,
73
            'val' => $value,
74
        ], [
75
            ParameterType::INTEGER,
76
            ParameterType::DOUBLE,
77
        ]);
78
79
        self::assertSame(1, $result);
80
    }
81
82
    /**
83
     * @return mixed
84
     */
85
    private function select(int $id)
86
    {
87
        return $this->connection->fetchColumn(
88
            'SELECT val FROM double_table WHERE id = ?',
89
            [$id],
90
            0,
91
            [ParameterType::INTEGER]
92
        );
93
    }
94
95
    /**
96
     * @return mixed
97
     */
98
    private function selectDouble(float $value)
99
    {
100
        return $this->connection->fetchColumn(
101
            'SELECT id FROM double_table WHERE val = ?',
102
            [$value],
103
            0,
104
            [ParameterType::DOUBLE]
105
        );
106
    }
107
}
108