public function test() { $relations = array(new Relation('self', '/users/1'), new Relation('friend', '/users/42', 'expr(object.getFriend())'), new Relation('expr(object.getManagerRel())', '/users/42', new Embedded('expr(object.getManager())', 'expr(object.getXmlElementName())'))); $object = new \StdClass(); $context = $this->prophesize('JMS\\Serializer\\SerializationContext')->reveal(); $relationsRepositoryProphecy = $this->prophesize('Hateoas\\Configuration\\RelationsRepository'); $relationsRepositoryProphecy->getRelations($object)->willReturn($relations)->shouldBeCalledTimes(1); $ELProphecy = $this->prophesize('Hateoas\\Expression\\ExpressionEvaluator'); $ELProphecy->evaluate('expr(object.getFriend())', $object)->willReturn(42)->shouldBeCalledTimes(1); $ELProphecy->evaluate('expr(object.getManager())', $object)->willReturn(42)->shouldBeCalledTimes(1); $ELProphecy->evaluate('expr(object.getManagerRel())', $object)->willReturn(42)->shouldBeCalledTimes(1); $ELProphecy->evaluate('expr(object.getXmlElementName())', $object)->willReturn(42)->shouldBeCalledTimes(1); $ELProphecy->evaluate(Argument::any(), $object)->willReturnArgument(); $exclusionManagerProphecy = $this->prophesize('Hateoas\\Serializer\\ExclusionManager'); $exclusionManagerProphecy->shouldSkipEmbedded($object, $relations[0], $context)->willReturn(true); $exclusionManagerProphecy->shouldSkipEmbedded($object, $relations[1], $context)->willReturn(false); $exclusionManagerProphecy->shouldSkipEmbedded($object, $relations[2], $context)->willReturn(false); $embeddedsFactory = new EmbeddedsFactory($relationsRepositoryProphecy->reveal(), $ELProphecy->reveal(), $exclusionManagerProphecy->reveal()); $embeddeds = $embeddedsFactory->create($object, $context); $this->assertCount(2, $embeddeds); $this->assertInstanceOf('Hateoas\\Model\\Embedded', $embeddeds[0]); $this->assertSame('friend', $embeddeds[0]->getRel()); $this->assertSame(42, $embeddeds[0]->getData()); $this->assertInstanceOf('Hateoas\\Model\\Embedded', $embeddeds[1]); $this->assertSame(42, $embeddeds[1]->getRel()); $this->assertSame(42, $embeddeds[1]->getData()); $this->assertSame(42, $embeddeds[1]->getXmlElementName()); }
public function onPostSerialize(ObjectEvent $event) { $context = $event->getContext(); $embeddeds = $this->embeddedsFactory->create($event->getObject(), $event->getContext()); $links = $this->linksFactory->create($event->getObject(), $event->getContext()); if (count($links) > 0) { $this->xmlSerializer->serializeLinks($links, $event->getVisitor(), $context); } if (count($embeddeds) > 0) { $this->xmlSerializer->serializeEmbeddeds($embeddeds, $event->getVisitor(), $context); } }
public function onPostSerialize(ObjectEvent $event) { $object = $event->getObject(); $context = $event->getContext(); $embeddeds = $this->embeddedsFactory->create($object, $context); $links = $this->linksFactory->create($object, $context); $embeddeds = $this->embeddedsInlineDeferrer->handleItems($object, $embeddeds, $context); $links = $this->linksInlineDeferrer->handleItems($object, $links, $context); if (count($links) > 0) { $this->jsonSerializer->serializeLinks($links, $event->getVisitor(), $context); } if (count($embeddeds) > 0) { $this->jsonSerializer->serializeEmbeddeds($embeddeds, $event->getVisitor(), $context); } }