vendor/bundles/FOS/UserBundle/Tests/DependencyInjection/FOSUserExtensionTest.php
changeset 3 e54dfe4d0b2b
equal deleted inserted replaced
2:806e57d67020 3:e54dfe4d0b2b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the FOSUserBundle package.
       
     5  *
       
     6  * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\UserBundle\Tests\DependencyInjection;
       
    13 
       
    14 use Symfony\Component\DependencyInjection\ContainerBuilder;
       
    15 use FOS\UserBundle\DependencyInjection\FOSUserExtension;
       
    16 use Symfony\Component\Yaml\Parser;
       
    17 
       
    18 class FOSUserExtensionTest extends \PHPUnit_Framework_TestCase
       
    19 {
       
    20     protected $configuration;
       
    21 
       
    22     /**
       
    23      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
       
    24      */
       
    25     public function testUserLoadThrowsExceptionUnlessDatabaseDriverSet()
       
    26     {
       
    27         $loader = new FOSUserExtension();
       
    28         $config = $this->getEmptyConfig();
       
    29         unset($config['db_driver']);
       
    30         $loader->load(array($config), new ContainerBuilder());
       
    31     }
       
    32 
       
    33     /**
       
    34      * @expectedException \InvalidArgumentException
       
    35      */
       
    36     public function testUserLoadThrowsExceptionUnlessDatabaseDriverIsValid()
       
    37     {
       
    38         $loader = new FOSUserExtension();
       
    39         $config = $this->getEmptyConfig();
       
    40         $config['db_driver'] = 'foo';
       
    41         $loader->load(array($config), new ContainerBuilder());
       
    42     }
       
    43 
       
    44     /**
       
    45      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
       
    46      */
       
    47     public function testUserLoadThrowsExceptionUnlessFirewallNameSet()
       
    48     {
       
    49         $loader = new FOSUserExtension();
       
    50         $config = $this->getEmptyConfig();
       
    51         unset($config['firewall_name']);
       
    52         $loader->load(array($config), new ContainerBuilder());
       
    53     }
       
    54 
       
    55     /**
       
    56      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
       
    57      */
       
    58     public function testUserLoadThrowsExceptionUnlessGroupModelClassSet()
       
    59     {
       
    60         $loader = new FOSUserExtension();
       
    61         $config = $this->getFullConfig();
       
    62         unset($config['group']['group_class']);
       
    63         $loader->load(array($config), new ContainerBuilder());
       
    64     }
       
    65 
       
    66     /**
       
    67      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
       
    68      */
       
    69     public function testUserLoadThrowsExceptionUnlessUserModelClassSet()
       
    70     {
       
    71         $loader = new FOSUserExtension();
       
    72         $config = $this->getEmptyConfig();
       
    73         unset($config['user_class']);
       
    74         $loader->load(array($config), new ContainerBuilder());
       
    75     }
       
    76 
       
    77     public function testDisableRegistration()
       
    78     {
       
    79         $this->configuration = new ContainerBuilder();
       
    80         $loader = new FOSUserExtension();
       
    81         $config = $this->getEmptyConfig();
       
    82         $config['registration'] = false;
       
    83         $loader->load(array($config), $this->configuration);
       
    84         $this->assertNotHasDefinition('fos_user.registration.form');
       
    85     }
       
    86 
       
    87     public function testDisableResetting()
       
    88     {
       
    89         $this->configuration = new ContainerBuilder();
       
    90         $loader = new FOSUserExtension();
       
    91         $config = $this->getEmptyConfig();
       
    92         $config['resetting'] = false;
       
    93         $loader->load(array($config), $this->configuration);
       
    94         $this->assertNotHasDefinition('fos_user.resetting.form');
       
    95     }
       
    96 
       
    97     public function testDisableProfile()
       
    98     {
       
    99         $this->configuration = new ContainerBuilder();
       
   100         $loader = new FOSUserExtension();
       
   101         $config = $this->getEmptyConfig();
       
   102         $config['profile'] = false;
       
   103         $loader->load(array($config), $this->configuration);
       
   104         $this->assertNotHasDefinition('fos_user.profile.form');
       
   105     }
       
   106 
       
   107     public function testDisableChangePassword()
       
   108     {
       
   109         $this->configuration = new ContainerBuilder();
       
   110         $loader = new FOSUserExtension();
       
   111         $config = $this->getEmptyConfig();
       
   112         $config['change_password'] = false;
       
   113         $loader->load(array($config), $this->configuration);
       
   114         $this->assertNotHasDefinition('fos_user.change_password.form');
       
   115     }
       
   116 
       
   117     public function testUserLoadModelClassWithDefaults()
       
   118     {
       
   119         $this->createEmptyConfiguration();
       
   120 
       
   121         $this->assertParameter('Acme\MyBundle\Document\User', 'fos_user.model.user.class');
       
   122     }
       
   123 
       
   124     public function testUserLoadModelClass()
       
   125     {
       
   126         $this->createFullConfiguration();
       
   127 
       
   128         $this->assertParameter('Acme\MyBundle\Entity\User', 'fos_user.model.user.class');
       
   129     }
       
   130 
       
   131     public function testUserLoadManagerClassWithDefaults()
       
   132     {
       
   133         $this->createEmptyConfiguration();
       
   134 
       
   135         $this->assertParameter(null, 'fos_user.model_manager_name');
       
   136         $this->assertAlias('fos_user.user_manager.default', 'fos_user.user_manager');
       
   137         $this->assertNotHasDefinition('fos_user.group_manager');
       
   138     }
       
   139 
       
   140     public function testUserLoadManagerClass()
       
   141     {
       
   142         $this->createFullConfiguration();
       
   143 
       
   144         $this->assertParameter('custom', 'fos_user.model_manager_name');
       
   145         $this->assertAlias('acme_my.user_manager', 'fos_user.user_manager');
       
   146         $this->assertAlias('fos_user.group_manager.default', 'fos_user.group_manager');
       
   147     }
       
   148 
       
   149     public function testUserLoadFormClassWithDefaults()
       
   150     {
       
   151         $this->createEmptyConfiguration();
       
   152 
       
   153         $this->assertParameter('fos_user_profile', 'fos_user.profile.form.type');
       
   154         $this->assertParameter('fos_user_registration', 'fos_user.registration.form.type');
       
   155         $this->assertParameter('fos_user_change_password', 'fos_user.change_password.form.type');
       
   156         $this->assertParameter('fos_user_resetting', 'fos_user.resetting.form.type');
       
   157     }
       
   158 
       
   159     public function testUserLoadFormClass()
       
   160     {
       
   161         $this->createFullConfiguration();
       
   162 
       
   163         $this->assertParameter('acme_my_profile', 'fos_user.profile.form.type');
       
   164         $this->assertParameter('acme_my_registration', 'fos_user.registration.form.type');
       
   165         $this->assertParameter('acme_my_group', 'fos_user.group.form.type');
       
   166         $this->assertParameter('acme_my_change_password', 'fos_user.change_password.form.type');
       
   167         $this->assertParameter('acme_my_resetting', 'fos_user.resetting.form.type');
       
   168     }
       
   169 
       
   170     public function testUserLoadFormNameWithDefaults()
       
   171     {
       
   172         $this->createEmptyConfiguration();
       
   173 
       
   174         $this->assertParameter('fos_user_profile_form', 'fos_user.profile.form.name');
       
   175         $this->assertParameter('fos_user_registration_form', 'fos_user.registration.form.name');
       
   176         $this->assertParameter('fos_user_change_password_form', 'fos_user.change_password.form.name');
       
   177         $this->assertParameter('fos_user_resetting_form', 'fos_user.resetting.form.name');
       
   178     }
       
   179 
       
   180     public function testUserLoadFormName()
       
   181     {
       
   182         $this->createFullConfiguration();
       
   183 
       
   184         $this->assertParameter('acme_profile_form', 'fos_user.profile.form.name');
       
   185         $this->assertParameter('acme_registration_form', 'fos_user.registration.form.name');
       
   186         $this->assertParameter('acme_group_form', 'fos_user.group.form.name');
       
   187         $this->assertParameter('acme_change_password_form', 'fos_user.change_password.form.name');
       
   188         $this->assertParameter('acme_resetting_form', 'fos_user.resetting.form.name');
       
   189     }
       
   190 
       
   191     public function testUserLoadFormServiceWithDefaults()
       
   192     {
       
   193         $this->createEmptyConfiguration();
       
   194 
       
   195         $this->assertHasDefinition('fos_user.profile.form');
       
   196         $this->assertHasDefinition('fos_user.registration.form');
       
   197         $this->assertNotHasDefinition('fos_user.group.form');
       
   198         $this->assertHasDefinition('fos_user.change_password.form');
       
   199         $this->assertHasDefinition('fos_user.resetting.form');
       
   200     }
       
   201 
       
   202     public function testUserLoadFormService()
       
   203     {
       
   204         $this->createFullConfiguration();
       
   205 
       
   206         $this->assertHasDefinition('fos_user.profile.form');
       
   207         $this->assertHasDefinition('fos_user.registration.form');
       
   208         $this->assertHasDefinition('fos_user.group.form');
       
   209         $this->assertHasDefinition('fos_user.change_password.form');
       
   210         $this->assertHasDefinition('fos_user.resetting.form');
       
   211     }
       
   212 
       
   213     public function testUserLoadConfirmationEmailWithDefaults()
       
   214     {
       
   215         $this->createEmptyConfiguration();
       
   216 
       
   217         $this->assertParameter(false, 'fos_user.registration.confirmation.enabled');
       
   218         $this->assertParameter(array('webmaster@example.com' => 'webmaster'), 'fos_user.registration.confirmation.from_email');
       
   219         $this->assertParameter('FOSUserBundle:Registration:email.txt.twig', 'fos_user.registration.confirmation.template');
       
   220         $this->assertParameter('FOSUserBundle:Resetting:email.txt.twig', 'fos_user.resetting.email.template');
       
   221         $this->assertParameter(array('webmaster@example.com' => 'webmaster'), 'fos_user.resetting.email.from_email');
       
   222         $this->assertParameter(86400, 'fos_user.resetting.token_ttl');
       
   223     }
       
   224 
       
   225     public function testUserLoadConfirmationEmail()
       
   226     {
       
   227         $this->createFullConfiguration();
       
   228 
       
   229         $this->assertParameter(true, 'fos_user.registration.confirmation.enabled');
       
   230         $this->assertParameter(array('register@acme.org' => 'Acme Corp'), 'fos_user.registration.confirmation.from_email');
       
   231         $this->assertParameter('AcmeMyBundle:Registration:mail.txt.twig', 'fos_user.registration.confirmation.template');
       
   232         $this->assertParameter('AcmeMyBundle:Resetting:mail.txt.twig', 'fos_user.resetting.email.template');
       
   233         $this->assertParameter(array('reset@acme.org' => 'Acme Corp'), 'fos_user.resetting.email.from_email');
       
   234         $this->assertParameter(1800, 'fos_user.resetting.token_ttl');
       
   235     }
       
   236 
       
   237     public function testUserLoadTemplateConfigWithDefaults()
       
   238     {
       
   239         $this->createEmptyConfiguration();
       
   240 
       
   241         $this->assertParameter('twig', 'fos_user.template.engine');
       
   242         $this->assertParameter('FOSUserBundle::form.html.twig', 'fos_user.template.theme');
       
   243     }
       
   244 
       
   245     public function testUserLoadTemplateConfig()
       
   246     {
       
   247         $this->createFullConfiguration();
       
   248 
       
   249         $this->assertParameter('php', 'fos_user.template.engine');
       
   250         $this->assertParameter('AcmeMyBundle:Form:theme.html.twig', 'fos_user.template.theme');
       
   251     }
       
   252 
       
   253     public function testUserLoadEncoderConfigWithDefaults()
       
   254     {
       
   255         $this->createEmptyConfiguration();
       
   256 
       
   257         $this->assertParameter('sha512', 'fos_user.encoder.algorithm');
       
   258         $this->assertParameter(false, 'fos_user.encoder.encode_as_base64');
       
   259         $this->assertParameter(1, 'fos_user.encoder.iterations');
       
   260     }
       
   261 
       
   262     public function testUserLoadEncoderConfig()
       
   263     {
       
   264         $this->createFullConfiguration();
       
   265 
       
   266         $this->assertParameter('sha1', 'fos_user.encoder.algorithm');
       
   267         $this->assertParameter(true, 'fos_user.encoder.encode_as_base64');
       
   268         $this->assertParameter(3, 'fos_user.encoder.iterations');
       
   269     }
       
   270 
       
   271     public function testUserLoadUtilServiceWithDefaults()
       
   272     {
       
   273         $this->createEmptyConfiguration();
       
   274 
       
   275         $this->assertAlias('fos_user.mailer.default', 'fos_user.mailer');
       
   276         $this->assertAlias('fos_user.util.email_canonicalizer.default', 'fos_user.util.email_canonicalizer');
       
   277         $this->assertAlias('fos_user.util.username_canonicalizer.default', 'fos_user.util.username_canonicalizer');
       
   278     }
       
   279 
       
   280     public function testUserLoadUtilService()
       
   281     {
       
   282         $this->createFullConfiguration();
       
   283 
       
   284         $this->assertAlias('acme_my.mailer', 'fos_user.mailer');
       
   285         $this->assertAlias('acme_my.email_canonicalizer', 'fos_user.util.email_canonicalizer');
       
   286         $this->assertAlias('acme_my.username_canonicalizer', 'fos_user.util.username_canonicalizer');
       
   287     }
       
   288 
       
   289     /**
       
   290      * @return ContainerBuilder
       
   291      */
       
   292     protected function createEmptyConfiguration()
       
   293     {
       
   294         $this->configuration = new ContainerBuilder();
       
   295         $loader = new FOSUserExtension();
       
   296         $config = $this->getEmptyConfig();
       
   297         $loader->load(array($config), $this->configuration);
       
   298         $this->assertTrue($this->configuration instanceof ContainerBuilder);
       
   299     }
       
   300 
       
   301     /**
       
   302      * @return ContainerBuilder
       
   303      */
       
   304     protected function createFullConfiguration()
       
   305     {
       
   306         $this->configuration = new ContainerBuilder();
       
   307         $loader = new FOSUserExtension();
       
   308         $config = $this->getFullConfig();
       
   309         $loader->load(array($config), $this->configuration);
       
   310         $this->assertTrue($this->configuration instanceof ContainerBuilder);
       
   311     }
       
   312 
       
   313     /**
       
   314      * getEmptyConfig
       
   315      *
       
   316      * @return array
       
   317      */
       
   318     protected function getEmptyConfig()
       
   319     {
       
   320         $yaml = <<<EOF
       
   321 db_driver: mongodb
       
   322 firewall_name: fos_user
       
   323 user_class: Acme\MyBundle\Document\User
       
   324 EOF;
       
   325         $parser = new Parser();
       
   326 
       
   327         return $parser->parse($yaml);
       
   328     }
       
   329 
       
   330     protected function getFullConfig()
       
   331     {
       
   332         $yaml = <<<EOF
       
   333 db_driver: orm
       
   334 firewall_name: fos_user
       
   335 use_listener: true
       
   336 user_class: Acme\MyBundle\Entity\User
       
   337 model_manager_name: custom
       
   338 from_email:
       
   339     address: admin@acme.org
       
   340     sender_name: Acme Corp
       
   341 profile:
       
   342     form:
       
   343         type: acme_my_profile
       
   344         handler: acme_my.form.handler.profile
       
   345         name: acme_profile_form
       
   346         validation_groups: [acme_profile]
       
   347 change_password:
       
   348     form:
       
   349         type: acme_my_change_password
       
   350         handler: acme_my.form.handler.change_password
       
   351         name: acme_change_password_form
       
   352         validation_groups: [acme_change_password]
       
   353 registration:
       
   354     confirmation:
       
   355         from_email:
       
   356             address: register@acme.org
       
   357             sender_name: Acme Corp
       
   358         enabled: true
       
   359         template: AcmeMyBundle:Registration:mail.txt.twig
       
   360     form:
       
   361         type: acme_my_registration
       
   362         handler: acme_my.form.handler.registration
       
   363         name: acme_registration_form
       
   364         validation_groups: [acme_registration]
       
   365 resetting:
       
   366     token_ttl: 1800
       
   367     email:
       
   368         from_email:
       
   369             address: reset@acme.org
       
   370             sender_name: Acme Corp
       
   371         template: AcmeMyBundle:Resetting:mail.txt.twig
       
   372     form:
       
   373         type: acme_my_resetting
       
   374         handler: acme_my.form.handler.resetting
       
   375         name: acme_resetting_form
       
   376         validation_groups: [acme_resetting]
       
   377 service:
       
   378     mailer: acme_my.mailer
       
   379     email_canonicalizer: acme_my.email_canonicalizer
       
   380     username_canonicalizer: acme_my.username_canonicalizer
       
   381     user_manager: acme_my.user_manager
       
   382 encoder:
       
   383     algorithm: sha1
       
   384     encode_as_base64: true
       
   385     iterations: 3
       
   386 template:
       
   387     engine: php
       
   388     theme: AcmeMyBundle:Form:theme.html.twig
       
   389 group:
       
   390     group_class: Acme\MyBundle\Entity\Group
       
   391     form:
       
   392         type: acme_my_group
       
   393         handler: acme_my.form.handler.group
       
   394         name: acme_group_form
       
   395         validation_groups: [acme_group]
       
   396 EOF;
       
   397         $parser = new Parser();
       
   398 
       
   399         return  $parser->parse($yaml);
       
   400     }
       
   401 
       
   402     private function assertAlias($value, $key)
       
   403     {
       
   404         $this->assertEquals($value, (string) $this->configuration->getAlias($key), sprintf('%s alias is correct', $key));
       
   405     }
       
   406 
       
   407     private function assertParameter($value, $key)
       
   408     {
       
   409         $this->assertEquals($value, $this->configuration->getParameter($key), sprintf('%s parameter is correct', $key));
       
   410     }
       
   411 
       
   412     private function assertHasDefinition($id)
       
   413     {
       
   414         $this->assertTrue(($this->configuration->hasDefinition($id) ?: $this->configuration->hasAlias($id)));
       
   415     }
       
   416 
       
   417     private function assertNotHasDefinition($id)
       
   418     {
       
   419         $this->assertFalse(($this->configuration->hasDefinition($id) ?: $this->configuration->hasAlias($id)));
       
   420     }
       
   421 
       
   422     protected function tearDown()
       
   423     {
       
   424         unset($this->configuration);
       
   425     }
       
   426 }