Skip to content

Commit

Permalink
Merge branch '2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Dec 14, 2011
2 parents 044ebea + 26069a0 commit 2905302
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Security/User/ModelUserProvider.php
Expand Up @@ -98,7 +98,11 @@ public function refreshUser(UserInterface $user)
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}

return $this->loadUserByUsername($user->getUsername());
$queryClass = $this->queryClass;
$user = $queryClass::create()->findPk($user->getPrimaryKey());
$proxyClass = $this->proxyClass;

return new $proxyClass($user);
}

/**
Expand Down
71 changes: 71 additions & 0 deletions Tests/Fixtures/UserProxy.php
@@ -0,0 +1,71 @@
<?php

/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Propel\PropelBundle\Tests\Fixtures;

use Symfony\Component\Security\Core\User\UserInterface;

class UserProxy implements UserInterface
{
protected $user;

public function __construct($user)

This comment has been minimized.

Copy link
@johnkary

johnkary Dec 14, 2011

Should $user be typehinted to an interface? Most of your method calls within the class access this property with getPropelUser(), which then infers existence of getRoles(), getPassword(), getSalt() and getPassword().

I guess they're tests so no big deal since it's a fixture class anyway. Should've kept reading :)

This comment has been minimized.

Copy link
@willdurand

willdurand Dec 14, 2011

Author

Not a big deal here :)

{
$this->user = $user;
}

public function getRoles()
{
$roles = $this->getPropelUser()->getRoles();
}

public function getPassword()
{
return $this->getPropelUser()->getPassword();
}

public function getSalt()
{
return $this->getPropelUser()->getSalt();
}

public function getUsername()
{
return $this->getPropelUser()->getUsername();
}

public function eraseCredentials()
{
}

public function equals(UserInterface $user)
{
return $this->getPropelUser()->equals($user);
}

public function getAlgorithm()
{
return $this->getPropelUser()->getAlgorithm();
}

public function __call($method, $arguments)
{
if (is_callable(array($this->user, $method))) {
return call_user_func_array(array($this->user, $method), $arguments);
}

throw new \BadMethodCallException('Can\'t call method '.$method);
}

public function getPropelUser()
{
return $this->user;
}
}
66 changes: 66 additions & 0 deletions Tests/Security/User/ModelUserProviderTest.php
@@ -0,0 +1,66 @@
<?php

/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Propel\PropelBundle\Tests\Security\User;

use Propel\PropelBundle\Security\User\ModelUserProvider;

use Propel\PropelBundle\Tests\TestCase;
use Propel\PropelBundle\Tests\Fixtures\UserProxy;

/**
* @author William Durand <william.durand1@gmail.com>
*/
class ModelUserProviderTest extends TestCase
{
protected $con = null;

public function setUp()
{
$this->loadPropelQuickBuilder();

$schema = <<<SCHEMA
<database name="users" defaultIdMethod="native">
<table name="user">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
<column name="username" type="varchar" size="255" primaryString="true" />
<column name="algorithm" type="varchar" size="50" />
<column name="salt" type="varchar" size="255" />
<column name="password" type="varchar" size="255" />
<column name="expires_at" type="timestamp" />
<column name="roles" type="array" />
</table>
</database>
SCHEMA;

$builder = new \PropelQuickBuilder();
$builder->setSchema($schema);
$this->con = $builder->build();
}

public function testRefreshUserGetsUserByPrimaryKey()
{
$user1 = new \User();
$user1->setUsername('user1');
$user1->save();

$user2 = new \User();
$user2->setUsername('user2');
$user2->save();

$provider = new ModelUserProvider('\User', 'Propel\PropelBundle\Tests\Fixtures\UserProxy', 'username');

// try to change the user identity
$user1->setUsername('user2');

$resultUser = $provider->refreshUser(new UserProxy($user1));
$this->assertSame($user1, $resultUser->getPropelUser());
}
}
9 changes: 9 additions & 0 deletions Tests/TestCase.php
Expand Up @@ -35,4 +35,13 @@ public function getContainer()
'kernel.debug' => false,
)));
}

protected function loadPropelQuickBuilder()
{
require_once __DIR__.'/../vendor/propel/runtime/lib/Propel.php';
require_once __DIR__.'/../vendor/propel/runtime/lib/adapter/DBAdapter.php';
require_once __DIR__.'/../vendor/propel/runtime/lib/adapter/DBSQLite.php';
require_once __DIR__.'/../vendor/propel/runtime/lib/connection/PropelPDO.php';
require_once __DIR__.'/../vendor/propel/generator/lib/util/PropelQuickBuilder.php';
}
}
2 changes: 2 additions & 0 deletions Tests/autoload.php.dist
Expand Up @@ -12,6 +12,8 @@ $loader->registerNamespaces(array(
));
$loader->register();

set_include_path(get_include_path() . PATH_SEPARATOR . $vendorDir.'/phing/classes');

spl_autoload_register(function($class) {
if (0 === strpos($class, 'Propel\\PropelBundle\\')) {
$path = __DIR__.'/../'.implode('/', array_slice(explode('\\', $class), 2)).'.php';
Expand Down

0 comments on commit 2905302

Please sign in to comment.