public function testParsingASimpleRequest()
 {
     // Given...
     $repository = Stub::makeEmpty('Doctrine\\ORM\\EntityRepository', ['__call' => function ($name) {
         switch ($name) {
             case 'findOneById':
                 return self::AUTHOR_ENTITY;
                 break;
             case 'findById':
                 return self::COMMENT_ENTITIES;
                 break;
         }
     }]);
     $authorRelationship = Stub::makeEmpty('GoIntegro\\Hateoas\\Metadata\\Resource\\ResourceRelationship');
     $commentsRelationship = Stub::makeEmpty('GoIntegro\\Hateoas\\Metadata\\Resource\\ResourceRelationship');
     $relationships = Stub::makeEmpty('GoIntegro\\Hateoas\\Metadata\\Resource\\ResourceRelationships', ['toOne' => ['author' => $authorRelationship], 'toMany' => ['comments' => $commentsRelationship]]);
     $metadata = Stub::makeEmpty('GoIntegro\\Hateoas\\Metadata\\Resource\\ResourceMetadata', ['relationships' => $relationships, 'isToOneRelationship' => TRUE, 'isToManyRelationship' => TRUE]);
     $em = Stub::makeEmpty('Doctrine\\ORM\\EntityManagerInterface', ['getRepository' => $repository]);
     $mm = Stub::makeEmpty('GoIntegro\\Hateoas\\Metadata\\Resource\\MetadataMinerInterface', ['mine' => $metadata]);
     $hydrant = new ResourceLinksHydrant($em, $mm);
     $params = Stub::makeEmpty('GoIntegro\\Hateoas\\JsonApi\\Request\\Params', ['primaryClass' => "HateoasInc\\Bundle\\ExampleBundle\\Entity\\User"]);
     $resourceObject = ['content' => "Meh.", 'links' => ['author' => '5', 'comments' => ['45', '54', '67']]];
     // When...
     $hydrant->hydrate($params, $resourceObject);
     $expected = ['content' => "Meh.", 'links' => ['author' => self::AUTHOR_ENTITY, 'comments' => self::COMMENT_ENTITIES]];
     // Then...
     $this->assertEquals($expected, $resourceObject);
 }
Example #2
0
 /**
  * @param Params $params
  * @param \stdClass|string $schema
  * @param array &$entityData
  */
 protected function prepareData(Params $params, $schema, array &$entityData)
 {
     foreach ($entityData as &$data) {
         $json = Util\ArrayHelper::toObject($data);
         if (!$this->jsonCoder->matchSchema($json, $schema)) {
             $message = $this->jsonCoder->getSchemaErrorMessage();
             throw new ParseException($message);
         }
         $this->hydrant->hydrate($params, $data);
     }
     return $entityData;
 }