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 localeconv; |
15
|
|
|
use function microtime; |
16
|
|
|
use function str_replace; |
17
|
|
|
|
18
|
|
|
class DoubleTest extends DbalFunctionalTestCase |
19
|
|
|
{ |
20
|
|
|
protected function setUp() : void |
21
|
|
|
{ |
22
|
|
|
parent::setUp(); |
23
|
|
|
|
24
|
|
|
$table = new Table('double_table'); |
25
|
|
|
$table->addColumn('id', 'integer'); |
26
|
|
|
$table->addColumn('val', 'float'); |
27
|
|
|
$table->setPrimaryKey(['id']); |
28
|
|
|
|
29
|
|
|
$sm = $this->connection->getSchemaManager(); |
30
|
|
|
$sm->dropAndCreateTable($table); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testInsertAndSelect() : void |
34
|
|
|
{ |
35
|
|
|
$value1 = 1.1; |
36
|
|
|
$value2 = 77.99999999999; |
37
|
|
|
$value3 = microtime(true); |
38
|
|
|
$localeInfo = localeconv(); |
39
|
|
|
|
40
|
|
|
$this->insert(1, $value1); |
41
|
|
|
$this->insert(2, $value2); |
42
|
|
|
$this->insert(3, $value3); |
43
|
|
|
|
44
|
|
|
$result1 = $this->select(1); |
45
|
|
|
$result2 = $this->select(2); |
46
|
|
|
$result3 = $this->select(3); |
47
|
|
|
|
48
|
|
|
if (is_string($result1)) { |
49
|
|
|
$result1 = str_replace('.', $localeInfo['decimal_point'] ?? '.', $result1); |
50
|
|
|
$result2 = str_replace('.', $localeInfo['decimal_point'] ?? '.', $result2); |
51
|
|
|
$result3 = str_replace('.', $localeInfo['decimal_point'] ?? '.', $result3); |
52
|
|
|
$result1 = floatval($result1); |
53
|
|
|
$result2 = floatval($result2); |
54
|
|
|
$result3 = floatval($result3); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$diff1 = abs($result1 - $value1); |
58
|
|
|
$diff2 = abs($result2 - $value2); |
59
|
|
|
$diff3 = abs($result3 - $value3); |
60
|
|
|
|
61
|
|
|
$this->assertLessThanOrEqual(0.10000000000000009, $diff1); |
62
|
|
|
$this->assertLessThanOrEqual(0.0001, $diff2); |
63
|
|
|
$this->assertLessThanOrEqual(0.0001, $diff3); |
64
|
|
|
|
65
|
|
|
$result1 = $this->selectDouble($value1); |
66
|
|
|
$result2 = $this->selectDouble($value2); |
67
|
|
|
$result3 = $this->selectDouble($value3); |
68
|
|
|
|
69
|
|
|
$this->assertSame(is_int($result1) ? 1 : '1', $result1); |
70
|
|
|
$this->assertSame(is_int($result2) ? 2 : '2', $result2); |
71
|
|
|
$this->assertSame(is_int($result3) ? 3 : '3', $result3); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function insert(int $id, float $value) : void |
75
|
|
|
{ |
76
|
|
|
$result = $this->connection->insert('double_table', [ |
77
|
|
|
'id' => $id, |
78
|
|
|
'val' => $value, |
79
|
|
|
], [ |
80
|
|
|
ParameterType::INTEGER, |
81
|
|
|
ParameterType::DOUBLE, |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
self::assertSame(1, $result); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
private function select(int $id) |
91
|
|
|
{ |
92
|
|
|
return $this->connection->fetchColumn( |
93
|
|
|
'SELECT val FROM double_table WHERE id = ?', |
94
|
|
|
[$id], |
95
|
|
|
0, |
96
|
|
|
[ParameterType::INTEGER] |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
|
private function selectDouble(float $value) |
104
|
|
|
{ |
105
|
|
|
return $this->connection->fetchColumn( |
106
|
|
|
'SELECT id FROM double_table WHERE val = ?', |
107
|
|
|
[$value], |
108
|
|
|
0, |
109
|
|
|
[ParameterType::DOUBLE] |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|