1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* |
5
|
|
|
* (c) Yaroslav Honcharuk <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Yarhon\RouteGuardBundle\Tests\DependencyInjection; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
15
|
|
|
use Yarhon\RouteGuardBundle\DependencyInjection\YarhonRouteGuardExtension; |
16
|
|
|
use Yarhon\RouteGuardBundle\Cache\DataCollector\RouteCollectionDataCollector; |
17
|
|
|
use Yarhon\RouteGuardBundle\Twig\Extension\RoutingExtension; |
18
|
|
|
use Yarhon\RouteGuardBundle\Security\TestProvider\ProviderInterface; |
19
|
|
|
use Yarhon\RouteGuardBundle\Security\TestResolver\SymfonySecurityResolverInterface; |
20
|
|
|
use Yarhon\RouteGuardBundle\Controller\ArgumentResolver\ArgumentValueResolverInterface; |
21
|
|
|
use Yarhon\RouteGuardBundle\Security\AuthorizationChecker\AuthorizationCheckerInterface; |
22
|
|
|
use Yarhon\RouteGuardBundle\YarhonRouteGuardBundle; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class YarhonRouteGuardExtensionTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var ContainerBuilder |
31
|
|
|
*/ |
32
|
|
|
private $container; |
33
|
|
|
|
34
|
|
|
public function setUp() |
35
|
|
|
{ |
36
|
|
|
$extension = new YarhonRouteGuardExtension(); |
37
|
|
|
$this->container = new ContainerBuilder(); |
38
|
|
|
$this->container->registerExtension($extension); |
39
|
|
|
|
40
|
|
|
$this->container->loadFromExtension($extension->getAlias()); |
41
|
|
|
|
42
|
|
|
// $bundle = new YarhonRouteGuardBundle(); |
43
|
|
|
// $bundle->build($this->container); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testSetConfigParameters() |
47
|
|
|
{ |
48
|
|
|
$config = [ |
49
|
|
|
'data_collector' => ['ignore_controllers' => ['class']], |
50
|
|
|
'twig' => ['tag_name' => 'test'], |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
$extension = new YarhonRouteGuardExtension(); |
54
|
|
|
$this->container->loadFromExtension($extension->getAlias(), $config); |
55
|
|
|
|
56
|
|
|
$this->container->getCompilerPassConfig()->setOptimizationPasses([]); |
57
|
|
|
$this->container->getCompilerPassConfig()->setRemovingPasses([]); |
58
|
|
|
$this->container->compile(); |
59
|
|
|
|
60
|
|
|
$argument = $this->container->getDefinition(RouteCollectionDataCollector::class)->getArgument(2); |
61
|
|
|
$this->assertInternalType('array', $argument); |
62
|
|
|
$this->assertArrayHasKey('ignore_controllers', $argument); |
63
|
|
|
$this->assertArraySubset(['class'], $argument['ignore_controllers']); |
64
|
|
|
|
65
|
|
|
$argument = $this->container->getDefinition(RoutingExtension::class)->getArgument(0); |
66
|
|
|
$this->assertInternalType('array', $argument); |
67
|
|
|
$this->assertArraySubset(['tag_name' => 'test'], $argument); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @dataProvider registerAutoConfigurationDataProvider |
72
|
|
|
*/ |
73
|
|
|
public function testRegisterAutoConfiguration($interface, $tag) |
74
|
|
|
{ |
75
|
|
|
$extension = new YarhonRouteGuardExtension(); |
76
|
|
|
$this->container->loadFromExtension($extension->getAlias(), []); |
77
|
|
|
|
78
|
|
|
$this->container->getCompilerPassConfig()->setOptimizationPasses([]); |
79
|
|
|
$this->container->getCompilerPassConfig()->setRemovingPasses([]); |
80
|
|
|
$this->container->compile(); |
81
|
|
|
|
82
|
|
|
$autoconfigured = $this->container->getAutoconfiguredInstanceof(); |
83
|
|
|
|
84
|
|
|
$this->assertArrayHasKey($interface, $autoconfigured); |
85
|
|
|
$tags = array_keys($autoconfigured[$interface]->getTags()); |
86
|
|
|
$this->assertEquals([$tag], $tags); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function registerAutoConfigurationDataProvider() |
90
|
|
|
{ |
91
|
|
|
return [ |
92
|
|
|
[ProviderInterface::class, 'yarhon_route_guard.test_provider'], |
93
|
|
|
[SymfonySecurityResolverInterface::class, 'yarhon_route_guard.test_resolver.symfony_security'], |
94
|
|
|
[ArgumentValueResolverInterface::class, 'yarhon_route_guard.argument_value_resolver'], |
95
|
|
|
[AuthorizationCheckerInterface::class, 'yarhon_route_guard.authorization_checker'], |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @dataProvider publicServicesDataProvider |
101
|
|
|
*/ |
102
|
|
|
public function testPublicServices($serviceId) |
103
|
|
|
{ |
104
|
|
|
$this->container->setParameter('kernel.cache_dir', 'test_cache_dir'); |
105
|
|
|
|
106
|
|
|
$this->container->register('request_stack')->setSynthetic(true); |
107
|
|
|
$this->container->register('router.default')->setSynthetic(true); |
108
|
|
|
$this->container->register('security.authorization_checker')->setSynthetic(true); |
109
|
|
|
|
110
|
|
|
$this->container->compile(); |
111
|
|
|
|
112
|
|
|
$this->assertTrue($this->container->has($serviceId)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function publicServicesDataProvider() |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
['yarhon_route_guard.authorized_url_generator'], |
119
|
|
|
['yarhon_route_guard.route_authorization_checker'], |
120
|
|
|
['yarhon_route_guard.test_loader'], |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|