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; |
||
4 | |||
5 | use Hateoas\HateoasBuilder; |
||
6 | use Hateoas\UrlGenerator\CallableUrlGenerator; |
||
7 | use Hateoas\Tests\Fixtures\Will; |
||
8 | |||
9 | class HateoasTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | private $hateoas; |
||
12 | |||
13 | View Code Duplication | protected function setUp() |
|
0 ignored issues
–
show
|
|||
14 | { |
||
15 | $this->hateoas = HateoasBuilder::create() |
||
16 | ->setUrlGenerator(null, new CallableUrlGenerator(function ($name, $parameters, $absolute) { |
||
17 | if ($name === 'user_get') { |
||
18 | return sprintf( |
||
19 | '%s%s', |
||
20 | $absolute ? 'http://example.com' : '', |
||
21 | strtr('/users/id', $parameters) |
||
22 | ); |
||
23 | } |
||
24 | |||
25 | if ($name === 'post_get') { |
||
26 | return sprintf( |
||
27 | '%s%s', |
||
28 | $absolute ? 'http://example.com' : '', |
||
29 | strtr('/posts/id', $parameters) |
||
30 | ); |
||
31 | } |
||
32 | |||
33 | throw new \RuntimeException('Cannot generate URL'); |
||
34 | })) |
||
35 | ->build(); |
||
36 | } |
||
37 | |||
38 | public function testGetLinkHrefUrlWithUnknownRelShouldReturnNull() |
||
39 | { |
||
40 | $this->assertNull($this->hateoas->getLinkHelper()->getLinkHref(new Will(123), 'unknown-rel')); |
||
41 | $this->assertNull($this->hateoas->getLinkHelper()->getLinkHref(new Will(123), 'unknown-rel', true)); |
||
42 | } |
||
43 | |||
44 | public function testGetLinkHrefUrl() |
||
45 | { |
||
46 | $this->assertEquals('/users/123', $this->hateoas->getLinkHelper()->getLinkHref(new Will(123), 'self')); |
||
47 | $this->assertEquals('/users/123', $this->hateoas->getLinkHelper()->getLinkHref(new Will(123), 'self', false)); |
||
48 | } |
||
49 | |||
50 | public function testGetLinkHrefUrlWithAbsoluteTrue() |
||
51 | { |
||
52 | $this->assertEquals('http://example.com/users/123', $this->hateoas->getLinkHelper()->getLinkHref(new Will(123), 'self', true)); |
||
53 | } |
||
54 | } |
||
55 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.