Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typecast when serializing primitive types #15

Merged
merged 3 commits into from
Dec 19, 2012
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/JMS/Serializer/GenericSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function visitString($data, array $type)
$this->root = $data;
}

return $data;
return (string) $data;
}

public function visitBoolean($data, array $type)
Expand All @@ -60,7 +60,7 @@ public function visitBoolean($data, array $type)
$this->root = $data;
}

return $data;
return (boolean) $data;
}

public function visitInteger($data, array $type)
Expand All @@ -69,7 +69,7 @@ public function visitInteger($data, array $type)
$this->root = $data;
}

return $data;
return (int) $data;
}

public function visitDouble($data, array $type)
Expand All @@ -78,7 +78,7 @@ public function visitDouble($data, array $type)
$this->root = $data;
}

return $data;
return (float) $data;
}

/**
Expand Down
52 changes: 52 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/GraphNavigatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,58 @@ public function testNavigatorChangeTypeOnSerialization()
$this->navigator->accept($object, null, $this->visitor);
}

public function getPrimitiveTypes()
{
return array(
array(
'type' => 'boolean',
'data' => true,
),
array(
'type' => 'boolean',
'data' => 1,
),
array(
'type' => 'integer',
'data' => 123,
),
array(
'type' => 'integer',
'data' => "123",
),
array(
'type' => 'string',
'data' => "hello",
),
array(
'type' => 'string',
'data' => 123,
),
array(
'type' => 'double',
'data' => 0.1234,
),
array(
'type' => 'double',
'data' => "0.1234",
),
);
}

/**
* @dataProvider getPrimitiveTypes
* @param string $primitiveType
* @param mixed $data
*/
public function testSerializationCastsType($primitiveType, $data)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing something, but this does not seem to actually test that the data was cast, no?

I think you need to add something like assertInternalType.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it was really just testing that the correct visitXXX() function would be called. I have since moved the function into the JsonSerializationTest, as the function is in the abstract GenericSerializationVisitor, so we can only test it in one of the concrete classes.

One more thing. I noticed that one of the functions in there is called visitDouble() where the PHP internal type is actually called float. Should we rename the function (in a seperate PR of course). Or add a wrapper visitFloat() that just calls the other one?

{
$this->navigator = new GraphNavigator(GraphNavigator::DIRECTION_SERIALIZATION, $this->metadataFactory, 'foo', $this->handlerRegistry, $this->objectConstructor, null, $this->dispatcher);

$this->visitor->expects($this->once())
->method('visit' . ucfirst($primitiveType));
$this->navigator->accept($data, array('name' => $primitiveType), $this->visitor);
}

protected function setUp()
{
$this->visitor = $this->getMock('JMS\Serializer\VisitorInterface');
Expand Down