Skip to content

Commit

Permalink
Fix Float and double type
Browse files Browse the repository at this point in the history
  • Loading branch information
Smeagolworms4 committed Feb 10, 2020
1 parent 8382775 commit d73dd22
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/TypeDiscover/Handler/AnnotationHandler.php
Expand Up @@ -60,6 +60,9 @@ public function getType(string $class, string $targetName): ?TypeInterface {
}

protected function createType(string $type): ?TypeInterface {
if ($type === 'float' || $type === 'double') {
return new NativeType('number');
} else
if ($type === 'datetime') {
return new DateTimeType();
} else
Expand Down
3 changes: 3 additions & 0 deletions src/TypeDiscover/Handler/DoctrineHandler.php
Expand Up @@ -49,6 +49,9 @@ public function getType(string $class, string $targetName): ?TypeInterface {
'string',
'boolean',
])) {
if ($type === 'float' || $type === 'double') {
$type = 'number';
}
return new NativeType($type);
}
if ($type === 'datetime') {
Expand Down
5 changes: 4 additions & 1 deletion src/TypeDiscover/Handler/PropertyInfosHandler.php
Expand Up @@ -49,12 +49,15 @@ protected function createType(array $types): ?TypeInterface {
$builtin = 'integer'; break;
case 'bool':
$builtin = 'boolean'; break;
case 'float':
case 'double':
$builtin = 'number'; break;
default: break;
}
if (in_array($builtin, [
'integer',
'float',
'double',
'number',
'string',
'boolean',
])) {
Expand Down
21 changes: 16 additions & 5 deletions tests/TypeDiscover/Handler/AnnotationHandlerTest.php
Expand Up @@ -237,8 +237,19 @@ public function testCreateTypeDate() {
$this->reflectionCallMethod($annotationHandler, 'createType', [ 'datetime' ])
);
}

public function provideCreateTypeNative() {
return [
[ 'integer', 'integer' ],
[ 'float' ,'number' ],
[ 'double' , 'number' ],
];
}

public function testCreateTypeNative() {
/**
* @dataProvider provideCreateTypeNative
*/
public function testCreateTypeNative($type, $result) {

$reader = $this->getMockBuilder(Reader::class)->disableOriginalConstructor()->getMock();
$modelBuilder = $this->getMockBuilder(ModelBuilderInterface::class)->getMockForAbstractClass();
Expand All @@ -253,10 +264,10 @@ public function testCreateTypeNative() {
->method('getModel')
;

/** @var NativeType $result */
$result = $this->reflectionCallMethod($annotationHandler, 'createType', [ 'integer' ]);
/** @var NativeType $returned */
$returned = $this->reflectionCallMethod($annotationHandler, 'createType', [ $type ]);

$this->assertInstanceOf(NativeType::class, $result);
$this->assertEquals($result->getType(), 'integer');
$this->assertInstanceOf(NativeType::class, $returned);
$this->assertEquals($returned->getType(), $result);
}
}
18 changes: 9 additions & 9 deletions tests/TypeDiscover/Handler/DoctrineHandlerTest.php
Expand Up @@ -18,18 +18,18 @@ class DoctrineHandlerTest extends TestCase {

public function providerGetTypeNative() {
return [
[ 'integer' ],
[ 'float' ],
[ 'double' ],
[ 'string' ],
[ 'boolean' ],
[ 'integer', 'integer' ],
[ 'float' , 'number' ],
[ 'double' , 'number' ],
[ 'string' , 'string' ],
[ 'boolean', 'boolean' ],
];
}

/**
* @dataProvider providerGetTypeNative
*/
public function testGetTypeNative($type) {
public function testGetTypeNative($type, $result) {

$doctrine = $this->getMockBuilder(ManagerRegistry::class)->disableOriginalConstructor()->getMock();
$modelBuilder = $this->getMockForAbstractClass(ModelBuilderInterface::class);
Expand Down Expand Up @@ -83,9 +83,9 @@ public function testGetTypeNative($type) {
$handler->setManagerRegistry($doctrine);

/** @var NativeType $result */
$result = $handler->getType(\stdClass::class, 'TARGET_NAME');
$this->assertInstanceOf(NativeType::class, $result);
$this->assertEquals($result->getType(), $type);
$returned = $handler->getType(\stdClass::class, 'TARGET_NAME');
$this->assertInstanceOf(NativeType::class, $returned);
$this->assertEquals($returned->getType(), $result);
}

public function testGetTypeDateTime() {
Expand Down
2 changes: 1 addition & 1 deletion tests/TypeDiscover/Handler/PropertyInfosHandlerTest.php
Expand Up @@ -99,7 +99,7 @@ public function providerCreateTypeNative() {
return [
[ [ new Type('int') ], 'integer' ],
[ [ new Type('resource'), new Type('int') ], 'integer' ],
[ [ new Type('float') ], 'float' ],
[ [ new Type('float') ], 'number' ],
[ [ new Type('string') ], 'string' ],
[ [ new Type('bool') ], 'boolean' ],
];
Expand Down

0 comments on commit d73dd22

Please sign in to comment.