not multiple classes are defined in the same file.
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->assertInstanceOf('Hateoas\Model\Link', $link); |
||
24 | $this->assertSame('foo', $link->getRel()); |
||
25 | $this->assertSame('/bar', $link->getHref()); |
||
26 | $this->assertSame(['templated' => false], $link->getAttributes()); |
||
27 | } |
||
28 | |||
29 | View Code Duplication | public function testRoute() |
|
30 | { |
||
31 | $link = $this->createLinkFactory()->createLink( |
||
32 | new TestedObject(), |
||
33 | new Relation('foo', new Route('/route', array('foo' => 'bar'))) |
||
34 | ); |
||
35 | |||
36 | $this->assertInstanceOf('Hateoas\Model\Link', $link); |
||
37 | $this->assertSame('foo', $link->getRel()); |
||
38 | $this->assertSame('/route?foo=bar', $link->getHref()); |
||
39 | } |
||
40 | |||
41 | public function testExpressions() |
||
42 | { |
||
43 | $link = $this->createLinkFactory()->createLink( |
||
44 | new TestedObject(), |
||
45 | new Relation('expr(object.getRel())', 'expr(object.getUrl())', null, array('expr(object.getRel())' => 'expr(object.getUrl())')) |
||
46 | ); |
||
47 | |||
48 | $this->assertInstanceOf('Hateoas\Model\Link', $link); |
||
49 | $this->assertSame('tested-rel', $link->getRel()); |
||
50 | $this->assertSame('/tested-url', $link->getHref()); |
||
51 | $this->assertSame(['tested-rel' => '/tested-url'], $link->getAttributes()); |
||
52 | } |
||
53 | |||
54 | public function testParametersExpression() |
||
55 | { |
||
56 | $link = $this->createLinkFactory()->createLink( |
||
57 | new TestedObject(), |
||
58 | new Relation('foo', new Route('/route', 'expr(object.getParameters())')) |
||
59 | ); |
||
60 | |||
61 | $this->assertInstanceOf('Hateoas\Model\Link', $link); |
||
62 | $this->assertSame('foo', $link->getRel()); |
||
63 | $this->assertSame('/route?a=b', $link->getHref()); |
||
64 | } |
||
65 | |||
66 | View Code Duplication | public function testParametersDeepArrayExpression() |
|
67 | { |
||
68 | $link = $this->createLinkFactory()->createLink( |
||
69 | new TestedObject(), |
||
70 | new Relation( |
||
71 | 'foo', |
||
72 | new Route( |
||
73 | '/route', |
||
74 | array( |
||
75 | 'expr(object.getRel())' => array('expr(object.getRel())') |
||
76 | ) |
||
77 | ) |
||
78 | ) |
||
79 | ); |
||
80 | |||
81 | $this->assertInstanceOf('Hateoas\Model\Link', $link); |
||
82 | $this->assertSame('foo', $link->getRel()); |
||
83 | $this->assertSame('/route?tested-rel%5B0%5D=tested-rel', $link->getHref()); |
||
84 | } |
||
85 | |||
86 | public function testRouteRequiresGenerator() |
||
87 | { |
||
88 | $expressionEvaluator = new ExpressionEvaluator(new ExpressionLanguage()); |
||
89 | $urlGeneratorRegistry = new UrlGeneratorRegistry(); |
||
90 | |||
91 | $linkFactory = new LinkFactory($expressionEvaluator, $urlGeneratorRegistry); |
||
92 | |||
93 | $this->setExpectedException('RuntimeException', 'You cannot use a route without an url generator.'); |
||
94 | |||
95 | $linkFactory->createLink( |
||
96 | new TestedObject(), |
||
97 | new Relation('foo', new Route('/route', array('foo' => 'bar'))) |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | public function testRouteParamatersNotArray() |
||
102 | { |
||
103 | $linkFactory = $this->createLinkFactory(); |
||
104 | |||
105 | $this->setExpectedException( |
||
106 | 'RuntimeException', |
||
107 | 'The route parameters should be an array, string given. Maybe you forgot to wrap the expression in expr(...).' |
||
108 | ); |
||
109 | |||
110 | $linkFactory->createLink( |
||
111 | new TestedObject(), |
||
112 | new Relation('foo', new Route('/route', 'yolo')) |
||
113 | ); |
||
114 | } |
||
115 | |||
116 | private function createLinkFactory() |
||
117 | { |
||
118 | $defaultUrlGenerator = new CallableUrlGenerator(function ($route, $parameters) { |
||
119 | return $route . '?' . http_build_query($parameters); |
||
120 | }); |
||
121 | $expressionEvaluator = new ExpressionEvaluator(new ExpressionLanguage()); |
||
122 | $urlGeneratorRegistry = new UrlGeneratorRegistry($defaultUrlGenerator); |
||
123 | |||
124 | return new LinkFactory($expressionEvaluator, $urlGeneratorRegistry); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | class TestedObject |
||
0 ignored issues
–
show
|
|||
129 | { |
||
130 | public function getRel() |
||
131 | { |
||
132 | return 'tested-rel'; |
||
133 | } |
||
134 | |||
135 | public function getUrl() |
||
136 | { |
||
137 | return '/tested-url'; |
||
138 | } |
||
139 | |||
140 | public function getParameters() |
||
141 | { |
||
142 | return array( |
||
143 | 'a' => 'b', |
||
144 | ); |
||
145 | } |
||
146 | } |
||
147 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.