|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hateoas\Tests\Configuration\Metadata\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Hateoas\Configuration\Metadata\ClassMetadata; |
|
6
|
|
|
use Hateoas\Configuration\Metadata\Driver\ExtensionDriver; |
|
7
|
|
|
use Hateoas\Configuration\Relation; |
|
8
|
|
|
use Hateoas\Tests\TestCase; |
|
9
|
|
|
use Prophecy\Argument; |
|
10
|
|
|
|
|
11
|
|
|
class ExtensionDriverTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
public function testDoesNothingIfNoExtension() |
|
14
|
|
|
{ |
|
15
|
|
|
$reflectionClass = new \ReflectionClass(get_class($this)); |
|
16
|
|
|
$classMetadata = new ClassMetadata(get_class($this)); |
|
17
|
|
|
$loadMetadataForClassCallCount = 0; |
|
18
|
|
|
|
|
19
|
|
|
$delegateDriverProphecy = $this->prophesize('Metadata\Driver\DriverInterface'); |
|
20
|
|
|
$delegateDriverProphecy |
|
21
|
|
|
->loadMetadataForClass($reflectionClass) |
|
22
|
|
|
->will(function () use (&$loadMetadataForClassCallCount, $classMetadata) { |
|
23
|
|
|
return $loadMetadataForClassCallCount++ < 1 ? $classMetadata : null; |
|
24
|
|
|
}) |
|
25
|
|
|
->shouldBeCalledTimes(2) |
|
26
|
|
|
; |
|
27
|
|
|
$extensionDriver = new ExtensionDriver($delegateDriverProphecy->reveal(), array()); |
|
28
|
|
|
|
|
29
|
|
|
$this->assertSame($classMetadata, $extensionDriver->loadMetadataForClass($reflectionClass)); |
|
30
|
|
|
$this->assertNull($extensionDriver->loadMetadataForClass($reflectionClass)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testExtensions() |
|
34
|
|
|
{ |
|
35
|
|
|
$reflectionClass = new \ReflectionClass(get_class($this)); |
|
36
|
|
|
$classMetadata = new ClassMetadata(get_class($this)); |
|
37
|
|
|
|
|
38
|
|
|
$extensions = array(); |
|
39
|
|
|
for ($i = 0; $i < 2; $i++) { |
|
40
|
|
|
$extensionProphecy = $this->prophesize('Hateoas\Configuration\Metadata\ConfigurationExtensionInterface'); |
|
41
|
|
|
$extensionProphecy |
|
42
|
|
|
->decorate($classMetadata) |
|
43
|
|
|
->shouldBeCalledTimes(1) |
|
44
|
|
|
; |
|
45
|
|
|
$extensions[] = $extensionProphecy->reveal(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$delegateDriverProphecy = $this->prophesize('Metadata\Driver\DriverInterface'); |
|
49
|
|
|
$delegateDriverProphecy |
|
50
|
|
|
->loadMetadataForClass($reflectionClass) |
|
51
|
|
|
->willReturn($classMetadata) |
|
52
|
|
|
; |
|
53
|
|
|
|
|
54
|
|
|
$extensionDriver = new ExtensionDriver($delegateDriverProphecy->reveal(), $extensions); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertSame($classMetadata, $extensionDriver->loadMetadataForClass($reflectionClass)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testDelegateReturnsNullAndNoExtensions() |
|
60
|
|
|
{ |
|
61
|
|
|
$reflectionClass = new \ReflectionClass(get_class($this)); |
|
62
|
|
|
|
|
63
|
|
|
$delegateDriverProphecy = $this->prophesize('Metadata\Driver\DriverInterface'); |
|
64
|
|
|
$delegateDriverProphecy |
|
65
|
|
|
->loadMetadataForClass($reflectionClass) |
|
66
|
|
|
->willReturn(null) |
|
67
|
|
|
; |
|
68
|
|
|
|
|
69
|
|
|
$extensionDriver = new ExtensionDriver($delegateDriverProphecy->reveal(), array()); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertNull($extensionDriver->loadMetadataForClass($reflectionClass)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testDelegateReturnsNullAndExtensionsDoNothing() |
|
75
|
|
|
{ |
|
76
|
|
|
$reflectionClass = new \ReflectionClass(get_class($this)); |
|
77
|
|
|
|
|
78
|
|
|
$extensionProphecy = $this->prophesize('Hateoas\Configuration\Metadata\ConfigurationExtensionInterface'); |
|
79
|
|
|
$extensionProphecy |
|
80
|
|
|
->decorate(Argument::type('Hateoas\Configuration\Metadata\ClassMetadataInterface')) |
|
81
|
|
|
->shouldBeCalledTimes(1) |
|
82
|
|
|
; |
|
83
|
|
|
|
|
84
|
|
|
$delegateDriverProphecy = $this->prophesize('Metadata\Driver\DriverInterface'); |
|
85
|
|
|
$delegateDriverProphecy |
|
86
|
|
|
->loadMetadataForClass($reflectionClass) |
|
87
|
|
|
->willReturn(null) |
|
88
|
|
|
; |
|
89
|
|
|
|
|
90
|
|
|
$extensionDriver = new ExtensionDriver($delegateDriverProphecy->reveal(), array($extensionProphecy->reveal())); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertNull($extensionDriver->loadMetadataForClass($reflectionClass)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testDelegateReturnsNullAndExtensionsAddRelations() |
|
96
|
|
|
{ |
|
97
|
|
|
$reflectionClass = new \ReflectionClass(get_class($this)); |
|
98
|
|
|
|
|
99
|
|
|
$extensionProphecy = $this->prophesize('Hateoas\Configuration\Metadata\ConfigurationExtensionInterface'); |
|
100
|
|
|
$extensionProphecy |
|
101
|
|
|
->decorate(Argument::type('Hateoas\Configuration\Metadata\ClassMetadataInterface')) |
|
102
|
|
|
->will(function ($args) { |
|
103
|
|
|
$args[0]->addRelation(new Relation('foo', 'bar')); |
|
104
|
|
|
}) |
|
105
|
|
|
->shouldBeCalledTimes(1) |
|
106
|
|
|
; |
|
107
|
|
|
|
|
108
|
|
|
$delegateDriverProphecy = $this->prophesize('Metadata\Driver\DriverInterface'); |
|
109
|
|
|
$delegateDriverProphecy |
|
110
|
|
|
->loadMetadataForClass($reflectionClass) |
|
111
|
|
|
->willReturn(null) |
|
112
|
|
|
; |
|
113
|
|
|
|
|
114
|
|
|
$extensionDriver = new ExtensionDriver($delegateDriverProphecy->reveal(), array($extensionProphecy->reveal())); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertInstanceOf( |
|
117
|
|
|
'Hateoas\Configuration\Metadata\ClassMetadataInterface', |
|
118
|
|
|
$extensionDriver->loadMetadataForClass($reflectionClass) |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|