These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Hateoas\Tests\Factory; |
||
4 | |||
5 | use Hateoas\Configuration\Relation; |
||
6 | use Hateoas\Configuration\Route; |
||
7 | use Hateoas\Expression\ExpressionEvaluator; |
||
8 | use Hateoas\Factory\LinkFactory; |
||
9 | use Hateoas\Tests\TestCase; |
||
10 | use Hateoas\UrlGenerator\CallableUrlGenerator; |
||
11 | use Hateoas\UrlGenerator\UrlGeneratorRegistry; |
||
12 | use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
||
13 | |||
14 | class LinkFactoryTest extends TestCase |
||
15 | { |
||
16 | public function test() |
||
17 | { |
||
18 | $link = $this->createLinkFactory()->createLink( |
||
19 | new TestedObject(), |
||
20 | new Relation('foo', '/bar', null, array('templated' => false)) |
||
21 | ); |
||
22 | |||
23 | $this |
||
0 ignored issues
–
show
|
|||
24 | ->object($link) |
||
25 | ->isInstanceOf('Hateoas\Model\Link') |
||
26 | ->string($link->getRel()) |
||
27 | ->isEqualTo('foo') |
||
28 | ->string($link->getHref()) |
||
29 | ->isEqualTo('/bar') |
||
30 | ->array($link->getAttributes()) |
||
31 | ->isEqualTo(array('templated' => false)) |
||
32 | ; |
||
33 | } |
||
34 | |||
35 | View Code Duplication | public function testRoute() |
|
36 | { |
||
37 | $link = $this->createLinkFactory()->createLink( |
||
38 | new TestedObject(), |
||
39 | new Relation('foo', new Route('/route', array('foo' => 'bar'))) |
||
40 | ); |
||
41 | |||
42 | $this |
||
0 ignored issues
–
show
The method
string() does not exist on mageekguy\atoum\asserters\object . Did you maybe mean __toString() ?
This check marks calls to methods that do not seem to exist on an object. This is most likely the result of a method being renamed without all references to it being renamed likewise.
Loading history...
|
|||
43 | ->object($link) |
||
44 | ->isInstanceOf('Hateoas\Model\Link') |
||
45 | ->string($link->getRel()) |
||
46 | ->isEqualTo('foo') |
||
47 | ->string($link->getHref()) |
||
48 | ->isEqualTo('/route?foo=bar') |
||
49 | ; |
||
50 | } |
||
51 | |||
52 | public function testExpressions() |
||
53 | { |
||
54 | $link = $this->createLinkFactory()->createLink( |
||
55 | new TestedObject(), |
||
56 | new Relation('expr(object.getRel())', 'expr(object.getUrl())', null, array('expr(object.getRel())' => 'expr(object.getUrl())')) |
||
57 | ); |
||
58 | |||
59 | $this |
||
0 ignored issues
–
show
The method
string() does not exist on mageekguy\atoum\asserters\object . Did you maybe mean __toString() ?
This check marks calls to methods that do not seem to exist on an object. This is most likely the result of a method being renamed without all references to it being renamed likewise.
Loading history...
|
|||
60 | ->object($link) |
||
61 | ->isInstanceOf('Hateoas\Model\Link') |
||
62 | ->string($link->getRel()) |
||
63 | ->isEqualTo('tested-rel') |
||
64 | ->string($link->getHref()) |
||
65 | ->isEqualTo('/tested-url') |
||
66 | ->array($link->getAttributes()) |
||
67 | ->isEqualTo(array('tested-rel' => '/tested-url')) |
||
68 | ; |
||
69 | } |
||
70 | |||
71 | public function testParametersExpression() |
||
72 | { |
||
73 | $link = $this->createLinkFactory()->createLink( |
||
74 | new TestedObject(), |
||
75 | new Relation('foo', new Route('/route', 'expr(object.getParameters())')) |
||
76 | ); |
||
77 | |||
78 | $this |
||
0 ignored issues
–
show
The method
string() does not exist on mageekguy\atoum\asserters\object . Did you maybe mean __toString() ?
This check marks calls to methods that do not seem to exist on an object. This is most likely the result of a method being renamed without all references to it being renamed likewise.
Loading history...
|
|||
79 | ->object($link) |
||
80 | ->isInstanceOf('Hateoas\Model\Link') |
||
81 | ->string($link->getRel()) |
||
82 | ->isEqualTo('foo') |
||
83 | ->string($link->getHref()) |
||
84 | ->isEqualTo('/route?a=b') |
||
85 | ; |
||
86 | } |
||
87 | |||
88 | View Code Duplication | public function testParametersDeepArrayExpression() |
|
89 | { |
||
90 | $link = $this->createLinkFactory()->createLink( |
||
91 | new TestedObject(), |
||
92 | new Relation( |
||
93 | 'foo', |
||
94 | new Route( |
||
95 | '/route', |
||
96 | array( |
||
97 | 'expr(object.getRel())' => array('expr(object.getRel())') |
||
98 | ) |
||
99 | ) |
||
100 | ) |
||
101 | ); |
||
102 | |||
103 | $this |
||
0 ignored issues
–
show
The method
string() does not exist on mageekguy\atoum\asserters\object . Did you maybe mean __toString() ?
This check marks calls to methods that do not seem to exist on an object. This is most likely the result of a method being renamed without all references to it being renamed likewise.
Loading history...
|
|||
104 | ->object($link) |
||
105 | ->isInstanceOf('Hateoas\Model\Link') |
||
106 | ->string($link->getRel()) |
||
107 | ->isEqualTo('foo') |
||
108 | ->string($link->getHref()) |
||
109 | ->isEqualTo('/route?tested-rel%5B0%5D=tested-rel') |
||
110 | ; |
||
111 | } |
||
112 | |||
113 | public function testRouteRequiresGenerator() |
||
114 | { |
||
115 | $expressionEvaluator = new ExpressionEvaluator(new ExpressionLanguage()); |
||
116 | $urlGeneratorRegistry = new UrlGeneratorRegistry(); |
||
117 | |||
118 | $linkFactory = new LinkFactory($expressionEvaluator, $urlGeneratorRegistry); |
||
119 | |||
120 | $this |
||
121 | ->exception(function () use ($linkFactory) { |
||
122 | $linkFactory->createLink( |
||
123 | new TestedObject(), |
||
124 | new Relation('foo', new Route('/route', array('foo' => 'bar'))) |
||
125 | ); |
||
126 | }) |
||
127 | ->isInstanceOf('RuntimeException') |
||
128 | ->hasMessage('You cannot use a route without an url generator.') |
||
129 | ; |
||
130 | } |
||
131 | |||
132 | public function testRouteParamatersNotArray() |
||
133 | { |
||
134 | $linkFactory = $this->createLinkFactory(); |
||
135 | |||
136 | $this |
||
137 | ->exception(function () use ($linkFactory) { |
||
138 | $linkFactory->createLink( |
||
139 | new TestedObject(), |
||
140 | new Relation('foo', new Route('/route', 'yolo')) |
||
141 | ); |
||
142 | }) |
||
143 | ->isInstanceOf('RuntimeException') |
||
144 | ->hasMessage('The route parameters should be an array, string given. Maybe you forgot to wrap the expression in expr(...).') |
||
145 | ; |
||
146 | } |
||
147 | |||
148 | private function createLinkFactory() |
||
149 | { |
||
150 | $defaultUrlGenerator = new CallableUrlGenerator(function ($route, $parameters) { |
||
151 | return $route . '?' . http_build_query($parameters); |
||
152 | }); |
||
153 | $expressionEvaluator = new ExpressionEvaluator(new ExpressionLanguage()); |
||
154 | $urlGeneratorRegistry = new UrlGeneratorRegistry($defaultUrlGenerator); |
||
155 | |||
156 | return new LinkFactory($expressionEvaluator, $urlGeneratorRegistry); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | class TestedObject |
||
161 | { |
||
162 | public function getRel() |
||
163 | { |
||
164 | return 'tested-rel'; |
||
165 | } |
||
166 | |||
167 | public function getUrl() |
||
168 | { |
||
169 | return '/tested-url'; |
||
170 | } |
||
171 | |||
172 | public function getParameters() |
||
173 | { |
||
174 | return array( |
||
175 | 'a' => 'b', |
||
176 | ); |
||
177 | } |
||
178 | } |
||
179 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.