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