Skip to content

Commit

Permalink
Merge pull request #104 from sebastianblum/float-type
Browse files Browse the repository at this point in the history
Integer is also valid float type
  • Loading branch information
codeliner authored Sep 19, 2018
2 parents 4be27b4 + 5b53342 commit 37b28e0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Data/ImmutableRecordLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private function isType(string $type, string $key, $value): bool
case ImmutableRecord::PHP_TYPE_INT:
return is_int($value);
case ImmutableRecord::PHP_TYPE_FLOAT:
return is_float($value);
return is_float($value) || is_int($value);
case ImmutableRecord::PHP_TYPE_BOOL:
return is_bool($value);
case ImmutableRecord::PHP_TYPE_ARRAY:
Expand Down
31 changes: 29 additions & 2 deletions tests/Data/ImmutableRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Prooph\EventMachineTest\Data\Stubs\TestDefaultPrice;
use Prooph\EventMachineTest\Data\Stubs\TestIdentityVO;
use Prooph\EventMachineTest\Data\Stubs\TestProduct;
use Prooph\EventMachineTest\Data\Stubs\TestProductPrice;
use Prooph\EventMachineTest\Data\Stubs\TestProductPriceVO;
use Prooph\EventMachineTest\Data\Stubs\TestProductVO;
use Prooph\EventMachineTest\Data\Stubs\TestUserVO;
Expand Down Expand Up @@ -197,11 +198,12 @@ public function it_calls_init_to_give_immutable_record_the_chance_to_set_default

/**
* @test
* @dataProvider provideTwoAsFloatAndInt
*/
public function it_calls_from_float_when_from_int_does_not_exist()
public function it_calls_from_float_when_from_int_does_not_exist($two)
{
$productPrice = TestProductPriceVO::fromArray([
'amount' => 2.0,
'amount' => $two,
'currency' => 'EUR',
]);

Expand All @@ -218,6 +220,25 @@ public function it_calls_from_float_when_from_int_does_not_exist()
$this->assertInternalType('float', $amount);
}

/**
* @test
* @dataProvider provideTwoAsFloatAndInt
*/
public function it_validates_float_type_when_input_is_integer($two)
{
$data = [
'amount' => $two,
'currency' => 'EUR',
];

$productPrice = TestProductPrice::fromArray($data);

$amount = $productPrice->toArray()['amount'];
$this->assertEquals(2.0, $amount);
$this->assertInternalType('float', $amount);
}


/**
* @test
*/
Expand Down Expand Up @@ -283,4 +304,10 @@ public function it_uses_collection_vo_when_mapping_from_and_to_array()

$this->assertEquals($data, $blacklist->toArray());
}

public function provideTwoAsFloatAndInt(): \Generator
{
yield [(float) 2.0];
yield [(int) 2];
}
}

0 comments on commit 37b28e0

Please sign in to comment.