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\Expression; |
||
4 | |||
5 | use Hateoas\Expression\ExpressionEvaluator; |
||
6 | use Hateoas\Expression\LinkExpressionFunction; |
||
7 | use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
||
8 | |||
9 | class LinkExpressionFunctionTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | public function testEvaluate() |
||
12 | { |
||
13 | $object = new \StdClass(); |
||
14 | |||
15 | $linkHelperMock = $this->mockHelper('/foo', $object, 'self', false); |
||
16 | |||
17 | $expressionEvaluator = new ExpressionEvaluator(new ExpressionLanguage()); |
||
18 | $expressionEvaluator->registerFunction(new LinkExpressionFunction($linkHelperMock)); |
||
19 | |||
20 | $this->assertEquals( |
||
21 | '/foo', |
||
22 | $expressionEvaluator->evaluate('expr(link(object, "self", false))', $object) |
||
23 | ); |
||
24 | } |
||
25 | |||
26 | public function testCompile() |
||
27 | { |
||
28 | $object = new \StdClass(); |
||
29 | |||
30 | $linkHelperMock = $this->mockHelper('/foo', $object, 'self', false); |
||
31 | |||
32 | $expressionLanguage = new ExpressionLanguage(); |
||
33 | $expressionEvaluator = new ExpressionEvaluator($expressionLanguage); |
||
34 | $expressionEvaluator->registerFunction(new LinkExpressionFunction($linkHelperMock)); |
||
35 | |||
36 | $compiledExpression = $expressionLanguage->compile('link(object, "self", false)', array('object', 'link_helper')); |
||
37 | |||
38 | // setup variables for expression eval |
||
39 | $object = $object; |
||
40 | $link_helper = $linkHelperMock; |
||
0 ignored issues
–
show
|
|||
41 | |||
42 | $this->assertEquals('/foo', eval(sprintf('return %s;', $compiledExpression))); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param string $result |
||
47 | * @param \stdClass $expectedObject |
||
48 | * @param string $expectedRel |
||
49 | * @param boolean $expectedAbsolute |
||
50 | * |
||
51 | * @return \Hateoas\Helper\LinkHelper |
||
52 | */ |
||
53 | private function mockHelper($result, $expectedObject, $expectedRel, $expectedAbsolute) |
||
0 ignored issues
–
show
|
|||
54 | { |
||
55 | $linkHelperMock = $this |
||
56 | ->getMockBuilder('Hateoas\Helper\LinkHelper') |
||
57 | ->disableOriginalConstructor() |
||
58 | ->getMock() |
||
59 | ; |
||
60 | |||
61 | $linkHelperMock |
||
62 | ->expects($this->once()) |
||
63 | ->method('getLinkHref') |
||
64 | ->will($this->returnValue('/foo')) |
||
65 | ->with($expectedObject, $expectedRel, $expectedAbsolute) |
||
66 | ; |
||
67 | |||
68 | return $linkHelperMock; |
||
69 | } |
||
70 | } |
||
71 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.