diff --git a/Controller/ProfileManager.php b/Controller/ProfileManager.php index a895880..6e4ed19 100644 --- a/Controller/ProfileManager.php +++ b/Controller/ProfileManager.php @@ -1,4 +1,5 @@ formFactory->createForm(); - + return $this->container->get('templating')->renderResponse($template, array( 'form' => $form->createView(), diff --git a/Controller/RegistrationManager.php b/Controller/RegistrationManager.php index fc968e6..97227e6 100644 --- a/Controller/RegistrationManager.php +++ b/Controller/RegistrationManager.php @@ -11,38 +11,33 @@ class RegistrationManager { /** - * - * @var \PUGX\MultiUserBundle\Model\UserDiscriminator + * @var \PUGX\MultiUserBundle\Model\UserDiscriminator */ protected $userDiscriminator; - + /** - * - * @var \Symfony\Component\DependencyInjection\ContainerInterface + * @var \Symfony\Component\DependencyInjection\ContainerInterface */ protected $container; - + /** - * - * @var \FOS\UserBundle\Controller\RegistrationController + * @var \FOS\UserBundle\Controller\RegistrationController */ protected $controller; - + /** - * * @var \PUGX\MultiUserBundle\Form\FormFactory */ protected $formFactory; - + /** - * - * @param \PUGX\MultiUserBundle\Model\UserDiscriminator $userDiscriminator + * @param \PUGX\MultiUserBundle\Model\UserDiscriminator $userDiscriminator * @param \Symfony\Component\DependencyInjection\ContainerInterface $container - * @param \FOS\UserBundle\Controller\RegistrationController $controller - * @param \PUGX\MultiUserBundle\Form\FormFactory $formFactory + * @param \FOS\UserBundle\Controller\RegistrationController $controller + * @param \PUGX\MultiUserBundle\Form\FormFactory $formFactory */ public function __construct(UserDiscriminator $userDiscriminator, - ContainerInterface $container, + ContainerInterface $container, RegistrationController $controller, FormFactory $formFactory) { @@ -51,28 +46,28 @@ public function __construct(UserDiscriminator $userDiscriminator, $this->controller = $controller; $this->formFactory = $formFactory; } - + /** - * * @param string $class + * * @return \Symfony\Component\HttpFoundation\RedirectResponse */ public function register($class, $templates = array()) { $this->userDiscriminator->setClass($class); - + $this->controller->setContainer($this->container); - $result = $this->controller->registerAction($this->container->get('request')); + $result = $this->controller->registerAction($this->container->get('request')); if ($result instanceof RedirectResponse) { return $result; } - + $template = $this->userDiscriminator->getTemplate('registration'); if (is_null($template)) { $template = 'FOSUserBundle:Registration:register.html.twig'; } - - $form = $this->formFactory->createForm(); + + $form = $this->formFactory->createForm(); return $this->container->get('templating')->renderResponse($template, array( 'form' => $form->createView(), diff --git a/DependencyInjection/Compiler/OverrideServiceCompilerPass.php b/DependencyInjection/Compiler/OverrideServiceCompilerPass.php index 56cf3a9..fdc6149 100644 --- a/DependencyInjection/Compiler/OverrideServiceCompilerPass.php +++ b/DependencyInjection/Compiler/OverrideServiceCompilerPass.php @@ -10,13 +10,13 @@ class OverrideServiceCompilerPass implements CompilerPassInterface public function process(ContainerBuilder $container) { $this->changeService( - $container, - 'fos_user.registration.form.factory', + $container, + 'fos_user.registration.form.factory', 'pugx_multi_user.registration_form_factory'); - + $this->changeService( - $container, - 'fos_user.profile.form.factory', + $container, + 'fos_user.profile.form.factory', 'pugx_multi_user.profile_form_factory'); $this->changeService( @@ -29,15 +29,15 @@ public function process(ContainerBuilder $container) 'fos_user.profile.form.type', 'pugx_multi_user.profile.form.type'); } - + private function changeService($container, $serviceName, $newServiceName) { $service = $container->getDefinition($serviceName); $newService = $container->getDefinition($newServiceName); - + if ($service && $newService) { $container->removeDefinition($serviceName); $container->setDefinition($serviceName, $newService); } } -} \ No newline at end of file +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c23f273..b47bf84 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -6,14 +6,14 @@ use Symfony\Component\Config\Definition\ConfigurationInterface; /** - * This is the class that validates and merges configuration from your app/config files + * This is the class that validates and merges configuration from your app/config files. * * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class} */ class Configuration implements ConfigurationInterface { /** - * {@inheritDoc} + * {@inheritdoc} */ public function getConfigTreeBuilder() { @@ -21,7 +21,7 @@ public function getConfigTreeBuilder() $rootNode = $treeBuilder->root('pugx_multi_user'); $supportedDrivers = array('orm'); - + $rootNode-> children() ->scalarNode('db_driver') @@ -31,7 +31,7 @@ public function getConfigTreeBuilder() ->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($supportedDrivers)) ->end() ->end(); - + $rootNode-> children() ->arrayNode('users')->prototype('array') @@ -40,7 +40,7 @@ public function getConfigTreeBuilder() ->children() ->scalarNode('class')->isRequired()->cannotBeEmpty()->end() ->scalarNode('factory')->defaultValue('PUGX\MultiUserBundle\Model\UserFactory')->end() - ->end() + ->end() ->end() ->end() ->children() @@ -89,7 +89,7 @@ public function getConfigTreeBuilder() ->end() ->end() ->end(); - + return $treeBuilder; } } diff --git a/DependencyInjection/PUGXMultiUserExtension.php b/DependencyInjection/PUGXMultiUserExtension.php index 2adf3b4..f912362 100644 --- a/DependencyInjection/PUGXMultiUserExtension.php +++ b/DependencyInjection/PUGXMultiUserExtension.php @@ -8,26 +8,26 @@ use Symfony\Component\DependencyInjection\Loader; /** - * This is the class that loads and manages your bundle configuration + * This is the class that loads and manages your bundle configuration. * * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} */ class PUGXMultiUserExtension extends Extension { /** - * {@inheritDoc} + * {@inheritdoc} */ public function load(array $configs, ContainerBuilder $container) { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - + $users = $config['users']; $container->setParameter('pugx_user_discriminator_users', $users); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.yml'); - + $loader->load(sprintf('%s.yml', $config['db_driver'])); } } diff --git a/Doctrine/UserManager.php b/Doctrine/UserManager.php index 2598ad9..19255f7 100644 --- a/Doctrine/UserManager.php +++ b/Doctrine/UserManager.php @@ -10,7 +10,7 @@ use PUGX\MultiUserBundle\Model\UserDiscriminator; /** - * Custom user manager for FOSUserBundle + * Custom user manager for FOSUserBundle. * * @author leonardo proietti (leonardo.proietti@gmail.com) * @author eux (eugenio@netmeans.net) @@ -18,13 +18,11 @@ class UserManager extends BaseUserManager { /** - * * @var ObjectManager */ protected $om; /** - * * @var UserDiscriminator */ protected $userDiscriminator; @@ -48,8 +46,7 @@ public function __construct(EncoderFactoryInterface $encoderFactory, Canonicaliz } /** - * - * {@inheritDoc} + * {@inheritdoc} */ public function createUser() { @@ -57,7 +54,7 @@ public function createUser() } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getClass() { @@ -65,14 +62,13 @@ public function getClass() } /** - * {@inheritDoc} + * {@inheritdoc} */ public function findUserBy(array $criteria) { $classes = $this->userDiscriminator->getClasses(); foreach ($classes as $class) { - $repo = $this->om->getRepository($class); if (!$repo) { @@ -88,15 +84,16 @@ public function findUserBy(array $criteria) if ($user) { $this->userDiscriminator->setClass($class); + return $user; } } - return null; + return; } /** - * {@inheritDoc} + * {@inheritdoc} */ public function findUsers() { @@ -116,16 +113,14 @@ public function findUsers() return $usersAll; } - /** - * {@inheritDoc} + * {@inheritdoc} */ protected function findConflictualUsers($value, array $fields) { $classes = $this->userDiscriminator->getClasses(); foreach ($classes as $class) { - $repo = $this->om->getRepository($class); $users = $repo->findBy($this->getCriteria($value, $fields)); diff --git a/Form/FormFactory.php b/Form/FormFactory.php index 3d68042..460e177 100644 --- a/Form/FormFactory.php +++ b/Form/FormFactory.php @@ -9,63 +9,57 @@ class FormFactory implements FactoryInterface { /** - * - * @var \PUGX\MultiUserBundle\Model\UserDiscriminator + * @var \PUGX\MultiUserBundle\Model\UserDiscriminator */ private $userDiscriminator; - + /** - * - * @var FormFactoryInterface + * @var FormFactoryInterface */ private $formFactory; - + /** - * - * @var string + * @var string */ private $type; - + /** - * - * @var array + * @var array */ private $forms = array(); - + /** - * * @param \PUGX\MultiUserBundle\Model\UserDiscriminator $userDiscriminator - * @param string $type registration|profile + * @param string $type registration|profile */ - public function __construct(UserDiscriminator $userDiscriminator, FormFactoryInterface $formFactory, $type) + public function __construct(UserDiscriminator $userDiscriminator, FormFactoryInterface $formFactory, $type) { $this->userDiscriminator = $userDiscriminator; $this->formFactory = $formFactory; $this->type = $type; } - + /** - * - * @return \Symfony\Component\Form\Form + * @return \Symfony\Component\Form\Form */ public function createForm() { $type = $this->userDiscriminator->getFormType($this->type); $name = $this->userDiscriminator->getFormName($this->type); $validationGroups = $this->userDiscriminator->getFormValidationGroups($this->type); - + if (array_key_exists($name, $this->forms)) { return $this->forms[$name]; } - + $form = $this->formFactory->createNamed( - $name, - $type, - null, + $name, + $type, + null, array('validation_groups' => $validationGroups)); - + $this->forms[$name] = $form; - + return $form; } -} \ No newline at end of file +} diff --git a/Form/ProfileFormType.php b/Form/ProfileFormType.php index d457d42..cf27202 100644 --- a/Form/ProfileFormType.php +++ b/Form/ProfileFormType.php @@ -47,7 +47,7 @@ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => $this->class, - 'intention' => 'profile', + 'intention' => 'profile', )); } diff --git a/Form/RegistrationFormType.php b/Form/RegistrationFormType.php index cab7d61..3fc18e6 100644 --- a/Form/RegistrationFormType.php +++ b/Form/RegistrationFormType.php @@ -49,7 +49,7 @@ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => $this->class, - 'intention' => 'registration', + 'intention' => 'registration', )); } @@ -63,6 +63,4 @@ public function getName() { return 'fos_user_registration'; } - - } diff --git a/Listener/AuthenticationListener.php b/Listener/AuthenticationListener.php index a6ebdcf..dbb5ed1 100644 --- a/Listener/AuthenticationListener.php +++ b/Listener/AuthenticationListener.php @@ -14,13 +14,11 @@ class AuthenticationListener implements EventSubscriberInterface { /** - * * @var UserDiscriminator */ protected $userDiscriminator; /** - * * @param UserDiscriminator $controllerHandler */ public function __construct(UserDiscriminator $userDiscriminator) @@ -29,7 +27,6 @@ public function __construct(UserDiscriminator $userDiscriminator) } /** - * * @return array */ public static function getSubscribedEvents() @@ -37,7 +34,7 @@ public static function getSubscribedEvents() return array( FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onSecurityImplicitLogin', SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin', - SecurityEvents::SWITCH_USER => 'onSecuritySwitchUser' + SecurityEvents::SWITCH_USER => 'onSecuritySwitchUser', ); } @@ -48,7 +45,6 @@ protected function discriminate($user) } /** - * * @param \FOS\UserBundle\Event\UserEvent $event */ public function onSecurityImplicitLogin(UserEvent $event) @@ -57,7 +53,6 @@ public function onSecurityImplicitLogin(UserEvent $event) } /** - * * @param \Symfony\Component\Security\Http\Event\InteractiveLoginEvent $event */ public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) @@ -66,7 +61,6 @@ public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) } /** - * * @param \Symfony\Component\Security\Http\Event\SwitchUserEvent $event */ public function onSecuritySwitchUser(SwitchUserEvent $event) diff --git a/Model/UserDiscriminator.php b/Model/UserDiscriminator.php index 94035ea..c472633 100644 --- a/Model/UserDiscriminator.php +++ b/Model/UserDiscriminator.php @@ -5,68 +5,62 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface; /** - * Description of UserDiscriminator + * Description of UserDiscriminator. * * @author leonardo proietti (leonardo.proietti@gmail.com) * @author eux (eugenio@netmeans.net) */ class UserDiscriminator { - const SESSION_NAME = 'pugx_user.user_discriminator.class'; - + const SESSION_NAME = 'pugx_user.user_discriminator.class'; + /** - * - * @var SessionInterface + * @var SessionInterface */ protected $session; - + /** - * - * @var array + * @var array */ protected $conf = array(); - + /** - * - * @var Symfony\Component\Form\Form + * @var Symfony\Component\Form\Form */ protected $registrationForm = null; - + /** - * - * @var Symfony\Component\Form\Form + * @var Symfony\Component\Form\Form */ protected $profileForm = null; - + /** - * - * @var string + * @var string */ protected $class = null; - + /** - * Current form - * @var type + * Current form. + * + * @var type */ protected $form = null; /** - * * @param SessionInterface $session - * @param array $parameters + * @param array $parameters */ public function __construct(SessionInterface $session, array $parameters) { - $this->session = $session; + $this->session = $session; $this->buildConfig($parameters); } - + /** - * - * @return array + * @return array */ public function getClasses() - { + { $classes = array(); foreach ($this->conf as $entity => $conf) { $classes[] = $entity; @@ -74,62 +68,58 @@ public function getClasses() return $classes; } - + /** - * - * @param string $class + * @param string $class */ public function setClass($class, $persist = false) { if (!in_array($class, $this->getClasses())) { throw new \LogicException(sprintf('Impossible to set the class discriminator, because the class "%s" is not present in the entities list', $class)); } - + if ($persist) { $this->session->set(static::SESSION_NAME, $class); } - + $this->class = $class; } - + /** - * - * @return string + * @return string */ public function getClass() - { + { if (!is_null($this->class)) { return $this->class; } - + $storedClass = $this->session->get(static::SESSION_NAME, null); if ($storedClass) { $this->class = $storedClass; } - + if (is_null($this->class)) { $entities = $this->getClasses(); $this->class = $entities[0]; } - + return $this->class; } - + /** - * - * @return type + * @return type */ public function createUser() { $factory = $this->getUserFactory(); - $user = $factory::build($this->getClass()); - + $user = $factory::build($this->getClass()); + return $user; } - + /** - * * @return string */ public function getUserFactory() @@ -138,7 +128,6 @@ public function getUserFactory() } /** - * * @return array */ public function getUserOptions() @@ -148,11 +137,12 @@ public function getUserOptions() return $options; } - + /** - * * @param string $name + * * @return + * * @throws \InvalidArgumentException */ public function getFormType($name) @@ -160,28 +150,29 @@ public function getFormType($name) $class = $this->getClass(); $className = $this->conf[$class][$name]['form']['type']; $options = $this->conf[$class]['options']; - + if (!class_exists($className)) { throw new \InvalidArgumentException(sprintf('UserDiscriminator, error getting form type : "%s" not found', $className)); } $type = new $className($class, $options); + return $type; } - + /** - * * @param string $name + * * @return string */ public function getFormName($name) { return $this->conf[$this->getClass()][$name]['form']['name']; } - + /** - * * @param type $name + * * @return type */ public function getFormValidationGroups($name) @@ -190,7 +181,6 @@ public function getFormValidationGroups($name) } /** - * * @return string */ public function getTemplate($name) @@ -199,21 +189,19 @@ public function getTemplate($name) } /** - * * @param array $entities * @param array $registrationForms - * @param array $profileForms + * @param array $profileForms */ protected function buildConfig(array $users) { foreach ($users as $user) { - $class = $user['entity']['class']; - + if (!class_exists($class)) { throw new \InvalidArgumentException(sprintf('UserDiscriminator, configuration error : "%s" not found', $class)); } - + $this->conf[$class] = array( 'factory' => $user['entity']['factory'], 'registration' => array( @@ -221,7 +209,7 @@ protected function buildConfig(array $users) 'type' => $user['registration']['form']['type'], 'name' => $user['registration']['form']['name'], 'validation_groups' => $user['registration']['form']['validation_groups'], - ), + ), 'template' => $user['registration']['template'], ), 'profile' => array( @@ -232,7 +220,7 @@ protected function buildConfig(array $users) ), 'template' => $user['profile']['template'], ), - 'options' => $user['options'] + 'options' => $user['options'], ); } } diff --git a/Model/UserFactory.php b/Model/UserFactory.php index a2bf034..f7e18a3 100644 --- a/Model/UserFactory.php +++ b/Model/UserFactory.php @@ -2,21 +2,20 @@ namespace PUGX\MultiUserBundle\Model; -use PUGX\MultiUserBundle\Model\UserFactoryInterface; - /** * @author leonardo proietti (leonardo.proietti@gmail.com) */ class UserFactory implements UserFactoryInterface { /** - * * @param type $class - * @return \PUGX\MultiUserBundle\Model\class + * + * @return \PUGX\MultiUserBundle\Model\class */ public static function build($class) - { - $user = new $class; + { + $user = new $class(); + return $user; } -} \ No newline at end of file +} diff --git a/Model/UserFactoryInterface.php b/Model/UserFactoryInterface.php index 5a4264a..1cabd31 100644 --- a/Model/UserFactoryInterface.php +++ b/Model/UserFactoryInterface.php @@ -5,7 +5,7 @@ interface UserFactoryInterface { /** - * @param string $class + * @param string $class */ - static function build($class); -} \ No newline at end of file + public static function build($class); +} diff --git a/Tests/Controller/ProfileManagerTest.php b/Tests/Controller/ProfileManagerTest.php index f72dfe8..79b8f37 100644 --- a/Tests/Controller/ProfileManagerTest.php +++ b/Tests/Controller/ProfileManagerTest.php @@ -71,7 +71,7 @@ public function testProfileReturnRedirectResponse() $this->assertSame($result, $this->redirectResponse); } - + public function testProfileReturnDefaultTemplate() { $this->common(); diff --git a/Tests/Controller/RegistrationManagerTest.php b/Tests/Controller/RegistrationManagerTest.php index 4a84311..b1777a7 100644 --- a/Tests/Controller/RegistrationManagerTest.php +++ b/Tests/Controller/RegistrationManagerTest.php @@ -10,46 +10,46 @@ public function setUp() { $this->discriminator = $this->getMockBuilder('PUGX\MultiUserBundle\Model\UserDiscriminator') ->disableOriginalConstructor()->getMock(); - + $this->container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface') ->disableOriginalConstructor()->getMock(); - + $this->controller = $this->getMockBuilder('FOS\UserBundle\Controller\RegistrationController') ->disableOriginalConstructor()->getMock(); - + $this->formFactory = $this->getMockBuilder('PUGX\MultiUserBundle\Form\FormFactory') ->disableOriginalConstructor()->getMock(); - + $this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request') ->disableOriginalConstructor()->getMock(); - + $this->redirectResponse = $this->getMockBuilder('Symfony\Component\HttpFoundation\RedirectResponse') ->disableOriginalConstructor()->getMock(); - + $this->form = $this->getMockBuilder('Symfony\Component\Form\Form') ->disableOriginalConstructor()->getMock(); - + $this->twig = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface') ->disableOriginalConstructor()->getMock(); - + $this->formView = $this->getMockBuilder('Symfony\Component\Form\FormView') ->disableOriginalConstructor()->getMock(); - + $this->userManager = new RegistrationManager($this->discriminator, $this->container, $this->controller, $this->formFactory); } - + public function common() { $this->discriminator ->expects($this->exactly(1)) ->method('setClass') ->with('MyUser'); - + $this->controller ->expects($this->exactly(1)) ->method('setContainer') ->with($this->container); - + $this->container ->expects($this->at(0)) ->method('get') @@ -57,99 +57,98 @@ public function common() ->will($this->returnValue($this->request)); } - public function testRegisterReturnRedirectResponse() { $this->common(); - + $this->controller ->expects($this->exactly(1)) ->method('registerAction') ->with($this->request) ->will($this->returnValue($this->redirectResponse)); - + $result = $this->userManager->register('MyUser'); - + $this->assertSame($result, $this->redirectResponse); } - + public function testRegisterReturnDefaultTemplate() { $this->common(); - + $this->controller ->expects($this->exactly(1)) ->method('registerAction') ->with($this->request) ->will($this->returnValue(null)); - + $this->discriminator ->expects($this->exactly(1)) ->method('getTemplate') ->with('registration') ->will($this->returnValue(null)); - + $this->formFactory ->expects($this->exactly(1)) ->method('createForm') ->will($this->returnValue($this->form)); - + $this->container ->expects($this->at(1)) ->method('get') ->with('templating') ->will($this->returnValue($this->twig)); - + $this->twig ->expects($this->exactly(1)) ->method('renderResponse') ->with('FOSUserBundle:Registration:register.html.twig', array('form' => $this->formView, 'templates' => array())); - + $this->form ->expects($this->exactly(1)) ->method('createView') ->will($this->returnValue($this->formView)); - + $result = $this->userManager->register('MyUser'); } - + public function testRegisterReturnSpecificTemplate() { $this->common(); - + $this->controller ->expects($this->exactly(1)) ->method('registerAction') ->with($this->request) ->will($this->returnValue(null)); - + $this->discriminator ->expects($this->exactly(1)) ->method('getTemplate') ->with('registration') ->will($this->returnValue('PUGXMultiUserBundle:Registration:register.html.twig')); - + $this->formFactory ->expects($this->exactly(1)) ->method('createForm') ->will($this->returnValue($this->form)); - + $this->container ->expects($this->at(1)) ->method('get') ->with('templating') ->will($this->returnValue($this->twig)); - + $this->twig ->expects($this->exactly(1)) ->method('renderResponse') ->with('PUGXMultiUserBundle:Registration:register.html.twig', array('form' => $this->formView, 'templates' => array())); - + $this->form ->expects($this->exactly(1)) ->method('createView') ->will($this->returnValue($this->formView)); - + $result = $this->userManager->register('MyUser', $templates = array()); } -} \ No newline at end of file +} diff --git a/Tests/Doctrine/UserManagerTest.php b/Tests/Doctrine/UserManagerTest.php index 97461f9..1174c39 100644 --- a/Tests/Doctrine/UserManagerTest.php +++ b/Tests/Doctrine/UserManagerTest.php @@ -19,38 +19,38 @@ public function setUp() $this->userDiscriminator = $this->getMockBuilder('PUGX\MultiUserBundle\Model\UserDiscriminator') ->disableOriginalConstructor()->getMock(); $this->class = 'PUGX\MultiUserBundle\Tests\Stub\User'; - + $this->repo = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectRepository') ->disableOriginalConstructor()->getMock(); - + //parent $this->metaData = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata') ->disableOriginalConstructor()->getMock(); - + $this->om->expects($this->exactly(1))->method('getClassMetadata') ->with($this->class) ->will($this->returnValue($this->metaData)); - + $this->metaData->expects($this->exactly(1))->method('getName') ->will($this->returnValue($this->class)); //end parent - + $this->userManager = new UserManager($this->encoderFactory, $this->usernameCanonicalizer, $this->emailCanonicalizer, $this->om, $this->class, $this->userDiscriminator); } - + public function testGetClass() { - $this->userDiscriminator->expects($this->exactly(1))->method('getClass')->will($this->returnValue('Acme\UserBundle\MyUser')); + $this->userDiscriminator->expects($this->exactly(1))->method('getClass')->will($this->returnValue('Acme\UserBundle\MyUser')); $result = $this->userManager->getClass(); $this->assertEquals('Acme\UserBundle\MyUser', $result); } - + public function testCreateUser() { - $this->userDiscriminator->expects($this->exactly(1))->method('createUser')->will($this->onConsecutiveCalls(null)); + $this->userDiscriminator->expects($this->exactly(1))->method('createUser')->will($this->onConsecutiveCalls(null)); $this->userManager->createUser(); } - + public function testFindUserBy() { $this->userDiscriminator @@ -73,22 +73,22 @@ public function testFindUserBy() $this->userManager->findUserBy(array('criteria' => 'dummy')); } - + public function testFindUsers() - { + { $this->userDiscriminator->expects($this->exactly(1))->method('getClasses')->will($this->onConsecutiveCalls(array('PUGX\MultiUserBundle\Tests\Stub\User'))); $this->om->expects($this->exactly(1))->method('getRepository')->with('PUGX\MultiUserBundle\Tests\Stub\User')->will($this->returnValue($this->repo)); $this->repo->expects($this->exactly(1))->method('findAll')->will($this->onConsecutiveCalls(array())); $this->userManager->findUsers(); } - + public function testFindUserByUserNotFound() - { + { $this->userDiscriminator->expects($this->exactly(1))->method('getClasses')->will($this->onConsecutiveCalls(array('PUGX\MultiUserBundle\Tests\Stub\User'))); $this->om->expects($this->exactly(1))->method('getRepository')->will($this->returnValue($this->repo)); - $this->repo->expects($this->exactly(1))->method('findOneBy')->with(array('criteria' => 'dummy'))->will($this->onConsecutiveCalls(null)); - $this->userDiscriminator->expects($this->exactly(0))->method('setClass'); - $user = $this->userManager->findUserBy(array('criteria' => 'dummy')); + $this->repo->expects($this->exactly(1))->method('findOneBy')->with(array('criteria' => 'dummy'))->will($this->onConsecutiveCalls(null)); + $this->userDiscriminator->expects($this->exactly(0))->method('setClass'); + $user = $this->userManager->findUserBy(array('criteria' => 'dummy')); $this->assertEquals(null, $user); } } diff --git a/Tests/Listener/AuthenticationListenerTest.php b/Tests/Listener/AuthenticationListenerTest.php index b249068..24acfd2 100644 --- a/Tests/Listener/AuthenticationListenerTest.php +++ b/Tests/Listener/AuthenticationListenerTest.php @@ -16,8 +16,8 @@ public function setUp() ->disableOriginalConstructor()->getMock(); $this->implicitLoginEvent = $this->getMockBuilder('FOS\UserBundle\Event\UserEvent') ->disableOriginalConstructor()->getMock(); - $this->switchUserEvent = $this->getMockBuilder('Symfony\Component\Security\Http\Event\SwitchUserEvent') - ->disableOriginalConstructor()->getMock(); + $this->switchUserEvent = $this->getMockBuilder('Symfony\Component\Security\Http\Event\SwitchUserEvent') + ->disableOriginalConstructor()->getMock(); $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken') ->disableOriginalConstructor()->getMock(); $this->user = new User(); @@ -42,13 +42,13 @@ public function testOnSecurityImplicitLogin() $this->listener->onSecurityImplicitLogin($this->implicitLoginEvent); } - public function testOnSecuritySwitchUser() - { - $this->switchUserEvent->expects($this->once())->method('getTargetUser')->will($this->returnValue($this->user)); - $this->userDiscriminator->expects($this->exactly(1))->method('setClass')->with('PUGX\MultiUserBundle\Tests\Stub\User', true); + public function testOnSecuritySwitchUser() + { + $this->switchUserEvent->expects($this->once())->method('getTargetUser')->will($this->returnValue($this->user)); + $this->userDiscriminator->expects($this->exactly(1))->method('setClass')->with('PUGX\MultiUserBundle\Tests\Stub\User', true); - $this->listener->onSecuritySwitchUser($this->switchUserEvent); - } + $this->listener->onSecuritySwitchUser($this->switchUserEvent); + } public function testDiscriminateNormalizedProxyClasses() { diff --git a/Tests/Model/UserDiscriminatorTest.php b/Tests/Model/UserDiscriminatorTest.php index 649d266..4e7de28 100644 --- a/Tests/Model/UserDiscriminatorTest.php +++ b/Tests/Model/UserDiscriminatorTest.php @@ -4,72 +4,67 @@ use PUGX\MultiUserBundle\Model\UserDiscriminator; use PUGX\MultiUserBundle\Tests\Stub\UserRegistrationForm; -use PUGX\MultiUserBundle\Tests\Stub\UserProfileForm; -use PUGX\MultiUserBundle\Tests\Stub\AnotherUserRegistrationForm; -use PUGX\MultiUserBundle\Tests\Stub\AnotherUserProfileForm; use PUGX\MultiUserBundle\Tests\Stub\User; use PUGX\MultiUserBundle\Tests\Stub\AnotherUser; -use Symfony\Component\Form\FormFactoryInterface; class UserDiscriminatorTest extends \PHPUnit_Framework_TestCase { - public function setUp() { $this->session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->disableOriginalConstructor()->getMock(); - $this->event = $this->getMockBuilder('Symfony\Component\Security\Http\Event\InteractiveLoginEvent')->disableOriginalConstructor()->getMock(); - $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken')->disableOriginalConstructor()->getMock(); - $this->user = new User(); + $this->event = $this->getMockBuilder('Symfony\Component\Security\Http\Event\InteractiveLoginEvent')->disableOriginalConstructor()->getMock(); + $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken')->disableOriginalConstructor()->getMock(); + $this->user = new User(); $this->userInvalid = $this->getMockBuilder('InvalidUser')->disableOriginalConstructor()->getMock(); $this->userFactory = $this->getMockBuilder('PUGX\MultiUserBundle\Model\UserFactoryInterface')->disableOriginalConstructor()->getMock(); $userParameters = array( 'entity' => array( 'class' => 'PUGX\MultiUserBundle\Tests\Stub\User', - 'factory' => 'PUGX\MultiUserBundle\Model\UserFactory' + 'factory' => 'PUGX\MultiUserBundle\Model\UserFactory', ), 'registration' => array( 'form' => array( 'type' => 'PUGX\MultiUserBundle\Tests\Stub\UserRegistrationForm', 'name' => 'fos_user_registration_form', - 'validation_groups' => array('Registration', 'Default') + 'validation_groups' => array('Registration', 'Default'), ), - 'template' => 'AcmeUserBundle:Registration:user_one.form.html.twig' + 'template' => 'AcmeUserBundle:Registration:user_one.form.html.twig', ), 'profile' => array( 'form' => array( 'type' => 'PUGX\MultiUserBundle\Tests\Stub\UserProfileForm', 'name' => 'fos_user_profile_form', - 'validation_groups' => array('Profile', 'Default') + 'validation_groups' => array('Profile', 'Default'), ), - 'template' => 'AcmeUserBundle:Profile:user_two.form.html.twig' + 'template' => 'AcmeUserBundle:Profile:user_two.form.html.twig', ), - 'options' => array('option1' => true) + 'options' => array('option1' => true), ); $anotherUserParameters = array( 'entity' => array( 'class' => 'PUGX\MultiUserBundle\Tests\Stub\AnotherUser', - 'factory' => 'PUGX\MultiUserBundle\Tests\Stub\CustomUserFactory' + 'factory' => 'PUGX\MultiUserBundle\Tests\Stub\CustomUserFactory', ), 'registration' => array( 'form' => array( 'type' => 'PUGX\MultiUserBundle\Tests\Stub\AnotherUserRegistrationForm', 'name' => 'fos_user_my_registration_form', - 'validation_groups' => array('MyRegistration', 'Default') + 'validation_groups' => array('MyRegistration', 'Default'), ), - 'template' => 'AcmeUserBundle:Registration:user_two.form.html.twig' + 'template' => 'AcmeUserBundle:Registration:user_two.form.html.twig', ), 'profile' => array( 'form' => array( 'type' => 'PUGX\MultiUserBundle\Tests\Stub\AnotherUserProfileForm', 'name' => 'fos_user_profile_form', - 'validation_groups' => array('Profile', 'Default') + 'validation_groups' => array('Profile', 'Default'), ), - 'template' => 'AcmeUserBundle:Profile:user_two.form.html.twig' + 'template' => 'AcmeUserBundle:Profile:user_two.form.html.twig', ), - 'options' => array('option1' => true) + 'options' => array('option1' => true), ); $this->parameters = array('user_one' => $userParameters, 'user_two' => $anotherUserParameters); @@ -86,24 +81,24 @@ public function testBuildException() $userParameters = array( 'entity' => array( 'class' => 'FakeUser', - 'factory' => 'PUGX\MultiUserBundle\Model\UserFactory' + 'factory' => 'PUGX\MultiUserBundle\Model\UserFactory', ), 'registration' => array( 'form' => 'PUGX\MultiUserBundle\Tests\Stub\UserRegistrationForm', 'options' => array( - 'validation_groups' => array('Registration', 'Default') - ) + 'validation_groups' => array('Registration', 'Default'), + ), ), 'profile' => array( 'form' => 'PUGX\MultiUserBundle\Tests\Stub\UserProfileForm', 'options' => array( - 'validation_groups' => array('Profile', 'Default') - ) + 'validation_groups' => array('Profile', 'Default'), + ), ), - 'options' => array() + 'options' => array(), ); - $parameters = array('user' => $userParameters); + $parameters = array('user' => $userParameters); $discriminator = new UserDiscriminator($this->session, $parameters); } @@ -196,9 +191,9 @@ public function testGetFormTypeRegistrationWithOptions() $reflectOptions->setAccessible(true); $this->discriminator->setClass('PUGX\MultiUserBundle\Tests\Stub\User'); - $result = $this->discriminator->getFormType('registration'); + $result = $this->discriminator->getFormType('registration'); $this->assertEquals(array('option1' => true), $reflectOptions->getValue($result)); - } + } public function testGetFormTypeProfile() { diff --git a/Tests/Stub/AnotherUser.php b/Tests/Stub/AnotherUser.php index bab856d..1e83737 100644 --- a/Tests/Stub/AnotherUser.php +++ b/Tests/Stub/AnotherUser.php @@ -4,5 +4,4 @@ class AnotherUser { - -} \ No newline at end of file +} diff --git a/Tests/Stub/AnotherUserProfileForm.php b/Tests/Stub/AnotherUserProfileForm.php index 60701f7..bf64a75 100644 --- a/Tests/Stub/AnotherUserProfileForm.php +++ b/Tests/Stub/AnotherUserProfileForm.php @@ -6,6 +6,6 @@ class AnotherUserProfileForm { public function getName() { - return "form_name"; + return 'form_name'; } -} \ No newline at end of file +} diff --git a/Tests/Stub/AnotherUserRegistrationForm.php b/Tests/Stub/AnotherUserRegistrationForm.php index e484795..e973e21 100644 --- a/Tests/Stub/AnotherUserRegistrationForm.php +++ b/Tests/Stub/AnotherUserRegistrationForm.php @@ -6,6 +6,6 @@ class AnotherUserRegistrationForm { public function getName() { - return "form_name"; + return 'form_name'; } -} \ No newline at end of file +} diff --git a/Tests/Stub/CustomUserFactory.php b/Tests/Stub/CustomUserFactory.php index 3051767..d97c072 100644 --- a/Tests/Stub/CustomUserFactory.php +++ b/Tests/Stub/CustomUserFactory.php @@ -5,8 +5,9 @@ use PUGX\MultiUserBundle\Model\UserFactoryInterface; class CustomUserFactory implements UserFactoryInterface -{ - public static function build($class) { - return new AnotherUser; +{ + public static function build($class) + { + return new AnotherUser(); } -} \ No newline at end of file +} diff --git a/Tests/Stub/ProfileController.php b/Tests/Stub/ProfileController.php index f64a005..b4cb5ac 100644 --- a/Tests/Stub/ProfileController.php +++ b/Tests/Stub/ProfileController.php @@ -6,5 +6,4 @@ class ProfileController extends BaseController { - -} \ No newline at end of file +} diff --git a/Tests/Stub/Proxy/__CG__/PUGX/MultiUserBundle/Tests/Stub/User.php b/Tests/Stub/Proxy/__CG__/PUGX/MultiUserBundle/Tests/Stub/User.php index c5d1f96..55073a1 100644 --- a/Tests/Stub/Proxy/__CG__/PUGX/MultiUserBundle/Tests/Stub/User.php +++ b/Tests/Stub/Proxy/__CG__/PUGX/MultiUserBundle/Tests/Stub/User.php @@ -1,7 +1,7 @@ class = $class; + $this->class = $class; $this->options = $options; } public function getName() { - return "form_name"; + return 'form_name'; } -} \ No newline at end of file +} diff --git a/Tests/Stub/UserRegistrationForm.php b/Tests/Stub/UserRegistrationForm.php index c8846e4..6c22a95 100644 --- a/Tests/Stub/UserRegistrationForm.php +++ b/Tests/Stub/UserRegistrationForm.php @@ -9,12 +9,12 @@ class UserRegistrationForm public function __construct($class, $options = null) { - $this->class = $class; + $this->class = $class; $this->options = $options; } public function getName() { - return "form_name"; + return 'form_name'; } } diff --git a/Tests/TestCase.php b/Tests/TestCase.php index 9ba2ec5..30d6887 100644 --- a/Tests/TestCase.php +++ b/Tests/TestCase.php @@ -3,10 +3,8 @@ namespace PUGX\MultiUserBundle\Tests; /** - * * @author leonardo proietti */ class TestCase extends \PHPUnit_Framework_TestCase { - } diff --git a/Validator/Constraints/UniqueEntity.php b/Validator/Constraints/UniqueEntity.php index 4d7596c..ab97dee 100644 --- a/Validator/Constraints/UniqueEntity.php +++ b/Validator/Constraints/UniqueEntity.php @@ -5,7 +5,7 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity as BaseConstraint; /** - * Constraint for the Unique Entity validator + * Constraint for the Unique Entity validator. * * @Annotation */ diff --git a/Validator/Constraints/UniqueEntityValidator.php b/Validator/Constraints/UniqueEntityValidator.php index d9f695e..3aa83f6 100644 --- a/Validator/Constraints/UniqueEntityValidator.php +++ b/Validator/Constraints/UniqueEntityValidator.php @@ -31,7 +31,7 @@ public function validate($entity, Constraint $constraint) { $violations = $this->context->getViolations(); foreach ($violations as $violation) { - if ($violation->getPropertyPath() == 'data.' . $constraint->fields) { + if ($violation->getPropertyPath() == 'data.'.$constraint->fields) { return; } } @@ -63,7 +63,7 @@ public function validate($entity, Constraint $constraint) $criteria = array(); foreach ($fields as $fieldName) { if (!$class->hasField($fieldName) && !$class->hasAssociation($fieldName)) { - throw new ConstraintDefinitionException("Only field names mapped by Doctrine can be validated for uniqueness."); + throw new ConstraintDefinitionException('Only field names mapped by Doctrine can be validated for uniqueness.'); } $criteria[$fieldName] = $class->reflFields[$fieldName]->getValue($entity); @@ -84,8 +84,8 @@ public function validate($entity, Constraint $constraint) if (count($relatedId) > 1) { throw new ConstraintDefinitionException( - "Associated entities are not allowed to have more than one identifier field to be " . - "part of a unique constraint in: " . $class->getName() . "#" . $fieldName + 'Associated entities are not allowed to have more than one identifier field to be '. + 'part of a unique constraint in: '.$class->getName().'#'.$fieldName ); } $criteria[$fieldName] = array_pop($relatedId);