我尝试用jms序列化器(在单元测试中)将数据从给定数组映射到对象,以测试理论实体:
给定的是一个简单的实体类:
/**
* CashPosition
*/
class CashPosition
{
/**
* @var integer
*/
protected $cashPositionId;
/**
* @var \DateTime
*/
protected $date;
/**
* @var float
*/
protected $value;
/**
* Get cashPositionId
*
* @return integer
*/
public function getCashPositionId()
{
return $this->cashPositionId;
}
/**
* Set date
*
* @param \DateTime $date
*
* @return $this
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set value
*
* @param string $value
*
* @return $this
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value
*
* @return float
*/
public function getValue()
{
return $this->value;
}
}我在Resources\config\serializer\Entity.CashPosition.yml下定义了序列化
MyBundle\Entity\CashPosition:
exclusion_policy: ALL
access_type: public_method
properties:
cashPositionId:
exclude: false
expose: true
type: integer
access_type: property
date:
exclude: false
expose: true
type: DateTime<'Y-m-d'>
value:
exclude: false
expose: true
type: float并试图用序列化测试来覆盖这个问题:
public function testSerialization()
{
$data = [
'cashPositionId' => 1,
'date' => date('Y-m-d'),
'value' => 1.0,
];
/* @var $serializer Serializer */
$serializer = $this->container->get('serializer');
$cashPosition = $serializer->fromArray($data, CashPosition::class);
$this->assertInstanceOf(CashPosition::class, $cashPosition);
$this->assertEquals($data, $serializer->toArray($cashPosition));
}但是测试失败了,因为fromArray方法没有设置cashPositionId。我尝试了一些不同的访问类型配置,但没有成功。我不知道这有什么问题。
我使用以下版本的jms序列化程序:
jms/metadata 1.6.0 Class/method/property metadata management in PHP
jms/parser-lib 1.0.0 A library for easily creating recursive-descent parsers.
jms/serializer 1.6.2 Library for (de-)serializing data of any complexity; supports XML, JSON, and YAML.
jms/serializer-bundle 1.4.0 Allows you to easily serialize, and deserialize data of any complexity发布于 2017-04-19 15:31:39
你好,我想您错过了用于serialized_name的cashPositionId属性,在默认情况下,jms会将属性从camel案例转换为蛇案例。
JMS医生
https://stackoverflow.com/questions/43498528
复制相似问题