|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hateoas\Tests\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Hateoas\Configuration\Embedded; |
|
6
|
|
|
use Hateoas\Configuration\Relation; |
|
7
|
|
|
use Hateoas\Factory\EmbeddedsFactory; |
|
8
|
|
|
use Hateoas\Tests\TestCase; |
|
9
|
|
|
use Prophecy\Argument; |
|
10
|
|
|
|
|
11
|
|
|
class EmbeddedsFactoryTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
public function test() |
|
14
|
|
|
{ |
|
15
|
|
|
$relations = array( |
|
16
|
|
|
new Relation('self', '/users/1'), |
|
17
|
|
|
new Relation('friend', '/users/42', 'expr(object.getFriend())'), |
|
18
|
|
|
new Relation('expr(object.getManagerRel())', '/users/42', new Embedded('expr(object.getManager())', 'expr(object.getXmlElementName())')), |
|
19
|
|
|
); |
|
20
|
|
|
$object = new \StdClass(); |
|
21
|
|
|
$context = $this->prophesize('JMS\Serializer\SerializationContext')->reveal(); |
|
22
|
|
|
|
|
23
|
|
|
$relationsRepositoryProphecy = $this->prophesize('Hateoas\Configuration\RelationsRepository'); |
|
24
|
|
|
$relationsRepositoryProphecy |
|
25
|
|
|
->getRelations($object) |
|
26
|
|
|
->willReturn($relations) |
|
27
|
|
|
->shouldBeCalledTimes(1) |
|
28
|
|
|
; |
|
29
|
|
|
|
|
30
|
|
|
$ELProphecy = $this->prophesize('Hateoas\Expression\ExpressionEvaluator'); |
|
31
|
|
|
$ELProphecy->evaluate('expr(object.getFriend())', $object)->willReturn(42)->shouldBeCalledTimes(1); |
|
32
|
|
|
$ELProphecy->evaluate('expr(object.getManager())', $object)->willReturn(42)->shouldBeCalledTimes(1); |
|
33
|
|
|
$ELProphecy->evaluate('expr(object.getManagerRel())', $object)->willReturn(42)->shouldBeCalledTimes(1); |
|
34
|
|
|
$ELProphecy->evaluate('expr(object.getXmlElementName())', $object)->willReturn(42)->shouldBeCalledTimes(1); |
|
35
|
|
|
$ELProphecy->evaluate(Argument::any(), $object)->willReturnArgument(); |
|
36
|
|
|
|
|
37
|
|
|
$exclusionManagerProphecy = $this->prophesize('Hateoas\Serializer\ExclusionManager'); |
|
38
|
|
|
$exclusionManagerProphecy->shouldSkipEmbedded($object, $relations[0], $context)->willReturn(true); |
|
39
|
|
|
$exclusionManagerProphecy->shouldSkipEmbedded($object, $relations[1], $context)->willReturn(false); |
|
40
|
|
|
$exclusionManagerProphecy->shouldSkipEmbedded($object, $relations[2], $context)->willReturn(false); |
|
41
|
|
|
|
|
42
|
|
|
$embeddedsFactory = new EmbeddedsFactory( |
|
43
|
|
|
$relationsRepositoryProphecy->reveal(), |
|
44
|
|
|
$ELProphecy->reveal(), |
|
45
|
|
|
$exclusionManagerProphecy->reveal() |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
$embeddeds = $embeddedsFactory->create($object, $context); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertCount(2, $embeddeds); |
|
51
|
|
|
$this->assertInstanceOf('Hateoas\Model\Embedded', $embeddeds[0]); |
|
52
|
|
|
$this->assertSame('friend', $embeddeds[0]->getRel()); |
|
53
|
|
|
$this->assertSame(42, $embeddeds[0]->getData()); |
|
54
|
|
|
$this->assertInstanceOf('Hateoas\Model\Embedded', $embeddeds[1]); |
|
55
|
|
|
$this->assertSame(42, $embeddeds[1]->getRel()); |
|
56
|
|
|
$this->assertSame(42, $embeddeds[1]->getData()); |
|
57
|
|
|
$this->assertSame(42, $embeddeds[1]->getXmlElementName()); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|