Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make db_driver parameter optional with "no_driver" default value #2708

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
45 changes: 45 additions & 0 deletions Model/GroupManagerNone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* 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 <[email protected]>
*/
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.');
}
}
50 changes: 50 additions & 0 deletions Model/UserManagerNone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* 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 <[email protected]>
*/
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.');
}
}
13 changes: 13 additions & 0 deletions Resources/config/no_driver.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="fos_user.user_manager.default" class="FOS\UserBundle\Model\UserManagerNone" public="false">
<argument type="service" id="fos_user.util.password_updater" />
<argument type="service" id="fos_user.util.canonical_fields_updater" />
</service>
</services>
</container>
10 changes: 10 additions & 0 deletions Resources/config/no_driver_group.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="fos_user.group_manager.default" class="FOS\UserBundle\Model\GroupManagerNone" public="false" />
</services>
</container>
10 changes: 10 additions & 0 deletions Resources/config/storage-validation/no_driver.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="FOS\UserBundle\Model\User" />

<class name="FOS\UserBundle\Model\Group" />
</constraint-mapping>
11 changes: 0 additions & 11 deletions Tests/DependencyInjection/FOSUserExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
42 changes: 42 additions & 0 deletions Tests/Model/GroupManagerNoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* 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)),
);
}
}
50 changes: 50 additions & 0 deletions Tests/Model/UserManagerNoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* 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)),
);
}
}