From ff1ef94114efd41b9be26667d28d5757f115ee5d Mon Sep 17 00:00:00 2001 From: "Andrey F. Mindubaev" Date: Wed, 10 Jan 2018 11:44:10 +0300 Subject: [PATCH 1/2] Make db_driver parameter optional with "no_driver" default value --- DependencyInjection/Configuration.php | 4 +- Model/GroupManagerNone.php | 45 +++++++++++++++++ Model/UserManagerNone.php | 50 +++++++++++++++++++ Resources/config/no_driver.xml | 13 +++++ Resources/config/no_driver_group.xml | 10 ++++ .../config/storage-validation/no_driver.xml | 10 ++++ .../FOSUserExtensionTest.php | 11 ---- 7 files changed, 130 insertions(+), 13 deletions(-) create mode 100644 Model/GroupManagerNone.php create mode 100644 Model/UserManagerNone.php create mode 100644 Resources/config/no_driver.xml create mode 100644 Resources/config/no_driver_group.xml create mode 100644 Resources/config/storage-validation/no_driver.xml diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 26ed0601f0..27332cd160 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -34,17 +34,17 @@ public function getConfigTreeBuilder() $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('fos_user'); - $supportedDrivers = array('orm', 'mongodb', 'couchdb', 'custom'); + $supportedDrivers = array('orm', 'mongodb', 'couchdb', 'custom', 'no_driver'); $rootNode ->children() ->scalarNode('db_driver') + ->defaultValue('no_driver') ->validate() ->ifNotInArray($supportedDrivers) ->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($supportedDrivers)) ->end() ->cannotBeOverwritten() - ->isRequired() ->cannotBeEmpty() ->end() ->scalarNode('user_class')->isRequired()->cannotBeEmpty()->end() diff --git a/Model/GroupManagerNone.php b/Model/GroupManagerNone.php new file mode 100644 index 0000000000..1b6e17afab --- /dev/null +++ b/Model/GroupManagerNone.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\UserBundle\Model; + +/** + * Fallback Group Manager implementation when db_driver is not configured. + * + * @author Andrey F. Mindubaev + */ +class GroupManagerNone extends GroupManager +{ + public function deleteGroup(GroupInterface $group) + { + throw new \RuntimeException('The child node "db_driver" at path "fos_user" must be configured.'); + } + + public function findGroupBy(array $criteria) + { + throw new \RuntimeException('The child node "db_driver" at path "fos_user" must be configured.'); + } + + public function findGroups() + { + throw new \RuntimeException('The child node "db_driver" at path "fos_user" must be configured.'); + } + + public function getClass() + { + throw new \RuntimeException('The child node "db_driver" at path "fos_user" must be configured.'); + } + + public function updateGroup(GroupInterface $group) + { + throw new \RuntimeException('The child node "db_driver" at path "fos_user" must be configured.'); + } +} diff --git a/Model/UserManagerNone.php b/Model/UserManagerNone.php new file mode 100644 index 0000000000..8b21468581 --- /dev/null +++ b/Model/UserManagerNone.php @@ -0,0 +1,50 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\UserBundle\Model; + +/** + * Fallback User Manager implementation when db_driver is not configured. + * + * @author Andrey F. Mindubaev + */ +class UserManagerNone extends UserManager +{ + public function deleteUser(UserInterface $user) + { + throw new \RuntimeException('The child node "db_driver" at path "fos_user" must be configured.'); + } + + public function findUserBy(array $criteria) + { + throw new \RuntimeException('The child node "db_driver" at path "fos_user" must be configured.'); + } + + public function findUsers() + { + throw new \RuntimeException('The child node "db_driver" at path "fos_user" must be configured.'); + } + + public function getClass() + { + throw new \RuntimeException('The child node "db_driver" at path "fos_user" must be configured.'); + } + + public function reloadUser(UserInterface $user) + { + throw new \RuntimeException('The child node "db_driver" at path "fos_user" must be configured.'); + } + + public function updateUser(UserInterface $user) + { + throw new \RuntimeException('The child node "db_driver" at path "fos_user" must be configured.'); + } +} diff --git a/Resources/config/no_driver.xml b/Resources/config/no_driver.xml new file mode 100644 index 0000000000..e0e9fe89fe --- /dev/null +++ b/Resources/config/no_driver.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + diff --git a/Resources/config/no_driver_group.xml b/Resources/config/no_driver_group.xml new file mode 100644 index 0000000000..0293962d3f --- /dev/null +++ b/Resources/config/no_driver_group.xml @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/Resources/config/storage-validation/no_driver.xml b/Resources/config/storage-validation/no_driver.xml new file mode 100644 index 0000000000..f6d7d2e2b6 --- /dev/null +++ b/Resources/config/storage-validation/no_driver.xml @@ -0,0 +1,10 @@ + + + + + + + diff --git a/Tests/DependencyInjection/FOSUserExtensionTest.php b/Tests/DependencyInjection/FOSUserExtensionTest.php index 4daed8694f..33f9dc8a0f 100644 --- a/Tests/DependencyInjection/FOSUserExtensionTest.php +++ b/Tests/DependencyInjection/FOSUserExtensionTest.php @@ -26,17 +26,6 @@ protected function tearDown() $this->configuration = null; } - /** - * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException - */ - public function testUserLoadThrowsExceptionUnlessDatabaseDriverSet() - { - $loader = new FOSUserExtension(); - $config = $this->getEmptyConfig(); - unset($config['db_driver']); - $loader->load(array($config), new ContainerBuilder()); - } - /** * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException */ From 8be05a64565ef85ecf04ad965911e909bf62abd5 Mon Sep 17 00:00:00 2001 From: "Andrey F. Mindubaev" Date: Fri, 12 Jan 2018 19:44:01 +0300 Subject: [PATCH 2/2] Added tests for UserManagerNone and GroupManagerNone --- Tests/Model/GroupManagerNoneTest.php | 42 +++++++++++++++++++++++ Tests/Model/UserManagerNoneTest.php | 50 ++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 Tests/Model/GroupManagerNoneTest.php create mode 100644 Tests/Model/UserManagerNoneTest.php diff --git a/Tests/Model/GroupManagerNoneTest.php b/Tests/Model/GroupManagerNoneTest.php new file mode 100644 index 0000000000..43338d1cb9 --- /dev/null +++ b/Tests/Model/GroupManagerNoneTest.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\UserBundle\Tests\Model; + +use FOS\UserBundle\Model\GroupManagerNone; +use PHPUnit\Framework\TestCase; + +class GroupManagerNoneTest extends TestCase +{ + /** + * @dataProvider methodsProvider + * @expectedException \RuntimeException + */ + public function testMethods($name, $arguments) + { + $manager = new GroupManagerNone(); + + call_user_func_array(array($manager, $name), $arguments); + } + + public function methodsProvider() + { + $group = $this->getMockBuilder('FOS\UserBundle\Model\GroupInterface')->getMock(); + + return array( + array('deleteGroup', array($group)), + array('findGroupBy', array(array('id' => 1))), + array('findGroups', array()), + array('getClass', array()), + array('updateGroup', array($group)), + ); + } +} diff --git a/Tests/Model/UserManagerNoneTest.php b/Tests/Model/UserManagerNoneTest.php new file mode 100644 index 0000000000..0aabbb5609 --- /dev/null +++ b/Tests/Model/UserManagerNoneTest.php @@ -0,0 +1,50 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\UserBundle\Tests\Model; + +use FOS\UserBundle\Model\UserManagerNone; +use PHPUnit\Framework\TestCase; + +class UserManagerNoneTest extends TestCase +{ + /** + * @dataProvider methodsProvider + * @expectedException \RuntimeException + */ + public function testMethods($name, $arguments) + { + /** @var \FOS\UserBundle\Util\PasswordUpdaterInterface $passwordUpdater */ + $passwordUpdater = $this->getMockBuilder('FOS\UserBundle\Util\PasswordUpdaterInterface')->getMock(); + /** @var \FOS\UserBundle\Util\CanonicalFieldsUpdater $fieldsUpdater */ + $fieldsUpdater = $this->getMockBuilder('FOS\UserBundle\Util\CanonicalFieldsUpdater') + ->disableOriginalConstructor() + ->getMock(); + + $manager = new UserManagerNone($passwordUpdater, $fieldsUpdater); + + call_user_func_array(array($manager, $name), $arguments); + } + + public function methodsProvider() + { + $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface')->getMock(); + + return array( + array('deleteUser', array($user)), + array('findUserBy', array(array('id' => 1))), + array('findUsers', array()), + array('getClass', array()), + array('reloadUser', array($user)), + array('updateUser', array($user)), + ); + } +}