2017-04-19 42 views
0

我試圖從一個給定的陣圖數據,以與JMS串行器的對象(在一個單元測試)來測試一個學說實體:JMS串行忽略教義實體ID

鑑於是一個簡單的實體類型:

/** 
* 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; 
    } 
} 

我定義下的資源\設置\串行\ 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 

回答

1

你好,我想你會錯過cashPositionId的serialized_name屬性,默認情況下,JMS會轉化駝情況下蛇的情況下的性能。

JMS Doc

+0

謝謝。我錯過的那件事... – torsten