Completed
Pull Request — master (#3645)
by Matthew
120:59 queued 55:56
created

DoubleTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 55
c 1
b 0
f 0
dl 0
loc 101
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A insert() 0 11 1
A selectDouble() 0 7 1
B testInsertAndSelect() 0 48 8
A select() 0 7 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
        if ($result1 === false) {
53
            $this->fail('Expected $result1 to not be false');
54
            return;
55
        }
56
        if ($result2 === false) {
57
            $this->fail('Expected $result2 to not be false');
58
            return;
59
        }
60
        if ($result3 === false) {
61
            $this->fail('Expected $result3 to not be false');
62
            return;
63
        }
64
65
        $diff1 = abs($result1 - $value1);
66
        $diff2 = abs($result2 - $value2);
67
        $diff3 = abs($result3 - $value3);
68
69
        $this->assertLessThanOrEqual(0.0001, $diff1, sprintf('%f, %f, %f', $diff1, $result1, $value1));
70
        $this->assertLessThanOrEqual(0.0001, $diff2, sprintf('%f, %f, %f', $diff2, $result2, $value2));
71
        $this->assertLessThanOrEqual(0.0001, $diff3, sprintf('%f, %f, %f', $diff3, $result3, $value3));
72
73
        $result1 = $this->selectDouble($value1);
74
        $result2 = $this->selectDouble($value2);
75
        $result3 = $this->selectDouble($value3);
76
77
        $this->assertSame(is_int($result1) ? 1 : '1', $result1);
78
        $this->assertSame(is_int($result2) ? 2 : '2', $result2);
79
        $this->assertSame(is_int($result3) ? 3 : '3', $result3);
80
    }
81
82
    private function insert(int $id, float $value) : void
83
    {
84
        $result = $this->connection->insert('double_table', [
85
            'id'  => $id,
86
            'val' => $value,
87
        ], [
88
            ParameterType::INTEGER,
89
            ParameterType::DOUBLE,
90
        ]);
91
92
        self::assertSame(1, $result);
93
    }
94
95
    /**
96
     * @return mixed
97
     */
98
    private function select(int $id)
99
    {
100
        return $this->connection->fetchColumn(
101
            'SELECT val FROM double_table WHERE id = ?',
102
            [$id],
103
            0,
104
            [ParameterType::INTEGER]
105
        );
106
    }
107
108
    /**
109
     * @return mixed
110
     */
111
    private function selectDouble(float $value)
112
    {
113
        return $this->connection->fetchColumn(
114
            'SELECT id FROM double_table WHERE val = ?',
115
            [$value],
116
            0,
117
            [ParameterType::DOUBLE]
118
        );
119
    }
120
}
121