SamlSpFactoryTest   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 431
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 23
c 5
b 0
f 0
lcom 2
cbo 9
dl 0
loc 431
rs 10

23 Methods

Rating   Name   Duplication   Size   Complexity  
A couldBeConstructedWithoutAnyArguments() 0 4 1
A shouldAllowGetKey() 0 5 1
A shouldAllowGetPosition() 0 5 1
A shouldAddCreateIfNotExistToConfigurationWithDefaultFalse() 0 15 1
A shouldAddRelyingPartyToConfigurationWithDefaultValueNull() 0 13 1
A shouldAddLoginPathToConfigurationWithExpectedDefaultValue() 0 13 1
A shouldAddCheckPathToConfigurationWithExpectedDefaultValue() 0 13 1
A shouldAddLogoutPathToConfigurationWithExpectedDefaultValue() 0 13 1
A shouldAddMetadataPathToConfigurationWithExpectedDefaultValue() 0 13 1
A shouldAddDiscoveryPathToConfigurationWithExpectedDefaultValue() 0 13 1
A shouldReturnArrayOfStrings() 0 13 1
A shouldReturnSamlSpProviderWithPostfixID() 0 11 1
A shouldReturnSamlSpListenerWithPostfixID() 0 11 1
A shouldReturnFormEntryPointyWithPostfixId() 0 11 1
B shouldCreateRelayingPartyWithListenerSetterMethod() 0 24 1
A shouldCreateSpEntityDescriptorBuilder() 0 11 1
B shouldCreateMetaProviderCollection() 0 36 1
A shouldCreateRequestStateStore() 0 11 1
A shouldCreateRelyingParties() 0 17 1
A shouldSetOnlyProviderKeyToAuthenticationProviderIfProviderNotSetInConfig() 0 15 1
B shouldSetProviderKeyUserProviderAdapterAndUserCheckerIfProviderSetInConfig() 0 34 1
A shouldSetCreateUserIfNotExistsToAuthenticationProviderWhenTrue() 0 14 1
A shouldSetCreateUserIfNotExistsToAuthenticationProviderWhenFalse() 0 14 1
1
<?php
2
3
namespace AerialShip\SamlSPBundle\Tests\DependencyInjection\Security;
4
5
use AerialShip\SamlSPBundle\DependencyInjection\Security\Factory\SamlSpFactory;
6
use Symfony\Component\Config\Definition\ArrayNode;
7
use Symfony\Component\Config\Definition\BooleanNode;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
11
12
class SamlSpFactoryTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @test
16
     */
17
    public function couldBeConstructedWithoutAnyArguments()
18
    {
19
        new SamlSpFactory();
20
    }
21
22
    /**
23
     * @test
24
     */
25
    public function shouldAllowGetKey()
26
    {
27
        $factory = new SamlSpFactory();
28
        $this->assertEquals('aerial_ship_saml_sp', $factory->getKey());
29
    }
30
31
32
    /**
33
     * @test
34
     */
35
    public function shouldAllowGetPosition()
36
    {
37
        $factory = new SamlSpFactory();
38
        $this->assertEquals('form', $factory->getPosition());
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function shouldAddCreateIfNotExistToConfigurationWithDefaultFalse()
45
    {
46
        $factory = new SamlSpFactory();
47
        $treeBuilder = new TreeBuilder();
48
        $factory->addConfiguration($treeBuilder->root('name'));
49
        /** @var $tree ArrayNode */
50
        $tree = $treeBuilder->buildTree();
51
        $children = $tree->getChildren();
52
53
        $this->assertArrayHasKey('create_user_if_not_exists', $children);
54
        $this->assertInstanceOf('Symfony\Component\Config\Definition\BooleanNode', $children['create_user_if_not_exists']);
55
        /** @var $node BooleanNode */
56
        $node = $children['create_user_if_not_exists'];
57
        $this->assertFalse($node->getDefaultValue());
58
    }
59
60
61
    /**
62
     * @test
63
     */
64
    public function shouldAddRelyingPartyToConfigurationWithDefaultValueNull()
65
    {
66
        $factory = new SamlSpFactory();
67
        $treeBuilder = new TreeBuilder();
68
        $factory->addConfiguration($treeBuilder->root('name'));
69
        /** @var $tree ArrayNode */
70
        $tree = $treeBuilder->buildTree();
71
        $children = $tree->getChildren();
72
73
        $this->assertArrayHasKey('relying_party', $children);
74
        $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $children['relying_party']);
75
        $this->assertNull($children['relying_party']->getDefaultValue());
76
    }
77
78
    /**
79
     * @test
80
     */
81
    public function shouldAddLoginPathToConfigurationWithExpectedDefaultValue()
82
    {
83
        $factory = new SamlSpFactory();
84
        $treeBuilder = new TreeBuilder();
85
        $factory->addConfiguration($treeBuilder->root('name'));
86
        /** @var $tree ArrayNode */
87
        $tree = $treeBuilder->buildTree();
88
        $children = $tree->getChildren();
89
90
        $this->assertArrayHasKey('login_path', $children);
91
        $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $children['login_path']);
92
        $this->assertEquals('/saml/sp/login', $children['login_path']->getDefaultValue());
93
    }
94
95
    /**
96
     * @test
97
     */
98
    public function shouldAddCheckPathToConfigurationWithExpectedDefaultValue()
99
    {
100
        $factory = new SamlSpFactory();
101
        $treeBuilder = new TreeBuilder();
102
        $factory->addConfiguration($treeBuilder->root('name'));
103
        /** @var $tree ArrayNode */
104
        $tree = $treeBuilder->buildTree();
105
        $children = $tree->getChildren();
106
107
        $this->assertArrayHasKey('check_path', $children);
108
        $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $children['check_path']);
109
        $this->assertEquals('/saml/sp/acs', $children['check_path']->getDefaultValue());
110
    }
111
112
    /**
113
     * @test
114
     */
115
    public function shouldAddLogoutPathToConfigurationWithExpectedDefaultValue()
116
    {
117
        $factory = new SamlSpFactory();
118
        $treeBuilder = new TreeBuilder();
119
        $factory->addConfiguration($treeBuilder->root('name'));
120
        /** @var $tree ArrayNode */
121
        $tree = $treeBuilder->buildTree();
122
        $children = $tree->getChildren();
123
124
        $this->assertArrayHasKey('logout_path', $children);
125
        $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $children['logout_path']);
126
        $this->assertEquals('/saml/sp/logout', $children['logout_path']->getDefaultValue());
127
    }
128
129
130
    /**
131
     * @test
132
     */
133
    public function shouldAddMetadataPathToConfigurationWithExpectedDefaultValue()
134
    {
135
        $factory = new SamlSpFactory();
136
        $treeBuilder = new TreeBuilder();
137
        $factory->addConfiguration($treeBuilder->root('name'));
138
        /** @var $tree ArrayNode */
139
        $tree = $treeBuilder->buildTree();
140
        $children = $tree->getChildren();
141
142
        $this->assertArrayHasKey('metadata_path', $children);
143
        $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $children['metadata_path']);
144
        $this->assertEquals('/saml/sp/FederationMetadata.xml', $children['metadata_path']->getDefaultValue());
145
    }
146
147
    /**
148
     * @test
149
     */
150
    public function shouldAddDiscoveryPathToConfigurationWithExpectedDefaultValue()
151
    {
152
        $factory = new SamlSpFactory();
153
        $treeBuilder = new TreeBuilder();
154
        $factory->addConfiguration($treeBuilder->root('name'));
155
        /** @var $tree ArrayNode */
156
        $tree = $treeBuilder->buildTree();
157
        $children = $tree->getChildren();
158
159
        $this->assertArrayHasKey('discovery_path', $children);
160
        $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $children['discovery_path']);
161
        $this->assertEquals('/saml/sp/discovery', $children['discovery_path']->getDefaultValue());
162
    }
163
164
165
    /**
166
     * @test
167
     */
168
    public function shouldReturnArrayOfStrings()
169
    {
170
        $factory = new SamlSpFactory();
171
        $configProcessor = new SamlSpFactoryConfiguration($factory, 'name');
172
        $config = $configProcessor->processCommonConfiguration();
173
        $containerBuilder = new ContainerBuilder(new ParameterBag());
174
175
        $result = $factory->create($containerBuilder, 'main', $config, 'user.provider.id', null);
176
177
        $this->assertInternalType('array', $result);
178
        $this->assertCount(3, $result);
179
        $this->assertContainsOnly('string', $result);
180
    }
181
182
    /**
183
     * @test
184
     */
185
    public function shouldReturnSamlSpProviderWithPostfixID()
186
    {
187
        $factory = new SamlSpFactory();
188
        $configProcessor = new SamlSpFactoryConfiguration($factory, 'name');
189
        $config = $configProcessor->processCommonConfiguration();
190
        $containerBuilder = new ContainerBuilder(new ParameterBag());
191
192
        list($providerID) = $factory->create($containerBuilder, 'main', $config, 'user.provider.id', null);
193
        $this->assertStringStartsWith('security.authentication.provider.aerial_ship_saml_sp', $providerID);
194
        $this->assertStringEndsWith('.main', $providerID);
195
    }
196
197
    /**
198
     * @test
199
     */
200
    public function shouldReturnSamlSpListenerWithPostfixID()
201
    {
202
        $factory = new SamlSpFactory();
203
        $configProcessor = new SamlSpFactoryConfiguration($factory, 'name');
204
        $config = $configProcessor->processCommonConfiguration();
205
        $containerBuilder = new ContainerBuilder(new ParameterBag());
206
207
        list(,$listenerID) = $factory->create($containerBuilder, 'main', $config, 'user.provider.id', null);
208
        $this->assertStringStartsWith('security.authentication.listener.aerial_ship_saml_sp', $listenerID);
209
        $this->assertStringEndsWith('.main', $listenerID);
210
    }
211
212
    /**
213
     * @test
214
     */
215
    public function shouldReturnFormEntryPointyWithPostfixId()
216
    {
217
        $factory = new SamlSpFactory();
218
        $configProcessor = new SamlSpFactoryConfiguration($factory, 'name');
219
        $config = $configProcessor->processCommonConfiguration();
220
        $containerBuilder = new ContainerBuilder(new ParameterBag());
221
222
        list(,,$entryPointID) = $factory->create($containerBuilder, 'main', $config, 'user.provider.id', null);
223
        $this->assertStringStartsWith('security.authentication.form_entry_point', $entryPointID);
224
        $this->assertStringEndsWith('.main', $entryPointID);
225
    }
226
227
    /**
228
     * @test
229
     */
230
    public function shouldCreateRelayingPartyWithListenerSetterMethod()
231
    {
232
        $expectedRelyingPartyId = 'custom.relying_party.id';
233
234
        $factory = new SamlSpFactory();
235
        $configProcessor = new SamlSpFactoryConfiguration($factory, 'name');
236
        $config = $configProcessor->processCommonConfiguration();
237
        $containerBuilder = new ContainerBuilder(new ParameterBag());
238
239
        $config['relying_party'] = $expectedRelyingPartyId;
240
241
        list(,$listenerID) = $factory->create($containerBuilder, 'main', $config, 'user.provider.id', null);
242
243
        $this->assertTrue($containerBuilder->hasDefinition($listenerID));
244
245
        $listenerDefinition = $containerBuilder->getDefinition($listenerID);
246
247
        $methodCalls = $listenerDefinition->getMethodCalls();
248
        $this->assertCount(1, $methodCalls);
249
        $this->assertEquals('setRelyingParty', $methodCalls[0][0]);
250
251
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $methodCalls[0][1][0]);
252
        $this->assertEquals($expectedRelyingPartyId, (string) $methodCalls[0][1][0]);
253
    }
254
255
    /**
256
     * @test
257
     */
258
    public function shouldCreateSpEntityDescriptorBuilder()
259
    {
260
        $factory = new SamlSpFactory();
261
        $configProcessor = new SamlSpFactoryConfiguration($factory, 'name');
262
        $config = $configProcessor->processCommonConfiguration();
263
        $containerBuilder = new ContainerBuilder(new ParameterBag());
264
265
        $factory->create($containerBuilder, 'main', $config, 'user.provider.id', null);
266
267
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.sp_entity_descriptor_builder.main.aaa'));
268
    }
269
270
    /**
271
     * @test
272
     */
273
    public function shouldCreateMetaProviderCollection()
274
    {
275
        $expectedIDPProvider = 'custom.idp.ed.provider';
276
        $expectedSPProvider = 'custom.sp.meta.provider';
277
278
        $factory = new SamlSpFactory();
279
        $configProcessor = new SamlSpFactoryConfiguration($factory, 'name');
280
        $config = $configProcessor->getCommonConfiguration();
281
282
        $config['services']['bbb']['idp']['id'] = $expectedIDPProvider;
283
        $config['services']['bbb']['sp']['meta']['id'] = $expectedSPProvider;
284
285
        $config = $configProcessor->processConfiguration($config);
286
        $containerBuilder = new ContainerBuilder(new ParameterBag());
287
288
        $factory->create($containerBuilder, 'main', $config, 'user.provider.id', null);
289
290
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.service_info_collection.main'));
291
        $metaProvidersDefinition = $containerBuilder->getDefinition('aerial_ship_saml_sp.service_info_collection.main');
292
293
        $methodCalls = $metaProvidersDefinition->getMethodCalls();
294
        $this->assertCount(2, $methodCalls);
295
        $this->assertEquals('add', $methodCalls[0][0]);
296
        $this->assertEquals('add', $methodCalls[1][0]);
297
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $methodCalls[0][1][0]);
298
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $methodCalls[1][1][0]);
299
300
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.service_info.main.aaa'));
301
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.service_info.main.bbb'));
302
303
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.idp_entity_descriptor_provider.main.aaa'));
304
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.sp_meta_provider.main.aaa'));
305
306
        $this->assertFalse($containerBuilder->hasDefinition('aerial_ship_saml_sp.idp_entity_descriptor_provider.main.bbb'));
307
        $this->assertFalse($containerBuilder->hasDefinition('aerial_ship_saml_sp.sp_meta_provider.main.bbb'));
308
    }
309
310
311
    /**
312
     * @test
313
     */
314
    public function shouldCreateRequestStateStore()
315
    {
316
        $factory = new SamlSpFactory();
317
        $configProcessor = new SamlSpFactoryConfiguration($factory, 'name');
318
        $config = $configProcessor->processCommonConfiguration();
319
        $containerBuilder = new ContainerBuilder(new ParameterBag());
320
321
        $factory->create($containerBuilder, 'main', $config, 'user.provider.id', null);
322
323
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.state.store.request.main'));
324
    }
325
326
    /**
327
     * @test
328
     */
329
    public function shouldCreateRelyingParties()
330
    {
331
        $factory = new SamlSpFactory();
332
        $configProcessor = new SamlSpFactoryConfiguration($factory, 'name');
333
        $config = $configProcessor->processCommonConfiguration();
334
        $containerBuilder = new ContainerBuilder(new ParameterBag());
335
336
        $factory->create($containerBuilder, 'main', $config, 'user.provider.id', null);
337
338
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.relying_party.discovery.main'));
339
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.relying_party.federation_metadata.main'));
340
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.relying_party.authenticate.main'));
341
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.relying_party.assertion_consumer.main'));
342
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.relying_party.logout.main'));
343
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.relying_party.sso_session_check.main'));
344
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.relying_party.composite.main'));
345
    }
346
347
348
    /**
349
     * @test
350
     */
351
    public function shouldSetOnlyProviderKeyToAuthenticationProviderIfProviderNotSetInConfig()
352
    {
353
        $expectedProviderKey = 'some_provider_key';
354
355
        $factory = new SamlSpFactory();
356
        $configProcessor = new SamlSpFactoryConfiguration($factory, 'name');
357
        $config = $configProcessor->processCommonConfiguration();
358
        $containerBuilder = new ContainerBuilder(new ParameterBag());
359
360
        list($providerID) = $factory->create($containerBuilder, $expectedProviderKey, $config, 'user.provider.id', null);
361
362
        $this->assertTrue($containerBuilder->hasDefinition($providerID));
363
        $providerDefinition = $containerBuilder->getDefinition($providerID);
364
        $this->assertEquals($expectedProviderKey, $providerDefinition->getArgument(0));
365
    }
366
367
    /**
368
     * @test
369
     */
370
    public function shouldSetProviderKeyUserProviderAdapterAndUserCheckerIfProviderSetInConfig()
371
    {
372
        $expectedProviderKey = 'some_provider_key';
373
        $expectedUserProvider = 'user.provider.id';
374
375
        $factory = new SamlSpFactory();
376
        $configProcessor = new SamlSpFactoryConfiguration($factory, 'name');
377
        $config = $configProcessor->getCommonConfiguration();
378
        $config['provider'] = $expectedUserProvider;
379
        $config = $configProcessor->processConfiguration($config);
380
        $containerBuilder = new ContainerBuilder(new ParameterBag());
381
382
        list($providerID) = $factory->create($containerBuilder, $expectedProviderKey, $config, $expectedUserProvider, null);
383
384
        $this->assertTrue($containerBuilder->hasDefinition($providerID));
385
386
        $providerDefinition = $containerBuilder->getDefinition($providerID);
387
        $this->assertCount(4, $providerDefinition->getArguments());
388
389
        $this->assertEquals($expectedProviderKey, $providerDefinition->getArgument(0));
390
391
        $adapterReference = $providerDefinition->getArgument(1);
392
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $adapterReference);
393
        $this->assertEquals('aerial_ship_saml_sp.user_provider_adapter.'.$expectedProviderKey, (string)$adapterReference);
394
395
        $this->assertTrue($containerBuilder->hasDefinition('aerial_ship_saml_sp.user_provider_adapter.'.$expectedProviderKey));
396
        $adapterDefinition = $containerBuilder->getDefinition('aerial_ship_saml_sp.user_provider_adapter.'.$expectedProviderKey);
397
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $adapterDefinition->getArgument(0));
398
        $this->assertEquals($expectedUserProvider, (string)$adapterDefinition->getArgument(0));
399
400
        $checkerReference = $providerDefinition->getArgument(2);
401
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $checkerReference);
402
        $this->assertEquals('security.user_checker', (string)$checkerReference);
403
    }
404
405
406
    /**
407
     * @test
408
     */
409
    public function shouldSetCreateUserIfNotExistsToAuthenticationProviderWhenTrue()
410
    {
411
        $factory = new SamlSpFactory();
412
        $configProcessor = new SamlSpFactoryConfiguration($factory, 'name');
413
        $config = $configProcessor->getCommonConfiguration();
414
        $config['create_user_if_not_exists'] = true;
415
        $config = $configProcessor->processConfiguration($config);
416
        $containerBuilder = new ContainerBuilder(new ParameterBag());
417
418
        list($providerID) = $factory->create($containerBuilder, 'main', $config, 'user.provider.id', null);
419
        $providerDefinition = $containerBuilder->getDefinition($providerID);
420
421
        $this->assertTrue($providerDefinition->getArgument(3));
422
    }
423
424
425
    /**
426
     * @test
427
     */
428
    public function shouldSetCreateUserIfNotExistsToAuthenticationProviderWhenFalse()
429
    {
430
        $factory = new SamlSpFactory();
431
        $configProcessor = new SamlSpFactoryConfiguration($factory, 'name');
432
        $config = $configProcessor->getCommonConfiguration();
433
        $config['create_user_if_not_exists'] = false;
434
        $config = $configProcessor->processConfiguration($config);
435
        $containerBuilder = new ContainerBuilder(new ParameterBag());
436
437
        list($providerID) = $factory->create($containerBuilder, 'main', $config, 'user.provider.id', null);
438
        $providerDefinition = $containerBuilder->getDefinition($providerID);
439
440
        $this->assertFalse($providerDefinition->getArgument(3));
441
    }
442
} 
443