forked from doctrine/dbal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address issue doctrine#3631 by adding support for a float datatype
Also try to add a new functional test per request.
- Loading branch information
Showing
6 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
tests/Doctrine/Tests/DBAL/Functional/Types/DoubleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\DBAL\Functional\Types; | ||
|
||
use Doctrine\DBAL\ParameterType; | ||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\Tests\DbalFunctionalTestCase; | ||
use function is_float; | ||
use function is_int; | ||
use function localeconv; | ||
use function microtime; | ||
use function number_format; | ||
use function strlen; | ||
use function strpos; | ||
use function substr; | ||
|
||
class DoubleTest extends DbalFunctionalTestCase | ||
{ | ||
protected function setUp() : void | ||
{ | ||
parent::setUp(); | ||
|
||
$table = new Table('double_table'); | ||
$table->addColumn('id', 'integer'); | ||
$table->addColumn('val', 'float'); | ||
$table->setPrimaryKey(['id']); | ||
|
||
$sm = $this->connection->getSchemaManager(); | ||
$sm->dropAndCreateTable($table); | ||
} | ||
|
||
public function testInsertAndSelect() : void | ||
{ | ||
$value1 = 1.1; | ||
$value2 = 77.99999999999; | ||
$value3 = microtime(true); | ||
$valStr = (string) $value3; | ||
$localeInfo = localeconv(); | ||
$len = strlen(substr($valStr, strpos($valStr, $localeInfo['decimal_point'] ?? '.') + 1)); | ||
|
||
$this->insert(1, $value1); | ||
$this->insert(2, $value2); | ||
$this->insert(3, $value3); | ||
|
||
$result1 = $this->select(1); | ||
$result2 = $this->select(2); | ||
$result3 = $this->select(3); | ||
|
||
$this->assertSame(is_float($result1) ? $value1 : number_format($value1, 1, '.', ''), $result1); | ||
$this->assertSame(is_float($result2) ? $value2 : number_format($value2, 11, '.', ''), $result2); | ||
$this->assertSame(is_float($result3) ? $value3 : number_format($value3, $len, '.', ''), $result3); | ||
|
||
$result1 = $this->selectDouble($value1); | ||
$result2 = $this->selectDouble($value2); | ||
$result3 = $this->selectDouble($value3); | ||
|
||
$this->assertSame(is_int($result1) ? 1 : '1', $result1); | ||
$this->assertSame(is_int($result2) ? 2 : '2', $result2); | ||
$this->assertSame(is_int($result3) ? 3 : '3', $result3); | ||
} | ||
|
||
private function insert(int $id, float $value) : void | ||
{ | ||
$result = $this->connection->insert('double_table', [ | ||
'id' => $id, | ||
'val' => $value, | ||
], [ | ||
ParameterType::INTEGER, | ||
ParameterType::DOUBLE, | ||
]); | ||
|
||
self::assertSame(1, $result); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
private function select(int $id) | ||
{ | ||
return $this->connection->fetchColumn( | ||
'SELECT val FROM double_table WHERE id = ?', | ||
[$id], | ||
0, | ||
[ParameterType::INTEGER] | ||
); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
private function selectDouble(float $value) | ||
{ | ||
return $this->connection->fetchColumn( | ||
'SELECT id FROM double_table WHERE val = ?', | ||
[$value], | ||
0, | ||
[ParameterType::DOUBLE] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
setlocale(LC_ALL, 'de_DE.UTF-8', 'de_DE', 'de', 'ge'); |