1
|
|
|
<?php |
2
|
|
|
namespace DkplusTest\CsrfApiUnprotectionBundle\DependencyInjection; |
3
|
|
|
|
4
|
|
|
use Dkplus\CsrfApiUnprotectionBundle\CsrfDisablingExtension; |
5
|
|
|
use Dkplus\CsrfApiUnprotectionBundle\DependencyInjection\Configuration; |
6
|
|
|
use Dkplus\CsrfApiUnprotectionBundle\DependencyInjection\Extension; |
7
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
10
|
|
|
use Symfony\Component\Yaml\Parser; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @covers Dkplus\CsrfApiUnprotectionBundle\DependencyInjection\Extension |
14
|
|
|
*/ |
15
|
|
|
class ExtensionTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testItShouldProvideTheRightAlias() |
18
|
|
|
{ |
19
|
|
|
$this->assertSame('dkplus_csrf_api_unprotection', (new Extension())->getAlias()); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testItShouldProvideTheRightConfiguration() |
23
|
|
|
{ |
24
|
|
|
$this->assertInstanceOf( |
25
|
|
|
Configuration::class, |
26
|
|
|
(new Extension())->getConfiguration([], new ContainerBuilder()) |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testItShouldConfigureTheDisablingExtension() |
31
|
|
|
{ |
32
|
|
|
$container = $this->createEmptyConfiguration(); |
33
|
|
|
|
34
|
|
|
$this->assertTrue($container->hasDefinition('dkplus_csrf_api_unprotection.csrf_disabling_extension')); |
35
|
|
|
$this->assertInstanceOf( |
36
|
|
|
CsrfDisablingExtension::class, |
37
|
|
|
$container->get('dkplus_csrf_api_unprotection.csrf_disabling_extension') |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testItShouldAddADefaultRuleWhenNoConfigHasBeenGiven() |
42
|
|
|
{ |
43
|
|
|
$container = $this->createEmptyConfiguration(); |
44
|
|
|
$this->assertSame( |
45
|
|
|
['#^(/app(_[a-zA-Z]*)?.php)?/api/#'], |
46
|
|
|
$container->getDefinition('dkplus_csrf_api_unprotection.unprotection_rule.uri_patterns')->getArgument(0) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testItShouldAllowCustomRules() |
51
|
|
|
{ |
52
|
|
|
$container = $this->createFullConfiguration(); |
53
|
|
|
$this->assertSame( |
54
|
|
|
['#^/api/#', '#^/rest/#'], |
55
|
|
|
$container->getDefinition('dkplus_csrf_api_unprotection.unprotection_rule.uri_patterns')->getArgument(0) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function createEmptyConfiguration() |
60
|
|
|
{ |
61
|
|
|
$configuration = new ContainerBuilder(); |
62
|
|
|
$configuration->set('request_stack', new RequestStack()); |
63
|
|
|
$loader = new Extension(); |
64
|
|
|
$config = $this->getEmptyConfig(); |
65
|
|
|
$loader->load(array($config), $configuration); |
66
|
|
|
return $configuration; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function getEmptyConfig() |
70
|
|
|
{ |
71
|
|
|
$yaml = ''; |
72
|
|
|
$parser = new Parser(); |
73
|
|
|
return $parser->parse($yaml); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function createFullConfiguration() |
77
|
|
|
{ |
78
|
|
|
$configuration = new ContainerBuilder(); |
79
|
|
|
$configuration->set('request_stack', new RequestStack()); |
80
|
|
|
$loader = new Extension(); |
81
|
|
|
$config = $this->getFullConfig(); |
82
|
|
|
$loader->load(array($config), $configuration); |
83
|
|
|
return $configuration; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function getFullConfig() |
87
|
|
|
{ |
88
|
|
|
$yaml = <<<EOF |
89
|
|
|
rules: |
90
|
|
|
match_uri: |
91
|
|
|
- "#^/api/#" |
92
|
|
|
- "#^/rest/#" |
93
|
|
|
EOF; |
94
|
|
|
$parser = new Parser(); |
95
|
|
|
return $parser->parse($yaml); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|