CsrfListenerSpec   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 2
dl 0
loc 71
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 8 1
A its_onKernelRequest_should_continue_if_csrf_valid() 0 9 1
A its_onKernelRequest_should_continue_if_no_csrf_provided_and_check_csrf_disabled() 0 8 1
A its_onKernelRequest_should_throw_exception_if_no_csrf_provided_and_check_csrf_enabled() 0 11 1
A its_onKernelRequest_should_throw_exception_if_csrf_invalid() 0 11 1
A its_onKernelRequest_should_use_the_link_string_as_csrf_intention() 0 9 1
1
<?php
2
3
namespace spec\Knp\RadBundle\EventListener;
4
5
use PhpSpec\ObjectBehavior;
6
use Prophecy\Argument as Arg;
7
8
class CsrfListenerSpec extends ObjectBehavior
9
{
10
    /**
11
     * @param Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface $csrfProvider
12
     * @param Symfony\Component\HttpKernel\Event\GetResponseEvent                      $event
13
     * @param Symfony\Component\HttpFoundation\Request                                 $request
14
     * @param Symfony\Component\HttpFoundation\ParameterBag                            $requestBag
15
     * @param Symfony\Component\HttpFoundation\ParameterBag                            $attributeBag
16
     */
17
    function let($csrfProvider, $event, $request, $requestBag, $attributeBag)
18
    {
19
        $event->getRequest()->willReturn($request);
20
        $request->request = $requestBag;
21
        $request->attributes = $attributeBag;
22
23
        $this->beConstructedWith($csrfProvider);
24
    }
25
26
    function its_onKernelRequest_should_continue_if_csrf_valid($event, $request, $requestBag, $attributeBag, $csrfProvider)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Method name "CsrfListenerSpec::its_onKernelRequest_should_continue_if_csrf_valid" is not in camel caps format
Loading history...
27
    {
28
        $attributeBag->get('_check_csrf', false)->shouldBeCalled()->willReturn(true);
29
        $requestBag->has('_link_token')->shouldBeCalled()->willReturn(true);
30
        $requestBag->get('_link_token')->shouldBeCalled()->willReturn('some token');
31
        $csrfProvider->isCsrfTokenValid('link', 'some token')->shouldBeCalled()->willReturn(true);
32
33
        $this->onKernelRequest($event);
34
    }
35
36
    function its_onKernelRequest_should_continue_if_no_csrf_provided_and_check_csrf_disabled($event, $request, $requestBag, $attributeBag, $csrfProvider)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Method name "CsrfListenerSpec::its_onKernelRequest_should_continue_if_no_csrf_provided_and_check_csrf_disabled" is not in camel caps format
Loading history...
37
    {
38
        $attributeBag->get('_check_csrf', false)->shouldBeCalled()->willReturn(false);
39
        $requestBag->get('_link_token')->shouldNotBeCalled();
40
        $csrfProvider->isCsrfTokenValid('link', Arg::type('string'))->shouldNotBeCalled();
41
42
        $this->onKernelRequest($event);
43
    }
44
45
    function its_onKernelRequest_should_throw_exception_if_no_csrf_provided_and_check_csrf_enabled($event, $request, $requestBag, $attributeBag, $csrfProvider)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Method name "CsrfListenerSpec::its_onKernelRequest_should_throw_exception_if_no_csrf_provided_and_check_csrf_enabled" is not in camel caps format
Loading history...
46
    {
47
        $attributeBag->get('_check_csrf', false)->shouldBeCalled()->willReturn(true);
48
        $requestBag->has('_link_token')->shouldBeCalled()->willReturn(false);
49
        $requestBag->get('_link_token')->shouldNotBeCalled();
50
        $csrfProvider->isCsrfTokenValid('link', Arg::type('string'))->shouldNotBeCalled();
51
52
        $this->shouldThrow(new \InvalidArgumentException(
53
            'The CSRF token verification is activated but you did not send a token. Please submit a request with a valid csrf token.'
54
        ))->duringOnKernelRequest($event);
55
    }
56
57
    function its_onKernelRequest_should_throw_exception_if_csrf_invalid($event, $request, $requestBag, $attributeBag, $csrfProvider)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Method name "CsrfListenerSpec::its_onKernelRequest_should_throw_exception_if_csrf_invalid" is not in camel caps format
Loading history...
58
    {
59
        $attributeBag->get('_check_csrf', false)->shouldBeCalled()->willReturn(true);
60
        $requestBag->has('_link_token')->shouldBeCalled()->willReturn(true);
61
        $requestBag->get('_link_token')->shouldBeCalled()->willReturn('some token');
62
        $csrfProvider->isCsrfTokenValid('link', 'some token')->shouldBeCalled()->willReturn(false);
63
64
        $this->shouldThrow(new \InvalidArgumentException(
65
            'The CSRF token is invalid. Please submit a request with a valid csrf token.'
66
        ))->duringOnKernelRequest($event);
67
    }
68
69
    function its_onKernelRequest_should_use_the_link_string_as_csrf_intention($event, $request, $requestBag, $attributeBag, $csrfProvider)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Method name "CsrfListenerSpec::its_onKernelRequest_should_use_the_link_string_as_csrf_intention" is not in camel caps format
Loading history...
70
    {
71
        $attributeBag->get('_check_csrf', false)->shouldBeCalled()->willReturn(true);
72
        $requestBag->has('_link_token')->shouldBeCalled()->willReturn(true);
73
        $requestBag->get('_link_token')->shouldBeCalled()->willReturn('some token');
74
        $csrfProvider->isCsrfTokenValid('link', 'some token')->shouldBeCalled()->willReturn(true);
75
76
        $this->onKernelRequest($event);
77
    }
78
}
79