Completed
Push — ezp26352-skip_csrf_check_on_re... ( 19f37a )
by
unknown
36:29
created

RequestHeaderPathExpansionCheckerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testNoRequest() 0 6 1
A testNoHeader() 0 9 1
A testEmptyHeader() 0 6 1
A testSimpleExpansion() 0 11 1
A testChildrenExpansion() 0 10 1
A buildRequestStackWithHeader() 0 10 1
A buildCheckerWithRequestStack() 0 7 1
1
<?php
2
/**
3
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
namespace eZ\Publish\Core\REST\Server\Tests\Output\PathExpansion;
7
8
use eZ\Publish\Core\REST\Server\Output\PathExpansion\RequestHeaderPathExpansionChecker;
9
use PHPUnit_Framework_TestCase;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\RequestStack;
12
13
class RequestHeaderPathExpansionCheckerTest extends PHPUnit_Framework_TestCase
14
{
15
    public function testNoRequest()
16
    {
17
        $checker = $this->buildCheckerWithRequestStack(new RequestStack());
18
19
        self::assertFalse($checker->needsExpansion('anything'));
20
    }
21
22
    public function testNoHeader()
23
    {
24
        $requestStack = new RequestStack();
25
        $requestStack->push(new Request());
26
27
        $checker = $this->buildCheckerWithRequestStack($requestStack);
28
29
        self::assertFalse($checker->needsExpansion('anything'));
30
    }
31
32
    public function testEmptyHeader()
33
    {
34
        $checker = $this->buildCheckerWithRequestStack($this->buildRequestStackWithHeader(''));
35
36
        self::assertFalse($checker->needsExpansion('anything'));
37
    }
38
39
    public function testSimpleExpansion()
40
    {
41
        $checker = $this->buildCheckerWithRequestStack(
0 ignored issues
show
Unused Code introduced by
$checker is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
42
            $this->buildRequestStackWithHeader('Content.MainLocation,Content.Owner')
43
        );
44
        $checker = $this->buildCheckerWithRequestStack($this->buildRequestStackWithHeader('Content.MainLocation,Content.Owner'));
45
46
        self::assertTrue($checker->needsExpansion('Content.MainLocation'));
47
        self::assertTrue($checker->needsExpansion('Content.Owner'));
48
        self::assertFalse($checker->needsExpansion('Content.SomethingElse'));
49
    }
50
51
    public function testChildrenExpansion()
52
    {
53
        $checker = $this->buildCheckerWithRequestStack(
54
            $this->buildRequestStackWithHeader('Location.Children.Location.ContentInfo')
55
        );
56
57
        self::assertTrue($checker->needsExpansion('Location.Children'));
58
        self::assertTrue($checker->needsExpansion('Location.Children.Location'));
59
        self::assertFalse($checker->needsExpansion('Location.urlAliases'));
60
    }
61
62
    private function buildRequestStackWithHeader($headerValue)
63
    {
64
        $requestStack = new RequestStack();
65
66
        $request = new Request();
67
        $request->headers->set('x-ez-embed-value', $headerValue);
68
        $requestStack->push($request);
69
70
        return $requestStack;
71
    }
72
73
    private function buildCheckerWithRequestStack(RequestStack $requestStack)
74
    {
75
        $checker = new RequestHeaderPathExpansionChecker();
76
        $checker->setRequestStack($requestStack);
77
78
        return $checker;
79
    }
80
}
81