|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Security\Factory; |
| 13 | + |
| 14 | +use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\GuardAuthenticationFactory; |
| 15 | +use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 17 | +use Symfony\Component\DependencyInjection\Reference; |
| 18 | + |
| 19 | +class GuardAuthenticationFactoryTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @dataProvider getValidConfigurationTests |
| 23 | + */ |
| 24 | + public function testAddValidConfiguration(array $inputConfig, array $expectedConfig) |
| 25 | + { |
| 26 | + $factory = new GuardAuthenticationFactory(); |
| 27 | + $nodeDefinition = new ArrayNodeDefinition('guard'); |
| 28 | + $factory->addConfiguration($nodeDefinition); |
| 29 | + |
| 30 | + $node = $nodeDefinition->getNode(); |
| 31 | + $normalizedConfig = $node->normalize($inputConfig); |
| 32 | + $finalizedConfig = $node->finalize($normalizedConfig); |
| 33 | + |
| 34 | + $this->assertEquals($expectedConfig, $finalizedConfig); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
| 39 | + * @dataProvider getInvalidConfigurationTests |
| 40 | + */ |
| 41 | + public function testAddInvalidConfiguration(array $inputConfig) |
| 42 | + { |
| 43 | + $factory = new GuardAuthenticationFactory(); |
| 44 | + $nodeDefinition = new ArrayNodeDefinition('guard'); |
| 45 | + $factory->addConfiguration($nodeDefinition); |
| 46 | + |
| 47 | + $node = $nodeDefinition->getNode(); |
| 48 | + $normalizedConfig = $node->normalize($inputConfig); |
| 49 | + // will validate and throw an exception on invalid |
| 50 | + $node->finalize($normalizedConfig); |
| 51 | + } |
| 52 | + |
| 53 | + public function getValidConfigurationTests() |
| 54 | + { |
| 55 | + $tests = array(); |
| 56 | + |
| 57 | + // completely basic |
| 58 | + $tests[] = array( |
| 59 | + array( |
| 60 | + 'authenticators' => array('authenticator1', 'authenticator2'), |
| 61 | + 'provider' => 'some_provider', |
| 62 | + 'entry_point' => 'the_entry_point', |
| 63 | + ), |
| 64 | + array( |
| 65 | + 'authenticators' => array('authenticator1', 'authenticator2'), |
| 66 | + 'provider' => 'some_provider', |
| 67 | + 'entry_point' => 'the_entry_point', |
| 68 | + ), |
| 69 | + ); |
| 70 | + |
| 71 | + // testing xml config fix: authenticator -> authenticators |
| 72 | + $tests[] = array( |
| 73 | + array( |
| 74 | + 'authenticator' => array('authenticator1', 'authenticator2'), |
| 75 | + ), |
| 76 | + array( |
| 77 | + 'authenticators' => array('authenticator1', 'authenticator2'), |
| 78 | + 'entry_point' => null, |
| 79 | + ), |
| 80 | + ); |
| 81 | + |
| 82 | + return $tests; |
| 83 | + } |
| 84 | + |
| 85 | + public function getInvalidConfigurationTests() |
| 86 | + { |
| 87 | + $tests = array(); |
| 88 | + |
| 89 | + // testing not empty |
| 90 | + $tests[] = array( |
| 91 | + array('authenticators' => array()), |
| 92 | + ); |
| 93 | + |
| 94 | + return $tests; |
| 95 | + } |
| 96 | + |
| 97 | + public function testBasicCreate() |
| 98 | + { |
| 99 | + // simple configuration |
| 100 | + $config = array( |
| 101 | + 'authenticators' => array('authenticator123'), |
| 102 | + 'entry_point' => null, |
| 103 | + ); |
| 104 | + list($container, $entryPointId) = $this->executeCreate($config, null); |
| 105 | + $this->assertEquals('authenticator123', $entryPointId); |
| 106 | + |
| 107 | + $providerDefinition = $container->getDefinition('security.authentication.provider.guard.my_firewall'); |
| 108 | + $this->assertEquals(array( |
| 109 | + 'index_0' => array(new Reference('authenticator123')), |
| 110 | + 'index_1' => new Reference('my_user_provider'), |
| 111 | + 'index_2' => 'my_firewall', |
| 112 | + ), $providerDefinition->getArguments()); |
| 113 | + |
| 114 | + $listenerDefinition = $container->getDefinition('security.authentication.listener.guard.my_firewall'); |
| 115 | + $this->assertEquals('my_firewall', $listenerDefinition->getArgument(2)); |
| 116 | + $this->assertEquals(array(new Reference('authenticator123')), $listenerDefinition->getArgument(3)); |
| 117 | + } |
| 118 | + |
| 119 | + public function testExistingDefaultEntryPointUsed() |
| 120 | + { |
| 121 | + // any existing default entry point is used |
| 122 | + $config = array( |
| 123 | + 'authenticators' => array('authenticator123'), |
| 124 | + 'entry_point' => null, |
| 125 | + ); |
| 126 | + list($container, $entryPointId) = $this->executeCreate($config, 'some_default_entry_point'); |
| 127 | + $this->assertEquals('some_default_entry_point', $entryPointId); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @expectedException \LogicException |
| 132 | + */ |
| 133 | + public function testCannotOverrideDefaultEntryPoint() |
| 134 | + { |
| 135 | + // any existing default entry point is used |
| 136 | + $config = array( |
| 137 | + 'authenticators' => array('authenticator123'), |
| 138 | + 'entry_point' => 'authenticator123', |
| 139 | + ); |
| 140 | + $this->executeCreate($config, 'some_default_entry_point'); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @expectedException \LogicException |
| 145 | + */ |
| 146 | + public function testMultipleAuthenticatorsRequiresEntryPoint() |
| 147 | + { |
| 148 | + // any existing default entry point is used |
| 149 | + $config = array( |
| 150 | + 'authenticators' => array('authenticator123', 'authenticatorABC'), |
| 151 | + 'entry_point' => null, |
| 152 | + ); |
| 153 | + $this->executeCreate($config, null); |
| 154 | + } |
| 155 | + |
| 156 | + public function testCreateWithEntryPoint() |
| 157 | + { |
| 158 | + // any existing default entry point is used |
| 159 | + $config = array( |
| 160 | + 'authenticators' => array('authenticator123', 'authenticatorABC'), |
| 161 | + 'entry_point' => 'authenticatorABC', |
| 162 | + ); |
| 163 | + list($container, $entryPointId) = $this->executeCreate($config, null); |
| 164 | + $this->assertEquals('authenticatorABC', $entryPointId); |
| 165 | + } |
| 166 | + |
| 167 | + private function executeCreate(array $config, $defaultEntryPointId) |
| 168 | + { |
| 169 | + $container = new ContainerBuilder(); |
| 170 | + $container->register('security.authentication.provider.guard'); |
| 171 | + $container->register('security.authentication.listener.guard'); |
| 172 | + $id = 'my_firewall'; |
| 173 | + $userProviderId = 'my_user_provider'; |
| 174 | + |
| 175 | + $factory = new GuardAuthenticationFactory(); |
| 176 | + list($providerId, $listenerId, $entryPointId) = $factory->create($container, $id, $config, $userProviderId, $defaultEntryPointId); |
| 177 | + |
| 178 | + return array($container, $entryPointId); |
| 179 | + } |
| 180 | +} |
0 commit comments