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\Helper; |
||
4 | |||
5 | use Hateoas\Configuration\Relation; |
||
6 | use Hateoas\Configuration\Route; |
||
7 | use Hateoas\HateoasBuilder; |
||
8 | use Hateoas\Helper\LinkHelper; |
||
9 | use Hateoas\UrlGenerator\CallableUrlGenerator; |
||
10 | use Hateoas\Tests\Fixtures\Will; |
||
11 | |||
12 | class LinkHelperTest extends \PHPUnit_Framework_TestCase |
||
13 | { |
||
14 | private $hateoas; |
||
15 | |||
16 | View Code Duplication | protected function setUp() |
|
0 ignored issues
–
show
|
|||
17 | { |
||
18 | $this->hateoas = HateoasBuilder::create() |
||
19 | ->setUrlGenerator(null, new CallableUrlGenerator(function ($name, $parameters, $absolute) { |
||
20 | if ($name === 'user_get') { |
||
21 | return sprintf( |
||
22 | '%s%s', |
||
23 | $absolute ? 'http://example.com' : '', |
||
24 | strtr('/users/id', $parameters) |
||
25 | ); |
||
26 | } |
||
27 | |||
28 | if ($name === 'post_get') { |
||
29 | return sprintf( |
||
30 | '%s%s', |
||
31 | $absolute ? 'http://example.com' : '', |
||
32 | strtr('/posts/id', $parameters) |
||
33 | ); |
||
34 | } |
||
35 | |||
36 | throw new \RuntimeException('Cannot generate URL'); |
||
37 | })) |
||
38 | ->build(); |
||
39 | } |
||
40 | |||
41 | View Code Duplication | public function testGetLinkHref() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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.
Loading history...
|
|||
42 | { |
||
43 | $linkHelper = new LinkHelper($this->getLinkFactoryMock(), $this->getRelationsRepositoryMock()); |
||
44 | |||
45 | $this->assertEquals( |
||
46 | 'http://example.com/me', |
||
47 | $linkHelper->getLinkHref(new Will(123), 'self') |
||
48 | ); |
||
49 | } |
||
50 | |||
51 | View Code Duplication | public function testGetLinkHrefWithRoute() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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.
Loading history...
|
|||
52 | { |
||
53 | $linkHelper = new LinkHelper($this->getLinkFactoryMock(), $this->getRelationsRepositoryMock()); |
||
54 | |||
55 | $this->assertEquals( |
||
56 | 'my-self-route', |
||
57 | $linkHelper->getLinkHref(new Will(123), 'self-route')->getName() |
||
58 | ); |
||
59 | } |
||
60 | |||
61 | public function testGetLinkHrefReturnsNullIfRelNotFound() |
||
62 | { |
||
63 | $linkHelper = new LinkHelper($this->getLinkFactoryMock($this->never()), $this->getRelationsRepositoryMock()); |
||
64 | |||
65 | $this->assertNull($linkHelper->getLinkHref(new Will(123), 'unknown-rel')); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return \Hateoas\Configuration\RelationsRepository |
||
70 | */ |
||
71 | private function getRelationsRepositoryMock() |
||
72 | { |
||
73 | $relationRepoMock = $this->getMockBuilder('Hateoas\Configuration\RelationsRepository') |
||
74 | ->disableOriginalConstructor() |
||
75 | ->getMock(); |
||
76 | |||
77 | $relationRepoMock |
||
78 | ->expects($this->once()) |
||
79 | ->method('getRelations') |
||
80 | ->will($this->returnValue(array( |
||
81 | new Relation('self', 'http://example.com/me'), |
||
82 | new Relation('self-route', new Route('my-self-route')), |
||
83 | ))); |
||
84 | |||
85 | return $relationRepoMock; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $expects |
||
90 | * |
||
91 | * @return \Hateoas\Factory\LinkFactory |
||
92 | */ |
||
93 | private function getLinkFactoryMock($expects = null) |
||
94 | { |
||
95 | if (null === $expects) { |
||
96 | $expects = $this->once(); |
||
97 | } |
||
98 | |||
99 | $linkFactoryMock = $this->getMockBuilder('Hateoas\Factory\LinkFactory') |
||
100 | ->disableOriginalConstructor() |
||
101 | ->getMock(); |
||
102 | |||
103 | $linkFactoryMock |
||
104 | ->expects($expects) |
||
105 | ->method('createLink') |
||
106 | ->will($this->returnArgument(1)); |
||
107 | |||
108 | return $linkFactoryMock; |
||
109 | } |
||
110 | } |
||
111 |
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.