CsrfDisablingExtensionTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 60
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testItShouldExtendFormsAsAHole() 0 8 1
A testItShouldNotModifyFormsWhenTheGivenRulesDoesNotMatchTheRequest() 0 16 1
A testItShouldDisableTheCsrfProtectionWhenTheGivenRuleDoesMatchTheRequest() 0 16 1
A testItShouldDisableTheCsrfProtectionWhenNoMasterRequestExistsToAllowProperFormTesting() 0 14 1
1
<?php
2
namespace DkplusTest\CsrfApiUnprotectionBundle;
3
4
use Dkplus\CsrfApiUnprotectionBundle\CsrfDisablingExtension;
5
use Dkplus\CsrfApiUnprotectionBundle\UnprotectionRule\UnprotectionRule;
6
use PHPUnit_Framework_TestCase as TestCase;
7
use Prophecy\Argument;
8
use Symfony\Component\Form\Extension\Core\Type\FormType;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
13
/**
14
 * @covers Dkplus\CsrfApiUnprotectionBundle\CsrfDisablingExtension
15
 */
16
class CsrfDisablingExtensionTest extends TestCase
17
{
18
    public function testItShouldExtendFormsAsAHole()
19
    {
20
        $extension = new CsrfDisablingExtension(
21
            $this->prophesize(RequestStack::class)->reveal(),
22
            $this->prophesize(UnprotectionRule::class)->reveal()
23
        );
24
        $this->assertSame(FormType::class, $extension->getExtendedType());
25
    }
26
27
    public function testItShouldNotModifyFormsWhenTheGivenRulesDoesNotMatchTheRequest()
28
    {
29
        $request  = $this->prophesize(Request::class);
30
        $requests = $this->prophesize(RequestStack::class);
31
        $requests->getMasterRequest()->willReturn($request);
32
33
        $rule = $this->prophesize(UnprotectionRule::class);
34
        $rule->matches($request)->willReturn(false);
35
36
        $resolver = $this->prophesize(OptionsResolver::class);
37
38
        $extension = new CsrfDisablingExtension($requests->reveal(), $rule->reveal());
39
        $extension->configureOptions($resolver->reveal());
40
41
        $resolver->setDefaults(Argument::any())->shouldNotHaveBeenCalled();
42
    }
43
44
    public function testItShouldDisableTheCsrfProtectionWhenTheGivenRuleDoesMatchTheRequest()
45
    {
46
        $request  = $this->prophesize(Request::class);
47
        $requests = $this->prophesize(RequestStack::class);
48
        $requests->getMasterRequest()->willReturn($request);
49
50
        $rule = $this->prophesize(UnprotectionRule::class);
51
        $rule->matches($request)->willReturn(true);
52
53
        $resolver = $this->prophesize(OptionsResolver::class);
54
55
        $extension = new CsrfDisablingExtension($requests->reveal(), $rule->reveal());
56
        $extension->configureOptions($resolver->reveal());
57
58
        $resolver->setDefaults(['csrf_protection' => false])->shouldHaveBeenCalled();
59
    }
60
61
    public function testItShouldDisableTheCsrfProtectionWhenNoMasterRequestExistsToAllowProperFormTesting()
62
    {
63
        $requests = $this->prophesize(RequestStack::class);
64
        $requests->getMasterRequest()->willReturn(null);
65
66
        $rule = $this->prophesize(UnprotectionRule::class);
67
68
        $resolver = $this->prophesize(OptionsResolver::class);
69
70
        $extension = new CsrfDisablingExtension($requests->reveal(), $rule->reveal());
71
        $extension->configureOptions($resolver->reveal());
72
73
        $resolver->setDefaults(['csrf_protection' => false])->shouldHaveBeenCalled();
74
    }
75
}
76