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